1 回答

TA貢獻1805條經驗 獲得超9個贊
一些帖子提到了簽名請求。但我不確定這是否是正確的方法
是的,IAM Auth 需要請求簽名(請參閱此處和此處的文檔)。當前方法是Signature v4。如果 Api Gateway 具有預期的標頭,則它會接受請求?!昂灻笔翘砑诱_標頭的過程:
# signature is derived from your secret key and the request contents.
--header 'Authorization: AWS4-HMAC-SHA256 Credential=AKIASIAXTWO8D5GSN4CS/20220712/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date, Signature=f2d8478ceff83d5cd0696502cb58a8331304846d11367d74608295c7acbfba0c'
# date prevents third parties from intercepting your request and resubmitting it later
--header 'X-Amz-Date: 20220712T124302Z'
AWS SDK 中必須有一種方法可以封裝簽名 HTTP 請求等并完成這項工作
對于多種語言(JS、Java 等,但不是 Go),get-sdk命令可以為您的 Rest API 生成一個SDK 客戶端,除了其他便利之外,它還包裝了簽名過程:client.privateGet(params, body, additionalParams)
.
必須有一種方法可以通過“以某種方式”附加 aws creds 使用 http inbuild 包來做到這一點
使用普通的舊 SDK,添加標頭需要做更多的工作,可以輕松包裝為可重用的類型:
ctx := context.TODO()
// define the request
endpoint := "https://cbi3vltq21.execute-api.us-east-1.amazonaws.com"
u, _ := url.ParseRequestURI(endpoint)
u.Path = "prod/private"
req, _ := http.NewRequest("GET", u.String(), nil)
// get the credentials from the local config files
cfg, _ := config.LoadDefaultConfig(ctx,
config.WithRegion("us-east-1"),
config.WithSharedConfigProfile("my-profile"))
creds, _ := cfg.Credentials.Retrieve(ctx)
// hash the request body - hex value used in the signature
hash := sha256.Sum256([]byte("")) // if the request has no body, use the empty string
hexHash := fmt.Sprintf("%x", hash)
// add the Authorization and X-Amz-Date headers to the request
signer := v4.NewSigner()
_ = signer.SignHTTP(ctx, creds, req, hexHash, "execute-api", cfg.Region, time.Now())
// execute the request
client := &http.Client{}
resp, _ := client.Do(req)
H/T 這個SO answer。
- 1 回答
- 0 關注
- 171 瀏覽
添加回答
舉報