-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetadata.go
201 lines (157 loc) · 4.73 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package run
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"path"
"strings"
"sync"
"time"
)
var rmu sync.Mutex
var (
runtimeID string
runtimeProjectID string
runtimeRegion string
runtimeNumericProjectID string
)
var metadataEndpoint = "http://metadata.google.internal"
// ErrMetadataNotFound is returned when a metadata key is not found.
var ErrMetadataNotFound = errors.New("run: metadata key not found")
// ErrMetadataInvalidRequest is returned when a metadata request is invalid.
var ErrMetadataInvalidRequest = errors.New("run: invalid metadata request")
// ErrMetadataUnknownError is return when calls to the metadata server
// return an unknown error.
var ErrMetadataUnknownError = errors.New("run: unexpected error retrieving metadata key")
// ErrMetadataUnexpectedResponse is returned when calls to the metadata server
// return an unexpected response.
type ErrMetadataUnexpectedResponse struct {
StatusCode int
Err error
}
func (e *ErrMetadataUnexpectedResponse) Error() string {
return "run: unexpected error retrieving metadata key"
}
func (e *ErrMetadataUnexpectedResponse) Unwrap() error { return e.Err }
// AccessToken holds a GCP access token.
type AccessToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
TokenType string `json:"token_type"`
}
// ProjectID returns the active project ID from the metadata service.
func ProjectID() (string, error) {
rmu.Lock()
defer rmu.Unlock()
if runtimeProjectID != "" {
return runtimeProjectID, nil
}
endpoint := fmt.Sprintf("%s/computeMetadata/v1/project/project-id", metadataEndpoint)
data, err := metadataRequest(endpoint)
if err != nil {
return "", err
}
runtimeProjectID = string(data)
return runtimeProjectID, nil
}
// NumericProjectID returns the active project ID from the metadata service.
func NumericProjectID() (string, error) {
rmu.Lock()
defer rmu.Unlock()
if runtimeNumericProjectID != "" {
return runtimeNumericProjectID, nil
}
endpoint := fmt.Sprintf("%s/computeMetadata/v1/project/numeric-project-id", metadataEndpoint)
data, err := metadataRequest(endpoint)
if err != nil {
return "", err
}
runtimeNumericProjectID = string(data)
return runtimeNumericProjectID, nil
}
// Token returns the default service account token.
func Token(scopes []string) (*AccessToken, error) {
s := strings.Join(scopes, ",")
endpoint := fmt.Sprintf("%s/computeMetadata/v1/instance/service-accounts/default/token?scopes=%s", metadataEndpoint, s)
data, err := metadataRequest(endpoint)
if err != nil {
return nil, err
}
var accessToken AccessToken
err = json.Unmarshal(data, &accessToken)
if err != nil {
return nil, fmt.Errorf("run/metadata: error retrieving access token: %v", err)
}
return &accessToken, nil
}
// IDToken returns an id token based on the service url.
func IDToken(serviceURL string) (string, error) {
endpoint := fmt.Sprintf("%s/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s", metadataEndpoint, serviceURL)
idToken, err := metadataRequest(endpoint)
if err != nil {
return "", fmt.Errorf("metadata.Get: failed to query id_token: %w", err)
}
return string(idToken), nil
}
// Region returns the name of the Cloud Run region.
func Region() (string, error) {
rmu.Lock()
defer rmu.Unlock()
if runtimeRegion != "" {
return runtimeRegion, nil
}
endpoint := fmt.Sprintf("%s/computeMetadata/v1/instance/region", metadataEndpoint)
data, err := metadataRequest(endpoint)
if err != nil {
return "", err
}
runtimeRegion = path.Base(string(data))
return runtimeRegion, nil
}
// ID returns the unique identifier of the container instance.
func ID() (string, error) {
rmu.Lock()
defer rmu.Unlock()
if runtimeID != "" {
return runtimeID, nil
}
endpoint := fmt.Sprintf("%s/computeMetadata/v1/instance/id", metadataEndpoint)
data, err := metadataRequest(endpoint)
if err != nil {
return "", err
}
runtimeID = string(data)
return runtimeID, nil
}
func metadataRequest(endpoint string) ([]byte, error) {
request, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
request.Header.Set("User-Agent", userAgent)
request.Header.Add("Metadata-Flavor", "Google")
timeout := time.Duration(5) * time.Second
httpClient := http.Client{Timeout: timeout}
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
switch s := response.StatusCode; s {
case 200:
break
case 400:
return nil, ErrMetadataInvalidRequest
case 404:
return nil, ErrMetadataNotFound
default:
return nil, &ErrMetadataUnexpectedResponse{s, ErrMetadataUnknownError}
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return data, nil
}