Skip to content

Commit 1225036

Browse files
authored
Merge pull request #11 from stuartleeks/gh-federated-creds-merge-queue
GH federated creds merge queue
2 parents fb974c0 + 208e673 commit 1225036

File tree

17 files changed

+125
-106
lines changed

17 files changed

+125
-106
lines changed

.devcontainer/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ USER $USERNAME
3030
# && sudo apt install snapd
3131
# RUN sudo snap install hugo --channel=extended
3232

33-
RUN sudo wget https://github.com/gohugoio/hugo/releases/download/v0.68.3/hugo_extended_0.68.3_Linux-64bit.deb
34-
RUN sudo dpkg -i hugo_extended_0.68.3_Linux-64bit.deb
33+
RUN sudo wget https://github.com/gohugoio/hugo/releases/download/v0.147.2/hugo_extended_0.147.2_Linux-64bit.deb
34+
RUN sudo dpkg -i hugo_extended_0.147.2_Linux-64bit.deb
3535

3636
# ** [Optional] Uncomment this section to install additional packages. **
3737
#

.devcontainer/devcontainer.json

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,34 @@
33
{
44
"name": "stuartleeks.com",
55
"dockerFile": "Dockerfile",
6-
7-
// Set *default* container specific settings.json values on container create.
8-
"settings": {
9-
"terminal.integrated.shell.linux": "/bin/bash"
10-
},
11-
126
"mounts": [
13-
// Keep command history
14-
"source=stuartleekscom-bashhistory,target=/home/vscode/commandhistory",
157
// Mounts the .config/gh host folder into the dev container to pick up host gh CLI login details
168
// NOTE that mounting directly to ~/.config/gh makes ~/.config only root-writable
17-
// Instead monut to another location and symlink in Dockerfile
9+
// Instead mount to another location and symlink in Dockerfile
1810
"type=bind,source=${env:HOME}${env:USERPROFILE}/.config/gh,target=/config/gh",
1911
],
20-
21-
// Add the IDs of extensions you want installed when the container is created.
22-
"extensions": [
23-
"bungcip.better-toml"
24-
],
25-
2612
// Use 'forwardPorts' to make a list of ports inside the container available locally.
27-
"forwardPorts": [1313],
28-
13+
"forwardPorts": [
14+
1313
15+
],
2916
// Use 'postCreateCommand' to run commands after the container is created.
3017
// "postCreateCommand": "uname -a",
31-
3218
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-in-docker.
3319
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
34-
3520
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
3621
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
37-
3822
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
39-
"remoteUser": "vscode"
23+
"remoteUser": "vscode",
24+
25+
"customizations": {
26+
"vscode": {
27+
// Add the IDs of extensions you want installed when the container is created.
28+
"extensions": [
29+
"bungcip.better-toml"
30+
]
31+
}
32+
},
33+
"features": {
34+
"ghcr.io/stuartleeks/dev-container-features/shell-history:0": {}
35+
}
4036
}

config/_default/config.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ googleAnalytics = "XXX"
5151

5252

5353

54-
[Author]
55-
name = "Stuart Leeks"
56-
website = "https://stuartleeks.com"
57-
github = "stuartleeks"
58-
twitter = "stuartleeks"
59-
linkedin = "stuartleeks"
60-
stackoverflow = "users/202415/stuart-leeks"
54+
[Params.Author]
55+
name = "Stuart Leeks"
56+
website = "https://stuartleeks.com"
57+
github = "stuartleeks"
58+
twitter = "stuartleeks"
59+
linkedin = "stuartleeks"
60+
stackoverflow = "users/202415/stuart-leeks"
6161

6262

