From 2849ef0ba4022aec0b44136b124bff3fc04cce55 Mon Sep 17 00:00:00 2001 From: Rotem Tamir Date: Thu, 3 Feb 2022 14:56:37 +0200 Subject: [PATCH] add support for error documents --- README.md | 1 + go.mod | 1 + go.sum | 12 +++++++++++- internal/config/config.go | 2 ++ internal/config/config_test.go | 2 ++ internal/controllers/s3.go | 3 +++ 6 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d730fa6..f51ffe3 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ AWS_ACCESS_KEY_ID | AWS `access key` for API access. | AWS_SECRET_ACCESS_KEY | AWS `secret key` for API access. | | EC2 Instance Role AWS_API_ENDPOINT | The endpoint for AWS API for local development. | | - INDEX_DOCUMENT | Name of your index document. | | index.html +ERROR_DOCUMENT | Name of the error document. | | DIRECTORY_LISTINGS | List files when a specified URL ends with /. | | false DIRECTORY_LISTINGS_FORMAT | Configures directory listing to be `html` (spider parsable) | | - HTTP_CACHE_CONTROL | Overrides S3's HTTP `Cache-Control` header. | | S3 Object metadata diff --git a/go.mod b/go.mod index ab42407..b7c9642 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,5 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/go-openapi/swag v0.19.5 github.com/stretchr/testify v1.3.0 + golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect ) diff --git a/go.sum b/go.sum index 99e50fa..6c2671d 100644 --- a/go.sum +++ b/go.sum @@ -9,18 +9,28 @@ github.com/go-openapi/swag v0.19.5 h1:lTz6Ys4CmqqCQmZPBlbQENR1/GucA2bzYTE12Pw4tF github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63 h1:nTT4s92Dgz2HlrB2NaMgvlfqHH39OgMhA7z3PK7PGD4= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk= +golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/config/config.go b/internal/config/config.go index 6e80c32..30f6a04 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,6 +22,7 @@ type config struct { // nolint S3Bucket string // AWS_S3_BUCKET S3KeyPrefix string // AWS_S3_KEY_PREFIX IndexDocument string // INDEX_DOCUMENT + ErrorDocument string // ERROR_DOCUMENT DirectoryListing bool // DIRECTORY_LISTINGS DirListingFormat string // DIRECTORY_LISTINGS_FORMAT HTTPCacheControl string // HTTP_CACHE_CONTROL (max-age=86400, no-cache ...) @@ -104,6 +105,7 @@ func Setup() { S3Bucket: os.Getenv("AWS_S3_BUCKET"), S3KeyPrefix: os.Getenv("AWS_S3_KEY_PREFIX"), IndexDocument: indexDocument, + ErrorDocument: os.Getenv("ERROR_DOCUMENT"), DirectoryListing: directoryListings, DirListingFormat: os.Getenv("DIRECTORY_LISTINGS_FORMAT"), HTTPCacheControl: os.Getenv("HTTP_CACHE_CONTROL"), diff --git a/internal/config/config_test.go b/internal/config/config_test.go index c1cd509..d064d97 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -56,6 +56,7 @@ func TestChangeDefaults(t *testing.T) { os.Setenv("IDLE_CONNECTION_TIMEOUT", "60") os.Setenv("DISABLE_COMPRESSION", "FALSE") os.Setenv("INSECURE_TLS", "t") + os.Setenv("ERROR_DOCUMENT", "404.html") Setup() @@ -69,6 +70,7 @@ func TestChangeDefaults(t *testing.T) { expected.IdleConnTimeout = time.Duration(60) * time.Second expected.DisableCompression = false expected.InsecureTLS = true + expected.ErrorDocument = "404.html" assert.Equal(t, expected, Config) } diff --git a/internal/controllers/s3.go b/internal/controllers/s3.go index 7243e7c..4749e24 100644 --- a/internal/controllers/s3.go +++ b/internal/controllers/s3.go @@ -65,6 +65,9 @@ func AwsS3(w http.ResponseWriter, r *http.Request) { } // Get a S3 object obj, err := client.S3get(c.S3Bucket, c.S3KeyPrefix+path, rangeHeader) + if err != nil && c.ErrorDocument != "" { + obj, err = client.S3get(c.S3Bucket, c.S3KeyPrefix+c.ErrorDocument, rangeHeader) + } if err != nil { code, message := toHTTPError(err) http.Error(w, message, code)