-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathazure.go
246 lines (208 loc) · 7 KB
/
azure.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azure
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/hashicorp/vault/api"
)
type AzureAuth struct {
roleName string
mountPath string
resource string
}
var _ api.AuthMethod = (*AzureAuth)(nil)
type LoginOption func(a *AzureAuth) error
type responseJSON struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn string `json:"expires_in"`
ExpiresOn string `json:"expires_on"`
NotBefore string `json:"not_before"`
Resource string `json:"resource"`
TokenType string `json:"token_type"`
}
type errorJSON struct {
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
}
type metadataJSON struct {
Compute computeJSON `json:"compute"`
}
type computeJSON struct {
VMName string `json:"name"`
VMScaleSetName string `json:"vmScaleSetName"`
SubscriptionID string `json:"subscriptionId"`
ResourceGroupName string `json:"resourceGroupName"`
}
const (
defaultMountPath = "azure"
defaultResourceURL = "https://management.azure.com/"
metadataEndpoint = "http://169.254.169.254"
metadataAPIVersion = "2021-05-01"
apiVersionQueryParam = "api-version"
resourceQueryParam = "resource"
clientTimeout = 10 * time.Second
)
// NewAzureAuth initializes a new Azure auth method interface to be
// passed as a parameter to the client.Auth().Login method.
//
// Supported options: WithMountPath, WithResource
func NewAzureAuth(roleName string, opts ...LoginOption) (*AzureAuth, error) {
if roleName == "" {
return nil, fmt.Errorf("no role name provided for login")
}
a := &AzureAuth{
roleName: roleName,
mountPath: defaultMountPath,
resource: defaultResourceURL,
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
// *AzureAuth as the argument
err := opt(a)
if err != nil {
return nil, fmt.Errorf("error with login option: %w", err)
}
}
// return the modified auth struct instance
return a, nil
}
// Login sets up the required request body for the Azure auth method's /login
// endpoint, and performs a write to it.
func (a *AzureAuth) Login(ctx context.Context, client *api.Client) (*api.Secret, error) {
if ctx == nil {
ctx = context.Background()
}
jwtResp, err := a.getJWT()
if err != nil {
return nil, fmt.Errorf("unable to get access token: %w", err)
}
metadataRespJSON, err := getMetadata()
if err != nil {
return nil, fmt.Errorf("unable to get instance metadata: %w", err)
}
loginData := map[string]interface{}{
"role": a.roleName,
"jwt": jwtResp,
"vm_name": metadataRespJSON.Compute.VMName,
"vmss_name": metadataRespJSON.Compute.VMScaleSetName,
"subscription_id": metadataRespJSON.Compute.SubscriptionID,
"resource_group_name": metadataRespJSON.Compute.ResourceGroupName,
}
path := fmt.Sprintf("auth/%s/login", a.mountPath)
resp, err := client.Logical().WriteWithContext(ctx, path, loginData)
if err != nil {
return nil, fmt.Errorf("unable to log in with Azure auth: %w", err)
}
return resp, nil
}
func WithMountPath(mountPath string) LoginOption {
return func(a *AzureAuth) error {
a.mountPath = mountPath
return nil
}
}
// WithResource allows you to specify a different resource URL to use as the aud value
// on the JWT token than the default of Azure Public Cloud's ARM URL.
// This should match the resource URI that an administrator configured your
// Vault server to use.
//
// See https://github.com/Azure/go-autorest/blob/master/autorest/azure/environments.go
// for a list of valid environments.
func WithResource(url string) LoginOption {
return func(a *AzureAuth) error {
a.resource = url
return nil
}
}
// Retrieves an access token from Managed Identities for Azure Resources
//
// Learn more here: https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token
func (a *AzureAuth) getJWT() (string, error) {
identityEndpoint, err := url.Parse(fmt.Sprintf("%s/metadata/identity/oauth2/token", metadataEndpoint))
if err != nil {
return "", fmt.Errorf("error creating metadata URL: %w", err)
}
identityParameters := identityEndpoint.Query()
identityParameters.Add(apiVersionQueryParam, metadataAPIVersion)
identityParameters.Add(resourceQueryParam, a.resource)
identityEndpoint.RawQuery = identityParameters.Encode()
req, err := http.NewRequest(http.MethodGet, identityEndpoint.String(), nil)
if err != nil {
return "", fmt.Errorf("error creating HTTP request: %w", err)
}
req.Header.Add("Metadata", "true")
client := &http.Client{
Timeout: clientTimeout,
}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error calling Azure token endpoint: %w", err)
}
defer resp.Body.Close()
responseBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body from Azure token endpoint: %w", err)
}
if resp.StatusCode != http.StatusOK {
var errResp errorJSON
err = json.Unmarshal(responseBytes, &errResp)
if err != nil {
return "", fmt.Errorf("received error message but was unable to unmarshal its contents")
}
return "", fmt.Errorf("%s error from Azure token endpoint: %s", errResp.Error, errResp.ErrorDescription)
}
var r responseJSON
err = json.Unmarshal(responseBytes, &r)
if err != nil {
return "", fmt.Errorf("error unmarshaling response from Azure token endpoint: %w", err)
}
return r.AccessToken, nil
}
func getMetadata() (metadataJSON, error) {
metadataEndpoint, err := url.Parse(fmt.Sprintf("%s/metadata/instance", metadataEndpoint))
if err != nil {
return metadataJSON{}, err
}
metadataParameters := metadataEndpoint.Query()
metadataParameters.Add(apiVersionQueryParam, metadataAPIVersion)
metadataEndpoint.RawQuery = metadataParameters.Encode()
req, err := http.NewRequest(http.MethodGet, metadataEndpoint.String(), nil)
if err != nil {
return metadataJSON{}, fmt.Errorf("error creating HTTP Request for metadata endpoint: %w", err)
}
req.Header.Add("Metadata", "true")
client := &http.Client{
Timeout: clientTimeout,
}
resp, err := client.Do(req)
if err != nil {
return metadataJSON{}, fmt.Errorf("error calling metadata endpoint: %w", err)
}
defer resp.Body.Close()
responseBytes, err := io.ReadAll(resp.Body)
if err != nil {
return metadataJSON{}, fmt.Errorf("error reading response body from metadata endpoint: %w", err)
}
if resp.StatusCode != http.StatusOK {
var errResp errorJSON
_ = json.Unmarshal(responseBytes, &errResp)
if err != nil {
return metadataJSON{}, fmt.Errorf("received error message but was unable to unmarshal its contents")
}
return metadataJSON{}, fmt.Errorf("%s error from metadata endpoint: %s", errResp.Error, errResp.ErrorDescription)
}
var r metadataJSON
err = json.Unmarshal(responseBytes, &r)
if err != nil {
return metadataJSON{}, fmt.Errorf("error unmarshaling the response from metadata endpoint: %w", err)
}
return r, nil
}