Skip to content

Commit b80f29a

Browse files
committed
Support a separate URL base for pre-signed URLs
This allows the Ark server to use one URL for the majority of communications with S3 (or compatible) object storage, and a different URL base for pre-signed URLs (for streaming logs, etc. to clients). Signed-off-by: Andy Goldstein <[email protected]>
1 parent 39d9155 commit b80f29a

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

pkg/cloudprovider/aws/object_store.go

+42-16
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
const (
3535
s3URLKey = "s3Url"
36+
publicURLKey = "publicUrl"
3637
kmsKeyIDKey = "kmsKeyId"
3738
s3ForcePathStyleKey = "s3ForcePathStyle"
3839
bucketKey = "bucket"
@@ -41,6 +42,7 @@ const (
4142
type objectStore struct {
4243
log logrus.FieldLogger
4344
s3 *s3.S3
45+
preSignS3 *s3.S3
4446
s3Uploader *s3manager.Uploader
4547
kmsKeyID string
4648
}
@@ -53,6 +55,7 @@ func (o *objectStore) Init(config map[string]string) error {
5355
var (
5456
region = config[regionKey]
5557
s3URL = config[s3URLKey]
58+
publicURL = config[publicURLKey]
5659
kmsKeyID = config[kmsKeyIDKey]
5760
s3ForcePathStyleVal = config[s3ForcePathStyleKey]
5861

@@ -82,20 +85,52 @@ func (o *objectStore) Init(config map[string]string) error {
8285
}
8386
}
8487

88+
serverConfig, err := newAWSConfig(s3URL, region, s3ForcePathStyle)
89+
if err != nil {
90+
return err
91+
}
92+
93+
serverSession, err := getSession(serverConfig)
94+
if err != nil {
95+
return err
96+
}
97+
98+
o.s3 = s3.New(serverSession)
99+
o.s3Uploader = s3manager.NewUploader(serverSession)
100+
o.kmsKeyID = kmsKeyID
101+
102+
if publicURL != "" {
103+
publicConfig, err := newAWSConfig(publicURL, region, s3ForcePathStyle)
104+
if err != nil {
105+
return err
106+
}
107+
publicSession, err := getSession(publicConfig)
108+
if err != nil {
109+
return err
110+
}
111+
o.preSignS3 = s3.New(publicSession)
112+
} else {
113+
o.preSignS3 = o.s3
114+
}
115+
116+
return nil
117+
}
118+
119+
func newAWSConfig(url, region string, forcePathStyle bool) (*aws.Config, error) {
85120
awsConfig := aws.NewConfig().
86121
WithRegion(region).
87-
WithS3ForcePathStyle(s3ForcePathStyle)
122+
WithS3ForcePathStyle(forcePathStyle)
88123

89-
if s3URL != "" {
90-
if !IsValidS3URLScheme(s3URL) {
91-
return errors.Errorf("Invalid s3Url: %s", s3URL)
124+
if url != "" {
125+
if !IsValidS3URLScheme(url) {
126+
return nil, errors.Errorf("Invalid s3 url: %s", url)
92127
}
93128

94129
awsConfig = awsConfig.WithEndpointResolver(
95130
endpoints.ResolverFunc(func(service, region string, optFns ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
96131
if service == endpoints.S3ServiceID {
97132
return endpoints.ResolvedEndpoint{
98-
URL: s3URL,
133+
URL: url,
99134
}, nil
100135
}
101136

@@ -104,16 +139,7 @@ func (o *objectStore) Init(config map[string]string) error {
104139
)
105140
}
106141

107-
sess, err := getSession(awsConfig)
108-
if err != nil {
109-
return err
110-
}
111-
112-
o.s3 = s3.New(sess)
113-
o.s3Uploader = s3manager.NewUploader(sess)
114-
o.kmsKeyID = kmsKeyID
115-
116-
return nil
142+
return awsConfig, nil
117143
}
118144

119145
func (o *objectStore) PutObject(bucket, key string, body io.Reader) error {
@@ -202,7 +228,7 @@ func (o *objectStore) DeleteObject(bucket, key string) error {
202228
}
203229

204230
func (o *objectStore) CreateSignedURL(bucket, key string, ttl time.Duration) (string, error) {
205-
req, _ := o.s3.GetObjectRequest(&s3.GetObjectInput{
231+
req, _ := o.preSignS3.GetObjectRequest(&s3.GetObjectInput{
206232
Bucket: aws.String(bucket),
207233
Key: aws.String(key),
208234
})

0 commit comments

Comments
 (0)