Skip to content

[Identity] Azure Arc, Fabric and ManagedIdentityCredential refactoring#11976

Merged
27 commits merged into
Azure:masterfrom
sadasant:identity/fix11653
Nov 4, 2020
Merged

[Identity] Azure Arc, Fabric and ManagedIdentityCredential refactoring#11976
27 commits merged into
Azure:masterfrom
sadasant:identity/fix11653

Conversation

@sadasant
Copy link
Copy Markdown
Contributor

@sadasant sadasant commented Oct 21, 2020

This PR does the following:

  1. Simplifies the workflow of ManagedIdentityCredential, fixing [Identity] Refactor Managed Identity Credential #11653
  2. Implements caching on the managed credential so that we only verify which MSI is available once per instance of this class. There's no issue for this, but this is necessary to align with .Net
  3. Adds Azure Arc support, fixing Azure.Identity Support for Managed Identity for Azure Arc Applications #10235
  4. Adds Azure Fabric support, fixing Azure.Identity Support for Managed Identity for Service Fabric Applications #10238

Simplifies the workflow of ManagedIdentityCredential

This is now how we pick what MSI credential is available:

const MSIs = [appServiceMsi2019, appServiceMsi2017, arcMsi, cloudShellMsi, imdsMsi];

for (const msi of MSIs) {
  if (await msi.isAvailable(this.identityClient, resource, clientId, getTokenOptions)) {
    this.cachedMSI = msi;
    return msi;
  }
}

Shows the order of verification more clearly. Reduces the complexity of the credential.

Implements caching on the managed credential

The managed credential was verifying the availability of the MSIs on each request - except for the IMDS one, which had a very weird flow with a stateful boolean value that was passed through from method to method.

Instead of doing that, the first time we authenticate we define what MSI is available, and subsequent calls won't run any validation.

This aligns with .Net.

Adds Azure Arc support

This PR also showcases the refactoring by how simple it is to add a new MSI to the main ManagedIdentityCredential class.

The notes on how I was able to write and test the Arc MSI are here: https://gist.github.com/sadasant/888dc7e88543a21ee7061997984dd207

The change on the ManagedIdentityCredential consists of adding arcMsi to the array of MSIs used inside of the cachedAvailableMSI function.

There's an important note on how I'm currently validating this environment here: link.


I'll leave this PR as draft until we find the time to do an internal review, then I'll move it out of draft.

Fixes #11653
Fixes #10235

New:
Fixes #11595
Fixes #10238
Closes #12164
Fixes #12058

@sadasant sadasant self-assigned this Oct 21, 2020
@ghost ghost added the Azure.Identity label Oct 21, 2020
Comment thread sdk/identity/identity/src/credentials/managedIdentityCredential/arcMsi.ts Outdated
@sophiajt
Copy link
Copy Markdown
Contributor

@sadasant - this definitely feels complete enough to move to being a full PR and to pull in folks who have worked on Arc on other languages for their feedback.

Thanks for taking the time to work on the refactor. The code felt readable and I was able to track where this new functionality was being added and could see where it was related to existing functionality.

@sadasant
Copy link
Copy Markdown
Contributor Author

This PR also fixes #11595. I'm not exactly clear why. I haven't had the time to dig through the previous code to see why. I tested as many times as I could think of with and without these changes and these changes definitely fix #11595.

@sadasant sadasant marked this pull request as ready for review October 30, 2020 22:13
@sadasant sadasant requested a review from bterlson as a code owner October 30, 2020 22:13
@sophiajt
Copy link
Copy Markdown
Contributor

sophiajt commented Nov 1, 2020

This looks good. I commented that we're currently removing the 2019 MSI support because it seems to cause issues with some services (#12058)

cc @schaabs for confirmation.

Other than that, I think this is ready from my perspective.

@sadasant sadasant changed the title [Identity] Azure Arc and ManagedIdentityCredential refactoring [Identity] Azure Arc, Fabric and ManagedIdentityCredential refactoring Nov 2, 2020
Comment thread sdk/identity/identity/package.json Outdated
"sinon": "^9.0.2",
"@types/sinon": "^9.0.4"
"@types/sinon": "^9.0.4",
"fs-mock": "~1.2.1"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Called fs-mock here ^

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks!

