From a0c0aec4860d77e5153ecdcc1001f5b7e85f068f Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 11 Aug 2022 15:05:32 +0200 Subject: [PATCH] lab: move s3 redirects to lambda function Signed-off-by: CrazyMax --- .github/workflows/publish.yml | 1 + _releaser/Dockerfile | 3 +- _releaser/_website-config-docs-lab.json | 388 ----------------------- _releaser/aws.go | 21 +- _releaser/cloudfront-lambda-redirects.js | 24 +- _releaser/redirects-prefixes.json | 21 ++ _releaser/s3-config.json | 9 + docker-bake.hcl | 2 +- 8 files changed, 69 insertions(+), 400 deletions(-) delete mode 100644 _releaser/_website-config-docs-lab.json create mode 100644 _releaser/redirects-prefixes.json create mode 100644 _releaser/s3-config.json diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index a3a423f908..503b95b736 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -37,6 +37,7 @@ jobs: elif [ "${{ github.ref }}" = "refs/heads/lab" ]; then DOCS_URL="https://docs-lab.docker.com" DOCS_S3_BUCKET="" + DOCS_S3_CONFIG="s3-config.json" DOCS_LAMBDA_FUNCTION_CACHE="" DOCS_S3_CONFIG="_website-config-docs-lab.json" else diff --git a/_releaser/Dockerfile b/_releaser/Dockerfile index a88ada60f8..cdd4e5edc4 100644 --- a/_releaser/Dockerfile +++ b/_releaser/Dockerfile @@ -63,5 +63,6 @@ RUN --mount=type=bind,target=. \ --mount=type=secret,id=AWS_SECRET_ACCESS_KEY \ --mount=type=secret,id=AWS_SESSION_TOKEN \ AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \ - REDIRECTS_JSON=$(cat /site/redirects.json) \ + REDIRECTS_JSON=$(jq -c '.' /site/redirects.json) \ + REDIRECTS_PREFIXES_JSON=$(jq -c '.' redirects-prefixes.json) \ releaser aws cloudfront-update diff --git a/_releaser/_website-config-docs-lab.json b/_releaser/_website-config-docs-lab.json deleted file mode 100644 index 85fc6e8f1d..0000000000 --- a/_releaser/_website-config-docs-lab.json +++ /dev/null @@ -1,388 +0,0 @@ -{ - "ErrorDocument": { - "Key": "404.html" - }, - "IndexDocument": { - "Suffix": "index.html" - }, - "RedirectAllRequestsTo": null, - "RoutingRules": [ - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.4/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.5/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.6/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.7/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.8/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.9/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.10/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.11/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.12/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v1.13/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v17.03/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v17.09/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v17.12/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/licensing/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/get-support/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/cluster/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/supported-platforms/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/ucp/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/dtr/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "compliance/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "datacenter/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v18.09/ee/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v18.03/ee/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v17.06/enterprise/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/docker-ee/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "ee/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": null, - "ReplaceKeyWith": "" - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v17.06/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v18.03/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - }, - { - "Condition": { - "HttpErrorCodeReturnedEquals": null, - "KeyPrefixEquals": "v18.09/" - }, - "Redirect": { - "HostName": "docs-lab.docker.com", - "HttpRedirectCode": null, - "Protocol": "https", - "ReplaceKeyPrefixWith": "", - "ReplaceKeyWith": null - } - } - ] -} diff --git a/_releaser/aws.go b/_releaser/aws.go index 529ff0ac2c..1c04f8f710 100644 --- a/_releaser/aws.go +++ b/_releaser/aws.go @@ -96,18 +96,19 @@ func (s *AwsLambdaInvokeCmd) Run() error { } type AwsCloudfrontUpdateCmd struct { - Region string `kong:"name='region',env='AWS_REGION'"` - Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"` - FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"` - CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"` - RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"` + Region string `kong:"name='region',env='AWS_REGION'"` + Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"` + FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"` + CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"` + RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"` + RedirectsPrefixesJSON string `kong:"name='redirects-prefixes-json',env='REDIRECTS_PREFIXES_JSON'"` } func (s *AwsCloudfrontUpdateCmd) Run() error { var err error ver := time.Now().UTC().Format(time.RFC3339) - zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON) + zipdt, err := getLambdaFunctionZip(s.FunctionFile, s.RedirectsJSON, s.RedirectsPrefixesJSON) if err != nil { return fmt.Errorf("cannot create lambda function zip: %w", err) } @@ -227,18 +228,20 @@ func (s *AwsCloudfrontUpdateCmd) Run() error { return nil } -func getLambdaFunctionZip(funcFilename string, redirectsJSON string) ([]byte, error) { +func getLambdaFunctionZip(funcFilename string, redirectsJSON string, redirectsPrefixesJSON string) ([]byte, error) { funcdt, err := os.ReadFile(funcFilename) if err != nil { - return nil, fmt.Errorf("failed to read lambda function file %q: %w", err) + return nil, fmt.Errorf("failed to read lambda function file %q: %w", funcFilename, err) } var funcbuf bytes.Buffer functpl := template.Must(template.New("").Parse(string(funcdt))) if err = functpl.Execute(&funcbuf, struct { - RedirectsJSON string + RedirectsJSON string + RedirectsPrefixesJSON string }{ redirectsJSON, + redirectsPrefixesJSON, }); err != nil { return nil, err } diff --git a/_releaser/cloudfront-lambda-redirects.js b/_releaser/cloudfront-lambda-redirects.js index 9d86e1288c..face10fe00 100644 --- a/_releaser/cloudfront-lambda-redirects.js +++ b/_releaser/cloudfront-lambda-redirects.js @@ -3,12 +3,13 @@ exports.handler = (event, context, callback) => { //console.log("event", JSON.stringify(event)); const request = event.Records[0].cf.request; + const redirects = JSON.parse(`{{.RedirectsJSON}}`); for (let key in redirects) { if (key !== request.uri) { continue; } - //console.log(`redirect: ${key} to ${redirects[key]}`); + //console.log(`redirect: ${request.uri} to ${redirects[key]}`); const response = { status: '301', statusDescription: 'Moved Permanently', @@ -22,5 +23,26 @@ exports.handler = (event, context, callback) => { callback(null, response); return } + + const redirectsPrefixes = JSON.parse(`{{.RedirectsPrefixesJSON}}`); + for (let key in redirectsPrefixes) { + if (!request.uri.startsWith(key)) { + continue; + } + //console.log(`redirect: ${request.uri} to ${redirectsPrefixes[key]}`); + const response = { + status: '301', + statusDescription: 'Moved Permanently', + headers: { + location: [{ + key: 'Location', + value: redirectsPrefixes[key], + }], + }, + } + callback(null, response); + return + } + callback(null, request); }; diff --git a/_releaser/redirects-prefixes.json b/_releaser/redirects-prefixes.json new file mode 100644 index 0000000000..a032ff2de9 --- /dev/null +++ b/_releaser/redirects-prefixes.json @@ -0,0 +1,21 @@ +{ + "/compliance/": "/", + "/datacenter/": "/", + "/ee/": "/", + "/v1.4/": "/", + "/v1.5/": "/", + "/v1.6/": "/", + "/v1.7/": "/", + "/v1.8/": "/", + "/v1.9/": "/", + "/v1.10/": "/", + "/v1.11/": "/", + "/v1.12/": "/", + "/v1.13/": "/", + "/v17.03/": "/", + "/v17.06/": "/", + "/v17.09/": "/", + "/v17.12/": "/", + "/v18.03/": "/", + "/v18.09/": "/" +} diff --git a/_releaser/s3-config.json b/_releaser/s3-config.json new file mode 100644 index 0000000000..1040162edf --- /dev/null +++ b/_releaser/s3-config.json @@ -0,0 +1,9 @@ +{ + "ErrorDocument": { + "Key": "404.html" + }, + "IndexDocument": { + "Suffix": "index.html" + }, + "RedirectAllRequestsTo": null +} diff --git a/docker-bake.hcl b/docker-bake.hcl index 32e9ca305c..4f19155b3b 100644 --- a/docker-bake.hcl +++ b/docker-bake.hcl @@ -112,7 +112,7 @@ variable "AWS_S3_BUCKET" { default = "" } variable "AWS_S3_CONFIG" { - default = "_website-config-docs-stage.json" + default = "" } variable "AWS_CLOUDFRONT_ID" { default = ""