Skip to content

Commit 16ff188

Browse files
gIthurielcodyoss
authored andcommitted
google: manual testing fixes
I found some errors while manually testing service account impersonation on Azure. This PR includes the fixes that I made. Change-Id: Ia2b194be6c9a7c843e615f9789c8f8203bcbc151 GitHub-Last-Rev: 5690716 GitHub-Pull-Request: #475 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/291209 Reviewed-by: Cody Oss <codyoss@google.com> Trust: Cody Oss <codyoss@google.com> Trust: Tyler Bui-Palsulich <tbp@google.com> Run-TryBot: Cody Oss <codyoss@google.com> TryBot-Result: Go Bot <gobot@golang.org>
1 parent 6667018 commit 16ff188

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

google/internal/externalaccount/basecredentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (c *Config) parse(ctx context.Context) (baseCredentialSource, error) {
9696
} else if c.CredentialSource.File != "" {
9797
return fileCredentialSource{File: c.CredentialSource.File, Format: c.CredentialSource.Format}, nil
9898
} else if c.CredentialSource.URL != "" {
99-
return urlCredentialSource{URL: c.CredentialSource.URL, Format: c.CredentialSource.Format, ctx: ctx}, nil
99+
return urlCredentialSource{URL: c.CredentialSource.URL, Headers: c.CredentialSource.Headers, Format: c.CredentialSource.Format, ctx: ctx}, nil
100100
}
101101
return nil, fmt.Errorf("oauth2/google: unable to parse credential source")
102102
}

google/internal/externalaccount/sts_exchange.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"io"
12+
"io/ioutil"
1213
"net/http"
1314
"net/url"
1415
"strconv"
@@ -63,9 +64,12 @@ func ExchangeToken(ctx context.Context, endpoint string, request *STSTokenExchan
6364
}
6465
defer resp.Body.Close()
6566

66-
bodyJson := json.NewDecoder(io.LimitReader(resp.Body, 1<<20))
67+
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
68+
if c := resp.StatusCode; c < 200 || c > 299 {
69+
return nil, fmt.Errorf("oauth2/google: status code %d: %s", c, body)
70+
}
6771
var stsResp STSTokenExchangeResponse
68-
err = bodyJson.Decode(&stsResp)
72+
err = json.Unmarshal(body, &stsResp)
6973
if err != nil {
7074
return nil, fmt.Errorf("oauth2/google: failed to unmarshal response body from Secure Token Server: %v", err)
7175

google/internal/externalaccount/urlcredsource.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ func (cs urlCredentialSource) subjectToken() (string, error) {
3939
}
4040
defer resp.Body.Close()
4141

42-
tokenBytes, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
42+
respBody, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
4343
if err != nil {
4444
return "", fmt.Errorf("oauth2/google: invalid body in subject token URL query: %v", err)
4545
}
46+
if c := resp.StatusCode; c < 200 || c > 299 {
47+
return "", fmt.Errorf("oauth2/google: status code %d: %s", c, respBody)
48+
}
4649

4750
switch cs.Format.Type {
4851
case "json":
4952
jsonData := make(map[string]interface{})
50-
err = json.Unmarshal(tokenBytes, &jsonData)
53+
err = json.Unmarshal(respBody, &jsonData)
5154
if err != nil {
5255
return "", fmt.Errorf("oauth2/google: failed to unmarshal subject token file: %v", err)
5356
}
@@ -61,9 +64,9 @@ func (cs urlCredentialSource) subjectToken() (string, error) {
6164
}
6265
return token, nil
6366
case "text":
64-
return string(tokenBytes), nil
67+
return string(respBody), nil
6568
case "":
66-
return string(tokenBytes), nil
69+
return string(respBody), nil
6770
default:
6871
return "", errors.New("oauth2/google: invalid credential_source file format type")
6972
}

google/internal/externalaccount/urlcredsource_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package externalaccount
77
import (
88
"context"
99
"encoding/json"
10+
"fmt"
1011
"net/http"
1112
"net/http/httptest"
1213
"testing"
@@ -19,11 +20,18 @@ func TestRetrieveURLSubjectToken_Text(t *testing.T) {
1920
if r.Method != "GET" {
2021
t.Errorf("Unexpected request method, %v is found", r.Method)
2122
}
23+
fmt.Println(r.Header)
24+
if r.Header.Get("Metadata") != "True" {
25+
t.Errorf("Metadata header not properly included.")
26+
}
2227
w.Write([]byte("testTokenValue"))
2328
}))
29+
heads := make(map[string]string)
30+
heads["Metadata"] = "True"
2431
cs := CredentialSource{
2532
URL: ts.URL,
2633
Format: format{Type: fileTypeText},
34+
Headers: heads,
2735
}
2836
tfc := testFileConfig
2937
tfc.CredentialSource = cs

0 commit comments

Comments
 (0)