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

Added FP Check for validation #3335

Merged
merged 4 commits into from
Feb 2, 2024
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
6 changes: 6 additions & 0 deletions pkg/backend/openshiftcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package backend
import (
"context"
"fmt"
"net/http"
"strings"
"sync/atomic"
"time"
Expand Down Expand Up @@ -339,6 +340,11 @@ func (ocb *openShiftClusterBackend) asyncOperationResultLog(log *logrus.Entry, i
return
}

if strings.Contains(strings.ToLower(backendErr.Error()), "one of the claims 'puid' or 'altsecid' or 'oid' should be present") {
backendErr = api.NewCloudError(http.StatusBadRequest, api.CloudErrorCodeInvalidServicePrincipalClaims,
"properties.servicePrincipalProfile", "The Azure Red Hat Openshift resource provider service principal has been removed from your tenant. To restore, please unregister and then re-register the Azure Red Hat OpenShift resource provider.")
s-fairchild marked this conversation as resolved.
Show resolved Hide resolved
}

_, ok := backendErr.(*api.CloudError)
if ok {
log = log.WithField("resultType", utillog.UserErrorResultType)
Expand Down
52 changes: 22 additions & 30 deletions pkg/validate/openshiftcluster_validatedynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func ensureAccessTokenClaims(ctx context.Context, spTokenCredential azcore.Token
return api.NewCloudError(
http.StatusBadRequest,
api.CloudErrorCodeInvalidServicePrincipalClaims,
"properties.servicePrincipleProfile",
"The provided service principal does not give an access token with at least one of the claims 'altsecid', 'oid' or 'puid'.")
"properties.servicePrincipalProfile",
s-fairchild marked this conversation as resolved.
Show resolved Hide resolved
"The Azure Red Hat Openshift resource provider service principal has been removed from your tenant. To restore, please unregister and then re-register the Azure Red Hat OpenShift resource provider.")
}

// Dynamic validates an OpenShift cluster
Expand All @@ -115,11 +115,21 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {
})
}

var fpClientCred azcore.TokenCredential
var spClientCred azcore.TokenCredential
var pdpClient remotepdp.RemotePDPClient
spp := dv.oc.Properties.ServicePrincipalProfile

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

useCheckAccess, err := dv.env.LiveConfig().UseCheckAccess(ctx)
dv.log.Info("USE_CHECKACCESS: ", useCheckAccess)
if err != nil {
Expand All @@ -132,21 +142,6 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {
) {
// TODO remove after successfully migrating to CheckAccess
dv.log.Info("Using CheckAccess instead of ListPermissions")
var err error
fpClientCred, err = dv.env.FPNewClientCertificateCredential(dv.subscriptionDoc.Subscription.Properties.TenantID)
if err != nil {
return err
}

spClientCred, err = azidentity.NewClientSecretCredential(
dv.subscriptionDoc.Subscription.Properties.TenantID,
spp.ClientID,
string(spp.ClientSecret),
nil,
)
if err != nil {
return err
}

aroEnv := dv.env.Environment()
pdpClient = remotepdp.NewRemotePDPClient(
Expand All @@ -156,20 +151,12 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {
)
}

tenantID := dv.subscriptionDoc.Subscription.Properties.TenantID
options := dv.env.Environment().ClientSecretCredentialOptions()
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, spTokenCredential, scopes)
err = ensureAccessTokenClaims(ctx, spClientCred, scopes)
if err != nil {
return err
}
spAuthorizer := azidext.NewTokenCredentialAdapter(spTokenCredential, scopes)
spAuthorizer := azidext.NewTokenCredentialAdapter(spClientCred, scopes)

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

// SP validation
err = spDynamic.ValidateServicePrincipal(ctx, spTokenCredential)
err = spDynamic.ValidateServicePrincipal(ctx, spClientCred)
if err != nil {
return err
}
Expand Down Expand Up @@ -225,6 +212,11 @@ func (dv *openShiftClusterDynamicValidator) Dynamic(ctx context.Context) error {
return err
}

err = ensureAccessTokenClaims(ctx, fpClientCred, scopes)
if err != nil {
return err
}

// FP validation
fpDynamic := dynamic.NewValidator(
dv.log,
Expand Down
Loading