builder.azure.common: make CloudEnvironment private so it doesn't temper with hcl2 code generation

the struct is not read from file but later set after some computations
This commit is contained in:
Adrien Delorme 2019-10-14 15:59:26 +02:00
parent 221e72e9c3
commit f8402f1b91
4 changed files with 25 additions and 20 deletions

View File

@ -79,7 +79,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
b.config.ClientConfig.SubscriptionID, b.config.ClientConfig.SubscriptionID,
b.config.ResourceGroupName, b.config.ResourceGroupName,
b.config.StorageAccount, b.config.StorageAccount,
b.config.ClientConfig.CloudEnvironment, b.config.ClientConfig.CloudEnvironment(),
b.config.SharedGalleryTimeout, b.config.SharedGalleryTimeout,
spnCloud, spnCloud,
spnKeyVault) spnKeyVault)

View File

@ -277,8 +277,8 @@ func TestConfigShouldDefaultToPublicCloud(t *testing.T) {
t.Errorf("Expected 'CloudEnvironmentName' to default to 'Public', but got '%s'.", c.ClientConfig.CloudEnvironmentName) t.Errorf("Expected 'CloudEnvironmentName' to default to 'Public', but got '%s'.", c.ClientConfig.CloudEnvironmentName)
} }
if c.ClientConfig.CloudEnvironment == nil || c.ClientConfig.CloudEnvironment.Name != "AzurePublicCloud" { if c.ClientConfig.CloudEnvironment() == nil || c.ClientConfig.CloudEnvironment().Name != "AzurePublicCloud" {
t.Errorf("Expected 'cloudEnvironment' to be set to 'AzurePublicCloud', but got '%s'.", c.ClientConfig.CloudEnvironment) t.Errorf("Expected 'cloudEnvironment' to be set to 'AzurePublicCloud', but got '%s'.", c.ClientConfig.CloudEnvironment())
} }
} }
@ -327,8 +327,8 @@ func TestConfigInstantiatesCorrectAzureEnvironment(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if c.ClientConfig.CloudEnvironment == nil || c.ClientConfig.CloudEnvironment.Name != x.environmentName { if c.ClientConfig.CloudEnvironment() == nil || c.ClientConfig.CloudEnvironment().Name != x.environmentName {
t.Errorf("Expected 'cloudEnvironment' to be set to '%s', but got '%s'.", x.environmentName, c.ClientConfig.CloudEnvironment) t.Errorf("Expected 'cloudEnvironment' to be set to '%s', but got '%s'.", x.environmentName, c.ClientConfig.CloudEnvironment())
} }
} }
} }

View File

