Fixed nits from Cody.

Change-Id: I037f7313217d75f29f7150c53c1cd28a0ec7ea35
This commit is contained in:
Patrick Jones 2021-02-11 13:15:22 -08:00
parent 483d2860a6
commit 3cb804a538
5 changed files with 16 additions and 8 deletions

View File

@ -64,7 +64,7 @@ func (its impersonateTokenSource) Token() (*oauth2.Token, error) {
return nil, fmt.Errorf("oauth2/google: unable to read body: %v", err) return nil, fmt.Errorf("oauth2/google: unable to read body: %v", err)
} }
if c := resp.StatusCode; c < 200 || c > 299 { if c := resp.StatusCode; c < 200 || c > 299 {
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, string(body)) return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
} }
var accessTokenResp impersonateTokenResponse var accessTokenResp impersonateTokenResponse

View File

@ -66,7 +66,7 @@ func ExchangeToken(ctx context.Context, endpoint string, request *STSTokenExchan
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
if c := resp.StatusCode; c < 200 || c > 299 { if c := resp.StatusCode; c < 200 || c > 299 {
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, string(body)) return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
} }
var stsResp STSTokenExchangeResponse var stsResp STSTokenExchangeResponse
err = json.Unmarshal(body, &stsResp) err = json.Unmarshal(body, &stsResp)

View File

@ -39,18 +39,18 @@ func (cs urlCredentialSource) subjectToken() (string, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
tokenBytes, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20)) respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
if err != nil { if err != nil {
return "", fmt.Errorf("oauth2/google: invalid body in subject token URL query: %v", err) return "", fmt.Errorf("oauth2/google: invalid body in subject token URL query: %v", err)
} }
if c := resp.StatusCode; c < 200 || c > 299 { if c := resp.StatusCode; c < 200 || c > 299 {
return "", fmt.Errorf("oauth2/google: status code %d: %s", c, string(tokenBytes)) return "", fmt.Errorf("oauth2/google: status code %d: %s", c, respBody)
} }
switch cs.Format.Type { switch cs.Format.Type {
case "json": case "json":
jsonData := make(map[string]interface{}) jsonData := make(map[string]interface{})
err = json.Unmarshal(tokenBytes, &jsonData) err = json.Unmarshal(respBody, &jsonData)
if err != nil { if err != nil {
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err) return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
} }
@ -64,9 +64,9 @@ func (cs urlCredentialSource) subjectToken() (string, error) {
} }
return token, nil return token, nil
case "text": case "text":
return string(tokenBytes), nil return string(respBody), nil
case "": case "":
return string(tokenBytes), nil return string(respBody), nil
default: default:
return "", errors.New("oauth2/google: invalid credential_source file format type") return "", errors.New("oauth2/google: invalid credential_source file format type")
} }

View File

@ -7,6 +7,7 @@ package externalaccount
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -19,11 +20,18 @@ func TestRetrieveURLSubjectToken_Text(t *testing.T) {
if r.Method != "GET" { if r.Method != "GET" {
t.Errorf("Unexpected request method, %v is found", r.Method) t.Errorf("Unexpected request method, %v is found", r.Method)
} }
fmt.Println(r.Header)
if r.Header.Get("Metadata") != "True" {
t.Errorf("Metadata header not properly included.")
}
w.Write([]byte("testTokenValue")) w.Write([]byte("testTokenValue"))
})) }))
heads := make(map[string]string)
heads["Metadata"] = "True"
cs := CredentialSource{ cs := CredentialSource{
URL: ts.URL, URL: ts.URL,
Format: format{Type: fileTypeText}, Format: format{Type: fileTypeText},
Headers: heads,
} }
tfc := testFileConfig tfc := testFileConfig
tfc.CredentialSource = cs tfc.CredentialSource = cs