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

Add support for Managed Identity auth for physical/Azure #10189

Merged
merged 2 commits into from
Oct 28, 2020

Conversation

sfc-gh-jelsesiy
Copy link
Contributor

Obtain OAuth token from IMDS to allow for access to Azure Blob with
short-lived dynamic credentials

Fix #7322

@hashicorp-cla
Copy link

hashicorp-cla commented Oct 20, 2020

CLA assistant check
All committers have signed the CLA.

@sfc-gh-jelsesiy
Copy link
Contributor Author

@calvn Please have a look. I'm hoping we can get this into the 1.6 milestone, thanks!

@sfc-gh-jelsesiy sfc-gh-jelsesiy force-pushed the feat/managed-identity branch 4 times, most recently from c0057ac to e59d71b Compare October 26, 2020 17:41
@sfc-gh-jelsesiy
Copy link
Contributor Author

@calvn Did you have a chance to triage this? Thank you!

@calvn calvn self-requested a review October 27, 2020 21:37
@calvn
Copy link
Contributor

calvn commented Oct 27, 2020

@sfc-gh-jelsesiy sorry, I missed the ping from your earlier messages! I'll be taking a look at this.

Copy link
Contributor

@calvn calvn left a comment

Choose a reason for hiding this comment

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

Looks good overall! We should also adjust the tests within azure_test.go so that they can run using creds from this flow if the account key is not provided.

Can you do a run of the tests afterwards and share the results over here?

physical/azure/azure.go Outdated Show resolved Hide resolved
@calvn calvn self-requested a review October 27, 2020 22:21
@calvn calvn added this to the 1.6 milestone Oct 27, 2020
@sfc-gh-jelsesiy
Copy link
Contributor Author

Looks good overall! We should also adjust the tests within azure_test.go so that they can run using creds from this flow if the account key is not provided.

Can you do a run of the tests afterwards and share the results over here?

Sure I can add that, what are you thinking specifically? Running these tests without mocking would require you run it on a host that has the IMDS available and a valid identity attached to it. This seems tricky for most CI systems which is why I haven't included any

@sfc-gh-jelsesiy sfc-gh-jelsesiy force-pushed the feat/managed-identity branch 2 times, most recently from 98e3735 to 21edd51 Compare October 27, 2020 23:25
@calvn
Copy link
Contributor

calvn commented Oct 27, 2020

Definitely agree on the trickiness bit! We'd want to verify at some capacity that this works as intended, even if things are set up manually at this point, so that's what we might end up doing.

@sfc-gh-jelsesiy
Copy link
Contributor Author

sfc-gh-jelsesiy commented Oct 28, 2020

Definitely agree on the trickiness bit! We'd want to verify at some capacity that this works as intended, even if things are set up manually at this point, so that's what we might end up doing.

Okay let me see what I can come up with. I also noticed that the current tests are duplicating logic that is actually part of NewAzureBackend implementation. This is done to get access to the underlying containerURL object it seems but also means that you don't get to test any of the error handling that happens prior to containerURL creation. I'm going to think about how to improve this as I'm adding the new tests

Obtain OAuth token from IMDS to allow for access to Azure Blob with
short-lived dynamic credentials

Fix hashicorp#7322
@sfc-gh-jelsesiy
Copy link
Contributor Author

@calvn I added a separate commit for the tests, let me know what you think. I documented what's needed to successfully run the tests using managed identities and did that myself, see:

go test -count=1 -v -run TestAzureBackend ./physical/azure
=== RUN   TestAzureBackend
    azure_test.go:57: using managed identity to authenticate against storage account
2020-10-28T18:09:21.738Z [INFO]  accountKey not set, using managed identity auth
2020-10-28T18:09:21.770Z [DEBUG] token refreshed, new token expires in: access_token_expiry=84287
--- PASS: TestAzureBackend (0.52s)
=== RUN   TestAzureBackend_ListPaging
    azure_test.go:71: using managed identity to authenticate against storage account
