Skip to content

Commit 67347cc

Browse files
feat: add username override functionality to jfrog-oauth and fix username extraction from oauth
1 parent 7740e9c commit 67347cc

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

registry/coder/modules/jfrog-oauth/main.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("jfrog-oauth", async () => {
1212
jfrog_url: string;
1313
package_managers: string;
1414

15+
username?: string;
1516
username_field?: string;
1617
jfrog_server_id?: string;
1718
external_auth_id?: string;
@@ -186,4 +187,28 @@ EOF`;
186187
'if [ -z "YES" ]; then\n not_configured maven',
187188
);
188189
});
190+
191+
it("accepts manual username override with special characters", async () => {
192+
const customUsername = "john.smith";
193+
const state = await runTerraformApply<TestVariables>(import.meta.dir, {
194+
agent_id: "some-agent-id",
195+
jfrog_url: fakeFrogUrl,
196+
username: customUsername,
197+
package_managers: JSON.stringify({
198+
npm: ["npm"],
199+
pypi: ["pypi"],
200+
docker: ["docker.jfrog.io"],
201+
}),
202+
});
203+
204+
const coderScript = findResourceInstance(state, "coder_script");
205+
206+
expect(coderScript.script).toContain(
207+
`docker login "$repo" --username ${customUsername}`,
208+
);
209+
210+
expect(coderScript.script).toContain(`https://${customUsername}:`);
211+
212+
expect(coderScript.script).toContain("cat << EOF > ~/.npmrc");
213+
});
189214
});

registry/coder/modules/jfrog-oauth/main.tf

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ variable "jfrog_server_id" {
2525
default = "0"
2626
}
2727

28+
variable "username" {
29+
type = string
30+
description = <<-EOF
31+
Override JFrog username. Leave empty for automatic extraction from OAuth token.
32+
The module automatically extracts your JFrog username from the OAuth token.
33+
Only set this if automatic extraction fails or you need to use a different username.
34+
EOF
35+
default = null
36+
}
37+
2838
variable "username_field" {
2939
type = string
3040
description = "The field to use for the artifactory username. i.e. Coder username or email."
@@ -76,8 +86,11 @@ variable "package_managers" {
7686
}
7787

7888
locals {
79-
# The username field to use for artifactory
80-
username = var.username_field == "email" ? data.coder_workspace_owner.me.email : data.coder_workspace_owner.me.name
89+
username = coalesce(
90+
var.username,
91+
try(data.external.jfrog_username[0].result.username != "" ? data.external.jfrog_username[0].result.username : null, null),
92+
var.username_field == "email" ? data.coder_workspace_owner.me.email : data.coder_workspace_owner.me.name
93+
)
8194
jfrog_host = split("://", var.jfrog_url)[1]
8295
common_values = {
8396
JFROG_URL = var.jfrog_url
@@ -116,6 +129,11 @@ data "coder_workspace_owner" "me" {}
116129
data "coder_external_auth" "jfrog" {
117130
id = var.external_auth_id
118131
}
132+
data "external" "jfrog_username" {
133+
count = var.username == null ? 1 : 0
134+
135+
program = ["bash", "-c", "TOKEN='${data.coder_external_auth.jfrog.access_token}'; PAYLOAD=$(echo \"$TOKEN\" | cut -d. -f2); LEN=$(printf '%s' \"$PAYLOAD\" | wc -c); MOD=$((LEN % 4)); if [ $MOD -eq 2 ]; then PAYLOAD=\"$PAYLOAD==\"; elif [ $MOD -eq 3 ]; then PAYLOAD=\"$PAYLOAD=\"; fi; USERNAME=$(echo \"$PAYLOAD\" | base64 -d 2>/dev/null | grep -oP '\"/users/\\K[^\"]+' 2>/dev/null | head -1 || echo \"\"); if [ -z \"$USERNAME\" ]; then echo '{\"username\":\"\"}'; else USERNAME=$(echo \"$USERNAME\" | sed 's/\\\\/\\\\\\\\/g; s/\"/\\\\\"/g'); echo \"{\\\"username\\\":\\\"$USERNAME\\\"}\"; fi"]
136+
}
119137

120138
resource "coder_script" "jfrog" {
121139
agent_id = var.agent_id

0 commit comments

Comments
 (0)