| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package frontend implements HTTP server that handles requests to default | 5 // Package frontend implements HTTP server that handles requests to default |
| 6 // module. | 6 // module. |
| 7 // | 7 // |
| 8 // It stitches together all the code. | 8 // It stitches together all the code. |
| 9 package frontend | 9 package frontend |
| 10 | 10 |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 c.Writer.WriteHeader(http.StatusOK) | 122 c.Writer.WriteHeader(http.StatusOK) |
| 123 } | 123 } |
| 124 | 124 |
| 125 // readConfigCron is handler for /internal/cron/read-config GAE cron task. | 125 // readConfigCron is handler for /internal/cron/read-config GAE cron task. |
| 126 func readConfigCron(c *router.Context) { | 126 func readConfigCron(c *router.Context) { |
| 127 // Don't override manually imported configs with 'nil' on devserver. | 127 // Don't override manually imported configs with 'nil' on devserver. |
| 128 if info.IsDevAppServer(c.Context) { | 128 if info.IsDevAppServer(c.Context) { |
| 129 c.Writer.WriteHeader(http.StatusOK) | 129 c.Writer.WriteHeader(http.StatusOK) |
| 130 return | 130 return |
| 131 } | 131 } |
| 132 » if _, err := adminServerWithoutAuth.ImportCAConfigs(c.Context, nil); err
!= nil { | 132 |
| 133 » » panic(err) // let panic catcher deal with it | 133 » wg := sync.WaitGroup{} |
| 134 » } | 134 » var errs [2]error |
| 135 » c.Writer.WriteHeader(http.StatusOK) | 135 |
| 136 » wg.Add(1) |
| 137 » go func() { |
| 138 » » defer wg.Done() |
| 139 » » _, errs[0] = adminServerWithoutAuth.ImportCAConfigs(c.Context, n
il) |
| 140 » » if errs[0] != nil { |
| 141 » » » logging.Errorf(c.Context, "ImportCAConfigs failed - %s",
errs[0]) |
| 142 » » } |
| 143 » }() |
| 144 |
| 145 » wg.Add(1) |
| 146 » go func() { |
| 147 » » defer wg.Done() |
| 148 » » _, errs[1] = adminServerWithoutAuth.ImportDelegationConfigs(c.Co
ntext, nil) |
| 149 » » if errs[1] != nil { |
| 150 » » » logging.Errorf(c.Context, "ImportDelegationConfigs faile
d - %s", errs[1]) |
| 151 » » } |
| 152 » }() |
| 153 |
| 154 » wg.Wait() |
| 155 |
| 156 » // Retry cron job only on transient errors. On fatal errors let it rerun
one |
| 157 » // minute later, as usual, to avoid spamming logs with errors. |
| 158 » c.Writer.WriteHeader(statusFromErrs(errs[:])) |
| 136 } | 159 } |
| 137 | 160 |
| 138 // fetchCRLCron is handler for /internal/cron/fetch-crl GAE cron task. | 161 // fetchCRLCron is handler for /internal/cron/fetch-crl GAE cron task. |
| 139 func fetchCRLCron(c *router.Context) { | 162 func fetchCRLCron(c *router.Context) { |
| 140 list, err := caServerWithoutAuth.ListCAs(c.Context, nil) | 163 list, err := caServerWithoutAuth.ListCAs(c.Context, nil) |
| 141 if err != nil { | 164 if err != nil { |
| 142 panic(err) // let panic catcher deal with it | 165 panic(err) // let panic catcher deal with it |
| 143 } | 166 } |
| 144 | 167 |
| 145 // Fetch CRL of each active CA in parallel. In practice there are very f
ew | 168 // Fetch CRL of each active CA in parallel. In practice there are very f
ew |
| 146 // CAs there (~= 1), so the risk of OOM is small. | 169 // CAs there (~= 1), so the risk of OOM is small. |
| 147 wg := sync.WaitGroup{} | 170 wg := sync.WaitGroup{} |
| 148 errs := make([]error, len(list.Cn)) | 171 errs := make([]error, len(list.Cn)) |
| 149 for i, cn := range list.Cn { | 172 for i, cn := range list.Cn { |
| 150 wg.Add(1) | 173 wg.Add(1) |
| 151 go func(i int, cn string) { | 174 go func(i int, cn string) { |
| 152 defer wg.Done() | 175 defer wg.Done() |
| 153 _, err := caServerWithoutAuth.FetchCRL(c.Context, &admin
.FetchCRLRequest{Cn: cn}) | 176 _, err := caServerWithoutAuth.FetchCRL(c.Context, &admin
.FetchCRLRequest{Cn: cn}) |
| 154 if err != nil { | 177 if err != nil { |
| 155 logging.Errorf(c.Context, "FetchCRL(%q) failed -
%s", cn, err) | 178 logging.Errorf(c.Context, "FetchCRL(%q) failed -
%s", cn, err) |
| 156 errs[i] = err | 179 errs[i] = err |
| 157 } | 180 } |
| 158 }(i, cn) | 181 }(i, cn) |
| 159 } | 182 } |
| 160 wg.Wait() | 183 wg.Wait() |
| 161 | 184 |
| 162 // Retry cron job only on transient errors. On fatal errors let it rerun
one | 185 // Retry cron job only on transient errors. On fatal errors let it rerun
one |
| 163 // minute later, as usual, to avoid spamming logs with errors. | 186 // minute later, as usual, to avoid spamming logs with errors. |
| 164 » status := http.StatusOK | 187 » c.Writer.WriteHeader(statusFromErrs(errs)) |
| 165 » for _, err = range errs { | 188 } |
| 189 |
| 190 // statusFromErrs returns 500 if any of gRPC errors is codes.Internal. |
| 191 func statusFromErrs(errs []error) int { |
| 192 » for _, err := range errs { |
| 166 if grpc.Code(err) == codes.Internal { | 193 if grpc.Code(err) == codes.Internal { |
| 167 » » » status = http.StatusInternalServerError | 194 » » » return http.StatusInternalServerError |
| 168 » » » break | |
| 169 } | 195 } |
| 170 } | 196 } |
| 171 » c.Writer.WriteHeader(status) | 197 » return http.StatusOK |
| 172 } | 198 } |
| OLD | NEW |