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

Rename TokenCredential variables to indicate credential used #2950

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/api/validate/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Subnet struct {
}

type ServicePrincipalValidator interface {
ValidateServicePrincipal(ctx context.Context, tokenCredential azcore.TokenCredential) error
ValidateServicePrincipal(ctx context.Context, spTokenCredential azcore.TokenCredential) error
}

// Dynamic validate in the operator context.
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/validate/dynamic/serviceprincipal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import (
"github.com/Azure/ARO-RP/pkg/util/azureclaim"
)

func (dv *dynamic) ValidateServicePrincipal(ctx context.Context, tokenCredential azcore.TokenCredential) error {
func (dv *dynamic) ValidateServicePrincipal(ctx context.Context, spTokenCredential azcore.TokenCredential) error {
dv.log.Print("ValidateServicePrincipal")

tokenRequestOptions := policy.TokenRequestOptions{
Scopes: []string{dv.azEnv.MicrosoftGraphScope},
}
token, err := tokenCredential.GetToken(ctx, tokenRequestOptions)
token, err := spTokenCredential.GetToken(ctx, tokenRequestOptions)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/api/validate/openshiftcluster_validatedynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type openShiftClusterDynamicValidator struct {
fpAuthorizer autorest.Authorizer
}

func ensureAccessTokenClaims(ctx context.Context, tokenCredential *azidentity.ClientSecretCredential, scopes []string) error {
func ensureAccessTokenClaims(ctx context.Context, spTokenCredential azcore.TokenCredential, scopes []string) error {
var err error

timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
Expand All @@ -68,7 +68,7 @@ func ensureAccessTokenClaims(ctx context.Context, tokenCredential *azidentity.Cl
// latest error to the user in case the wait exceeds the timeout.
_ = wait.PollImmediateUntil(10*time.Second, func() (bool, error) {
options := policy.TokenRequestOptions{Scopes: scopes}
token, err := tokenCredential.GetToken(ctx, options)
token, err := spTokenCredential.GetToken(ctx, options)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -201,18 +201,18 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {

tenantID := dv.subscriptionDoc.Subscription.Properties.TenantID
options := dv.env.Environment().ClientSecretCredentialOptions()
tokenCredential, err := azidentity.NewClientSecretCredential(
spTokenCredential, err := azidentity.NewClientSecretCredential(
tenantID, spp.ClientID, string(spp.ClientSecret), options)
if err != nil {
return err
}

scopes := []string{dv.env.Environment().ResourceManagerScope}
err = ensureAccessTokenClaims(ctx, tokenCredential, scopes)
err = ensureAccessTokenClaims(ctx, spTokenCredential, scopes)
if err != nil {
return err
}
spAuthorizer := azidext.NewTokenCredentialAdapter(tokenCredential, scopes)
spAuthorizer := azidext.NewTokenCredentialAdapter(spTokenCredential, scopes)

spDynamic := dynamic.NewValidator(
dv.log,
Expand All @@ -227,7 +227,7 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {
)

// SP validation
err = spDynamic.ValidateServicePrincipal(ctx, tokenCredential)
err = spDynamic.ValidateServicePrincipal(ctx, spTokenCredential)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/serviceprincipal.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import (
func (m *manager) initializeClusterSPClients(ctx context.Context) error {
spp := m.doc.OpenShiftCluster.Properties.ServicePrincipalProfile
options := m.env.Environment().ClientSecretCredentialOptions()
tokenCredential, err := azidentity.NewClientSecretCredential(
spTokenCredential, err := azidentity.NewClientSecretCredential(
m.subscriptionDoc.Subscription.Properties.TenantID,
spp.ClientID, string(spp.ClientSecret), options)
if err != nil {
return err
}

m.spGraphClient, err = m.env.Environment().NewGraphServiceClient(tokenCredential)
m.spGraphClient, err = m.env.Environment().NewGraphServiceClient(spTokenCredential)

return err
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/env/armhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
mgmtauthorization "github.com/Azure/azure-sdk-for-go/services/preview/authorization/mgmt/2018-09-01-preview/authorization"
"github.com/Azure/go-autorest/autorest"
Expand Down Expand Up @@ -74,7 +73,6 @@ func newARMHelper(ctx context.Context, log *logrus.Entry, env Interface) (ARMHel
return &noopARMHelper{}, nil
}

var tokenCredential azcore.TokenCredential
var err error

key, certs, err := env.ServiceKeyvault().GetCertificateSecret(ctx, RPDevARMSecretName)
Expand All @@ -83,21 +81,21 @@ func newARMHelper(ctx context.Context, log *logrus.Entry, env Interface) (ARMHel
}

options := env.Environment().ClientCertificateCredentialOptions()
tokenCredential, err = azidentity.NewClientCertificateCredential(env.TenantID(), os.Getenv("AZURE_ARM_CLIENT_ID"), certs, key, options)
armHelperTokenCredential, err := azidentity.NewClientCertificateCredential(env.TenantID(), os.Getenv("AZURE_ARM_CLIENT_ID"), certs, key, options)
if err != nil {
return nil, err
}

scopes := []string{env.Environment().ResourceManagerScope}
armAuthorizer := azidext.NewTokenCredentialAdapter(tokenCredential, scopes)
armHelperAuthorizer := azidext.NewTokenCredentialAdapter(armHelperTokenCredential, scopes)

// Graph service client uses the first party service principal.
tokenCredential, err = env.FPNewClientCertificateCredential(env.TenantID())
fpTokenCredential, err := env.FPNewClientCertificateCredential(env.TenantID())
if err != nil {
return nil, err
}

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

fpGraphClient: fpGraphClient,
roleassignments: authorization.NewRoleAssignmentsClient(env.Environment(), env.SubscriptionID(), armAuthorizer),
roleassignments: authorization.NewRoleAssignmentsClient(env.Environment(), env.SubscriptionID(), armHelperAuthorizer),
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/env/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func (d *dev) Listen() (net.Listener, error) {
}

func (d *dev) FPAuthorizer(tenantID string, scopes ...string) (autorest.Authorizer, error) {
tokenCredential, err := d.FPNewClientCertificateCredential(tenantID)
fpTokenCredential, err := d.FPNewClientCertificateCredential(tenantID)
if err != nil {
return nil, err
}

return azidext.NewTokenCredentialAdapter(tokenCredential, scopes), nil
return azidext.NewTokenCredentialAdapter(fpTokenCredential, scopes), nil
}

func (d *dev) FPNewClientCertificateCredential(tenantID string) (*azidentity.ClientCertificateCredential, error) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/env/msiauthorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ const (
func (c *core) NewMSIAuthorizer(msiContext MSIContext, scopes ...string) (autorest.Authorizer, error) {
if !c.IsLocalDevelopmentMode() {
options := c.Environment().ManagedIdentityCredentialOptions()
tokenCredential, err := azidentity.NewManagedIdentityCredential(options)
msiTokenCredential, err := azidentity.NewManagedIdentityCredential(options)
if err != nil {
return nil, err
}

return azidext.NewTokenCredentialAdapter(tokenCredential, scopes), nil
return azidext.NewTokenCredentialAdapter(msiTokenCredential, scopes), nil
}

tenantIdKey := "AZURE_TENANT_ID"
Expand All @@ -44,10 +44,10 @@ func (c *core) NewMSIAuthorizer(msiContext MSIContext, scopes ...string) (autore

options := c.Environment().ClientSecretCredentialOptions()

tokenCredential, err := azidentity.NewClientSecretCredential(tenantId, azureClientId, azureClientSecret, options)
clientSecretCredential, err := azidentity.NewClientSecretCredential(tenantId, azureClientId, azureClientSecret, options)
if err != nil {
return nil, err
}

return azidext.NewTokenCredentialAdapter(tokenCredential, scopes), nil
return azidext.NewTokenCredentialAdapter(clientSecretCredential, scopes), nil
}
4 changes: 2 additions & 2 deletions pkg/env/prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,12 @@ func (p *prod) FeatureIsSet(f Feature) bool {
}

func (p *prod) FPAuthorizer(tenantID string, scopes ...string) (autorest.Authorizer, error) {
tokenCredential, err := p.FPNewClientCertificateCredential(tenantID)
fpTokenCredential, err := p.FPNewClientCertificateCredential(tenantID)
if err != nil {
return nil, err
}

return azidext.NewTokenCredentialAdapter(tokenCredential, scopes), nil
return azidext.NewTokenCredentialAdapter(fpTokenCredential, scopes), nil
}

func (p *prod) FPClientID() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func (r *checker) Check(ctx context.Context, AZEnvironment string) error {

spDynamic := r.newSPValidator(&azEnv)

tokenCredential, err := r.getTokenCredential(&azEnv, azCred)
spTokenCredential, err := r.getTokenCredential(&azEnv, azCred)
if err != nil {
return err
}

return spDynamic.ValidateServicePrincipal(ctx, tokenCredential)
return spDynamic.ValidateServicePrincipal(ctx, spTokenCredential)
}
6 changes: 3 additions & 3 deletions pkg/util/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ func New(log *logrus.Entry, environment env.Core, ci bool) (*Cluster, error) {
}

options := environment.Environment().EnvironmentCredentialOptions()
tokenCredential, err := azidentity.NewEnvironmentCredential(options)
spTokenCredential, err := azidentity.NewEnvironmentCredential(options)
if err != nil {
return nil, err
}

spGraphClient, err := environment.Environment().NewGraphServiceClient(tokenCredential)
spGraphClient, err := environment.Environment().NewGraphServiceClient(spTokenCredential)
if err != nil {
return nil, err
}

scopes := []string{environment.Environment().ResourceManagerScope}
authorizer := azidext.NewTokenCredentialAdapter(tokenCredential, scopes)
authorizer := azidext.NewTokenCredentialAdapter(spTokenCredential, scopes)

c := &Cluster{
log: log,
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/instancemetadata/prod.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ func newProd(ctx context.Context) (InstanceMetadata, error) {

func (p *prod) populateTenantIDFromMSI(ctx context.Context) error {
options := p.Environment().ManagedIdentityCredentialOptions()
tokenCredential, err := azidentity.NewManagedIdentityCredential(options)
msiTokenCredential, err := azidentity.NewManagedIdentityCredential(options)
if err != nil {
return err
}

tokenRequestOptions := policy.TokenRequestOptions{
Scopes: []string{p.Environment().ResourceManagerScope},
}
token, err := tokenCredential.GetToken(ctx, tokenRequestOptions)
token, err := msiTokenCredential.GetToken(ctx, tokenRequestOptions)
if err != nil {
return err
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/util/mocks/dynamic/dynamic.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/util/purge/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ type ResourceCleaner struct {
// NewResourceCleaner instantiates the new RC object
func NewResourceCleaner(log *logrus.Entry, env env.Core, shouldDelete checkFn, dryRun bool) (*ResourceCleaner, error) {
options := env.Environment().EnvironmentCredentialOptions()
tokenCredential, err := azidentity.NewEnvironmentCredential(options)
spTokenCredential, err := azidentity.NewEnvironmentCredential(options)
if err != nil {
return nil, err
}

scopes := []string{env.Environment().ResourceManagerScope}
authorizer := azidext.NewTokenCredentialAdapter(tokenCredential, scopes)
authorizer := azidext.NewTokenCredentialAdapter(spTokenCredential, scopes)

return &ResourceCleaner{
log: log,
Expand Down