Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 484bc00

Browse files
committed
create signatre based on different region
1 parent 241f751 commit 484bc00

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

api/client/cli.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,24 @@ func NewDockerCli(in io.ReadCloser, out, err io.Writer, clientFlags *cli.ClientF
174174
if cloudConfig.AccessKey == "" || cloudConfig.SecretKey == "" {
175175
fmt.Fprintf(cli.err, "WARNING: null cloud config\n")
176176
}
177-
178-
client, err := client.NewClient(host, verStr, httpClient, customHeaders, cloudConfig.AccessKey, cloudConfig.SecretKey)
179-
if err != nil {
180-
return err
181-
}
182-
cli.client = client
183-
cli.host = host
184177
cli.region = clientFlags.Common.Region
185178
if cli.region == "" {
186179
if cli.region = cc.Region; cli.region == "" {
187180
cli.region = cli.getDefaultRegion()
188181
}
189182
}
183+
if !dft {
184+
if cli.region = cc.Region; cli.region == "" {
185+
cli.region = cliconfig.DefaultHyperRegion
186+
}
187+
}
190188

189+
client, err := client.NewClient(host, verStr, httpClient, customHeaders, cloudConfig.AccessKey, cloudConfig.SecretKey, cli.region)
190+
if err != nil {
191+
return err
192+
}
193+
cli.client = client
194+
cli.host = host
191195
if cli.in != nil {
192196
cli.inFd, cli.isTerminalIn = term.GetFdInfo(cli.in)
193197
}

vendor/src/github.com/docker/engine-api/client/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ type Client struct {
3131
version string
3232
// custom http headers configured by users.
3333
customHTTPHeaders map[string]string
34+
35+
// region
36+
region string
3437
}
3538

3639
// NewEnvClient initializes a new API client based on environment variables.
@@ -65,14 +68,15 @@ func NewEnvClient() (*Client, error) {
6568
}
6669
accessKey := os.Getenv("ACCESSKEY")
6770
secretKey := os.Getenv("SECRETKEY")
68-
return NewClient(host, os.Getenv("DOCKER_API_VERSION"), client, nil, accessKey, secretKey)
71+
region := os.Getenv("HYPER_REGION")
72+
return NewClient(host, os.Getenv("DOCKER_API_VERSION"), client, nil, accessKey, secretKey, region)
6973
}
7074

7175
// NewClient initializes a new API client for the given host and API version.
7276
// It won't send any version information if the version number is empty.
7377
// It uses the given http client as transport.
7478
// It also initializes the custom http headers to add to each request.
75-
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string, ak, sk string) (*Client, error) {
79+
func NewClient(host string, version string, client *http.Client, httpHeaders map[string]string, ak, sk, region string) (*Client, error) {
7680
proto, addr, basePath, err := ParseHost(host)
7781
if err != nil {
7882
return nil, err
@@ -92,6 +96,7 @@ func NewClient(host string, version string, client *http.Client, httpHeaders map
9296
secretKey: sk,
9397
version: version,
9498
customHTTPHeaders: httpHeaders,
99+
region: region,
95100
}, nil
96101
}
97102

vendor/src/github.com/docker/engine-api/client/hijack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (cli *Client) postHijacked(ctx context.Context, path string, query url.Valu
4545
req.Header.Set("Connection", "Upgrade")
4646
req.Header.Set("Upgrade", "tcp")
4747

48-
req = Sign4(cli.accessKey, cli.secretKey, req)
48+
req = Sign4(cli.accessKey, cli.secretKey, req, cli.region)
4949
conn, err := dial(cli.proto, cli.addr, cli.transport.TLSConfig())
5050

5151
if err != nil {

vendor/src/github.com/docker/engine-api/client/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (cli *Client) sendClientRequest(ctx context.Context, method, path string, q
101101
req.Header.Set("Content-Type", "text/plain")
102102
}
103103

104-
req = Sign4(cli.accessKey, cli.secretKey, req)
104+
req = Sign4(cli.accessKey, cli.secretKey, req, cli.region)
105105
resp, err := cancellable.Do(ctx, cli.transport, req)
106106

107107
if err != nil {

vendor/src/github.com/docker/engine-api/client/sign4.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ type AuthnHeader struct {
4848
Date string
4949
}
5050

51-
func Signiture4(secretKey string, req *http.Request, header *AuthnHeader) (bool, error) {
51+
func Signiture4(secretKey string, req *http.Request, header *AuthnHeader, region string) (bool, error) {
5252
meta := &metadata{
5353
algorithm: header.Algorithm,
5454
credentialScope: header.Scope,
5555
signedHeaders: header.SignedHeader,
5656
date: header.Date,
57-
region: "us-west-1",
57+
region: region,
5858
service: "hyper",
5959
}
6060

@@ -70,7 +70,7 @@ func Signiture4(secretKey string, req *http.Request, header *AuthnHeader) (bool,
7070
return signature == header.Signature, nil
7171
}
7272

73-
func Sign4(accessKey, secretKey string, req *http.Request) *http.Request {
73+
func Sign4(accessKey, secretKey string, req *http.Request, region string) *http.Request {
7474

7575
prepareRequestV4(req)
7676
meta := &metadata{}
@@ -79,7 +79,7 @@ func Sign4(accessKey, secretKey string, req *http.Request) *http.Request {
7979
hashedCanonReq := hashedCanonicalRequestV4(req, meta)
8080

8181
// Task 2
82-
stringToSign := stringToSignV4(req, hashedCanonReq, meta)
82+
stringToSign := stringToSignV4(req, hashedCanonReq, meta, region)
8383

8484
// Task 3
8585
signingKey := signingKeyV4(secretKey, meta.date, meta.region, meta.service)
@@ -179,13 +179,13 @@ func canonicalRequestV4FromMeta(request *http.Request, meta *metadata) (string,
179179
return canonicalRequest, true
180180
}
181181

182-
func stringToSignV4(request *http.Request, hashedCanonReq string, meta *metadata) string {
182+
func stringToSignV4(request *http.Request, hashedCanonReq string, meta *metadata, region string) string {
183183
// TASK 2. http://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
184184

185185
requestTs := request.Header.Get(headerDate)
186186

187187
meta.algorithm = metaAlgorithm
188-
meta.service, meta.region = serviceAndRegion(request.Host)
188+
meta.service, meta.region = serviceAndRegion(request.Host, region)
189189
meta.date = tsDateV4(requestTs)
190190
meta.credentialScope = concat("/", meta.date, meta.region, meta.service, keyPartsRequest)
191191

@@ -343,9 +343,9 @@ func normquery(v url.Values) string {
343343
}
344344

345345
// serviceAndRegion parsers a hostname to find out which ones it is.
346-
func serviceAndRegion(host string) (service string, region string) {
346+
func serviceAndRegion(host, r string) (service string, region string) {
347347
// These are the defaults if the hostname doesn't suggest something else
348-
region = "us-west-1"
348+
region = r
349349
service = "hyper"
350350

351351
// region.hyper.sh

0 commit comments

Comments
 (0)