Skip to content

Commit

Permalink
fix keyvault sample (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT authored Aug 5, 2019
1 parent ad38292 commit 79e3f3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 15 additions & 0 deletions graphrbac/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphrbac

import (
"context"
"fmt"
"time"

"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config"
Expand Down Expand Up @@ -113,3 +114,17 @@ func DeleteADGroup(ctx context.Context, groupObjID string) (autorest.Response, e
groupClient := getADGroupsClient()
return groupClient.Delete(ctx, groupObjID)
}

// GetServicePrincipalObjectID returns the service principal object ID for the specified client ID.
func GetServicePrincipalObjectID(ctx context.Context, clientID string) (string, error) {
spClient := getServicePrincipalsClient()
page, err := spClient.List(ctx, fmt.Sprintf("servicePrincipalNames/any(c:c eq '%s')", clientID))
if err != nil {
return "", err
}
servicePrincipals := page.Values()
if len(servicePrincipals) == 0 {
return "", fmt.Errorf("didn't find any service principals for client ID %s", clientID)
}
return *servicePrincipals[0].ObjectID, nil
}
8 changes: 6 additions & 2 deletions keyvault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/Azure/azure-sdk-for-go/services/keyvault/mgmt/2016-10-01/keyvault"

"github.com/Azure-Samples/azure-sdk-for-go-samples/graphrbac"
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/config"
"github.com/Azure-Samples/azure-sdk-for-go-samples/internal/iam"
"github.com/Azure/go-autorest/autorest"
Expand Down Expand Up @@ -113,7 +114,10 @@ func SetVaultPermissions(ctx context.Context, vaultName string) (keyvault.Vault,
return keyvault.Vault{}, err
}

clientID := config.ClientID()
objectID, err := graphrbac.GetServicePrincipalObjectID(ctx, config.ClientID())
if err != nil {
return keyvault.Vault{}, err
}

return vaultsClient.CreateOrUpdate(
ctx,
Expand All @@ -129,7 +133,7 @@ func SetVaultPermissions(ctx context.Context, vaultName string) (keyvault.Vault,
},
AccessPolicies: &[]keyvault.AccessPolicyEntry{
{
ObjectID: &clientID,
ObjectID: &objectID,
TenantID: &tenantID,
Permissions: &keyvault.Permissions{
Keys: &[]keyvault.KeyPermissions{
Expand Down

0 comments on commit 79e3f3a

Please sign in to comment.