6363
[[menu.main]]
49.3 KB
Loading
27.3 KB
Loading
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
type: post
3+
title: "GitHub EntraID Federated Credentials With Merge Queue"
4+
subtitle: ""
5+
date: 2025-05-08T15:00:23+0100
6+
lastMod: 2025-05-08T15:00:23+0100
7+
draft: false
8+
categories:
9+
- technical
10+
tags:
11+
- github
12+
- azure-entra-id
13+
---
14+
15+
## Introduction
16+
17+
I often have GitHub workflows that deploy resources to Azure.
18+
In the past, I have created a service principal and use the client secret to authenticate to Azure.
19+
This isn't ideal as if the client secret is leaked then it can be used to access the Azure resources.
20+
Additionally, the client secret needs to be rotated periodically which can be a pain (yeah, I'm lazy!).
21+
22+
A while back several colleagues pushed me to start using [federated credentials](https://docs.github.com/en/actions/security-for-github-actions/security-hardening-your-deployments/configuring-openid-connect-in-azure).
23+
Initially I pushed back as it was another thing to learn and I had other things I needed to get done.
24+
When I finally got around to it, I found that it was easier than I expected and I'm gradually migrating all of my projects to use federated credentials.
25+
26+
With federated credentials, you can use Azure Entra ID to authenticate to Azure without needing to manage client secrets.
27+
Instead, your GitHub workflow gets a token from GitHub and then uses that token to get an Azure Entra ID token.
28+
This flow is shown in the [Entra ID docs](https://learn.microsoft.com/en-gb/entra/workload-id/workload-identity-federation).
29+
30+
31+
## Merge Queue - The Challenge
32+
33+
When configuring federated credentials, you need to set up a trust relationship between GitHub and Azure.
34+
As part of this, you set up the contexts that GitHub will use to authenticate to Azure.
35+
For example, the screenshot below shows the configuration for the `main` branch of a repository.
36+
37+
![Configuring federated credentials for main branch builds](./gh-fed-creds-main.png)
38+
39+
40+
To use these credentials in a GitHub workflow, you can use the [azure/login](https://github.com/azure/login) action.
41+
This action needs the client ID of the Azure Entra ID application and the tenant ID of the Azure Entra ID tenant, e.g. from repo variables/secrets.
42+
43+
When the action runs it uses the token from GitHub to get a token from Azure Entra ID.
44+
It is able to do this because of the federated credentials configuration above.
45+
46+
If you are using the same workflow for multiple contexts, for example pushing to `main` and PRs to `main`, then you need to set up the federated credentials for each context.
47+
This can be useful, for example if you only want to allow access to your Azure resources from the `main` branch and not from PRs. Or if you want to use different Azure Entra ID identities for different contexts and grant different access to each identity.
48+
49+
At the time of writing, the available options for the entity type in Entra ID are `Environment`, `Branch`, `PullRequest`, and `Tag`.
50+
Note the lack of `MergeQueue` as an option!
51+
If you trying running a workflow that uses federated credentials as part of a merge queue run you will get an error similar to the one below.
52+
53+
```
54+
Error: AADSTS7002131: No matching federated identity record found for presented assertion subject 'repo:testing-turtle/transient-terrapin:ref:refs/heads/gh-readonly-queue/main/pr-13-7b1f002b00ec25a5facc43e470ef7dfe6d32f300' or no federated identity credential expression matched. Please check your federated identity credential Subject, Expression, Audience and Issuer against the presented assertion. https://learn.microsoft.com/entra/workload-id/workload-identity-federation
55+
```
56+
57+
58+
## Merge Queue - The workaround
59+
60+
While the configuration UI in Entra ID gives a guided flow for GitHub, it doesn't currently support merge queues.
61+
Fortunately, it gives us an escape hatch - we can pick the "Other issuer" option from the scenario dropdown and get more control over the configuration.
62+
63+
When using the GitHub token to get the Entra ID token, the token has a subject claim that varies depending on the context.
64+
The error message above shows us the subject claim that is being presented to Entra ID.
65+
From this we can see that the subject claim is in the format `repo:{owner}/{repo}:ref:refs/heads/{branch}/pr-{number}-{id}`.
66+
67+
Since the subject claim varies based on the PR number and some other ID value that changes, we can't use standard claim matching.
68+
Instead, we need to use the preview of claim expressions to match the subject claim.
69+
70+
To enable merge queue runs with federated credentials, we can set the issuer to `https://token.actions.githubusercontent.com` and the claim expression to `claims['sub'] matches 'repo:<{owner}>/{repo}:ref:refs/heads/gh-readonly-queue/{branch}/pr*'` (note that the values for `{owner}`, `{repo}`, and `{branch}` need to be replaced with the actual values for your repository).
71+
This is shown in the screenshot below.
72+
73+
![Configuring federated credentials with a claim expression to enable merge queue](./gh-fed-creds-merge-queue.png)
74+
75+
76+
## Summary
77+
78+
Federated credentials seem like a great way to authenticate to Azure from GitHub workflows.
79+
They are better for locking down the access to Azure resources and avoid the need to manage client secrets.
80+
81+
Currently, the configuration experience for federated credentials with GitHub merge queues needs a workaround, but hopefully this will be improved in the future.

themes/stuartleeks1/layouts/_default/single.html

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -80,34 +80,6 @@ <h4 class="see-also">{{ i18n "seeAlso" }}</h4>
8080
</ul>
8181
{{ end }}
8282

83-
84-
{{ if (.Params.comments) | or (and (or (not (isset .Params "comments")) (eq .Params.comments nil)) (and .Site.Params.comments (ne .Type "page"))) }}
85-
{{ if .Site.DisqusShortname }}
86-
{{ if .Site.Params.delayDisqus }}
87-
<div class="disqus-comments">
88-
<button id="show-comments" class="btn btn-default" type="button">{{ i18n "show" }} <span class="disqus-comment-count" data-disqus-url="{{ trim .Permalink "/" }}">{{ i18n "comments" }}</span></button>
89-
<div id="disqus_thread"></div>
90-
91-
<script type="text/javascript">
92-
var disqus_config = function () {
93-
this.page.url = '{{ trim .Permalink "/" }}';
94-
};
95-
96-
</script>
97-
</div>
98-
{{ else }}
99-
<div class="disqus-comments">
100-
{{ template "_internal/disqus.html" . }}
101-
</div>
102-
{{ end }}
103-
{{ end }}
104-
{{ if .Site.Params.staticman }}
105-
<div class="staticman-comments">
106-
{{ partial "staticman-comments.html" . }}
107-
</div>
108-
{{ end }}
109-
{{ end }}
110-
11183
</div>
11284
</div>
11385
</div>

themes/stuartleeks1/layouts/partials/disqus.html

Lines changed: 0 additions & 7 deletions
This file was deleted.

themes/stuartleeks1/layouts/partials/footer.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
88
<ul class="list-inline text-center footer-links">
99
{{ range .Site.Data.beautifulhugo.social.social_icons }}
10-
{{- if isset $.Site.Author .id }}
10+
{{- if isset $.Site.Params.Author .id }}
1111
<li>
12-
<a href="{{ printf .url (index $.Site.Author .id) }}" title="{{ .title }}">
12+
<a href="{{ printf .url (index $.Site.Params.Author .id) }}" title="{{ .title }}">
1313
<span class="fa-stack fa-lg">
1414
<i class="fas fa-circle fa-stack-2x"></i>
1515
<i class="{{ .icon }} fa-stack-1x fa-inverse"></i>
@@ -31,18 +31,18 @@
3131
</ul>
3232
<p class="credits copyright text-muted">
3333
&copy;
34-
{{ if .Site.Author.name }}
35-
{{ if .Site.Author.website }}
36-
<a href="{{ .Site.Author.website }}">{{ .Site.Author.name }}</a>
34+
{{ if .Site.Params.Author.name }}
35+
{{ if .Site.Params.Author.website }}
36+
<a href="{{ .Site.Params.Author.website }}">{{ .Site.Params.Author.name }}</a>
3737
{{ else }}
38-
{{ .Site.Author.name }}
38+
{{ .Site.Params.Author.name }}
3939
{{ end }}
4040
{{ end }}
4141

4242
{{ if .Site.Params.since }}
43-
{{ .Site.Params.since }} - {{ .Site.LastChange.Format "2006" }}
43+
{{ .Site.Params.since }} - {{ .Site.Lastmod.Format "2006" }}
4444
{{ else }}
45-
{{ .Site.LastChange.Format "2006" }}
45+
{{ .Site.Lastmod.Format "2006" }}
4646
{{ end }}
4747
</p>
4848
<!-- Please don't remove this, keep my open source work credited :) -->

themes/stuartleeks1/layouts/partials/head.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
{{- with ($.Scratch.Get "Description") }}
4141
<meta name="description" content="{{ . }}">
4242
{{- end }}
43-
{{- with .Site.Author.name }}
43+
{{- with .Site.Params.Author.name }}
4444
<meta name="author" content="{{ . }}"/>
4545
{{- end }}
4646
{{- partial "seo/main.html" . }}

0 commit comments

Comments
 (0)