Skip to content

Commit e62d73d

Browse files
authored
fix(x2a): git using authN credentials better (#2745)
1 parent 29571f6 commit e62d73d

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

workspaces/x2a/plugins/x2a-backend/templates/x2a-job-script.sh

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ run_x2a() {
109109
ERROR_MESSAGE=""
110110
}
111111

112+
# Authenticated git wrappers.
113+
# Use url.<auth>.insteadOf to inject the token at the transport layer only.
114+
# The -c flag is transient (applies only to that git invocation), so the
115+
# token never appears in remote URLs, git config, or generated files like
116+
# Policyfile.lock.json. This works across GitHub, GitLab, and Bitbucket
117+
# because git natively handles the https://token@host URL format.
118+
git_source_repo() {
119+
local auth_url="https://${SOURCE_REPO_TOKEN}@${SOURCE_REPO_URL#https://}"
120+
git -c "url.${auth_url}.insteadOf=${SOURCE_REPO_URL}" "$@"
121+
}
122+
123+
git_target_repo() {
124+
local auth_url="https://${TARGET_REPO_TOKEN}@${TARGET_REPO_URL#https://}"
125+
git -c "url.${auth_url}.insteadOf=${TARGET_REPO_URL}" "$@"
126+
}
127+
112128
# Cleanup trap: fires on every exit (success or failure).
113129
# Guarantees exactly one report_result call regardless of how the script ends.
114130
cleanup() {
@@ -132,9 +148,9 @@ Job: ${JOB_ID}
132148
133149
Co-Authored-By: ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}>
134150
" || true
135-
git pull --rebase origin "${TARGET_REPO_BRANCH}" 2>/dev/null || true
151+
git_target_repo pull --rebase origin "${TARGET_REPO_BRANCH}" 2>/dev/null || true
136152
COMMIT_ID=$(git rev-parse HEAD 2>/dev/null || echo "")
137-
if ! git push origin "${TARGET_REPO_BRANCH}"; then
153+
if ! git_target_repo push origin "${TARGET_REPO_BRANCH}"; then
138154
PUSH_FAILED="Failed to push to ${TARGET_REPO_URL} branch ${TARGET_REPO_BRANCH}"
139155
echo "ERROR: ${PUSH_FAILED}"
140156
fi
@@ -154,25 +170,17 @@ Co-Authored-By: ${GIT_AUTHOR_NAME} <${GIT_AUTHOR_EMAIL}>
154170
git_clone_repos() {
155171
echo "=== Cloning source repository ==="
156172
ERROR_MESSAGE="Failed to clone source repository from ${SOURCE_REPO_URL}"
157-
git clone --depth=1 --single-branch --branch="${SOURCE_REPO_BRANCH}" \
158-
"https://${SOURCE_REPO_TOKEN}@${SOURCE_REPO_URL#https://}" \
159-
/workspace/source
160-
161-
# Strip the token from the git remote URL so that tools like Chef's
162-
# CookbookProfiler::Git (which reads `git config --get remote.origin.url`)
163-
# never see the credential. This prevents tokens from leaking into
164-
# generated files such as Policyfile.lock.json.
165-
git -C /workspace/source remote set-url origin "${SOURCE_REPO_URL}"
173+
git_source_repo clone --depth=1 --single-branch \
174+
--branch="${SOURCE_REPO_BRANCH}" "${SOURCE_REPO_URL}" /workspace/source
166175

167176
echo "=== Cloning target repository ==="
168-
local target_auth_url="https://${TARGET_REPO_TOKEN}@${TARGET_REPO_URL#https://}"
169-
170177
ERROR_MESSAGE="Failed to clone target repository from ${TARGET_REPO_URL}"
171-
if git clone --depth=1 --single-branch --branch="${TARGET_REPO_BRANCH}" \
172-
"${target_auth_url}" /workspace/target 2>/dev/null; then
178+
if git_target_repo clone --depth=1 --single-branch \
179+
--branch="${TARGET_REPO_BRANCH}" "${TARGET_REPO_URL}" /workspace/target 2>/dev/null; then
173180
# Repo and branch exist — cloned successfully
174181
:
175-
elif git clone --depth=1 "${target_auth_url}" /workspace/target 2>/dev/null; then
182+
elif git_target_repo clone --depth=1 \
183+
"${TARGET_REPO_URL}" /workspace/target 2>/dev/null; then
176184
# Repo exists but branch doesn't — create target branch locally
177185
echo "Branch '${TARGET_REPO_BRANCH}' not found on remote, creating it"
178186
cd /workspace/target
@@ -184,7 +192,7 @@ git_clone_repos() {
184192
cd /workspace/target
185193
git init
186194
git checkout -b "${TARGET_REPO_BRANCH}"
187-
git remote add origin "${target_auth_url}"
195+
git remote add origin "${TARGET_REPO_URL}"
188196
fi
189197

190198
ERROR_MESSAGE=""

0 commit comments

Comments
 (0)