Skip to content

Commit 679fa4f

Browse files
committed
Only rewrite repo URL for public repos cloned over SSH
Fixes the following issues in the current implementation: * HTTP git remotes are rewritten to invalid URL https://http://<...> * Private repos with SSH git remotes are converted to HTTPS URLs for the validate-origin check. This requires username/PAT auth on GitHub instead of just checking the remote via SSH
1 parent db2b0a7 commit 679fa4f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

Makefile

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endif
77
# Set this to true if you want to skip any origin validation
88
DISABLE_VALIDATE_ORIGIN ?= false
99
ifeq ($(DISABLE_VALIDATE_ORIGIN),true)
10-
VALIDATE_ORIGIN :=
10+
VALIDATE_ORIGIN :=
1111
else
1212
VALIDATE_ORIGIN := validate-origin
1313
endif
@@ -31,9 +31,8 @@ TARGET_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD)
3131
#default to the branch remote
3232
TARGET_ORIGIN ?= $(shell git config branch.$(TARGET_BRANCH).remote)
3333

34-
# This is to ensure that whether we start with a git@ or https:// URL, we end up with an https:// URL
35-
# This is because we expect to use tokens for repo authentication as opposed to SSH keys
36-
TARGET_REPO=$(shell git ls-remote --get-url --symref $(TARGET_ORIGIN) | sed -e 's/.*URL:[[:space:]]*//' -e 's%^git@%%' -e 's%^https://%%' -e 's%:%/%' -e 's%^%https://%')
34+
# The URL for the configured origin (could be HTTP/HTTPS/SSH)
35+
TARGET_REPO_RAW := $(shell git ls-remote --get-url --symref $(TARGET_ORIGIN))
3736

3837
UUID_FILE ?= ~/.config/validated-patterns/pattern-uuid
3938
UUID_HELM_OPTS ?=
@@ -50,13 +49,22 @@ TOKEN_SECRET ?=
5049
TOKEN_NAMESPACE ?=
5150

5251
ifeq ($(TOKEN_SECRET),)
53-
HELM_OPTS=-f values-global.yaml --set main.git.repoURL="$(TARGET_REPO)" --set main.git.revision=$(TARGET_BRANCH) $(TARGET_SITE_OPT) $(UUID_HELM_OPTS) $(EXTRA_HELM_OPTS)
52+
# SSH agents are not created for public repos (repos with no secret token) by the patterns operator so we convert to HTTPS
53+
TARGET_REPO := $(shell echo "$(TARGET_REPO_RAW)" | sed 's/^git@\(.*\):\(.*\)/https:\/\/\1\/\2/')
54+
SECRET_OPTS :=
5455
else
55-
# When we are working with a private repository we do not escape the git URL as it might be using an ssh secret which does not use https://
56-
TARGET_CLEAN_REPO=$(shell git ls-remote --get-url --symref $(TARGET_ORIGIN))
57-
HELM_OPTS=-f values-global.yaml --set main.tokenSecret=$(TOKEN_SECRET) --set main.tokenSecretNamespace=$(TOKEN_NAMESPACE) --set main.git.repoURL="$(TARGET_CLEAN_REPO)" --set main.git.revision=$(TARGET_BRANCH) $(TARGET_SITE_OPT) $(UUID_HELM_OPTS) $(EXTRA_HELM_OPTS)
56+
TARGET_REPO := $(TARGET_REPO_RAW)
57+
SECRET_OPTS := --set main.tokenSecret=$(TOKEN_SECRET) --set main.tokenSecretNamespace=$(TOKEN_NAMESPACE)
5858
endif
5959

60+
HELM_OPTS := -f values-global.yaml \
61+
--set main.git.repoURL="$(TARGET_REPO)" \
62+
--set main.git.revision=$(TARGET_BRANCH) \
63+
$(SECRET_OPTS) \
64+
$(TARGET_SITE_OPT) \
65+
$(UUID_HELM_OPTS) \
66+
$(EXTRA_HELM_OPTS)
67+
6068
# Helm does the right thing and fetches all the tags and detects the newest one
6169
PATTERN_INSTALL_CHART ?= oci://quay.io/hybridcloudpatterns/pattern-install
6270

@@ -138,8 +146,6 @@ token-kubeconfig: ## Create a local ~/.kube/config with password (not usually ne
138146

139147
##@ Validation Tasks
140148

141-
# We only check the remote ssh git branch's existance if we're not running inside a container
142-
# as getting ssh auth working inside a container seems a bit brittle
143149
# If the main repoUpstreamURL field is set, then we need to check against
144150
# that and not target_repo
145151
.PHONY: validate-origin

0 commit comments

Comments
 (0)