Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
locate and delete app registrations by tag
With cloud-cred-operator tagging Service Principals, we can now located the Service Principals by tag, and find their parent Application Registration and delete them as part of cluster destroy.
- Loading branch information
Joel Diaz
committed
Aug 23, 2019
1 parent
9447003
commit 80ca614
Showing
5 changed files
with
114 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac" | ||
"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns" | ||
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2018-05-01/resources" | ||
"github.com/Azure/go-autorest/autorest" | ||
|
@@ -24,27 +25,37 @@ import ( | |
|
||
// ClusterUninstaller holds the various options for the cluster we want to delete. | ||
type ClusterUninstaller struct { | ||
SubscriptionID string | ||
Authorizer autorest.Authorizer | ||
SubscriptionID string | ||
TenantID string | ||
GraphAuthorizer autorest.Authorizer | ||
ResourceManagerAuthorizer autorest.Authorizer | ||
This comment has been minimized.
Sorry, something went wrong.
abhinavdahiya
Contributor
|
||
|
||
InfraID string | ||
|
||
Logger logrus.FieldLogger | ||
|
||
resourceGroupsClient resources.GroupsGroupClient | ||
zonesClient dns.ZonesClient | ||
recordsClient dns.RecordSetsClient | ||
resourceGroupsClient resources.GroupsGroupClient | ||
zonesClient dns.ZonesClient | ||
recordsClient dns.RecordSetsClient | ||
serviceprincipalsClient graphrbac.ServicePrincipalsClient | ||
applicationsClient graphrbac.ApplicationsClient | ||
} | ||
|
||
func (o *ClusterUninstaller) configureClients() { | ||
o.resourceGroupsClient = resources.NewGroupsGroupClient(o.SubscriptionID) | ||
o.resourceGroupsClient.Authorizer = o.Authorizer | ||
o.resourceGroupsClient.Authorizer = o.ResourceManagerAuthorizer | ||
|
||
o.zonesClient = dns.NewZonesClient(o.SubscriptionID) | ||
o.zonesClient.Authorizer = o.Authorizer | ||
o.zonesClient.Authorizer = o.ResourceManagerAuthorizer | ||
|
||
o.recordsClient = dns.NewRecordSetsClient(o.SubscriptionID) | ||
o.recordsClient.Authorizer = o.Authorizer | ||
o.recordsClient.Authorizer = o.ResourceManagerAuthorizer | ||
|
||
o.serviceprincipalsClient = graphrbac.NewServicePrincipalsClient(o.TenantID) | ||
o.serviceprincipalsClient.Authorizer = o.GraphAuthorizer | ||
|
||
o.applicationsClient = graphrbac.NewApplicationsClient(o.TenantID) | ||
o.applicationsClient.Authorizer = o.GraphAuthorizer | ||
} | ||
|
||
// New returns an Azure destroyer from ClusterMetadata. | ||
|
@@ -55,10 +66,12 @@ func New(logger logrus.FieldLogger, metadata *types.ClusterMetadata) (providers. | |
} | ||
|
||
return &ClusterUninstaller{ | ||
SubscriptionID: session.Credentials.SubscriptionID, | ||
Authorizer: session.Authorizer, | ||
InfraID: metadata.InfraID, | ||
Logger: logger, | ||
SubscriptionID: session.Credentials.SubscriptionID, | ||
TenantID: session.Credentials.TenantID, | ||
GraphAuthorizer: session.GraphAuthorizer, | ||
ResourceManagerAuthorizer: session.ResourceManagerAuthorizer, | ||
InfraID: metadata.InfraID, | ||
Logger: logger, | ||
}, nil | ||
} | ||
|
||
|
@@ -76,6 +89,11 @@ func (o *ClusterUninstaller) Run() error { | |
o.Logger.Debug(err) | ||
return errors.Wrap(err, "failed to delete resource group") | ||
} | ||
o.Logger.Debug("deleting application registrations") | ||
if err := deleteApplicationRegistrations(context.TODO(), o.applicationsClient, o.serviceprincipalsClient, o.Logger, o.InfraID); err != nil { | ||
o.Logger.Debug(err) | ||
return errors.Wrap(err, "failed to delete application registrations and their service principals") | ||
} | ||
|
||
return nil | ||
} | ||
|
@@ -217,3 +235,71 @@ func deleteResourceGroup(ctx context.Context, client resources.GroupsGroupClient | |
func wasNotFound(resp *http.Response) bool { | ||
return resp != nil && resp.StatusCode == http.StatusNotFound | ||
} | ||
|
||
func deleteApplicationRegistrations(ctx context.Context, appClient graphrbac.ApplicationsClient, spClient graphrbac.ServicePrincipalsClient, logger logrus.FieldLogger, infraID string) error { | ||
errorList := []error{} | ||
|
||
tag := fmt.Sprintf("kubernetes.io_cluster.%s=owned", infraID) | ||
servicePrincipals, err := getServicePrincipalsByTag(ctx, spClient, tag, infraID) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to gather list of Service Principals by tag") | ||
} | ||
|
||
for _, sp := range servicePrincipals { | ||
logger = logger.WithField("appID", *sp.AppID) | ||
appFilter := fmt.Sprintf("appId eq '%s'", *sp.AppID) | ||
appResults, err := appClient.List(ctx, appFilter) | ||
if err != nil { | ||
logger.WithError(err).Errorf("unable to get App") | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
errorList = append(errorList, err) | ||
continue | ||
} | ||
|
||
apps := appResults.Values() | ||
if len(apps) != 1 { | ||
msg := fmt.Sprintf("should have recieved only a single result matching AppID, received %d instead", len(apps)) | ||
logger.Error(msg) | ||
errorList = append(errorList, errors.New(msg)) | ||
continue | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
joelddiaz
Contributor
|
||
} | ||
|
||
_, err = appClient.Delete(ctx, *apps[0].ObjectID) | ||
if err != nil { | ||
logger.WithError(err).Error("error deleting Application Registration") | ||
errorList = append(errorList, err) | ||
continue | ||
} | ||
logger.Info("deleted") | ||
} | ||
|
||
return utilerrors.NewAggregate(errorList) | ||
} | ||
|
||
func getServicePrincipalsByTag(ctx context.Context, spClient graphrbac.ServicePrincipalsClient, matchTag, infraID string) ([]graphrbac.ServicePrincipal, error) { | ||
matchedSPs := []graphrbac.ServicePrincipal{} | ||
|
||
infraFilter := fmt.Sprintf("startswith(displayName,'%s')", infraID) | ||
spResults, err := spClient.List(ctx, infraFilter) | ||
if err != nil { | ||
return matchedSPs, err | ||
} | ||
|
||
for { | ||
for _, sp := range spResults.Values() { | ||
for _, tag := range *sp.Tags { | ||
if tag == matchTag { | ||
matchedSPs = append(matchedSPs, sp) | ||
break | ||
} | ||
} | ||
} | ||
|
||
if spResults.NotDone() { | ||
spResults.NextWithContext(ctx) | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
return matchedSPs, nil | ||
} |
Don't change the name for the default one.