validate if user assigned identity exists

This commit is contained in:
Feiyu Shi 2020-05-24 16:53:26 -07:00
parent 430fac1a2b
commit 9bd19df040
2 changed files with 22 additions and 0 deletions

View File

@ -14,6 +14,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-04-01/compute"
newCompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-03-01/compute"
"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2018-02-14/keyvault"
"github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-01-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-02-01/resources"
armStorage "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2017-10-01/storage"
@ -47,6 +48,7 @@ type AzureClient struct {
compute.SnapshotsClient
newCompute.GalleryImageVersionsClient
newCompute.GalleryImagesClient
msi.UserAssignedIdentitiesClient
InspectorMaxLength int
Template *CaptureTemplate
@ -240,6 +242,13 @@ func NewAzureClient(subscriptionID, resourceGroupName, storageAccountName string
azureClient.GalleryImagesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(), azureClient.GalleryImagesClient.UserAgent)
azureClient.GalleryImagesClient.Client.PollingDuration = PollingDuration
azureClient.UserAssignedIdentitiesClient = msi.NewUserAssignedIdentitiesClientWithBaseURI(cloud.ResourceManagerEndpoint, subscriptionID)
azureClient.UserAssignedIdentitiesClient.Authorizer = autorest.NewBearerAuthorizer(servicePrincipalToken)
azureClient.UserAssignedIdentitiesClient.RequestInspector = withInspection(maxlen)
azureClient.UserAssignedIdentitiesClient.ResponseInspector = byConcatDecorators(byInspecting(maxlen), errorCapture(azureClient))
azureClient.UserAssignedIdentitiesClient.UserAgent = fmt.Sprintf("%s %s", useragent.String(), azureClient.UserAssignedIdentitiesClient.UserAgent)
azureClient.UserAssignedIdentitiesClient.Client.PollingDuration = PollingDuration
keyVaultURL, err := url.Parse(cloud.KeyVaultEndpoint)
if err != nil {
return nil, err

View File

@ -16,6 +16,7 @@ import (
"github.com/dgrijalva/jwt-go"
"github.com/hashicorp/hcl/v2/hcldec"
packerAzureCommon "github.com/hashicorp/packer/builder/azure/common"
"github.com/hashicorp/packer/builder/azure/common/client"
"github.com/hashicorp/packer/builder/azure/common/constants"
"github.com/hashicorp/packer/builder/azure/common/lin"
packerCommon "github.com/hashicorp/packer/common"
@ -110,6 +111,18 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
return nil, fmt.Errorf("could not determine the ObjectID for the user, which is required for Windows builds")
}
if len(b.config.UserAssignedManagedIdentities) != 0 {
for _, rid := range b.config.UserAssignedManagedIdentities {
r, err := client.ParseResourceID(rid)
if err != nil {
return nil, err
}
if _, err = azureClient.UserAssignedIdentitiesClient.Get(ctx, r.ResourceGroup, r.ResourceName.String()); err != nil {
return nil, fmt.Errorf("Cannot locate user assigned managed identity %s", rid)
}
}
}
if b.config.isManagedImage() {
_, err := azureClient.GroupsClient.Get(ctx, b.config.ManagedImageResourceGroupName)
if err != nil {