lab: move s3 redirects to lambda function

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-08-11 15:05:32 +02:00
parent 92948cbfd7
commit a0c0aec486
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
8 changed files with 69 additions and 400 deletions

View File

@ -37,6 +37,7 @@ jobs:
elif [ "${{ github.ref }}" = "refs/heads/lab" ]; then elif [ "${{ github.ref }}" = "refs/heads/lab" ]; then
DOCS_URL="https://docs-lab.docker.com" DOCS_URL="https://docs-lab.docker.com"
DOCS_S3_BUCKET="" DOCS_S3_BUCKET=""
DOCS_S3_CONFIG="s3-config.json"
DOCS_LAMBDA_FUNCTION_CACHE="" DOCS_LAMBDA_FUNCTION_CACHE=""
DOCS_S3_CONFIG="_website-config-docs-lab.json" DOCS_S3_CONFIG="_website-config-docs-lab.json"
else else

View File

@ -63,5 +63,6 @@ RUN --mount=type=bind,target=. \
--mount=type=secret,id=AWS_SECRET_ACCESS_KEY \ --mount=type=secret,id=AWS_SECRET_ACCESS_KEY \
--mount=type=secret,id=AWS_SESSION_TOKEN \ --mount=type=secret,id=AWS_SESSION_TOKEN \
AWS_LAMBDA_FUNCTION_FILE=cloudfront-lambda-redirects.js \ 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 releaser aws cloudfront-update

View File

@ -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
}
}
]
}

View File

@ -96,18 +96,19 @@ func (s *AwsLambdaInvokeCmd) Run() error {
} }
type AwsCloudfrontUpdateCmd struct { type AwsCloudfrontUpdateCmd struct {
Region string `kong:"name='region',env='AWS_REGION'"` Region string `kong:"name='region',env='AWS_REGION'"`
Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"` Function string `kong:"name='lambda-function',env='AWS_LAMBDA_FUNCTION'"`
FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"` FunctionFile string `kong:"name='lambda-function-file',env='AWS_LAMBDA_FUNCTION_FILE'"`
CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"` CloudfrontID string `kong:"name='cloudfront-id',env='AWS_CLOUDFRONT_ID'"`
RedirectsJSON string `kong:"name='redirects-json',env='REDIRECTS_JSON'"` 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 { func (s *AwsCloudfrontUpdateCmd) Run() error {
var err error var err error
ver := time.Now().UTC().Format(time.RFC3339) 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 { if err != nil {
return fmt.Errorf("cannot create lambda function zip: %w", err) return fmt.Errorf("cannot create lambda function zip: %w", err)
} }
@ -227,18 +228,20 @@ func (s *AwsCloudfrontUpdateCmd) Run() error {
return nil 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) funcdt, err := os.ReadFile(funcFilename)
if err != nil { 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 var funcbuf bytes.Buffer
functpl := template.Must(template.New("").Parse(string(funcdt))) functpl := template.Must(template.New("").Parse(string(funcdt)))
if err = functpl.Execute(&funcbuf, struct { if err = functpl.Execute(&funcbuf, struct {
RedirectsJSON string RedirectsJSON string
RedirectsPrefixesJSON string
}{ }{
redirectsJSON, redirectsJSON,
redirectsPrefixesJSON,
}); err != nil { }); err != nil {
return nil, err return nil, err
} }

View File

@ -3,12 +3,13 @@
exports.handler = (event, context, callback) => { exports.handler = (event, context, callback) => {
//console.log("event", JSON.stringify(event)); //console.log("event", JSON.stringify(event));
const request = event.Records[0].cf.request; const request = event.Records[0].cf.request;
const redirects = JSON.parse(`{{.RedirectsJSON}}`); const redirects = JSON.parse(`{{.RedirectsJSON}}`);
for (let key in redirects) { for (let key in redirects) {
if (key !== request.uri) { if (key !== request.uri) {
continue; continue;
} }
//console.log(`redirect: ${key} to ${redirects[key]}`); //console.log(`redirect: ${request.uri} to ${redirects[key]}`);
const response = { const response = {
status: '301', status: '301',
statusDescription: 'Moved Permanently', statusDescription: 'Moved Permanently',
@ -22,5 +23,26 @@ exports.handler = (event, context, callback) => {
callback(null, response); callback(null, response);
return 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); callback(null, request);
}; };

View File

@ -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/": "/"
}

9
_releaser/s3-config.json Normal file
View File

@ -0,0 +1,9 @@
{
"ErrorDocument": {
"Key": "404.html"
},
"IndexDocument": {
"Suffix": "index.html"
},
"RedirectAllRequestsTo": null
}

View File

@ -112,7 +112,7 @@ variable "AWS_S3_BUCKET" {
default = "" default = ""
} }
variable "AWS_S3_CONFIG" { variable "AWS_S3_CONFIG" {
default = "_website-config-docs-stage.json" default = ""
} }
variable "AWS_CLOUDFRONT_ID" { variable "AWS_CLOUDFRONT_ID" {
default = "" default = ""