Skip to content

Commit d1022a2

Browse files
Hyperkid123claude
andcommitted
feat(auth): migrate git auth from SSH to HTTPS + token
RHCLOUD-47113 Replace SSH key authentication with HTTPS credential helpers for both GitHub (gh auth setup-git) and GitLab (custom credential script). Parameterize all identity values (username, email) as env vars with no hardcoded defaults. Remove openssh-clients from container image. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent eae08e8 commit d1022a2

7 files changed

Lines changed: 138 additions & 197 deletions

File tree

Dockerfile

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ FROM registry.access.redhat.com/ubi9/ubi:latest
1010
RUN dnf install -y --nodocs --allowerasing \
1111
python3.12 python3.12-pip python3.12-devel \
1212
git \
13-
openssh-clients \
1413
curl \
1514
jq \
1615
socat \
@@ -158,19 +157,9 @@ RUN mkdir -p /home/botuser/.config/containers /home/botuser/.local/share/contain
158157
&& echo -e '[registries.search]\nregistries = ["registry.access.redhat.com", "quay.io", "docker.io"]' \
159158
> /home/botuser/.config/containers/registries.conf
160159

161-
# SSH directory — config is generated at runtime by entrypoint.sh
162-
RUN mkdir -p /home/botuser/.ssh && chmod 700 /home/botuser/.ssh
163-
ENV GIT_SSH_COMMAND="ssh -F /home/botuser/.ssh/config"
164160

165-
# Pre-add known host keys so first connection doesn't warn
166-
RUN ssh-keyscan -t ed25519,rsa,ecdsa github.com >> /home/botuser/.ssh/known_hosts 2>/dev/null \
167-
&& ssh-keyscan -t ed25519,rsa,ecdsa gitlab.cee.redhat.com >> /home/botuser/.ssh/known_hosts 2>/dev/null; \
168-
chmod 600 /home/botuser/.ssh/known_hosts
169-
170-
# Git config
171-
RUN git config --global user.name "platex-rehor-bot" \
172-
&& git config --global user.email "platform-experience-services@redhat.com" \
173-
&& git config --global http.https://gitlab.cee.redhat.com.sslVerify false \
161+
# Git config (identity is set at runtime via GIT_USER_NAME/GIT_USER_EMAIL env vars)
162+
RUN git config --global http.https://gitlab.cee.redhat.com.sslVerify false \
174163
&& git config --global gpg.format openpgp \
175164
&& git config --global commit.gpgsign true
176165

bot/config.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ def _resolve_env_vars(obj):
8383
"GH_TOKEN",
8484
"GITHUB_TOKEN",
8585
"GITLAB_TOKEN",
86-
"SSH_PRIVATE_KEY_B64",
8786
"GPG_PRIVATE_KEY_B64",
8887
"GOOGLE_SA_KEY_B64",
89-
"BOT_SSH_KEY",
90-
"GITLAB_SSH_KEY",
91-
"GITLAB_SSH_KEY_B64",
92-
"GITLAB_SSH_PASSPHRASE",
9388
"GPG_SIGNING_KEY",
9489
"SSO_USERNAME",
9590
"SSO_PASSWORD",

bot/run.py

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,6 @@ def _resolve_path(p: str) -> str:
2929
return str(path.resolve())
3030

3131

32-
def setup_ssh(script_dir: Path) -> None:
33-
"""Generate SSH config from env vars and set GIT_SSH_COMMAND.
34-
35-
Reads BOT_SSH_KEY (for github.com) and GITLAB_SSH_KEY (for gitlab)
36-
from the environment. Skips if neither is set.
37-
"""
38-
bot_key = os.environ.get("BOT_SSH_KEY")
39-
gitlab_key = os.environ.get("GITLAB_SSH_KEY")
40-
41-
if not bot_key and not gitlab_key:
42-
# No keys configured — check if there's an existing SSH config
43-
# in the project dir (e.g. committed to the repo) and use it.
44-
fallback = ssh_dir / "config" if (ssh_dir := script_dir / ".ssh").exists() else None
45-
if fallback and fallback.exists():
46-
os.environ["GIT_SSH_COMMAND"] = f"ssh -F {fallback}"
47-
return
48-
49-
ssh_dir = script_dir / ".ssh"
50-
ssh_dir.mkdir(exist_ok=True)
51-
config_path = ssh_dir / "config"
52-
53-
lines = ["# Auto-generated by bot/run.py — do not edit manually"]
54-
55-
if bot_key:
56-
lines += [
57-
"",
58-
"Host github.com-bot",
59-
" HostName github.com",
60-
" User git",
61-
f" IdentityFile {_resolve_path(bot_key)}",
62-
" IdentitiesOnly yes",
63-
" StrictHostKeyChecking accept-new",
64-
]
65-
66-
if gitlab_key:
67-
lines += [
68-
"",
69-
"Host gitlab.cee.redhat.com",
70-
f" IdentityFile {_resolve_path(gitlab_key)}",
71-
" IdentitiesOnly yes",
72-
" StrictHostKeyChecking accept-new",
73-
]
74-
75-
config_path.write_text("\n".join(lines) + "\n")
76-
config_path.chmod(0o600)
77-
os.environ["GIT_SSH_COMMAND"] = f"ssh -F {config_path}"
78-
7932

