Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,23 @@
};
});

// Can be consumed by "az login --identity" by specifying MSI_ENDPOINT environment variable to this action URL
// https://github.com/Azure/msrestazure-for-python/blob/master/msrestazure/azure_active_directory.py#L474

app.MapPost("/token", async (HttpContext context, HttpRequest request) =>
{
var form = await request.ReadFormAsync();
var resource = form["resource"].ToString();
var token = await tokenCredential.GetTokenAsync(new TokenRequestContext(new[] { resource }));
context.Response.Headers.Add("Content-Type", "application/json"); // Set the Content-Type header to JSON
return new Dictionary<string, string>
{
["access_token"] = token.Token,
["expiresOn"] = token.ExpiresOn.ToString("O", CultureInfo.InvariantCulture),
["expires_on"] = token.ExpiresOn.ToUnixTimeSeconds().ToString(),
["token_type"] = "Bearer",
["resource"] = resource
};
});

app.Run();
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ services:
environment:
- "IDENTITY_ENDPOINT=http://azclicredsproxy:8080/token"
- "IMDS_ENDPOINT=dummy_required_value"
# Specify MSI_ENDPOINT below if using "az login --identity" in your service.
- "MSI_ENDPOINT=http://azclicredsproxy:8080/token"
```


Expand Down Expand Up @@ -65,6 +67,9 @@ Then, we must add two environment variables to each service:

With these two environment variables, any service that uses `DefaultAzureCredential` or `ManagedIdentityCredential` will now call the proxy when Azure credentials are needed. This is because one of `ManagedIdentityCredential`'s [source implementations](https://github.com/Azure/azure-sdk-for-net/blob/Azure.Identity_1.6.0/sdk/identity/Azure.Identity/src/AzureArcManagedIdentitySource.cs) explicitly looks for both of these environment variables if they are specified.

> [!NOTE]
> If you are using using `az cli` in your service and your service wants to do `az login --identity` then specify `MSI_ENDPOINT`: the URL of the proxy endpoint (e.g., `http://azclicredsproxy:8080/token`) environment variable instead. `IDENTITY_ENDPOINT` and `IMDS_ENDPOINT` are not required for `az login --identity`.

With this proxy, Dockerfiles can remain untouched and production-ready. The proxy can easily be added to an existing `docker-compose.yml`, and the environment variables are also easy to add. Now, the containerized environment looks like this:

<img src="https://user-images.githubusercontent.com/14242083/224446855-35880df8-1ccd-42df-b226-5afa7b93caa6.png" width="800" />
Expand Down