-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
427 lines (360 loc) · 12.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"context"
"crypto/ecdsa"
"encoding/base64"
"encoding/json"
"fmt"
html "html/template"
"log/slog"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/golang-jwt/jwt/v5"
"github.com/gorilla/handlers"
"github.com/ministryofjustice/opg-go-common/env"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/app"
"github.com/ministryofjustice/opg-modernising-lpa/internal/dynamo"
"github.com/ministryofjustice/opg-modernising-lpa/internal/event"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lambda"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
"github.com/ministryofjustice/opg-modernising-lpa/internal/lpastore"
"github.com/ministryofjustice/opg-modernising-lpa/internal/notify"
"github.com/ministryofjustice/opg-modernising-lpa/internal/onelogin"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/pay"
"github.com/ministryofjustice/opg-modernising-lpa/internal/place"
"github.com/ministryofjustice/opg-modernising-lpa/internal/s3"
"github.com/ministryofjustice/opg-modernising-lpa/internal/search"
"github.com/ministryofjustice/opg-modernising-lpa/internal/secrets"
"github.com/ministryofjustice/opg-modernising-lpa/internal/sesh"
"github.com/ministryofjustice/opg-modernising-lpa/internal/telemetry"
"github.com/ministryofjustice/opg-modernising-lpa/internal/templatefn"
"github.com/ministryofjustice/opg-modernising-lpa/internal/uid"
"go.opentelemetry.io/contrib/detectors/aws/ecs"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/mod/sumdb/dirhash"
)
var Tag string
func main() {
ctx := context.Background()
handler := telemetry.NewSlogHandler(slog.
NewJSONHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
switch a.Value.Kind() {
case slog.KindAny:
switch v := a.Value.Any().(type) {
case *http.Request:
return slog.Group(a.Key,
slog.String("method", v.Method),
slog.String("uri", v.URL.String()))
}
}
return a
},
}))
logger := slog.New(handler.
WithAttrs([]slog.Attr{
slog.String("service_name", "opg-modernising-lpa"),
slog.String("tag", Tag),
}))
if err := run(ctx, logger); err != nil {
logger.Error("run error", slog.Any("err", err.Error()))
os.Exit(1)
}
}
func run(ctx context.Context, logger *slog.Logger) error {
var (
devMode = env.Get("DEV_MODE", "") == "1"
appPublicURL = env.Get("APP_PUBLIC_URL", "http://localhost:5050")
authRedirectBaseURL = env.Get("AUTH_REDIRECT_BASE_URL", "http://localhost:5050")
webDir = env.Get("WEB_DIR", "web")
awsBaseURL = env.Get("AWS_BASE_URL", "")
clientID = env.Get("CLIENT_ID", "client-id-value")
issuer = env.Get("ISSUER", "http://mock-onelogin:8080")
dynamoTableLpas = env.Get("DYNAMODB_TABLE_LPAS", "lpas")
notifyBaseURL = env.Get("GOVUK_NOTIFY_BASE_URL", "http://mock-notify:8080")
notifyIsProduction = env.Get("GOVUK_NOTIFY_IS_PRODUCTION", "") == "1"
ordnanceSurveyBaseURL = env.Get("ORDNANCE_SURVEY_BASE_URL", "http://mock-os-api:8080")
payBaseURL = env.Get("GOVUK_PAY_BASE_URL", "http://mock-pay:8080")
port = env.Get("APP_PORT", "8080")
xrayEnabled = env.Get("XRAY_ENABLED", "") == "1"
rumConfig = templatefn.RumConfig{
GuestRoleArn: env.Get("AWS_RUM_GUEST_ROLE_ARN", ""),
Endpoint: env.Get("AWS_RUM_ENDPOINT", ""),
ApplicationRegion: env.Get("AWS_RUM_APPLICATION_REGION", ""),
IdentityPoolID: env.Get("AWS_RUM_IDENTITY_POOL_ID", ""),
ApplicationID: env.Get("AWS_RUM_APPLICATION_ID", ""),
}
uidBaseURL = env.Get("UID_BASE_URL", "http://mock-uid:8080")
lpaStoreBaseURL = env.Get("LPA_STORE_BASE_URL", "http://mock-lpa-store:8080")
metadataURL = env.Get("ECS_CONTAINER_METADATA_URI_V4", "")
oneloginURL = env.Get("ONELOGIN_URL", "https://home.integration.account.gov.uk")
evidenceBucketName = env.Get("UPLOADS_S3_BUCKET_NAME", "evidence")
eventBusName = env.Get("EVENT_BUS_NAME", "default")
mockIdentityPublicKey = env.Get("MOCK_IDENTITY_PUBLIC_KEY", "")
searchEndpoint = env.Get("SEARCH_ENDPOINT", "")
searchIndexName = env.Get("SEARCH_INDEX_NAME", "lpas")
searchIndexingEnabled = env.Get("SEARCH_INDEXING_DISABLED", "") != "1"
)
staticHash, err := dirhash.HashDir(webDir+"/static", webDir, dirhash.DefaultHash)
if err != nil {
return err
}
staticHash = url.QueryEscape(staticHash[3:11])
httpClient := &http.Client{Timeout: 10 * time.Second}
if xrayEnabled {
resource, err := ecs.NewResourceDetector().Detect(ctx)
if err != nil {
return err
}
shutdown, err := telemetry.Setup(ctx, resource)
if err != nil {
return err
}
defer shutdown(ctx)
httpClient.Transport = otelhttp.NewTransport(httpClient.Transport)
}
var region string
if metadataURL != "" {
region, err = awsRegion(metadataURL)
if err != nil {
logger.Warn("error getting region:", err)
}
}
layouts, err := parseLayoutTemplates(webDir+"/template/layout", templatefn.All(&templatefn.Globals{
DevMode: devMode,
Tag: Tag,
Region: region,
OneloginURL: oneloginURL,
StaticHash: staticHash,
RumConfig: rumConfig,
ActorTypes: actor.ActorTypes,
Paths: page.Paths,
}))
if err != nil {
return err
}
tmpls, err := parseTemplates(webDir+"/template", layouts)
if err != nil {
return err
}
donorTmpls, err := parseTemplates(webDir+"/template/donor", layouts)
if err != nil {
return err
}
certificateProviderTmpls, err := parseTemplates(webDir+"/template/certificateprovider", layouts)
if err != nil {
return err
}
attorneyTmpls, err := parseTemplates(webDir+"/template/attorney", layouts)
if err != nil {
return err
}
supporterTmpls, err := parseTemplates(webDir+"/template/supporter", layouts)
if err != nil {
return err
}
bundle, err := localize.NewBundle("lang/en.json", "lang/cy.json")
if err != nil {
return err
}
cfg, err := config.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("unable to load SDK config: %w", err)
}
if len(awsBaseURL) > 0 {
cfg.BaseEndpoint = aws.String(awsBaseURL)
}
otelaws.AppendMiddlewares(&cfg.APIOptions)
lpasDynamoClient, err := dynamo.NewClient(cfg, dynamoTableLpas)
if err != nil {
return err
}
eventClient := event.NewClient(cfg, eventBusName)
searchClient, err := search.NewClient(cfg, searchEndpoint, searchIndexName, searchIndexingEnabled)
if err != nil {
return err
}
if err := searchClient.CreateIndices(ctx); err != nil {
return err
}
secretsClient, err := secrets.NewClient(cfg, time.Hour)
if err != nil {
return err
}
sessionKeys, err := secretsClient.CookieSessionKeys(ctx)
if err != nil {
return err
}
sessionStore := sesh.NewStore(sessionKeys)
redirectURL := authRedirectBaseURL + page.Paths.AuthRedirect.Format()
identityPublicKeyFunc := func(ctx context.Context) (*ecdsa.PublicKey, error) {
bytes, err := secretsClient.SecretBytes(ctx, secrets.GovUkOneLoginIdentityPublicKey)
if err != nil {
return nil, err
}
return jwt.ParseECPublicKeyFromPEM(bytes)
}
if mockIdentityPublicKey != "" {
identityPublicKeyFunc = func(ctx context.Context) (*ecdsa.PublicKey, error) {
bytes, err := base64.StdEncoding.DecodeString(mockIdentityPublicKey)
if err != nil {
return nil, err
}
return jwt.ParseECPublicKeyFromPEM(bytes)
}
}
oneloginClient := onelogin.New(ctx, logger, httpClient, secretsClient, issuer, clientID, redirectURL, identityPublicKeyFunc)
payApiKey, err := secretsClient.Secret(ctx, secrets.GovUkPay)
if err != nil {
return err
}
payClient := pay.New(logger, httpClient, payBaseURL, payApiKey)
osApiKey, err := secretsClient.Secret(ctx, secrets.OrdnanceSurvey)
if err != nil {
return err
}
addressClient := place.NewClient(ordnanceSurveyBaseURL, osApiKey, httpClient)
notifyApiKey, err := secretsClient.Secret(ctx, secrets.GovUkNotify)
if err != nil {
return err
}
notifyClient, err := notify.New(logger, notifyIsProduction, notifyBaseURL, notifyApiKey, httpClient, eventClient)
if err != nil {
return err
}
evidenceS3Client := s3.NewClient(cfg, evidenceBucketName)
lambdaClient := lambda.New(cfg, v4.NewSigner(), httpClient, time.Now)
uidClient := uid.New(uidBaseURL, lambdaClient)
lpaStoreClient := lpastore.New(lpaStoreBaseURL, secretsClient, lambdaClient)
mux := http.NewServeMux()
mux.HandleFunc(page.Paths.HealthCheck.Service.String(), func(w http.ResponseWriter, r *http.Request) {})
mux.Handle(page.Paths.HealthCheck.Dependency.String(), page.DependencyHealthCheck(map[string]page.HealthChecker{
"uid": uidClient,
"onelogin": oneloginClient,
"lpaStore": lpaStoreClient,
}))
mux.HandleFunc("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, webDir+"/robots.txt")
})
mux.Handle("/static/", http.StripPrefix("/static", handlers.CompressHandler(page.CacheControlHeaders(http.FileServer(http.Dir(webDir+"/static/"))))))
mux.Handle(page.Paths.AuthRedirect.String(), page.AuthRedirect(logger, sessionStore))
mux.Handle(page.Paths.CookiesConsent.String(), page.CookieConsent(page.Paths))
mux.Handle("/cy/", http.StripPrefix("/cy", app.App(
devMode,
logger,
bundle.For(localize.Cy),
localize.Cy,
tmpls,
donorTmpls,
certificateProviderTmpls,
attorneyTmpls,
supporterTmpls,
sessionStore,
lpasDynamoClient,
appPublicURL,
payClient,
notifyClient,
addressClient,
oneloginClient,
evidenceS3Client,
eventClient,
lpaStoreClient,
searchClient,
)))
mux.Handle("/", app.App(
devMode,
logger,
bundle.For(localize.En),
localize.En,
tmpls,
donorTmpls,
certificateProviderTmpls,
attorneyTmpls,
supporterTmpls,
sessionStore,
lpasDynamoClient,
appPublicURL,
payClient,
notifyClient,
addressClient,
oneloginClient,
evidenceS3Client,
eventClient,
lpaStoreClient,
searchClient,
))
var handler http.Handler = mux
if xrayEnabled {
handler = telemetry.WrapHandler(mux)
}
server := &http.Server{
Addr: ":" + port,
Handler: page.Recover(tmpls.Get("error-500.gohtml"), logger, bundle, handler),
ReadHeaderTimeout: 20 * time.Second,
}
go func() {
if err := server.ListenAndServe(); err != nil {
logger.Error("listen and serve error", slog.Any("err", err))
os.Exit(1)
}
}()
logger.Info("started", slog.String("port", port))
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
sig := <-c
logger.Info("signal received", slog.String("signal", sig.String()))
tc, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
return server.Shutdown(tc)
}
func awsRegion(metadataURL string) (string, error) {
resp, err := http.Get(metadataURL + "/task")
if err != nil {
return "", err
}
defer resp.Body.Close()
var metadata struct{ TaskARN string }
if err := json.NewDecoder(resp.Body).Decode(&metadata); err != nil {
return "", err
}
parts := strings.Split(metadata.TaskARN, ":")
if len(parts) < 4 {
return "", fmt.Errorf("TaskARN contained only %d parts", len(parts))
}
return parts[3], nil
}
func parseLayoutTemplates(layoutDir string, funcs html.FuncMap) (*html.Template, error) {
return html.New("").Funcs(funcs).ParseGlob(filepath.Join(layoutDir, "*.*"))
}
func parseTemplates(templateDir string, layouts *html.Template) (template.Templates, error) {
files, err := filepath.Glob(filepath.Join(templateDir, "*.*"))
if err != nil {
return nil, err
}
tmpls := map[string]*html.Template{}
for _, file := range files {
clone, err := layouts.Clone()
if err != nil {
return nil, err
}
tmpl, err := clone.ParseFiles(file)
if err != nil {
return nil, err
}
tmpls[filepath.Base(file)] = tmpl
}
return tmpls, nil
}