Copy link
Copy Markdown
Contributor

@sophiajt sophiajt left a comment

Choose a reason for hiding this comment

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

Left some comments, but this looks pretty good

Copy link
Copy Markdown
Member

@laiapat laiapat left a comment

Choose a reason for hiding this comment

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

Looking really good so far! This is a lot of great work 👍

Comment thread sdk/identity/identity/CHANGELOG.md Outdated
Comment thread sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts Outdated
Comment thread sdk/identity/identity/src/credentials/managedIdentityCredential/arcMsi.ts Outdated
Comment thread sdk/identity/identity/src/credentials/managedIdentityCredential/fabricMsi.ts Outdated
Comment thread sdk/identity/identity/src/credentials/managedIdentityCredential/fabricMsi.ts Outdated
Comment thread sdk/identity/identity/src/credentials/managedIdentityCredential/fabricMsi.ts Outdated
all wonderful suggestions by McCoy!

Co-authored-by: McCoy Patiño <39780829+mccoyp@users.noreply.github.com>
@check-enforcer
Copy link
Copy Markdown

check-enforcer Bot commented Nov 3, 2020

This pull request is protected by Check Enforcer.

What is Check Enforcer?

Check Enforcer helps ensure all pull requests are covered by at least one check-run (typically an Azure Pipeline). When all check-runs associated with this pull request pass then Check Enforcer itself will pass.

Why am I getting this message?

You are getting this message because Check Enforcer did not detect any check-runs being associated with this pull request within five minutes. This may indicate that your pull request is not covered by any pipelines and so Check Enforcer is correctly blocking the pull request being merged.

What should I do now?

If the check-enforcer check-run is not passing and all other check-runs associated with this PR are passing (excluding license-cla) then you could try telling Check Enforcer to evaluate your pull request again. You can do this by adding a comment to this pull request as follows:
/check-enforcer evaluate
Typically evaulation only takes a few seconds. If you know that your pull request is not covered by a pipeline and this is expected you can override Check Enforcer using the following command:
/check-enforcer override
Note that using the override command triggers alerts so that follow-up investigations can occur (PRs still need to be approved as normal).

What if I am onboarding a new service?

Often, new services do not have validation pipelines associated with them, in order to bootstrap pipelines for a new service, you can issue the following command as a pull request comment:
/azp run prepare-pipelines
This will run a pipeline that analyzes the source tree and creates the pipelines necessary to build and validate your pull request. Once the pipeline has been created you can trigger the pipeline using the following comment:
/azp run js - [service] - ci

@sadasant sadasant requested a review from laiapat November 3, 2020 22:56
Copy link
Copy Markdown
Member

@laiapat laiapat left a comment

Choose a reason for hiding this comment

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

Service Fabric side looks good! 🎉

return this.cachedMSI;
}

const MSIs = [fabricMsi, appServiceMsi2017, cloudShellMsi, arcMsi, imdsMsi];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me.

// If we received any response, the endpoint is available
logger.info(`IMDS endpoint is available`);

// IMDS MSI available!
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Well, the endpoint is available. If it has no assigned identity, it will respond 400 to a token request. Looks like getToken would throw CredentialUnavailable in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If getToken raises a 400 error, it will bubble up to this point: #11807 (comment)

Where we throw CredentialUnavailable!

How does that sound?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm asking more information about this through Teams. In any case, the changes related to any retry policy will happen through a separate issue and PR.

if (response.status !== 401) {
throw new AuthenticationError(
response.status,
"To authenticate with Azure Arc MSI, status code 401 is expected on the first request."
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be helpful to users to return some info from the response from the service in the error. I'm wondering if it could have some useful information, for instance something like a malformed request or missing parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll do that! thank you!

Copy link
Copy Markdown
Member

@catalinaperalta catalinaperalta left a comment

Choose a reason for hiding this comment

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

The Arc MSI support looks good to me!

@ghost
Copy link
Copy Markdown

ghost commented Nov 4, 2020

Hello @sadasant!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 3f6f5ed into Azure:master Nov 4, 2020
@sadasant sadasant deleted the identity/fix11653 branch November 4, 2020 22:53
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

5 participants