Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(storage): add allow list check on S3 creation #858

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion storage/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Scalingo/go-utils/storage

go 1.20
go 1.21

require (
github.com/Scalingo/go-utils/logger v1.2.0
Expand Down
5 changes: 5 additions & 0 deletions storage/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
<<<<<<< HEAD
=======
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
>>>>>>> 9ef694d (fix(s3): update go version)
Comment on lines +40 to +44
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still a remaining conflict here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry, missed that one

github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
37 changes: 31 additions & 6 deletions storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
stderrors "errors"
"fmt"
"io"
"os"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -302,12 +304,15 @@ func s3Config(cfg S3Config) aws.Config {
Credentials: credentials,
}
if cfg.Endpoint != "" {
config.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: "https://" + cfg.Endpoint,
SigningRegion: cfg.Region,
}, nil
})
if endpointIsAllowed(cfg.Endpoint) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion(wording):

Suggested change
if endpointIsAllowed(cfg.Endpoint) {
if isEndpointAllowed(cfg.Endpoint) {

//nolint:all // AWS v1 function is deprecated, switching to v2 requires a refactoring of the package
config.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
URL: "https://" + cfg.Endpoint,
SigningRegion: cfg.Region,
}, nil
})
}
}

return config
Expand All @@ -316,3 +321,23 @@ func s3Config(cfg S3Config) aws.Config {
func fullPath(path string) string {
return strings.TrimLeft("/"+path, "/")
}

func getAllowListFromEnv() []string {
s3EndpointsAllowList := os.Getenv("OBJECT_STORAGE_ALLOWLIST")
if s3EndpointsAllowList == "" {
log := logger.Default()
log.Warningln("S3 endpoint allowlist is not configured in your environment")
return nil
}
return strings.Split(s3EndpointsAllowList, ",")
}

func endpointIsAllowed(endpoint string) bool {
allowedEndpoints := getAllowListFromEnv()
if allowedEndpoints != nil {
return slices.ContainsFunc(getAllowListFromEnv(), func(allowedEndpoint string) bool {
return strings.HasPrefix(endpoint, allowedEndpoint)
})
}
return true
}