Skip to content

Commit

Permalink
Add flag to allow s3 virtual style addressing
Browse files Browse the repository at this point in the history
  • Loading branch information
EdwardDowling committed Jan 20, 2025
1 parent ea5d935 commit aabc379
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ const (
// SSEKMSKey is an optional switch to use an KMS CMK key for S3 SSE.
SSEKMSKey = "sse_kms_key"

// S3UseVirtualStyleAddressing is an optional switch to use use a virtual-hosted–style URI.
S3UseVirtualStyleAddressing = "use_s3_virtual_style_addressing"

// SchemeFile configures local disk-based file storage for audit events
SchemeFile = "file"

Expand Down
4 changes: 4 additions & 0 deletions docs/pages/reference/backends.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,10 @@ Service reads these parameters to configure its interactions with S3:
- `use_fips_endpoint=true` - [Configure S3 FIPS
endpoints](#configuring-aws-fips-endpoints)

- `use_s3_virtual_style_addressing` - Whether to use virtual-host-style instead of path-style URLs for the
bucket. Only applies when a custom endpoint is set. Defaults to false when unset. If used
without a custom endpoint set, this option has no effect.

### S3 IAM policy

(!docs/pages/includes/s3-iam-policy.mdx!)
Expand Down
21 changes: 20 additions & 1 deletion lib/events/s3sessions/s3handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ type Config struct {
Insecure bool
// DisableServerSideEncryption is an optional switch to opt out of SSE in case the provider does not support it
DisableServerSideEncryption bool

// UseVirtualStyleAddressing use a virtual-hosted–style URI.
// Path style e.g. https://s3.region-code.amazonaws.com/bucket-name/key-name
// Virtual hosted style e.g. https://bucket-name.s3.region-code.amazonaws.com/key-name
// Teleport defaults to path-style addressing for better interoperability
// with 3rd party S3-compatible services out of the box.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/VirtualHosting.html for more details.
UseVirtualStyleAddressing bool
}

// SetFromURL sets values on the Config from the supplied URI
Expand Down Expand Up @@ -148,6 +156,17 @@ func (s *Config) SetFromURL(in *url.URL, inRegion string) error {
}
}

if val := in.Query().Get(teleport.S3UseVirtualStyleAddressing); val != "" {
useVirtualStyleAddressing, err := strconv.ParseBool(val)
if err != nil {
return trace.BadParameter(boolErrorTemplate, in.String(), teleport.S3UseVirtualStyleAddressing, val)
}
s.UseVirtualStyleAddressing = useVirtualStyleAddressing
} else {
// Default to false for backwards compatibility
s.UseVirtualStyleAddressing = false
}

s.Region = region
s.Bucket = in.Host
s.Path = in.Path
Expand Down Expand Up @@ -225,7 +244,7 @@ func NewHandler(ctx context.Context, cfg Config) (*Handler, error) {
opts = append(opts, config.WithBaseEndpoint(cfg.Endpoint))

s3Opts = append(s3Opts, func(options *s3.Options) {
options.UsePathStyle = true
options.UsePathStyle = !cfg.UseVirtualStyleAddressing
})
}

Expand Down
21 changes: 21 additions & 0 deletions lib/events/s3sessions/s3handler_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ func TestConfig_SetFromURL(t *testing.T) {
require.Equal(t, types.ClusterAuditConfigSpecV2_FIPS_DISABLED, config.UseFIPSEndpoint)
},
},
{
name: "path style addressing enabled via url",
url: "s3://path/bucket/adit?use_s3_virtual_style_addressing=false",
cfgAssertion: func(t *testing.T, config Config) {
require.False(t, config.UseVirtualStyleAddressing)
},
},
{
name: "path style addressing enabled by default",
url: "s3://path/bucket/audit",
cfgAssertion: func(t *testing.T, config Config) {
require.False(t, config.UseVirtualStyleAddressing)
},
},
{
name: "path style addressing disabled via url",
url: "s3://path/bucket/audit?use_s3_virtual_style_addressing=true",
cfgAssertion: func(t *testing.T, config Config) {
require.True(t, config.UseVirtualStyleAddressing)
},
},
}

for _, tt := range cases {
Expand Down

0 comments on commit aabc379

Please sign in to comment.