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

Support managed identity for Azure App Service/Azure Container Apps #7086

Merged

Conversation

apc-kamezaki
Copy link
Contributor

@apc-kamezaki apc-kamezaki commented Oct 2, 2024

Why the changes in this PR are needed?

As you can see on the discussion https://github.com/orgs/open-policy-agent/discussions/592 , opa server cannot connect azure blob storage on Azure App Service using managed identity.

It seems that IMDS(Instance MetaData Service) endpoint is not available on Azure App Service/Container Apps. It should use special endpoint for getting token instead.
See :
https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Chttp#connect-to-azure-services-in-app-code
https://learn.microsoft.com/en-us/azure/container-apps/managed-identity?tabs=bicep%2Chttp#connect-to-azure-services-in-app-code

What are the changes in this PR?

IDENTITY_ENDPOINT and IDENTITY_HEADER envirnnment variables are defined on Azure App Service for getting the token.
Detect these variables and switch the endpoint and header value from IMDS to IDENTITY_ENDPOINT

Notes to assist PR review:

IMDS endpoint (169.254.169.254) and IDENTITY_ENDPOINT is the almost same request/response format.
But minimum api-version is bit different. It should be 2019-08-01 or later.

Here is the endpoint reference : https://learn.microsoft.com/en-us/azure/app-service/overview-managed-identity?tabs=portal%2Chttp#rest-endpoint-reference

Further comments:

This PR is related to #7085

Copy link

netlify bot commented Oct 2, 2024

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 7405de4
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/6705eee36c6d580008a3e19f
😎 Deploy Preview https://deploy-preview-7086--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -55,15 +58,25 @@ func (ap *azureManagedIdentitiesAuthPlugin) NewClient(c Config) (*http.Client, e
}

if ap.Endpoint == "" {
ap.Endpoint = azureIMDSEndpoint
identityEndpoint := os.Getenv("IDENTITY_ENDPOINT")
Copy link
Member

Choose a reason for hiding this comment

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

Are there any docs or links we can add here that point to the ordering used here? I mean if IDENTITY_ENDPOINT is set, does that determine the flow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

@apc-kamezaki the changes looks good. Just a comment for clarification 👇

@ashutosh-narkar
Copy link
Member

@apc-kamezaki if you could please update your commit message as per the guidelines documented here, we can get this in. Thanks.

@apc-kamezaki
Copy link
Contributor Author

@ashutosh-narkar I updated the commit message.

@ashutosh-narkar
Copy link
Member

Do we need to update anything in the docs?

@apc-kamezaki
Copy link
Contributor Author

I'm sorry I forgot to write document.

I tried to wrote the docs at this time.
Unfortunately I'm not good at English, so please let me know if there are someting wrong.

| `services[_].credentials.azure_managed_identity.resource` | `string` | No | App ID URI of the target resource. (default: `https://storage.azure.com/`) |
| `services[_].credentials.azure_managed_identity.object_id` | `string` | No | Optional object ID of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities. |
| `services[_].credentials.azure_managed_identity.client_id` | `string` | No | Optional client ID of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities. |
| `services[_].credentials.azure_managed_identity.mi_res_id` | `string` | No | Optional Azure Resource ID of the managed identity you would like the token for. Required, if your VM has multiple user-assigned managed identities. |
| `services[_].credentials.azure_managed_identity.use_app_service_msi` | `bool` | No | Set true if you would like to set `x-identity-header` instead of `Metadata` header for token request. Default: set true automatically when you use managed identity on Azure App Servic or Container Apps. |
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to expose this? From the code, it looks like if IDENTITY_ENDPOINT is set, then .UseAppServiceMsi is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I included this value in case we want to explicitly use IMDS and disable auto detection.

I updated the document again at this time.

Copy link
Member

Choose a reason for hiding this comment

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

Let's remove use_app_service_msi from the docs. If we need it, we can always add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll remove it.


The following is an example of how to use an [Azure storage
The following is an example of how to use an [Azure storageß
Copy link
Member

Choose a reason for hiding this comment

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

Typo: storageß

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sorry. I'll fix it.

Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

@apc-kamezaki please make the doc update and we can then get this in. Thanks for the contribution!

@apc-kamezaki apc-kamezaki force-pushed the feature/appservice-msi branch 2 times, most recently from 08a7fe1 to abb9c96 Compare October 8, 2024 23:39
@apc-kamezaki
Copy link
Contributor Author

Thank you for your review. I've updated the docs.

…iner Apps

IDENTITY_ENDPOINT and IDENTITY_HEADER envirnnment variables are
provided on Azure App Service for getting the token.
We can detect these variables and switch the endpoint
and header value from IMDS.

Fixes: open-policy-agent#7085
Signed-off-by: Hitoshi Kamezaki <[email protected]>
@ashutosh-narkar ashutosh-narkar merged commit 2c76de4 into open-policy-agent:main Oct 9, 2024
28 checks passed
@apc-kamezaki apc-kamezaki deleted the feature/appservice-msi branch October 9, 2024 03:56
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.

2 participants