Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 357bd41

Browse files
authored
feat(jfrog): add JFrog vscode extension, CLI completion and docker support (#115)
1 parent 382933a commit 357bd41

File tree

6 files changed

+324
-73
lines changed

6 files changed

+324
-73
lines changed

jfrog-oauth/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ module "jfrog" {
2121
source = "https://registry.coder.com/modules/jfrog-oauth"
2222
agent_id = coder_agent.example.id
2323
jfrog_url = "https://jfrog.example.com"
24-
auth_method = "oauth"
2524
username_field = "username" # If you are using GitHub to login to both Coder and Artifactory, use username_field = "username"
2625
package_managers = {
2726
"npm": "npm",
@@ -106,6 +105,25 @@ jf pip install requests
106105
pip install requests
107106
```
108107

108+
### Configure code-server with JFrog extension
109+
110+
The [JFrog extension](https://open-vsx.org/extension/JFrog/jfrog-vscode-extension) for VS Code allows you to interact with Artifactory from within the IDE.
111+
112+
```hcl
113+
module "jfrog" {
114+
source = "https://registry.coder.com/modules/jfrog-oauth"
115+
agent_id = coder_agent.example.id
116+
jfrog_url = "https://jfrog.example.com"
117+
username_field = "username" # If you are using GitHub to login to both Coder and Artifactory, use username_field = "username"
118+
configure_code_server = true # Add JFrog extension configuration for code-server
119+
package_managers = {
120+
"npm": "npm",
121+
"go": "go",
122+
"pypi": "pypi"
123+
}
124+
}
125+
```
126+
109127
### Using the access token in other terraform resources
110128

111129
JFrog Access token is also available as a terraform output. You can use it in other terraform resources. For example, you can use it to configure an [Artifactory docker registry](https://jfrog.com/help/r/jfrog-artifactory-documentation/docker-registry) with the [docker terraform provider](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs).

jfrog-oauth/main.tf

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@ terraform {
44
required_providers {
55
coder = {
66
source = "coder/coder"
7-
version = ">= 0.12"
7+
version = ">= 0.12.4"
88
}
99
}
1010
}
1111

1212
variable "jfrog_url" {
1313
type = string
14-
description = "JFrog instance URL. e.g. https://jfrog.example.com"
14+
description = "JFrog instance URL. e.g. https://myartifactory.jfrog.io"
15+
# ensue the URL is HTTPS or HTTP
16+
validation {
17+
condition = can(regex("^(https|http)://", var.jfrog_url))
18+
error_message = "jfrog_url must be a valid URL starting with either 'https://' or 'http://'"
19+
}
1520
}
1621

1722
variable "username_field" {
@@ -35,22 +40,30 @@ variable "agent_id" {
3540
description = "The ID of a Coder agent."
3641
}
3742

43+
variable "configure_code_server" {
44+
type = bool
45+
description = "Set to true to configure code-server to use JFrog."
46+
default = false
47+
}
48+
3849
variable "package_managers" {
3950
type = map(string)
4051
description = <<EOF
4152
A map of package manager names to their respective artifactory repositories.
4253
For example:
4354
{
44-
"npm": "npm-local",
45-
"go": "go-local",
46-
"pypi": "pypi-local"
55+
"npm": "YOUR_NPM_REPO_KEY",
56+
"go": "YOUR_GO_REPO_KEY",
57+
"pypi": "YOUR_PYPI_REPO_KEY",
58+
"docker": "YOUR_DOCKER_REPO_KEY"
4759
}
4860
EOF
4961
}
5062

5163
locals {
5264
# The username field to use for artifactory
53-
username = var.username_field == "email" ? data.coder_workspace.me.owner_email : data.coder_workspace.me.owner
65+
username = var.username_field == "email" ? data.coder_workspace.me.owner_email : data.coder_workspace.me.owner
66+
jfrog_host = replace(var.jfrog_url, "https://", "")
5467
}
5568

5669
data "coder_workspace" "me" {}
@@ -65,17 +78,47 @@ resource "coder_script" "jfrog" {
6578
icon = "/icon/jfrog.svg"
6679
script = templatefile("${path.module}/run.sh", {
6780
JFROG_URL : var.jfrog_url,
68-
JFROG_HOST : replace(var.jfrog_url, "https://", ""),
81+
JFROG_HOST : local.jfrog_host,
6982
ARTIFACTORY_USERNAME : local.username,
7083
ARTIFACTORY_EMAIL : data.coder_workspace.me.owner_email,
7184
ARTIFACTORY_ACCESS_TOKEN : data.coder_external_auth.jfrog.access_token,
85+
CONFIGURE_CODE_SERVER : var.configure_code_server,
7286
REPOSITORY_NPM : lookup(var.package_managers, "npm", ""),
7387
REPOSITORY_GO : lookup(var.package_managers, "go", ""),
7488
REPOSITORY_PYPI : lookup(var.package_managers, "pypi", ""),
89+
REPOSITORY_DOCKER : lookup(var.package_managers, "docker", ""),
7590
})
7691
run_on_start = true
7792
}
7893

94+
resource "coder_env" "jfrog_ide_url" {
95+
count = var.configure_code_server ? 1 : 0
96+
agent_id = var.agent_id
97+
name = "JFROG_IDE_URL"
98+
value = var.jfrog_url
99+
}
100+
101+
resource "coder_env" "jfrog_ide_access_token" {
102+
count = var.configure_code_server ? 1 : 0
103+
agent_id = var.agent_id
104+
name = "JFROG_IDE_ACCESS_TOKEN"
105+
value = data.coder_external_auth.jfrog.access_token
106+
}
107+
108+
resource "coder_env" "jfrog_ide_store_connection" {
109+
count = var.configure_code_server ? 1 : 0
110+
agent_id = var.agent_id
111+
name = "JFROG_IDE_STORE_CONNECTION"
112+
value = true
113+
}
114+
115+
resource "coder_env" "goproxy" {
116+
count = lookup(var.package_managers, "go", "") == "" ? 0 : 1
117+
agent_id = var.agent_id
118+
name = "GOPROXY"
119+
value = "https://${local.username}:${data.coder_external_auth.jfrog.access_token}@${local.jfrog_host}/artifactory/api/go/${lookup(var.package_managers, "go", "")}"
120+
}
121+
79122
output "access_token" {
80123
description = "value of the JFrog access token"
81124
value = data.coder_external_auth.jfrog.access_token

jfrog-oauth/run.sh

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,38 @@ if command -v jf > /dev/null 2>&1; then
77
echo "✅ JFrog CLI is already installed, skipping installation."
88
else
99
echo "📦 Installing JFrog CLI..."
10-
# Install the JFrog CLI.
1110
curl -fL https://install-cli.jfrog.io | sudo sh
1211
sudo chmod 755 /usr/local/bin/jf
1312
fi
1413

1514
# The jf CLI checks $CI when determining whether to use interactive
1615
# flows.
1716
export CI=true
18-
# Authenticate with the JFrog CLI.
19-
jf c rm 0 || true
20-
echo "${ARTIFACTORY_ACCESS_TOKEN}" | jf c add --access-token-stdin --url "${JFROG_URL}" 0
17+
# Authenticate JFrog CLI with Artifactory.
18+
echo "${ARTIFACTORY_ACCESS_TOKEN}" | jf c add --access-token-stdin --url "${JFROG_URL}" --overwrite 0
19+
# Set the configured server as the default.
20+
jf c use 0
2121

22+
# Configure npm to use the Artifactory "npm" repository.
2223
if [ -z "${REPOSITORY_NPM}" ]; then
23-
echo "🤔 REPOSITORY_NPM is not set, skipping npm configuration."
24+
echo "🤔 no npm repository is set, skipping npm configuration."
25+
echo "You can configure an npm repository by providing the a key for 'npm' in the 'package_managers' input."
2426
else
25-
# check if npm is installed and configure it to use the Artifactory "npm" repository.
26-
if command -v npm > /dev/null 2>&1; then
27-
echo "📦 Configuring npm..."
28-
jf npmc --global --repo-resolve "${REPOSITORY_NPM}"
29-
fi
27+
echo "📦 Configuring npm..."
28+
jf npmc --global --repo-resolve "${REPOSITORY_NPM}"
3029
cat << EOF > ~/.npmrc
31-
email = ${ARTIFACTORY_EMAIL}
32-
registry = ${JFROG_URL}/artifactory/api/npm/${REPOSITORY_NPM}
30+
email=${ARTIFACTORY_EMAIL}
31+
registry=${JFROG_URL}/artifactory/api/npm/${REPOSITORY_NPM}
3332
EOF
34-
jf rt curl /api/npm/auth >> ~/.npmrc
33+
echo "//${JFROG_HOST}/artifactory/api/npm/${REPOSITORY_NPM}/:_authToken=${ARTIFACTORY_ACCESS_TOKEN}" >> ~/.npmrc
3534
fi
3635

3736
# Configure the `pip` to use the Artifactory "python" repository.
3837
if [ -z "${REPOSITORY_PYPI}" ]; then
39-
echo "🤔 REPOSITORY_PYPI is not set, skipping pip configuration."
38+
echo "🤔 no pypi repository is set, skipping pip configuration."
39+
echo "You can configure a pypi repository by providing the a key for 'pypi' in the 'package_managers' input."
4040
else
41-
echo "🐍 Configuring pip..."
41+
echo "📦 Configuring pip..."
4242
jf pipc --global --repo-resolve "${REPOSITORY_PYPI}"
4343
mkdir -p ~/.pip
4444
cat << EOF > ~/.pip/pip.conf
@@ -47,12 +47,76 @@ index-url = https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_
4747
EOF
4848
fi
4949

50-
# Set GOPROXY to use the Artifactory "go" repository.
50+
# Configure Artifactory "go" repository.
5151
if [ -z "${REPOSITORY_GO}" ]; then
52-
echo "🤔 REPOSITORY_GO is not set, skipping go configuration."
52+
echo "🤔 no go repository is set, skipping go configuration."
53+
echo "You can configure a go repository by providing the a key for 'go' in the 'package_managers' input."
5354
else
5455
echo "🐹 Configuring go..."
55-
jf go-config --global --repo-resolve "${REPOSITORY_GO}"
56-
export GOPROXY="https://${ARTIFACTORY_USERNAME}:${ARTIFACTORY_ACCESS_TOKEN}@${JFROG_HOST}/artifactory/api/go/${REPOSITORY_GO}"
56+
jf goc --global --repo-resolve "${REPOSITORY_GO}"
5757
fi
5858
echo "🥳 Configuration complete!"
59+
60+
# Configure the JFrog CLI to use the Artifactory "docker" repository.
61+
if [ -z "${REPOSITORY_DOCKER}" ]; then
62+
echo "🤔 no docker repository is set, skipping docker configuration."
63+
echo "You can configure a docker repository by providing the a key for 'docker' in the 'package_managers' input."
64+
else
65+
if command -v docker > /dev/null 2>&1; then
66+
echo "🔑 Configuring 🐳 docker credentials..."
67+
mkdir -p ~/.docker
68+
echo -n "${ARTIFACTORY_ACCESS_TOKEN}" | docker login ${JFROG_HOST} --username ${ARTIFACTORY_USERNAME} --password-stdin
69+
else
70+
echo "🤔 no docker is installed, skipping docker configuration."
71+
fi
72+
fi
73+
74+
# Install the JFrog vscode extension for code-server.
75+
if [ "${CONFIGURE_CODE_SERVER}" == "true" ]; then
76+
while ! [ -x /tmp/code-server/bin/code-server ]; do
77+
counter=0
78+
if [ $counter -eq 60 ]; then
79+
echo "Timed out waiting for /tmp/code-server/bin/code-server to be installed."
80+
exit 1
81+
fi
82+
echo "Waiting for /tmp/code-server/bin/code-server to be installed..."
83+
sleep 1
84+
((counter++))
85+
done
86+
echo "📦 Installing JFrog extension..."
87+
/tmp/code-server/bin/code-server --install-extension jfrog.jfrog-vscode-extension
88+
echo "🥳 JFrog extension installed!"
89+
else
90+
echo "🤔 Skipping JFrog extension installation. Set configure_code_server to true to install the JFrog extension."
91+
fi
92+
93+
# Configure the JFrog CLI completion
94+
echo "📦 Configuring JFrog CLI completion..."
95+
# Get the user's shell
96+
SHELLNAME=$(grep "^$USER" /etc/passwd | awk -F':' '{print $7}' | awk -F'/' '{print $NF}')
97+
# Generate the completion script
98+
jf completion $SHELLNAME --install
99+
# Add the completion script to the user's shell profile
100+
if [ "$SHELLNAME" == "bash" ] && [ -f ~/.bashrc ]; then
101+
if ! grep -q "# jf CLI shell completion" ~/.bashrc; then
102+
echo "" >> ~/.bashrc
103+
echo "# BEGIN: jf CLI shell completion (added by coder module jfrog-oauth)" >> ~/.bashrc
104+
echo 'source "$HOME/.jfrog/jfrog_bash_completion"' >> ~/.bashrc
105+
echo "# END: jf CLI shell completion" >> ~/.bashrc
106+
else
107+
echo "🥳 ~/.bashrc already contains jf CLI shell completion configuration, skipping."
108+
fi
109+
elif [ "$SHELLNAME" == "zsh" ] && [ -f ~/.zshrc ]; then
110+
if ! grep -q "# jf CLI shell completion" ~/.zshrc; then
111+
echo "" >> ~/.zshrc
112+
echo "# BEGIN: jf CLI shell completion (added by coder module jfrog-oauth)" >> ~/.zshrc
113+
echo "autoload -Uz compinit" >> ~/.zshrc
114+
echo "compinit" >> ~/.zshrc
115+
echo 'source "$HOME/.jfrog/jfrog_zsh_completion"' >> ~/.zshrc
116+
echo "# END: jf CLI shell completion" >> ~/.zshrc
117+
else
118+
echo "🥳 ~/.zshrc already contains jf CLI shell completion configuration, skipping."
119+
fi
120+
else
121+
echo "🤔 ~/.bashrc or ~/.zshrc does not exist, skipping jf CLI shell completion configuration."
122+
fi

jfrog-token/README.md

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ Install the JF CLI and authenticate package managers with Artifactory using Arti
1414

1515
```hcl
1616
module "jfrog" {
17-
source = "https://registry.coder.com/modules/jfrog-token"
18-
agent_id = coder_agent.example.id
19-
jfrog_url = "https://XXXX.jfrog.io"
20-
artifactory_access_token = var.artifactory_access_token
21-
package_managers = {
22-
"npm": "npm",
23-
"go": "go",
24-
"pypi": "pypi"
25-
}
17+
source = "https://registry.coder.com/modules/jfrog-token"
18+
agent_id = coder_agent.example.id
19+
jfrog_url = "https://XXXX.jfrog.io"
20+
artifactory_access_token = var.artifactory_access_token
21+
package_managers = {
22+
"npm": "npm",
23+
"go": "go",
24+
"pypi": "pypi"
25+
}
2626
}
2727
```
2828

2929
Get a JFrog access token from your Artifactory instance. The token must be an [admin token](https://registry.terraform.io/providers/jfrog/artifactory/latest/docs#access-token). It is recommended to store the token in a secret terraform variable.
3030

3131
```hcl
3232
variable "artifactory_access_token" {
33-
type = string
34-
sensitive = true
33+
type = string
34+
sensitive = true
3535
}
3636
```
3737

@@ -46,15 +46,15 @@ variable "artifactory_access_token" {
4646

4747
```hcl
4848
module "jfrog" {
49-
source = "https://registry.coder.com/modules/jfrog-token"
50-
agent_id = coder_agent.example.id
51-
jfrog_url = "https://YYYY.jfrog.io"
52-
artifactory_access_token = var.artifactory_access_token # An admin access token
53-
package_managers = {
54-
"npm": "npm-local",
55-
"go": "go-local",
56-
"pypi": "pypi-local"
57-
}
49+
source = "https://registry.coder.com/modules/jfrog-token"
50+
agent_id = coder_agent.example.id
51+
jfrog_url = "https://YYYY.jfrog.io"
52+
artifactory_access_token = var.artifactory_access_token # An admin access token
53+
package_managers = {
54+
"npm": "npm-local",
55+
"go": "go-local",
56+
"pypi": "pypi-local"
57+
}
5858
}
5959
```
6060

@@ -72,6 +72,25 @@ go get github.com/golang/example/hello
7272
pip install requests
7373
```
7474

75+
### Configure code-server with JFrog extension
76+
77+
The [JFrog extension](https://open-vsx.org/extension/JFrog/jfrog-vscode-extension) for VS Code allows you to interact with Artifactory from within the IDE.
78+
79+
```hcl
80+
module "jfrog" {
81+
source = "https://registry.coder.com/modules/jfrog-token"
82+
agent_id = coder_agent.example.id
83+
jfrog_url = "https://XXXX.jfrog.io"
84+
artifactory_access_token = var.artifactory_access_token
85+
configure_code_server = true # Add JFrog extension configuration for code-server
86+
package_managers = {
87+
"npm": "npm",
88+
"go": "go",
89+
"pypi": "pypi"
90+
}
91+
}
92+
```
93+
7594
### Using the access token in other terraform resources
7695

7796
JFrog Access token is also available as a terraform output. You can use it in other terraform resources. For example, you can use it to configure an [Artifactory docker registry](https://jfrog.com/help/r/jfrog-artifactory-documentation/docker-registry) with the [docker terraform provider](https://registry.terraform.io/providers/kreuzwerker/docker/latest/docs).

0 commit comments

Comments
 (0)