@ -4,11 +4,12 @@ package client
import ( import (
"fmt" "fmt"
"github.com/hashicorp/packer/builder/azure/common"
"os" "os"
"strings" "strings"
"time" "time"
"github.com/hashicorp/packer/builder/azure/common"
"github.com/Azure/go-autorest/autorest/adal" "github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
@ -28,7 +29,7 @@ type Config struct {
// USGovernment. Defaults to Public. Long forms such as // USGovernment. Defaults to Public. Long forms such as
// USGovernmentCloud and AzureUSGovernmentCloud are also supported. // USGovernmentCloud and AzureUSGovernmentCloud are also supported.
CloudEnvironmentName string `mapstructure:"cloud_environment_name" required:"false"` CloudEnvironmentName string `mapstructure:"cloud_environment_name" required:"false"`
CloudEnvironment *azure.Environment cloudEnvironment *azure.Environment
// Authentication fields // Authentication fields
@ -73,6 +74,10 @@ func (c *Config) SetDefaultValues() error {
return c.setCloudEnvironment() return c.setCloudEnvironment()
} }
func (c *Config) CloudEnvironment() *azure.Environment {
return c.cloudEnvironment
}
func (c *Config) setCloudEnvironment() error { func (c *Config) setCloudEnvironment() error {
lookup := map[string]string{ lookup := map[string]string{
"CHINA": "AzureChinaCloud", "CHINA": "AzureChinaCloud",
@ -103,7 +108,7 @@ func (c *Config) setCloudEnvironment() error {
} }
env, err := azure.EnvironmentFromName(envName) env, err := azure.EnvironmentFromName(envName)
c.CloudEnvironment = &env c.cloudEnvironment = &env
return err return err
} }
@ -210,22 +215,22 @@ func (c Config) GetServicePrincipalTokens(
switch c.authType { switch c.authType {
case authTypeDeviceLogin: case authTypeDeviceLogin:
say("Getting tokens using device flow") say("Getting tokens using device flow")
auth = NewDeviceFlowOAuthTokenProvider(*c.CloudEnvironment, say, tenantID) auth = NewDeviceFlowOAuthTokenProvider(*c.cloudEnvironment, say, tenantID)
case authTypeMSI: case authTypeMSI:
say("Getting tokens using Managed Identity for Azure") say("Getting tokens using Managed Identity for Azure")
auth = NewMSIOAuthTokenProvider(*c.CloudEnvironment) auth = NewMSIOAuthTokenProvider(*c.cloudEnvironment)
case authTypeClientSecret: case authTypeClientSecret:
say("Getting tokens using client secret") say("Getting tokens using client secret")
auth = NewSecretOAuthTokenProvider(*c.CloudEnvironment, c.ClientID, c.ClientSecret, tenantID) auth = NewSecretOAuthTokenProvider(*c.cloudEnvironment, c.ClientID, c.ClientSecret, tenantID)
case authTypeClientCert: case authTypeClientCert:
say("Getting tokens using client certificate") say("Getting tokens using client certificate")
auth, err = NewCertOAuthTokenProvider(*c.CloudEnvironment, c.ClientID, c.ClientCertPath, tenantID) auth, err = NewCertOAuthTokenProvider(*c.cloudEnvironment, c.ClientID, c.ClientCertPath, tenantID)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
case authTypeClientBearerJWT: case authTypeClientBearerJWT:
say("Getting tokens using client bearer JWT") say("Getting tokens using client bearer JWT")
auth = NewJWTOAuthTokenProvider(*c.CloudEnvironment, c.ClientID, c.ClientJWT, tenantID) auth = NewJWTOAuthTokenProvider(*c.cloudEnvironment, c.ClientID, c.ClientJWT, tenantID)
default: default:
panic("authType not set, call FillParameters, or set explicitly") panic("authType not set, call FillParameters, or set explicitly")
} }
@ -241,7 +246,7 @@ func (c Config) GetServicePrincipalTokens(
} }
servicePrincipalTokenVault, err = auth.getServicePrincipalTokenWithResource( servicePrincipalTokenVault, err = auth.getServicePrincipalTokenWithResource(
strings.TrimRight(c.CloudEnvironment.KeyVaultEndpoint, "/")) strings.TrimRight(c.cloudEnvironment.KeyVaultEndpoint, "/"))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -280,7 +285,7 @@ func (c *Config) FillParameters() error {
c.SubscriptionID = subscriptionID c.SubscriptionID = subscriptionID
} }
if c.CloudEnvironment == nil { if c.cloudEnvironment == nil {
err := c.setCloudEnvironment() err := c.setCloudEnvironment()
if err != nil { if err != nil {
return err return err
@ -288,7 +293,7 @@ func (c *Config) FillParameters() error {
} }
if c.TenantID == "" { if c.TenantID == "" {
tenantID, err := findTenantID(*c.CloudEnvironment, c.SubscriptionID) tenantID, err := findTenantID(*c.cloudEnvironment, c.SubscriptionID)
if err != nil { if err != nil {
return err return err
} }

View File

@ -133,7 +133,7 @@ func Test_ClientConfig_DeviceLogin(t *testing.T) {
getEnvOrSkip(t, "AZURE_DEVICE_LOGIN") getEnvOrSkip(t, "AZURE_DEVICE_LOGIN")
cfg := Config{ cfg := Config{
SubscriptionID: getEnvOrSkip(t, "AZURE_SUBSCRIPTION"), SubscriptionID: getEnvOrSkip(t, "AZURE_SUBSCRIPTION"),
CloudEnvironment: getCloud(), cloudEnvironment: getCloud(),
} }
assertValid(t, cfg) assertValid(t, cfg)
@ -164,7 +164,7 @@ func Test_ClientConfig_ClientPassword(t *testing.T) {
ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"), ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"),
ClientSecret: getEnvOrSkip(t, "AZURE_CLIENTSECRET"), ClientSecret: getEnvOrSkip(t, "AZURE_CLIENTSECRET"),
TenantID: getEnvOrSkip(t, "AZURE_TENANTID"), TenantID: getEnvOrSkip(t, "AZURE_TENANTID"),
CloudEnvironment: getCloud(), cloudEnvironment: getCloud(),
} }
assertValid(t, cfg) assertValid(t, cfg)
@ -194,7 +194,7 @@ func Test_ClientConfig_ClientCert(t *testing.T) {
ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"), ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"),
ClientCertPath: getEnvOrSkip(t, "AZURE_CLIENTCERT"), ClientCertPath: getEnvOrSkip(t, "AZURE_CLIENTCERT"),
TenantID: getEnvOrSkip(t, "AZURE_TENANTID"), TenantID: getEnvOrSkip(t, "AZURE_TENANTID"),
CloudEnvironment: getCloud(), cloudEnvironment: getCloud(),
} }
assertValid(t, cfg) assertValid(t, cfg)
@ -224,7 +224,7 @@ func Test_ClientConfig_ClientJWT(t *testing.T) {
ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"), ClientID: getEnvOrSkip(t, "AZURE_CLIENTID"),
ClientJWT: getEnvOrSkip(t, "AZURE_CLIENTJWT"), ClientJWT: getEnvOrSkip(t, "AZURE_CLIENTJWT"),
TenantID: getEnvOrSkip(t, "AZURE_TENANTID"), TenantID: getEnvOrSkip(t, "AZURE_TENANTID"),
CloudEnvironment: getCloud(), cloudEnvironment: getCloud(),
} }
assertValid(t, cfg) assertValid(t, cfg)