2020-10-28T18:09:22.255Z [INFO]  accountKey not set, using managed identity auth
2020-10-28T18:09:22.293Z [DEBUG] token refreshed, new token expires in: access_token_expiry=84286
--- PASS: TestAzureBackend_ListPaging (70.05s)
PASS
ok  	github.com/hashicorp/vault/physical/azure	70.574s

@calvn
Copy link
Contributor

calvn commented Oct 28, 2020

Perfect, thanks for sharing the test result as well!

Do the new test changes remove the ability to test the physical backend locally? Ideally we would want to be able to do both. That is, if ran on an Azure VM it will use IMDS, and if ran locally all the creds need to be provided as they were before in order be able to use Azure storage for local testing.

@sfc-gh-jelsesiy
Copy link
Contributor Author

Perfect, thanks for sharing the test result as well!

Do the new test changes remove the ability to test the physical backend locally? Ideally we would want to be able to do both. That is, if ran on an Azure VM it will use IMDS, and if ran locally all the creds need to be provided as they were before in order be able to use Azure storage for local testing.

The way it's implemented is that if AZURE_ACCOUNT_KEY is specified it will always use that and only if empty attempts to use managed identity auth via IMDS. Even if you specify AZURE_ACCOUNT_KEY on a VM that has IMDS available it will still use the access key unless removed

@calvn
Copy link
Contributor

calvn commented Oct 28, 2020

Why are the env vars AZURE_ENVIRONMENT and AZURE_ARM_ENDPOINT removed from the test consumption?

@sfc-gh-jelsesiy
Copy link
Contributor Author

Why are the env vars AZURE_ENVIRONMENT and AZURE_ARM_ENDPOINT removed from the test consumption?

The way the tests have been set up before wasn't correct. The credential creation has been duplicated in the tests instead of relying on the logic in NewAzureBackend which is done now

@calvn
Copy link
Contributor

calvn commented Oct 28, 2020

Ah, gotcha!

@sfc-gh-jelsesiy
Copy link
Contributor Author

@calvn Please let me know if you have any code comments so I can address them today as I got the time allocated from my company for it :)

Copy link
Contributor

@calvn calvn left a comment

Choose a reason for hiding this comment

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

Looks good, thanks for the contribution!

Ran the test locally and they passed as well:

=== RUN   TestAzureBackend
    azure_test.go:63: using account key provided to authenticate against storage account
--- PASS: TestAzureBackend (3.07s)
=== RUN   TestAzureBackend_ListPaging
    azure_test.go:73: using account key provided to authenticate against storage account
--- PASS: TestAzureBackend_ListPaging (256.73s)
PASS

@sfc-gh-jelsesiy
Copy link
Contributor Author

sfc-gh-jelsesiy commented Oct 28, 2020

Great!! Should I be worried about the test failures (PR checks) or is that not blocking the merge?

@calvn
Copy link
Contributor

calvn commented Oct 28, 2020

Doesn't look like it's related to your changes, but can you merge/rebase origin/master into this branch if you haven't done so recently?

@sfc-gh-jelsesiy
Copy link
Contributor Author

Doesn't look like it's related to your changes, but can you merge/rebase from origin/master if you haven't done so recently?

I'm up to date to origin/master

@calvn calvn merged commit 652fae3 into hashicorp:master Oct 28, 2020
calvn pushed a commit that referenced this pull request Oct 28, 2020
* Add support for Managed Identity auth for physical/Azure

Obtain OAuth token from IMDS to allow for access to Azure Blob with
short-lived dynamic credentials

Fix #7322

* add tests & update docs/dependencies
@sfc-gh-jelsesiy sfc-gh-jelsesiy deleted the feat/managed-identity branch October 28, 2020 22:24
calvn added a commit that referenced this pull request Oct 29, 2020
…ure (#10189) (#10260)

* Add support for Managed Identity auth for physical/Azure (#10189)

* Add support for Managed Identity auth for physical/Azure

Obtain OAuth token from IMDS to allow for access to Azure Blob with
short-lived dynamic credentials

Fix #7322

* add tests & update docs/dependencies

* mod: fix go.mod and go.sum conflicts

* mod: update [email protected] (#10261)

Co-authored-by: Jonas-Taha El Sesiy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Azure storage backend with OAuth token
3 participants