Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate RP from Azure AD Graph to Microsoft Graph #1970

Merged
merged 12 commits into from
Jun 14, 2023
23 changes: 17 additions & 6 deletions pkg/env/armhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package env

import (
"context"
"fmt"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand All @@ -14,10 +15,11 @@ import (
"github.com/Azure/go-autorest/autorest/azure"
"github.com/Azure/go-autorest/autorest/to"
"github.com/jongio/azidext/go/azidext"
msgraph "github.com/microsoftgraph/msgraph-sdk-go"
"github.com/sirupsen/logrus"

"github.com/Azure/ARO-RP/pkg/util/azureclient/graphrbac"
"github.com/Azure/ARO-RP/pkg/util/azureclient/mgmt/authorization"
utilgraph "github.com/Azure/ARO-RP/pkg/util/graph"
"github.com/Azure/ARO-RP/pkg/util/rbac"
"github.com/Azure/ARO-RP/pkg/util/uuid"
)
Expand Down Expand Up @@ -63,8 +65,8 @@ type armHelper struct {
log *logrus.Entry
env Interface

fpGraphClient *msgraph.GraphServiceClient
roleassignments authorization.RoleAssignmentsClient
applications graphrbac.ApplicationsClient
}

func newARMHelper(ctx context.Context, log *logrus.Entry, env Interface) (ARMHelper, error) {
bennerv marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -104,7 +106,13 @@ func newARMHelper(ctx context.Context, log *logrus.Entry, env Interface) (ARMHel
scopes := []string{env.Environment().ResourceManagerScope}
armAuthorizer := azidext.NewTokenCredentialAdapter(tokenCredential, scopes)

fpGraphAuthorizer, err := env.FPAuthorizer(env.TenantID(), env.Environment().ActiveDirectoryGraphScope)
// Graph service client uses the first party service principal.
mbarnes marked this conversation as resolved.
Show resolved Hide resolved
tokenCredential, err = env.FPNewClientCertificateCredential(env.TenantID())
mbarnes marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

fpGraphClient, err := env.Environment().NewGraphServiceClient(tokenCredential)
if err != nil {
return nil, err
}
Expand All @@ -113,23 +121,26 @@ func newARMHelper(ctx context.Context, log *logrus.Entry, env Interface) (ARMHel
log: log,
env: env,

fpGraphClient: fpGraphClient,
roleassignments: authorization.NewRoleAssignmentsClient(env.Environment(), env.SubscriptionID(), armAuthorizer),
applications: graphrbac.NewApplicationsClient(env.Environment(), env.TenantID(), fpGraphAuthorizer),
}, nil
}

func (ah *armHelper) EnsureARMResourceGroupRoleAssignment(ctx context.Context, fpAuthorizer autorest.Authorizer, resourceGroup string) error {
ah.log.Print("ensuring resource group role assignment")

res, err := ah.applications.GetServicePrincipalsIDByAppID(ctx, ah.env.FPClientID())
principalID, err := utilgraph.GetServicePrincipalIDByAppID(ctx, ah.fpGraphClient, ah.env.FPClientID())
if err != nil {
return err
}
if principalID == nil {
return fmt.Errorf("no service principal found for application ID '%s'", ah.env.FPClientID())
bennerv marked this conversation as resolved.
Show resolved Hide resolved
}

_, err = ah.roleassignments.Create(ctx, "/subscriptions/"+ah.env.SubscriptionID()+"/resourceGroups/"+resourceGroup, uuid.DefaultGenerator.Generate(), mgmtauthorization.RoleAssignmentCreateParameters{
RoleAssignmentProperties: &mgmtauthorization.RoleAssignmentProperties{
RoleDefinitionID: to.StringPtr("/subscriptions/" + ah.env.SubscriptionID() + "/providers/Microsoft.Authorization/roleDefinitions/" + rbac.RoleOwner),
PrincipalID: res.Value,
PrincipalID: principalID,
},
})
if detailedErr, ok := err.(autorest.DetailedError); ok {
Expand Down