8033
def setup_git(script_dir: Path) -> None:
8134
"""Generate a .gitconfig with identity and optional GPG signing.
@@ -142,8 +95,7 @@ def main() -> None:
14295
if gac and not os.path.isabs(gac):
14396
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = str(SCRIPT_DIR / gac)
14497

145-
# Set up SSH and git identity from env vars (no-op if not configured)
146-
setup_ssh(SCRIPT_DIR)
98+
# Set up git identity from env vars (no-op if not configured)
14799
setup_git(SCRIPT_DIR)
148100

149101
parser = argparse.ArgumentParser(description="Dev bot agent loop")

deploy/template.yaml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ parameters:
2121
value: hcc-ai-bot
2222
- name: BOT_REPLICAS
2323
value: "1"
24+
- name: GIT_USER_NAME
25+
required: true
26+
- name: GIT_USER_EMAIL
27+
required: true
28+
- name: GH_USERNAME
29+
required: true
30+
- name: GITLAB_USERNAME
31+
required: true
2432
objects:
2533

2634
# ============================================================================
@@ -257,7 +265,16 @@ objects:
257265
value: "60"
258266
- name: GOOGLE_APPLICATION_CREDENTIALS
259267
value: /home/botuser/sa-key.json
260-
# Proxy — PROXY_HOST for SSH config, HTTP(S)_PROXY for HTTP clients
268+
# Git identity
269+
- name: GIT_USER_NAME
270+
value: ${GIT_USER_NAME}
271+
- name: GIT_USER_EMAIL
272+
value: ${GIT_USER_EMAIL}
273+
- name: GH_USERNAME
274+
value: ${GH_USERNAME}
275+
- name: GITLAB_USERNAME
276+
value: ${GITLAB_USERNAME}
277+
# Proxy — HTTP(S)_PROXY for git and HTTP clients
261278
- name: PROXY_HOST
262279
value: devbot-proxy
263280
- name: HTTP_PROXY
@@ -273,11 +290,6 @@ objects:
273290
- name: no_proxy
274291
value: devbot-memory-server,localhost,127.0.0.1
275292
# Secrets from Vault (devbot-secrets)
276-
- name: SSH_PRIVATE_KEY_B64
277-
valueFrom:
278-
secretKeyRef:
279-
name: devbot-secrets
280-
key: gh-bot-private-key
281293
- name: GPG_PRIVATE_KEY_B64
282294
valueFrom:
283295
secretKeyRef:
@@ -288,6 +300,11 @@ objects:
288300
secretKeyRef:
289301
name: devbot-secrets
290302
key: gh-bot-cli-token
303+
- name: GITLAB_TOKEN
304+
valueFrom:
305+
secretKeyRef:
306+
name: devbot-secrets
307+
key: gl-bot-cli-token
291308
- name: GOOGLE_SA_KEY_B64
292309
valueFrom:
293310
secretKeyRef:

docker-compose.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ services:
6565
env_file:
6666
- .env
6767
environment:
68-
- SSH_PRIVATE_KEY_B64
69-
- GITLAB_SSH_KEY_B64
7068
- GPG_PRIVATE_KEY_B64
7169
- GH_TOKEN
7270
- GITLAB_TOKEN
71+
- GH_USERNAME
72+
- GITLAB_USERNAME
73+
- GIT_USER_NAME
74+
- GIT_USER_EMAIL
7375
- GOOGLE_SA_KEY_B64
7476
- SSO_USERNAME
7577
- SSO_PASSWORD

entrypoint.sh

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,19 @@ decode_or_raw() {
2727
esac
2828
}
2929

30-
# Decode SSH keys (separate keys for GitHub and GitLab)
31-
if [ -n "${SSH_PRIVATE_KEY_B64:-}" ]; then
32-
decode_or_raw "$SSH_PRIVATE_KEY_B64" > ~/.ssh/id_gh
33-
chmod 600 ~/.ssh/id_gh
34-
unset SSH_PRIVATE_KEY_B64
35-
fi
36-
37-
if [ -n "${GITLAB_SSH_KEY_B64:-}" ]; then
38-
decode_or_raw "$GITLAB_SSH_KEY_B64" > ~/.ssh/id_gl
39-
chmod 600 ~/.ssh/id_gl
40-
unset GITLAB_SSH_KEY_B64
30+
# Git credential helpers for HTTPS auth (replaces SSH keys)
31+
# GitHub: gh CLI acts as credential helper
32+
# GitLab: custom helper script injects token
33+
if [ -n "${GITLAB_TOKEN:-}" ]; then
34+
cat > /home/botuser/.git-credential-gitlab <<CREDEOF
35+
#!/bin/bash
36+
echo "username=${GITLAB_USERNAME}"
37+
echo "password=${GITLAB_TOKEN}"
38+
CREDEOF
39+
chmod 700 /home/botuser/.git-credential-gitlab
40+
git config --global credential.https://gitlab.cee.redhat.com.helper "/home/botuser/.git-credential-gitlab"
4141
fi
4242

43-
# Generate SSH config — PROXY_HOST defaults to "proxy" (matches docker-compose service name)
44-
PROXY_HOST="${PROXY_HOST:-proxy}"
45-
cat > ~/.ssh/config <<SSHEOF
46-
Host github.com
47-
HostName github.com
48-
User git
49-
IdentityFile /home/botuser/.ssh/id_gh
50-
IdentitiesOnly yes
51-
StrictHostKeyChecking accept-new
52-
ProxyCommand socat - PROXY:${PROXY_HOST}:%h:%p,proxyport=3128
53-
54-
Host gitlab.cee.redhat.com
55-
IdentityFile /home/botuser/.ssh/id_gl
56-
IdentitiesOnly yes
57-
StrictHostKeyChecking accept-new
58-
ProxyCommand socat - PROXY:${PROXY_HOST}:%h:%p,proxyport=3128
59-
SSHEOF
60-
chmod 600 ~/.ssh/config
61-
6243
# Write SSO credentials file for stage auth (chrome-devtools)
6344
if [ -n "${SSO_USERNAME:-}" ] && [ -n "${SSO_PASSWORD:-}" ]; then
6445
cat > /home/botuser/app/.credentials <<EOF
@@ -68,6 +49,10 @@ EOF
6849
unset SSO_USERNAME SSO_PASSWORD
6950
fi
7051

52+
# Git identity from env vars
53+
git config --global user.name "${GIT_USER_NAME}"
54+
git config --global user.email "${GIT_USER_EMAIL}"
55+
7156
# Import GPG key for commit signing
7257
if [ -n "${GPG_PRIVATE_KEY_B64:-}" ]; then
7358
gpg --batch --import <(decode_or_raw "$GPG_PRIVATE_KEY_B64") 2>/dev/null
@@ -83,14 +68,15 @@ fi
8368
# Point MCP config to the memory server
8469
sed -i "s|http://localhost:8080/mcp|${BOT_MEMORY_URL}|" .mcp.json
8570

86-
# Configure gh CLI auth
71+
# Configure gh CLI auth (HTTPS + credential helper for git)
8772
mkdir -p ~/.config/gh
8873
cat > ~/.config/gh/hosts.yml <<EOF
8974
github.com:
9075
oauth_token: ${GH_TOKEN}
91-
user: platex-rehor-bot
92-
git_protocol: ssh
76+
user: ${GH_USERNAME}
77+
git_protocol: https
9378
EOF
79+
gh auth setup-git 2>/dev/null || true
9480

9581
# Remove token from env — gh uses the config file from now on
9682
unset GH_TOKEN
@@ -99,7 +85,7 @@ unset GH_TOKEN
9985
if [ -n "${GITLAB_TOKEN:-}" ]; then
10086
mkdir -p ~/.config/glab-cli
10187
cat > ~/.config/glab-cli/config.yml <<EOF
102-
git_protocol: ssh
88+
git_protocol: https
10389
check_update: false
10490
no_prompt: true
10591
host: gitlab.cee.redhat.com
@@ -108,7 +94,7 @@ hosts:
10894
token: ${GITLAB_TOKEN}
10995
api_protocol: https
11096
api_host: gitlab.cee.redhat.com
111-
git_protocol: ssh
97+
git_protocol: https
11298
skip_tls_verify: true
11399
EOF
114100
chmod 600 ~/.config/glab-cli/config.yml
@@ -162,5 +148,5 @@ CHROME_BIN=$(find "$PLAYWRIGHT_BROWSERS_PATH" -name chrome -type f | head -1)
162148
# Wait for Chromium to be ready
163149
until curl -s http://127.0.0.1:9222/json/version > /dev/null 2>&1; do sleep 1; done
164150

165-
echo "Keys loaded. Chromium started. Starting bot with label: ${BOT_LABEL}"
151+
echo "Credentials configured. Chromium started. Starting bot with label: ${BOT_LABEL}"
166152
exec uv run dev-bot --label "$BOT_LABEL"

0 commit comments

Comments
 (0)