generated from hashicorp/packer-plugin-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 80
/
config.go
342 lines (296 loc) · 11.9 KB
/
config.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright (c) HashiCorp, Inc.
// spdx-license-identifier: mpl-2.0
//go:generate packer-sdc struct-markdown
package client
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/cli"
"github.com/hashicorp/packer-plugin-azure/builder/azure/common/log"
jwt "github.com/golang-jwt/jwt"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
)
// This error is thrown whenever the Azure SDK returns a null model with no error
// We do not expect this error to happen ever, but also don't want to throw a null pointer exception here.
var NullModelSDKErr = fmt.Errorf("Unexpected SDK response, please open an issue on the Azure plugin issue tracker")
// Config allows for various ways to authenticate Azure clients. When
// `client_id` and `subscription_id` are specified in addition to one and only
// one of the following: `client_secret`, `client_jwt`, `client_cert_path` --
// Packer will use the specified Azure Active Directory (AAD) Service Principal
// (SP).
// If none of these options are specified, Packer will attempt to use the Managed Identity
// and subscription of the VM that Packer is running on. This will only work if
// Packer is running on an Azure VM with either a System Assigned Managed
// Identity or User Assigned Managed Identity.
type Config struct {
// One of Public, China, or
// USGovernment. Defaults to Public. Long forms such as
// USGovernmentCloud and AzureUSGovernmentCloud are also supported.
CloudEnvironmentName string `mapstructure:"cloud_environment_name" required:"false"`
cloudEnvironment *environments.Environment
// The Hostname of the Azure Metadata Service
// (for example management.azure.com), used to obtain the Cloud Environment
// when using a Custom Azure Environment. This can also be sourced from the
// ARM_METADATA_HOST Environment Variable.
// Note: CloudEnvironmentName must be set to the requested environment
// name in the list of available environments held in the metadata_host.
MetadataHost string `mapstructure:"metadata_host" required:"false"`
// Authentication fields
// The application ID of the AAD Service Principal.
// Requires either `client_secret`, `client_cert_path` or `client_jwt` to be set as well.
ClientID string `mapstructure:"client_id"`
// A password/secret registered for the AAD SP.
ClientSecret string `mapstructure:"client_secret"`
// The path to a PKCS#12 bundle (.pfx file) to be used as the client certificate
// that will be used to authenticate as the specified AAD SP.
ClientCertPath string `mapstructure:"client_cert_path"`
// The password for decrypting the client certificate bundle.
ClientCertPassword string `mapstructure:"client_cert_password"`
// A JWT bearer token for client auth (RFC 7523, Sec. 2.2) that will be used
// to authenticate the AAD SP. Provides more control over token the expiration
// when using certificate authentication than when using `client_cert_path`.
ClientJWT string `mapstructure:"client_jwt"`
// The object ID for the AAD SP. Optional, will be derived from the oAuth token if left empty.
ObjectID string `mapstructure:"object_id"`
// The Active Directory tenant identifier with which your `client_id` and
// `subscription_id` are associated. If not specified, `tenant_id` will be
// looked up using `subscription_id`.
TenantID string `mapstructure:"tenant_id" required:"false"`
// The subscription to use.
SubscriptionID string `mapstructure:"subscription_id"`
authType string
// Flag to use Azure CLI authentication. Defaults to false.
// CLI auth will use the information from an active `az login` session to connect to Azure and set the subscription id and tenant id associated to the signed in account.
// If enabled, it will use the authentication provided by the `az` CLI.
// Azure CLI authentication will use the credential marked as `isDefault` and can be verified using `az account show`.
// Works with normal authentication (`az login`) and service principals (`az login --service-principal --username APP_ID --password PASSWORD --tenant TENANT_ID`).
// Ignores all other configurations if enabled.
UseAzureCLIAuth bool `mapstructure:"use_azure_cli_auth" required:"false"`
}
// allow override for unit tests
var findTenantID = FindTenantID
const (
AuthTypeMSI = "ManagedIdentity"
AuthTypeClientSecret = "ClientSecret"
AuthTypeClientCert = "ClientCertificate"
AuthTypeClientBearerJWT = "ClientBearerJWT"
AuthTypeAzureCLI = "AzureCLI"
)
const DefaultCloudEnvironmentName = "Public"
// CloudEnvironmentName is deprecated in favor of MetadataHost. This is retained
// for now to preserve backward compatability, but should eventually be removed.
func (c *Config) SetDefaultValues() error {
if c.CloudEnvironmentName == "" {
c.CloudEnvironmentName = DefaultCloudEnvironmentName
}
return c.setCloudEnvironment()
}
func (c *Config) CloudEnvironment() *environments.Environment {
return c.cloudEnvironment
}
func (c *Config) AuthType() string {
return c.authType
}
func (c *Config) setCloudEnvironment() error {
if c.MetadataHost == "" {
if v := os.Getenv("ARM_METADATA_URL"); v != "" {
c.MetadataHost = v
}
}
environmentContext, cancel := context.WithTimeout(context.Background(), time.Minute*3)
defer cancel()
env, err := environments.FromEndpoint(environmentContext, c.MetadataHost)
c.cloudEnvironment = env
if err != nil {
// fall back to old method of normalizing and looking up cloud names.
log.Printf("Error looking up environment using metadata host: %s. \n"+
"Falling back to hardcoded mechanism...", err.Error())
lookup := map[string]string{
"CHINA": "china",
"CHINACLOUD": "china",
"AZURECHINACLOUD": "china",
"PUBLIC": "public",
"PUBLICCLOUD": "public",
"AZUREPUBLICCLOUD": "public",
"USGOVERNMENT": "usgovernment",
"USGOVERNMENTCLOUD": "usgovernment",
"AZUREUSGOVERNMENTCLOUD": "usgovernment",
}
name := strings.ToUpper(c.CloudEnvironmentName)
envName, ok := lookup[name]
if !ok {
return fmt.Errorf("There is no cloud environment matching the name '%s'!", c.CloudEnvironmentName)
}
env, err := environments.FromName(envName)
if err != nil {
return err
}
c.cloudEnvironment = env
}
return nil
}
//nolint:ineffassign //this triggers a false positive because errs is passed by reference
func (c Config) Validate(errs *packersdk.MultiError) {
/////////////////////////////////////////////
// Authentication via OAUTH
if c.UseCLI() {
return
}
if c.UseMSI() {
return
}
if c.SubscriptionID != "" && c.ClientID != "" &&
c.ClientSecret != "" &&
c.ClientCertPath == "" &&
c.ClientJWT == "" {
// Service principal using secret
return
}
if c.SubscriptionID != "" && c.ClientID != "" &&
c.ClientSecret == "" &&
c.ClientCertPath != "" &&
c.ClientJWT == "" {
// Service principal using certificate
if _, err := os.Stat(c.ClientCertPath); err != nil {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("client_cert_path is not an accessible file: %v", err))
}
return
}
if c.SubscriptionID != "" && c.ClientID != "" &&
c.ClientSecret == "" &&
c.ClientCertPath == "" &&
c.ClientJWT != "" {
p := jwt.Parser{}
claims := jwt.StandardClaims{}
token, _, err := p.ParseUnverified(c.ClientJWT, &claims)
if err != nil {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("client_jwt is not a JWT: %v", err))
} else {
if t, ok := token.Header["x5t"]; !ok || t == "" {
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("client_jwt is missing the x5t header value, which is required for bearer JWT client authentication to Azure"))
}
}
return
}
errs = packersdk.MultiErrorAppend(errs, fmt.Errorf("No valid set of authentication values specified:\n"+
" to use the Managed Identity of the current machine, do not specify any of the fields below:\n"+
" - client_secret\n"+
" - client_jwt\n"+
" - client_cert_path\n"+
" - use_azure_cli_auth\n"+
" to use an Azure Active Directory service principal, specify either:\n"+
" - subscription_id, client_id and client_secret\n"+
" - subscription_id, client_id and client_cert_path\n"+
" - subscription_id, client_id and client_jwt."))
}
func (c Config) UseCLI() bool {
return c.UseAzureCLIAuth
}
func (c Config) UseMSI() bool {
return !c.UseAzureCLIAuth &&
c.ClientSecret == "" &&
c.ClientJWT == "" &&
c.ClientCertPath == "" &&
c.TenantID == ""
}
// FillParameters capture the user intent from the supplied parameter set in AuthType, retrieves the TenantID and CloudEnvironment if not specified.
// The SubscriptionID is also retrieved in case MSI auth is requested.
func (c *Config) FillParameters() error {
if c.authType == "" {
if c.UseCLI() {
c.authType = AuthTypeAzureCLI
} else if c.UseMSI() {
c.authType = AuthTypeMSI
} else if c.ClientSecret != "" {
c.authType = AuthTypeClientSecret
} else if c.ClientCertPath != "" {
c.authType = AuthTypeClientCert
} else {
c.authType = AuthTypeClientBearerJWT
}
}
if c.authType == AuthTypeMSI && c.SubscriptionID == "" {
subscriptionID, err := getSubscriptionFromIMDS()
if err != nil {
return fmt.Errorf("error fetching subscriptionID from VM metadata service for Managed Identity authentication: %v", err)
}
c.SubscriptionID = subscriptionID
}
if c.cloudEnvironment == nil {
newCloudErr := c.setCloudEnvironment()
if newCloudErr != nil {
return newCloudErr
}
}
if c.authType == AuthTypeAzureCLI {
tenantID, subscriptionID, err := getIDsFromAzureCLI()
if err != nil {
return fmt.Errorf("error fetching tenantID and subscriptionID from Azure CLI (are you logged on using `az login`?): %v", err)
}
c.TenantID = tenantID
c.SubscriptionID = subscriptionID
}
// Get Tenant ID from Access token
if c.TenantID == "" && !c.UseAzureCLIAuth {
tenantID, err := findTenantID(*c.cloudEnvironment, c.SubscriptionID)
if err != nil {
return err
}
c.TenantID = tenantID
}
return nil
}
// getIDsFromAzureCLI returns the TenantID and SubscriptionID from an active Azure CLI login session
func getIDsFromAzureCLI() (string, string, error) {
profilePath, err := cli.ProfilePath()
if err != nil {
return "", "", err
}
profile, err := cli.LoadProfile(profilePath)
if err != nil {
return "", "", err
}
for _, p := range profile.Subscriptions {
if p.IsDefault {
return p.TenantID, p.ID, nil
}
}
return "", "", errors.New("Unable to find default subscription")
}
// FindTenantID figures out the AAD tenant ID of the subscription by making an
// unauthenticated request to the Get Subscription Details endpoint and parses
// the value from WWW-Authenticate header.
func FindTenantID(env environments.Environment, subscriptionID string) (string, error) {
const hdrKey = "WWW-Authenticate"
resourceManagerEndpoint, _ := env.ResourceManager.Endpoint()
if resourceManagerEndpoint == nil {
return "", fmt.Errorf("invalid environment passed to FindTenantID")
}
getSubscriptionsEndpoint := fmt.Sprintf("%s/subscriptions/%s?api-version=2022-12-01", *resourceManagerEndpoint, subscriptionID)
httpClient := &http.Client{}
req, err := http.NewRequest("GET", getSubscriptionsEndpoint, nil)
if err != nil {
return "", fmt.Errorf("Could not create request to find tenant ID: %s", err.Error())
}
req.Header.Add("Metadata", "true")
resp, err := httpClient.Do(req)
if err != nil {
return "", fmt.Errorf("Could not send request to find tenant ID: %s", err.Error())
}
hdr := resp.Header.Get(hdrKey)
// Example value for hdr:
// Bearer authorization_uri="https://login.windows.net/996fe9d1-6171-40aa-945b-4c64b63bf655", error="invalid_token", error_description="The authentication failed because of missing 'Authorization' header."
r := regexp.MustCompile(`authorization_uri=".*/([0-9a-f\-]+)"`)
m := r.FindStringSubmatch(hdr)
if m == nil {
return "", fmt.Errorf("Could not find the tenant ID in header: %s %q", hdrKey, hdr)
}
return m[1], nil
}