@@ -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+
2838variable "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
7888locals {
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" {}
116129data "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
120138resource "coder_script" "jfrog" {
121139 agent_id = var. agent_id
0 commit comments