Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Adds support for MIC to authenticate with azure using system assigned/user assigned MSI #265

Merged
merged 17 commits into from
Jul 16, 2019

Conversation

kkmsft
Copy link
Contributor

@kkmsft kkmsft commented Jun 25, 2019

This PR adds the capability for MIC to look at azure.json or environment variables
to determine whether the system assigned or user assigned MSI has to be used for accessing
azure resources. The MIC requests for token based on MSI. Also contains changes in NMI to determine if the request is originating from an MIC replicaset. If so, NMI directly generates the tokens instead of looking up the azure assigned identity for the pod-binding match.

Reason for Change:

Adds ability for MIC to authenticate using system assigned/user assigned MSI.

Issue Fixed:

Notes for Reviewers:
TODO: run the e2e on system assigned identity cluster.

@kkmsft kkmsft changed the title Adds support for MIC to authenticate with azure using system assigned/user assigned MSI [WIP] Adds support for MIC to authenticate with azure using system assigned/user assigned MSI Jun 25, 2019
@kkmsft kkmsft requested a review from aramase June 25, 2019 15:37

var spt *adal.ServicePrincipalToken
if azureConfig.UseManagedIdentityExtension {
// MSI endpoing is required for both types of MSI - system assigned and user assigned.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/endpoing/endpoint

// MSI endpoing is required for both types of MSI - system assigned and user assigned.
msiEndpoint, err := adal.GetMSIVMEndpoint()
if err != nil {
glog.Errorf("Failed to get msiEndpoint: %+v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to get msiEndpoint to Failed to get msiEndpoint. Error:
Following the same pattern we use in other parts of the code.

// TODO: Filter out the deployment owner references.
deployment := ""
if podList.Items[0].OwnerReferences[0].Kind == "ReplicaSet" {
deployment = podList.Items[0].OwnerReferences[0].Name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the replicaset name, so maybe we should change deployment to rsName to reflect the value we are getting

}
if err != nil {
logger.Errorf("failed to get service principal token for pod:%s/%s, %+v", podns, podname, err)
http.Error(w, err.Error(), http.StatusForbidden)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to return the status code returned by metadata ep instead of 403 since we are really just proxying the request here? I think metadata ep will return a 400 or 404. 403 is not part of retry status codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure yet, how to extract this info from ada. For time being have put a TODO to handle in a future PR.

@kkmsft kkmsft changed the title [WIP] Adds support for MIC to authenticate with azure using system assigned/user assigned MSI Adds support for MIC to authenticate with azure using system assigned/user assigned MSI Jul 15, 2019
kkmsft added 16 commits July 15, 2019 15:02
… or user assigned MSI.

Resolves the item in Azure#261.
This PR adds the capability for MIC to look at azure.json or environment variables
to determine whether the system assigned or user assigned MSI has to be used for accessing
azure resources. The MIC requests for token based on MSI. Also contains changes in NMI to determine
if the request is originating from an MIC replicaset. If so, NMI directly generates the tokens
instead of looking up the azure assigned identity for the pod-binding match.
In pod retry the object returned from listing was used to cast even when error was returned. This PR
fixes this issue. The retry call has been refactored to account for other error conditions and simplify
the usage. This PR introduces getPodList call and then a retry call built on top of that.
@@ -0,0 +1,50 @@
## Introduction

The MIC component in aad-pod-identity needs to authenticate with the cloud to assign and remove user assign identities onto
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/user assign/user assigned

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

nodes in the Kubernetes cluster. The system/user assigned MSI needs to have role assignments authorizing such operations on the vms/vmss
and also operations on the user assigned identity.

After the cluster is created to perform the following steps to obtain the principal id:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the cluster is created, run these commands to retrieve the principal id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

and also operations on the user assigned identity.

After the cluster is created to perform the following steps to obtain the principal id:
for VMAs:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: VMAs -> VMAS

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -59,6 +60,8 @@ func NewCloudProvider(configFile string) (c *Client, e error) {
azureConfig.SubscriptionID = os.Getenv("SUBSCRIPTION_ID")
azureConfig.ResourceGroupName = os.Getenv("RESOURCE_GROUP")
azureConfig.VMType = os.Getenv("VM_TYPE")
azureConfig.UseManagedIdentityExtension = strings.EqualFold(os.Getenv("USE_MSI"), "True")
azureConfig.UserAssignedIdentityID = os.Getenv("USER_ASSIGNED_MSI_CLIENTID")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should split CLIENTID into CLIENT_ID to stay in sync with the convention we already have.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
response, err := json.Marshal(*token)
if err != nil {
logger.Errorf("Failed to unmarshal service principal token. Error: %+v", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to marshal service principal token

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Anish. Done.

logger.Errorf("Failed to unmarshal service principal token. Error: %+v", err)
return nil, http.StatusInternalServerError, err
}
return response, 0, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think 0 is a valid http status code. We should return maybe 200 OK, because request was successful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For success case, the return code is not used. We just write the response. In any case have changed it to http.StatusOK

@kkmsft kkmsft merged commit a02bc1c into Azure:master Jul 16, 2019
@kkmsft kkmsft deleted the system_msi branch July 16, 2019 01:56
@kkmsft kkmsft added this to the v1.5 milestone Jul 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants