Use HTTPS directly for GitLab release creation

master
Simon 4 weeks ago
parent 33ca2747fc
commit 5c8f946892

@ -11,13 +11,23 @@ fi
: "${CI_PROJECT_ID:?CI_PROJECT_ID is required}"
: "${CI_JOB_TOKEN:?CI_JOB_TOKEN is required}"
# GitLab may advertise HTTP behind a TLS proxy; never send the token before a redirect.
case "$CI_API_V4_URL" in
http://*) api_url="https://${CI_API_V4_URL#http://}" ;;
https://*) api_url=$CI_API_V4_URL ;;
*)
echo "CI_API_V4_URL must use HTTP or HTTPS" >&2
exit 2
;;
esac
tag_name=$1
release_name=$2
description=$3
response_file=$(mktemp)
trap 'rm -f "$response_file"' EXIT HUP INT TERM
status=$(curl --silent --show-error \
status=$(curl --silent --show-error --proto '=https' \
--output "$response_file" \
--write-out '%{http_code}' \
--request POST \
@ -25,7 +35,7 @@ status=$(curl --silent --show-error \
--data-urlencode "tag_name=$tag_name" \
--data-urlencode "name=$release_name" \
--data-urlencode "description=$description" \
"$CI_API_V4_URL/projects/$CI_PROJECT_ID/releases")
"$api_url/projects/$CI_PROJECT_ID/releases")
case "$status" in
201)

@ -1,3 +1,7 @@
# Unreleased Changes
No additional changes. See the [v1.6.4 release notes](release-notes-v1.6.4.md).
- The CI release helper upgrades GitLab's advertised HTTP API URL to HTTPS
before sending its job token. Redirects remain errors and are not followed.
This fixes release-page creation behind the HTTPS redirect without changing
runtime containers. See the [v1.6.4 release notes](release-notes-v1.6.4.md)
for the current runtime release.

@ -17,6 +17,7 @@ class GitLabReleaseHelperTests(unittest.TestCase):
fake_curl = self.bin_dir / "curl"
fake_curl.write_text(
"""#!/bin/sh
printf '%s\\n' \"$@\" > \"$FAKE_CURL_ARGUMENTS\"
output=
while [ \"$#\" -gt 0 ]; do
case \"$1\" in
@ -39,15 +40,16 @@ printf '%s' \"${FAKE_CURL_STATUS:-201}\"
def tearDown(self):
self.temporary.cleanup()
def run_helper(self, status, body=""):
def run_helper(self, status, body="", api_url="https://gitlab.example/api/v4"):
environment = os.environ.copy()
environment.update(
{
"CI_API_V4_URL": "https://gitlab.example/api/v4",
"CI_API_V4_URL": api_url,
"CI_PROJECT_ID": "123",
"CI_JOB_TOKEN": "not-logged",
"FAKE_CURL_STATUS": str(status),
"FAKE_CURL_BODY": body,
"FAKE_CURL_ARGUMENTS": str(self.bin_dir / "curl-arguments"),
"PATH": "{}:{}".format(self.bin_dir, environment["PATH"]),
}
)
@ -77,6 +79,33 @@ printf '%s' \"${FAKE_CURL_STATUS:-201}\"
self.assertIn('"message":"failure"', result.stderr)
self.assertNotIn("not-logged", result.stderr)
def test_http_api_url_uses_https_without_following_redirects(self):
result = self.run_helper(201, api_url="http://gitlab.example/gitlab/api/v4")
self.assertEqual(result.returncode, 0, result.stderr)
arguments = (self.bin_dir / "curl-arguments").read_text().splitlines()
self.assertEqual(arguments[-1], "https://gitlab.example/gitlab/api/v4/projects/123/releases")
self.assertEqual(arguments[arguments.index("--proto") + 1], "=https")
self.assertNotIn("--location", arguments)
self.assertNotIn("--location-trusted", arguments)
self.assertNotIn("-L", arguments)
self.assertNotIn("not-logged", result.stdout + result.stderr)
def test_https_api_url_preserves_explicit_port_and_path(self):
result = self.run_helper(201, api_url="https://gitlab.example:8443/gitlab/api/v4")
self.assertEqual(result.returncode, 0, result.stderr)
arguments = (self.bin_dir / "curl-arguments").read_text().splitlines()
self.assertEqual(arguments[-1], "https://gitlab.example:8443/gitlab/api/v4/projects/123/releases")
def test_other_schemes_are_rejected_before_curl(self):
result = self.run_helper(201, api_url="file:///tmp/api/v4")
self.assertNotEqual(result.returncode, 0)
self.assertFalse((self.bin_dir / "curl-arguments").exists())
def test_https_redirect_remains_an_error(self):
result = self.run_helper(307, "Temporary Redirect")
self.assertNotEqual(result.returncode, 0)
self.assertIn("HTTP 307", result.stderr)
def test_pipeline_does_not_use_runner_release_keyword(self):
pipeline = PIPELINE.read_text(encoding="utf-8")
self.assertNotIn("\n release:\n", pipeline)

Loading…
Cancel
Save

Powered by TurnKey Linux.