-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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): respect WithEndpoint for SignedURLs and PostPolicy #8113
Changes from 2 commits
dbc2d68
672ec6f
35111f9
0e36ff5
9569db6
2bd4ab5
9e90cff
64440bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,13 +262,13 @@ const ( | |
SigningSchemeV4 | ||
) | ||
|
||
// URLStyle determines the style to use for the signed URL. pathStyle is the | ||
// URLStyle determines the style to use for the signed URL. PathStyle is the | ||
// default. All non-default options work with V4 scheme only. See | ||
// https://cloud.google.com/storage/docs/request-endpoints for details. | ||
type URLStyle interface { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also update the docs for PathStyle to indicate that the provided hostname is taken into account? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. To-do on virtual hosted style, if we decide to support it. |
||
// host should return the host portion of the signed URL, not including | ||
// the scheme (e.g. storage.googleapis.com). | ||
host(endpoint, bucket string) string | ||
host(hostname, bucket string) string | ||
|
||
// path should return the path portion of the signed URL, which may include | ||
// both the bucket and object name or only the object name depending on the | ||
|
@@ -284,33 +284,27 @@ type bucketBoundHostname struct { | |
hostname string | ||
} | ||
|
||
func (s pathStyle) host(endpoint, bucket string) string { | ||
if endpoint != "" { | ||
return stripScheme(endpoint) | ||
func (s pathStyle) host(hostname, bucket string) string { | ||
if hostname != "" { | ||
return stripScheme(hostname) | ||
} | ||
|
||
// This check is needed for clientless calls to SignedURL | ||
if host := os.Getenv("STORAGE_EMULATOR_HOST"); host != "" { | ||
return stripScheme(host) | ||
} | ||
|
||
// Fallback to default endpoint - clientless calls to SignURL/PostPolicy | ||
// will not have an endpoint attached | ||
return "storage.googleapis.com" | ||
} | ||
|
||
func (s virtualHostedStyle) host(endpoint, bucket string) string { | ||
if endpoint != "" { | ||
return bucket + "." + stripScheme(endpoint) | ||
func (s virtualHostedStyle) host(hostname, bucket string) string { | ||
if hostname != "" { | ||
return bucket + "." + stripScheme(hostname) | ||
} | ||
|
||
// This check is needed for clientless calls to SignedURL | ||
if host := os.Getenv("STORAGE_EMULATOR_HOST"); host != "" { | ||
return bucket + "." + stripScheme(host) | ||
} | ||
|
||
// Fallback to default endpoint - clientless calls to SignURL/PostPolicy | ||
// will not have an endpoint attached | ||
return bucket + "." + "storage.googleapis.com" | ||
} | ||
|
||
|
@@ -335,7 +329,10 @@ func (s bucketBoundHostname) path(bucket, object string) string { | |
} | ||
|
||
// PathStyle is the default style, and will generate a URL of the form | ||
// "storage.googleapis.com/<bucket-name>/<object-name>". | ||
// "<host-name>/<bucket-name>/<object-name>". By default, <host-name> is | ||
// storage.googleapis.com, but setting an endpoint on the storage Client or | ||
// through STORAGE_EMULATOR_HOST overrides this. Setting Hostname on | ||
// SignedURLOptions or PostPolicyV4Options overrides everything else. | ||
func PathStyle() URLStyle { | ||
return pathStyle{} | ||
} | ||
|
@@ -457,7 +454,11 @@ type SignedURLOptions struct { | |
// SigningSchemeV2. | ||
Scheme SigningScheme | ||
|
||
endpoint string | ||
// Hostname sets the host of the signed URL. This field overrides any | ||
// endpoint set on a storage Client or through STORAGE_EMULATOR_HOST. | ||
// Not compatible with BucketBoundHostname URLStyle. | ||
// Optional. | ||
Hostname string | ||
} | ||
|
||
func (opts *SignedURLOptions) clone() *SignedURLOptions { | ||
|
@@ -474,7 +475,7 @@ func (opts *SignedURLOptions) clone() *SignedURLOptions { | |
Style: opts.Style, | ||
Insecure: opts.Insecure, | ||
Scheme: opts.Scheme, | ||
endpoint: opts.endpoint, | ||
Hostname: opts.Hostname, | ||
} | ||
} | ||
|
||
|
@@ -733,7 +734,7 @@ func signedURLV4(bucket, name string, opts *SignedURLOptions, now time.Time) (st | |
fmt.Fprintf(buf, "%s\n", escapedQuery) | ||
|
||
// Fill in the hostname based on the desired URL style. | ||
u.Host = opts.Style.host(opts.endpoint, bucket) | ||
u.Host = opts.Style.host(opts.Hostname, bucket) | ||
|
||
// Fill in the URL scheme. | ||
if opts.Insecure { | ||
|
@@ -867,7 +868,7 @@ func signedURLV2(bucket, name string, opts *SignedURLOptions) (string, error) { | |
} | ||
encoded := base64.StdEncoding.EncodeToString(b) | ||
u.Scheme = "https" | ||
u.Host = PathStyle().host(opts.endpoint, bucket) | ||
u.Host = PathStyle().host(opts.Hostname, bucket) | ||
q := u.Query() | ||
q.Set("GoogleAccessId", opts.GoogleAccessID) | ||
q.Set("Expires", fmt.Sprintf("%d", opts.Expires.Unix())) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say "only compatible with PathStyle" because we omitted VirtualHosted also. Same in the docs for SignedURLOptions below.