Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ inherited by every process the boot and control scripts start, the board include
```sh
export GH_TOKEN=... # GitHub, as above
export GITLAB_TOKEN=... # GitLab, as above
export GITLAB_HOST=gitlab.com # the same server as in mini-runner.env; the control scripts do not pass it on
export GITLAB_HOST=gitlab.com # the token's server, the only one check-repo.sh sends it to
exec sh "${MINIRUNNER_HOME:-$HOME/mini-runner}/scripts/check-repo.sh" "$@"
```

Expand Down
57 changes: 57 additions & 0 deletions board/tests/test_check_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
[ -f "$f" ] && cat "$f" || exit 1
"""
FAKE_PROOT = "#!/bin/sh\nsleep 300\n"
# fake curl: records its arguments, one call per line, and fails like curl -f on an HTTP error
FAKE_CURL = """#!/bin/sh
printf '%s\\n' "$*" >>"$CURL_LOG"
exit 22
"""
FAKE_TOKEN = "fake-gitlab-token"


class Base(unittest.TestCase):
Expand All @@ -36,6 +42,7 @@ def setUp(self):
(self.bin / name).chmod(0o755)
self.env = {**os.environ, "FIXTURES": str(self.fix), "MINIRUNNER_GH": str(self.bin / "gh"),
"MINIRUNNER_GLAB": str(self.bin / "glab"), "GH_TOKEN": "", "GITLAB_TOKEN": ""}
self.env.pop("GITLAB_HOST", None)

def put(self, path, body):
name = path
Expand All @@ -57,6 +64,17 @@ def gitlab(self, project, visibility, parent=True, runners=()):
def check(self, *targets, env=None):
return subprocess.run(["sh", str(CHECK), *targets], capture_output=True, text=True, env=env or self.env)

def token_without_glab(self):
"""GITLAB_TOKEN set, `glab` absent and a recording fake curl first on PATH: the curl path."""
(self.bin / "curl").write_text(FAKE_CURL)
(self.bin / "curl").chmod(0o755)
self.curl_log = self.tmp / "curl.log"
self.env.update(GITLAB_TOKEN=FAKE_TOKEN, MINIRUNNER_GLAB=str(self.bin / "absent"),
CURL_LOG=str(self.curl_log), PATH=f"{self.bin}:{os.environ.get('PATH', '/usr/bin:/bin')}")

def curl_calls(self):
return self.curl_log.read_text().splitlines() if self.curl_log.exists() else []


class CheckRepo(Base):
def test_private_github_repo_passes(self):
Expand Down Expand Up @@ -116,6 +134,24 @@ def test_gitlab_public_passes_with_the_member_gate_and_prints_the_stricter_setti
def test_gitlab_unreadable_is_unverified(self):
self.assertEqual(self.check("gitlab:acme/none").returncode, 3)

def test_gitlab_token_without_a_named_host_is_never_sent(self):
self.token_without_glab()
r = self.check("gitlab:acme/api")
self.assertEqual(r.returncode, 3, r.stdout)
self.assertIn("UNVERIFIED gitlab:acme/api", r.stdout)
self.assertIn("GITLAB_HOST", r.stdout)
self.assertEqual(self.curl_calls(), [])

def test_gitlab_token_goes_to_the_named_host_only(self):
self.token_without_glab()
for host in ("gitlab.example.com", "gitlab.com"):
self.curl_log.unlink(missing_ok=True)
self.check("gitlab:acme/api", env={**self.env, "GITLAB_HOST": host})
calls = self.curl_calls()
self.assertTrue(calls, host)
for call in calls:
self.assertIn(f"https://{host}/api/v4/projects/acme%2Fapi", call)


@unittest.skipUnless(shutil.which("setsid"), "needs setsid")
class StartRefusal(Base):
Expand Down Expand Up @@ -175,6 +211,27 @@ def test_gitlab_runner_checks_its_projects(self):
self.assertNotEqual(r.returncode, 0)
self.assertIn("never verified", r.stdout)

def test_gitlab_host_from_the_env_file_reaches_the_check(self):
# set in mini-runner.env without export, as the example does
(self.tmp / "mini-runner.env").write_text('GITLAB_RUNNER_PROJECTS="acme/api"\nGITLAB_HOST=gitlab.example.com\n')
self.token_without_glab()
r = self.ctl("runner-ctl.sh", "start")
self.assertNotEqual(r.returncode, 0, r.stdout) # the fake curl reads nothing: never verified
calls = self.curl_calls()
self.assertTrue(calls, r.stdout)
for call in calls:
self.assertIn("https://gitlab.example.com/api/v4/", call)
self.assertNotIn("https://gitlab.com/", call)

def test_glab_is_asked_about_the_host_from_the_env_file(self):
(self.tmp / "mini-runner.env").write_text('GITLAB_RUNNER_PROJECTS="acme/api"\nGITLAB_HOST=gitlab.example.com\n')
hosts = self.tmp / "glab-hosts"
(self.bin / "glab").write_text(f'#!/bin/sh\nprintf "%s\\n" "$3" >>"{hosts}"\n' + FAKE_API.split("\n", 1)[1])
self.gitlab("acme/api", "private")
r = self.ctl("runner-ctl.sh", "start")
self.assertEqual(r.returncode, 0, r.stdout + r.stderr)
self.assertEqual(set(hosts.read_text().split()), {"gitlab.example.com"})


if __name__ == "__main__":
unittest.main()
3 changes: 2 additions & 1 deletion docs/public-repos.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ GITLAB_HOST=gitlab.example.com sh scripts/check-repo.sh gitlab:GROUP/PROJECT
It only reads. It needs `gh` or `GH_TOKEN` with repository admin read for GitHub, and `glab` or
`GITLAB_TOKEN` with Maintainer read for GitLab. On the device, give it the narrow tokens in the README,
[Device token for the start check](../README.md#device-token-for-the-start-check), never a `gh` or `glab`
login.
login. `GITLAB_TOKEN` is sent only to the host named in `GITLAB_HOST`, so set it for gitlab.com
too (`GITLAB_HOST=gitlab.com`).

Name the repositories in `mini-runner.env` (`GH_RUNNER_REPOS="id=owner/repo ..."`,
`GITLAB_RUNNER_PROJECTS="group/project ..."`) and the control scripts run the check on every start. An
Expand Down
10 changes: 8 additions & 2 deletions scripts/check-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#
# Reads only: it never changes a setting. GitHub needs repository admin read, through `gh` (MINIRUNNER_GH
# overrides the binary) or GH_TOKEN with curl. GitLab needs Maintainer read, through `glab`
# (MINIRUNNER_GLAB) or GITLAB_TOKEN with curl.
# (MINIRUNNER_GLAB) or GITLAB_TOKEN with curl. GITLAB_TOKEN is sent only to a host named in GITLAB_HOST,
# gitlab.com included: with the token and no GITLAB_HOST, a GitLab target is UNVERIFIED and nothing is sent.
# Exit: 0 every repository is safe, 1 at least one is unsafe, 2 usage, 3 could not verify.
set -u
GH=${MINIRUNNER_GH:-gh}
Expand All @@ -33,12 +34,13 @@ api() {
}

GLAB=${MINIRUNNER_GLAB:-glab}
GITLAB_HOST_GIVEN=${GITLAB_HOST:-}
GITLAB_HOST=${GITLAB_HOST:-gitlab.com}

gl_api() {
if command -v "$GLAB" >/dev/null 2>&1; then
"$GLAB" api --hostname "$GITLAB_HOST" "$1" 2>/dev/null
elif [ -n "${GITLAB_TOKEN:-}" ] && command -v curl >/dev/null 2>&1; then
elif [ -n "${GITLAB_TOKEN:-}" ] && [ -n "$GITLAB_HOST_GIVEN" ] && command -v curl >/dev/null 2>&1; then
curl -fsS -H "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://$GITLAB_HOST/api/v4/$1" 2>/dev/null
else
return 3
Expand All @@ -55,6 +57,10 @@ except Exception: print("unknown")'; }
check_gitlab() {
project=$1
id=$(printf %s "$project" | sed 's|/|%2F|g')
if ! command -v "$GLAB" >/dev/null 2>&1 && [ -n "${GITLAB_TOKEN:-}" ] && [ -z "$GITLAB_HOST_GIVEN" ]; then
echo "UNVERIFIED gitlab:$project: GITLAB_TOKEN is sent only to the host named in GITLAB_HOST, and it is not set (for gitlab.com, set GITLAB_HOST=gitlab.com)"
worse 3; return
fi
body=$(gl_api "projects/$id")
visibility=$(printf %s "$body" | field visibility)
if [ -z "$visibility" ]; then
Expand Down
3 changes: 2 additions & 1 deletion scripts/mini-runner.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ GITLAB_RUNNER_WORKDIR=/root
# The projects the GitLab runner serves, as "group/project" paths separated by spaces. With this set,
# start checks each one first (scripts/check-repo.sh gitlab:group/project). GITLAB_HOST names the server
# (default gitlab.com). The check needs GITLAB_TOKEN with Maintainer read: see MINIRUNNER_CHECK_REPO below.
# The token is sent only to the host named in GITLAB_HOST, so keep it set, gitlab.com included.
GITLAB_RUNNER_PROJECTS=""
GITLAB_HOST=gitlab.com

Expand All @@ -34,7 +35,7 @@ GH_RUNNER_REPOS=""
# device for this (README, Device token for the start check).
# export GH_TOKEN=... # fine-grained, GH_RUNNER_REPOS only, Administration: read-only
# export GITLAB_TOKEN=... # project access token, Maintainer role, scope read_api
# export GITLAB_HOST=gitlab.com # same as above; not passed on to the check otherwise
# export GITLAB_HOST=gitlab.com # the token's server, the only one it is sent to
# exec sh "${MINIRUNNER_HOME:-$HOME/mini-runner}/scripts/check-repo.sh" "$@"
#MINIRUNNER_CHECK_REPO="$HOME/.config/mini-runner/check-repo"

Expand Down
2 changes: 2 additions & 0 deletions scripts/runner-ctl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set -u
MINIRUNNER_HOME=${MINIRUNNER_HOME:-$HOME/mini-runner}
# shellcheck disable=SC1091
[ -f "$MINIRUNNER_HOME/mini-runner.env" ] && . "$MINIRUNNER_HOME/mini-runner.env"
# a GITLAB_HOST set there without export must still reach check-repo.sh, which otherwise asks gitlab.com
[ -n "${GITLAB_HOST:-}" ] && export GITLAB_HOST
RUN=${MINIRUNNER_RUN:-$MINIRUNNER_HOME/run}
PROOT_DISTRO=${PROOT_DISTRO:-ubuntu}
GITLAB_RUNNER_BIN=${GITLAB_RUNNER_BIN:-/usr/local/bin/gitlab-runner}
Expand Down
Loading