Skip to content

Commit 1e5b0d5

Browse files
refactor: remove username override from jfrog-oauth module and enhance username extraction
1 parent 67347cc commit 1e5b0d5

File tree

3 files changed

+11
-39
lines changed

3 files changed

+11
-39
lines changed

registry/coder/modules/jfrog-oauth/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ module "jfrog" {
3939

4040
This module is usable by JFrog self-hosted (on-premises) Artifactory as it requires configuring a custom integration. This integration benefits from Coder's [external-auth](https://coder.com/docs/v2/latest/admin/external-auth) feature and allows each user to authenticate with Artifactory using an OAuth flow and issues user-scoped tokens to each user. For configuration instructions, see this [guide](https://coder.com/docs/v2/latest/guides/artifactory-integration#jfrog-oauth) on the Coder documentation.
4141

42+
## Username Handling
43+
44+
The module automatically extracts your JFrog username directly from the OAuth token's JWT payload. This preserves special characters like dots (`.`), hyphens (`-`), and accented characters that Coder normalizes in usernames.
45+
46+
**Priority order:**
47+
48+
1. **JWT extraction** (default) - Extracts username from OAuth token, preserving special characters
49+
2. **Fallback to `username_field`** - If JWT extraction fails, uses Coder username or email
50+
4251
## Examples
4352

4453
Configure the Python pip package manager to fetch packages from Artifactory while mapping the Coder email to the Artifactory username.

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

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

15-
username?: string;
1615
username_field?: string;
1716
jfrog_server_id?: string;
1817
external_auth_id?: string;
@@ -187,28 +186,4 @@ EOF`;
187186
'if [ -z "YES" ]; then\n not_configured maven',
188187
);
189188
});
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-
});
214189
});

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,6 @@ 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-
3828
variable "username_field" {
3929
type = string
4030
description = "The field to use for the artifactory username. i.e. Coder username or email."
@@ -87,8 +77,7 @@ variable "package_managers" {
8777

8878
locals {
8979
username = coalesce(
90-
var.username,
91-
try(data.external.jfrog_username[0].result.username != "" ? data.external.jfrog_username[0].result.username : null, null),
80+
try(data.external.jfrog_username.result.username != "" ? data.external.jfrog_username.result.username : null, null),
9281
var.username_field == "email" ? data.coder_workspace_owner.me.email : data.coder_workspace_owner.me.name
9382
)
9483
jfrog_host = split("://", var.jfrog_url)[1]
@@ -129,9 +118,8 @@ data "coder_workspace_owner" "me" {}
129118
data "coder_external_auth" "jfrog" {
130119
id = var.external_auth_id
131120
}
132-
data "external" "jfrog_username" {
133-
count = var.username == null ? 1 : 0
134121

122+
data "external" "jfrog_username" {
135123
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"]
136124
}
137125

0 commit comments

Comments
 (0)