...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package externalaccount
16
17 import (
18 "context"
19 "encoding/json"
20 "errors"
21 "fmt"
22 "net/http"
23
24 "cloud.google.com/go/auth/internal"
25 "cloud.google.com/go/auth/internal/credsfile"
26 )
27
28 const (
29 fileTypeText = "text"
30 fileTypeJSON = "json"
31 urlProviderType = "url"
32 programmaticProviderType = "programmatic"
33 )
34
35 type urlSubjectProvider struct {
36 URL string
37 Headers map[string]string
38 Format *credsfile.Format
39 Client *http.Client
40 }
41
42 func (sp *urlSubjectProvider) subjectToken(ctx context.Context) (string, error) {
43 req, err := http.NewRequestWithContext(ctx, "GET", sp.URL, nil)
44 if err != nil {
45 return "", fmt.Errorf("credentials: HTTP request for URL-sourced credential failed: %w", err)
46 }
47
48 for key, val := range sp.Headers {
49 req.Header.Add(key, val)
50 }
51 resp, err := sp.Client.Do(req)
52 if err != nil {
53 return "", fmt.Errorf("credentials: invalid response when retrieving subject token: %w", err)
54 }
55 defer resp.Body.Close()
56
57 respBody, err := internal.ReadAll(resp.Body)
58 if err != nil {
59 return "", fmt.Errorf("credentials: invalid body in subject token URL query: %w", err)
60 }
61 if c := resp.StatusCode; c < http.StatusOK || c >= http.StatusMultipleChoices {
62 return "", fmt.Errorf("credentials: status code %d: %s", c, respBody)
63 }
64
65 if sp.Format == nil {
66 return string(respBody), nil
67 }
68 switch sp.Format.Type {
69 case "json":
70 jsonData := make(map[string]interface{})
71 err = json.Unmarshal(respBody, &jsonData)
72 if err != nil {
73 return "", fmt.Errorf("credentials: failed to unmarshal subject token file: %w", err)
74 }
75 val, ok := jsonData[sp.Format.SubjectTokenFieldName]
76 if !ok {
77 return "", errors.New("credentials: provided subject_token_field_name not found in credentials")
78 }
79 token, ok := val.(string)
80 if !ok {
81 return "", errors.New("credentials: improperly formatted subject token")
82 }
83 return token, nil
84 case fileTypeText:
85 return string(respBody), nil
86 default:
87 return "", errors.New("credentials: invalid credential_source file format type: " + sp.Format.Type)
88 }
89 }
90
91 func (sp *urlSubjectProvider) providerType() string {
92 return urlProviderType
93 }
94
View as plain text