azure: Update to SDK 10.0.3

The OAuth code was moved to the adal package.
Authorizers now ues an interface instead of a simple token.
Long running operations return a channel for the operation, and the
error.
This commit is contained in:
Christopher Boumenot 2017-05-28 00:38:45 -07:00
parent 054a75de26
commit 8cea6f5be5
14 changed files with 65 additions and 52 deletions

View File

@ -3,7 +3,10 @@
package arm
import "github.com/Azure/go-autorest/autorest/azure"
import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/adal"
)
type Authenticate struct {
env azure.Environment
@ -21,17 +24,17 @@ func NewAuthenticate(env azure.Environment, clientID, clientSecret, tenantID str
}
}
func (a *Authenticate) getServicePrincipalToken() (*azure.ServicePrincipalToken, error) {
func (a *Authenticate) getServicePrincipalToken() (*adal.ServicePrincipalToken, error) {
return a.getServicePrincipalTokenWithResource(a.env.ResourceManagerEndpoint)
}
func (a *Authenticate) getServicePrincipalTokenWithResource(resource string) (*azure.ServicePrincipalToken, error) {
oauthConfig, err := a.env.OAuthConfigForTenant(a.tenantID)
func (a *Authenticate) getServicePrincipalTokenWithResource(resource string) (*adal.ServicePrincipalToken, error) {
oauthConfig, err := adal.NewOAuthConfig(a.env.ActiveDirectoryEndpoint, a.tenantID)
if err != nil {
return nil, err
}
spt, err := azure.NewServicePrincipalToken(
spt, err := adal.NewServicePrincipalToken(
*oauthConfig,
a.clientID,
a.clientSecret,

View File

@ -21,6 +21,7 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/packer/builder/azure/common"
"github.com/hashicorp/packer/version"
"github.com/Azure/go-autorest/autorest/adal"
)
const (
@ -107,56 +108,56 @@ func byConcatDecorators(decorators ...autorest.RespondDecorator) autorest.Respon
func NewAzureClient(subscriptionID, resourceGroupName, storageAccountName string,
cloud *azure.Environment,
servicePrincipalToken, servicePrincipalTokenVault *azure.ServicePrincipalToken) (*AzureClient, error) {
servicePrincipalToken, servicePrincipalTokenVault *adal.ServicePrincipalToken) (*AzureClient, error) {
var azureClient = &AzureClient{}
maxlen := getInspectorMaxLength()
azureClient.DeploymentsClient = resources.NewDeploymentsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.DeploymentsClient.Authorizer = servicePrincipalToken
azureClient.DeploymentsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.DeploymentsClient.RequestInspector = withInspection(maxlen)
azureClient.DeploymentsClient.ResponseInspector = byInspecting(maxlen)
azureClient.DeploymentsClient.UserAgent += packerUserAgent
azureClient.GroupsClient = resources.NewGroupsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.GroupsClient.Authorizer = servicePrincipalToken
azureClient.GroupsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.GroupsClient.RequestInspector = withInspection(maxlen)
azureClient.GroupsClient.ResponseInspector = byInspecting(maxlen)
azureClient.GroupsClient.UserAgent += packerUserAgent
azureClient.InterfacesClient = network.NewInterfacesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.InterfacesClient.Authorizer = servicePrincipalToken
azureClient.InterfacesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.InterfacesClient.RequestInspector = withInspection(maxlen)
azureClient.InterfacesClient.ResponseInspector = byInspecting(maxlen)
azureClient.InterfacesClient.UserAgent += packerUserAgent
azureClient.SubnetsClient = network.NewSubnetsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.SubnetsClient.Authorizer = servicePrincipalToken
azureClient.SubnetsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.SubnetsClient.RequestInspector = withInspection(maxlen)
azureClient.SubnetsClient.ResponseInspector = byInspecting(maxlen)
azureClient.SubnetsClient.UserAgent += packerUserAgent
azureClient.VirtualNetworksClient = network.NewVirtualNetworksClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.VirtualNetworksClient.Authorizer = servicePrincipalToken
azureClient.VirtualNetworksClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.VirtualNetworksClient.RequestInspector = withInspection(maxlen)
azureClient.VirtualNetworksClient.ResponseInspector = byInspecting(maxlen)
azureClient.VirtualNetworksClient.UserAgent += packerUserAgent
azureClient.PublicIPAddressesClient = network.NewPublicIPAddressesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.PublicIPAddressesClient.Authorizer = servicePrincipalToken
azureClient.PublicIPAddressesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.PublicIPAddressesClient.RequestInspector = withInspection(maxlen)
azureClient.PublicIPAddressesClient.ResponseInspector = byInspecting(maxlen)
azureClient.PublicIPAddressesClient.UserAgent += packerUserAgent
azureClient.VirtualMachinesClient = compute.NewVirtualMachinesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.VirtualMachinesClient.Authorizer = servicePrincipalToken
azureClient.VirtualMachinesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.VirtualMachinesClient.RequestInspector = withInspection(maxlen)
azureClient.VirtualMachinesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), templateCapture(azureClient))
azureClient.VirtualMachinesClient.UserAgent += packerUserAgent
azureClient.AccountsClient = armStorage.NewAccountsClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.AccountsClient.Authorizer = servicePrincipalToken
azureClient.AccountsClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.AccountsClient.RequestInspector = withInspection(maxlen)
azureClient.AccountsClient.ResponseInspector = byInspecting(maxlen)
azureClient.AccountsClient.UserAgent += packerUserAgent
@ -167,7 +168,7 @@ func NewAzureClient(subscriptionID, resourceGroupName, storageAccountName string
}
azureClient.VaultClient = common.NewVaultClient(*keyVaultURL)
azureClient.VaultClient.Authorizer = servicePrincipalTokenVault
azureClient.VaultClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalTokenVault)
azureClient.VaultClient.RequestInspector = withInspection(maxlen)
azureClient.VaultClient.ResponseInspector = byInspecting(maxlen)
azureClient.VaultClient.UserAgent += packerUserAgent

View File

@ -12,8 +12,6 @@ import (
packerAzureCommon "github.com/hashicorp/packer/builder/azure/common"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/builder/azure/common/lin"
@ -21,6 +19,7 @@ import (
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/packer"
"github.com/mitchellh/multistep"
"github.com/Azure/go-autorest/autorest/adal"
)
type Builder struct {
@ -179,7 +178,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
template.(*CaptureTemplate),
func(name string) string {
month := time.Now().AddDate(0, 1, 0).UTC()
sasUrl, _ := azureClient.BlobStorageClient.GetBlobSASURI(DefaultSasBlobContainer, name, month, DefaultSasBlobPermission)
blob := azureClient.BlobStorageClient.GetContainerReference(DefaultSasBlobContainer).GetBlobReference(name)
sasUrl, _ := blob.GetSASURI(month, DefaultSasBlobPermission)
return sasUrl
})
}
@ -204,7 +204,7 @@ func (b *Builder) getBlobEndpoint(client *AzureClient, resourceGroupName string,
return "", err
}
return *account.Properties.PrimaryEndpoints.Blob, nil
return *account.AccountProperties.PrimaryEndpoints.Blob, nil
}
func (b *Builder) configureStateBag(stateBag multistep.StateBag) {
@ -226,9 +226,9 @@ func (b *Builder) setTemplateParameters(stateBag multistep.StateBag) {
stateBag.Put(constants.ArmVirtualMachineCaptureParameters, b.config.toVirtualMachineCaptureParameters())
}
func (b *Builder) getServicePrincipalTokens(say func(string)) (*azure.ServicePrincipalToken, *azure.ServicePrincipalToken, error) {
var servicePrincipalToken *azure.ServicePrincipalToken
var servicePrincipalTokenVault *azure.ServicePrincipalToken
func (b *Builder) getServicePrincipalTokens(say func(string)) (*adal.ServicePrincipalToken, *adal.ServicePrincipalToken, error) {
var servicePrincipalToken *adal.ServicePrincipalToken
var servicePrincipalTokenVault *adal.ServicePrincipalToken
var err error

View File

@ -39,7 +39,9 @@ func (s *StepCaptureImage) captureImage(resourceGroupName string, computeName st
return err
}
_, err = s.client.Capture(resourceGroupName, computeName, *parameters, cancelCh)
_, errChan := s.client.Capture(resourceGroupName, computeName, *parameters, cancelCh)
err = <-errChan
if err != nil {
return err
}

View File

@ -31,7 +31,7 @@ func NewStepCreateResourceGroup(client *AzureClient, ui packer.Ui) *StepCreateRe
}
func (s *StepCreateResourceGroup) createResourceGroup(resourceGroupName string, location string, tags *map[string]*string) error {
_, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.ResourceGroup{
_, err := s.client.GroupsClient.CreateOrUpdate(resourceGroupName, resources.Group{
Location: &location,
Tags: tags,
})

View File

@ -33,7 +33,8 @@ func NewStepDeleteOSDisk(client *AzureClient, ui packer.Ui) *StepDeleteOSDisk {
}
func (s *StepDeleteOSDisk) deleteBlob(storageContainerName string, blobName string) error {
return s.client.BlobStorageClient.DeleteBlob(storageContainerName, blobName, nil)
blob := s.client.BlobStorageClient.GetContainerReference(storageContainerName).GetBlobReference(blobName)
return blob.Delete(nil)
}
func (s *StepDeleteOSDisk) Run(state multistep.StateBag) multistep.StepAction {

View File

@ -31,8 +31,9 @@ func NewStepDeleteResourceGroup(client *AzureClient, ui packer.Ui) *StepDeleteRe
}
func (s *StepDeleteResourceGroup) deleteResourceGroup(resourceGroupName string, cancelCh <-chan struct{}) error {
_, err := s.client.GroupsClient.Delete(resourceGroupName, cancelCh)
_, errChan := s.client.GroupsClient.Delete(resourceGroupName, cancelCh)
err := <-errChan
return err
}

View File

@ -40,7 +40,9 @@ func (s *StepDeployTemplate) deployTemplate(resourceGroupName string, deployment
return err
}
_, err = s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
_, errChan := s.client.DeploymentsClient.CreateOrUpdate(resourceGroupName, deploymentName, *deployment, cancelCh)
err = <-errChan
return err
}

View File

@ -57,7 +57,7 @@ func (s *StepGetIPAddress) getPrivateIP(resourceGroupName string, ipAddressName
return "", err
}
return *(*resp.Properties.IPConfigurations)[0].Properties.PrivateIPAddress, nil
return *(*resp.IPConfigurations)[0].PrivateIPAddress, nil
}
func (s *StepGetIPAddress) getPublicIP(resourceGroupName string, ipAddressName string, interfaceName string) (string, error) {
@ -66,7 +66,7 @@ func (s *StepGetIPAddress) getPublicIP(resourceGroupName string, ipAddressName s
return "", err
}
return *resp.Properties.IPAddress, nil
return *resp.IPAddress, nil
}
func (s *StepGetIPAddress) Run(state multistep.StateBag) multistep.StepAction {

View File

@ -53,8 +53,8 @@ func (s *StepGetOSDisk) Run(state multistep.StateBag) multistep.StepAction {
return multistep.ActionHalt
}
s.say(fmt.Sprintf(" -> OS Disk : '%s'", *vm.Properties.StorageProfile.OsDisk.Vhd.URI))
state.Put(constants.ArmOSDiskVhd, *vm.Properties.StorageProfile.OsDisk.Vhd.URI)
s.say(fmt.Sprintf(" -> OS Disk : '%s'", *vm.StorageProfile.OsDisk.Vhd.URI))
state.Put(constants.ArmOSDiskVhd, *vm.StorageProfile.OsDisk.Vhd.URI)
return multistep.ActionContinue
}

View File

@ -110,7 +110,7 @@ func createTestStateBagStepGetOSDisk() multistep.StateBag {
func createVirtualMachineFromUri(vhdUri string) compute.VirtualMachine {
vm := compute.VirtualMachine{
Properties: &compute.VirtualMachineProperties{
VirtualMachineProperties: &compute.VirtualMachineProperties{
StorageProfile: &compute.StorageProfile{
OsDisk: &compute.OSDisk{
Vhd: &compute.VirtualHardDisk{

View File

@ -31,7 +31,9 @@ func NewStepPowerOffCompute(client *AzureClient, ui packer.Ui) *StepPowerOffComp
}
func (s *StepPowerOffCompute) powerOffCompute(resourceGroupName string, computeName string, cancelCh <-chan struct{}) error {
_, err := s.client.PowerOff(resourceGroupName, computeName, cancelCh)
_, errChan := s.client.PowerOff(resourceGroupName, computeName, cancelCh)
err := <-errChan
if err != nil {
return err
}

View File

@ -13,6 +13,7 @@ import (
"github.com/Azure/go-autorest/autorest/to"
"github.com/hashicorp/packer/version"
"github.com/mitchellh/go-homedir"
"github.com/Azure/go-autorest/autorest/adal"
)
var (
@ -39,13 +40,13 @@ var (
// Authenticate fetches a token from the local file cache or initiates a consent
// flow and waits for token to be obtained.
func Authenticate(env azure.Environment, tenantID string, say func(string)) (*azure.ServicePrincipalToken, error) {
func Authenticate(env azure.Environment, tenantID string, say func(string)) (*adal.ServicePrincipalToken, error) {
clientID, ok := clientIDs[env.Name]
if !ok {
return nil, fmt.Errorf("packer-azure application not set up for Azure environment %q", env.Name)
}
oauthCfg, err := env.OAuthConfigForTenant(tenantID)
oauthCfg, err := adal.NewOAuthConfig(env.ActiveDirectoryEndpoint, tenantID)
if err != nil {
return nil, fmt.Errorf("Failed to obtain oauth config for azure environment: %v", err)
}
@ -56,7 +57,7 @@ func Authenticate(env azure.Environment, tenantID string, say func(string)) (*az
tokenPath := tokenCachePath(tenantID)
saveToken := mkTokenCallback(tokenPath)
saveTokenCallback := func(t azure.Token) error {
saveTokenCallback := func(t adal.Token) error {
say("Azure token expired. Saving the refreshed token...")
return saveToken(t)
}
@ -110,8 +111,8 @@ func Authenticate(env azure.Environment, tenantID string, say func(string)) (*az
// tokenFromFile returns a token from the specified file if it is found, otherwise
// returns nil. Any error retrieving or creating the token is returned as an error.
func tokenFromFile(say func(string), oauthCfg azure.OAuthConfig, tokenPath, clientID, resource string,
callback azure.TokenRefreshCallback) (*azure.ServicePrincipalToken, error) {
func tokenFromFile(say func(string), oauthCfg adal.OAuthConfig, tokenPath, clientID, resource string,
callback adal.TokenRefreshCallback) (*adal.ServicePrincipalToken, error) {
say(fmt.Sprintf("Loading auth token from file: %s", tokenPath))
if _, err := os.Stat(tokenPath); err != nil {
if os.IsNotExist(err) { // file not found
@ -120,12 +121,12 @@ func tokenFromFile(say func(string), oauthCfg azure.OAuthConfig, tokenPath, clie
return nil, err
}
token, err := azure.LoadToken(tokenPath)
token, err := adal.LoadToken(tokenPath)
if err != nil {
return nil, fmt.Errorf("Failed to load token from file: %v", err)
}
spt, err := azure.NewServicePrincipalTokenFromManualToken(oauthCfg, clientID, resource, *token, callback)
spt, err := adal.NewServicePrincipalTokenFromManualToken(oauthCfg, clientID, resource, *token, callback)
if err != nil {
return nil, fmt.Errorf("Error constructing service principal token: %v", err)
}
@ -136,9 +137,9 @@ func tokenFromFile(say func(string), oauthCfg azure.OAuthConfig, tokenPath, clie
// consent application on a browser and in the meanwhile the authentication
// endpoint is polled until user gives consent, denies or the flow times out.
// Returned token must be saved.
func tokenFromDeviceFlow(say func(string), oauthCfg azure.OAuthConfig, clientID, resource string) (*azure.ServicePrincipalToken, error) {
func tokenFromDeviceFlow(say func(string), oauthCfg adal.OAuthConfig, clientID, resource string) (*adal.ServicePrincipalToken, error) {
cl := autorest.NewClientWithUserAgent(userAgent)
deviceCode, err := azure.InitiateDeviceAuth(&cl, oauthCfg, clientID, resource)
deviceCode, err := adal.InitiateDeviceAuth(&cl, oauthCfg, clientID, resource)
if err != nil {
return nil, fmt.Errorf("Failed to start device auth: %v", err)
}
@ -147,12 +148,12 @@ func tokenFromDeviceFlow(say func(string), oauthCfg azure.OAuthConfig, clientID,
// the code 0000000 to authenticate.”
say(fmt.Sprintf("Microsoft Azure: %s", to.String(deviceCode.Message)))
token, err := azure.WaitForUserCompletion(&cl, deviceCode)
token, err := adal.WaitForUserCompletion(&cl, deviceCode)
if err != nil {
return nil, fmt.Errorf("Failed to complete device auth: %v", err)
}
spt, err := azure.NewServicePrincipalTokenFromManualToken(oauthCfg, clientID, resource, *token)
spt, err := adal.NewServicePrincipalTokenFromManualToken(oauthCfg, clientID, resource, *token)
if err != nil {
return nil, fmt.Errorf("Error constructing service principal token: %v", err)
}
@ -173,9 +174,9 @@ func tokenCachePath(tenantID string) string {
// mkTokenCallback returns a callback function that can be used to save the
// token initially or register to the Azure SDK to be called when the token is
// refreshed.
func mkTokenCallback(path string) azure.TokenRefreshCallback {
return func(t azure.Token) error {
if err := azure.SaveToken(path, 0600, t); err != nil {
func mkTokenCallback(path string) adal.TokenRefreshCallback {
return func(t adal.Token) error {
if err := adal.SaveToken(path, 0600, t); err != nil {
return err
}
return nil
@ -186,9 +187,9 @@ func mkTokenCallback(path string) azure.TokenRefreshCallback {
// sure if the access_token valid, if not it uses SDKs functionality to
// automatically refresh the token using refresh_token (which might have
// expired). This check is essentially to make sure refresh_token is good.
func validateToken(env azure.Environment, token *azure.ServicePrincipalToken) error {
func validateToken(env azure.Environment, token *adal.ServicePrincipalToken) error {
c := subscriptionsClient(env.ResourceManagerEndpoint)
c.Authorizer = token
c.Authorizer = autorest.NewBearerAuthorizer(token)
_, err := c.List()
if err != nil {
return fmt.Errorf("Token validity check failed: %v", err)
@ -230,7 +231,7 @@ func FindTenantID(env azure.Environment, subscriptionID string) (string, error)
return m[1], nil
}
func subscriptionsClient(baseURI string) subscriptions.Client {
client := subscriptions.NewClientWithBaseURI(baseURI)
func subscriptionsClient(baseURI string) subscriptions.GroupClient {
client := subscriptions.NewGroupClientWithBaseURI(baseURI)
return client
}

View File

@ -169,7 +169,7 @@ func (s *TemplateBuilder) SetVirtualNetwork(virtualNetworkResourceGroup, virtual
strings.Contains(s, "Microsoft.Network/publicIPAddresses")
})
(*resource.Properties.IPConfigurations)[0].Properties.PublicIPAddress = nil
(*resource.Properties.IPConfigurations)[0].PublicIPAddress = nil
return nil
}