Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/git/vole.git

Write over SSH with a registered key:

git clone ssh://git@0.0.0.0:2222/vole.git

A free and open-source forge that lives in your Git repo.

Blame · ci/agent/credential_test.go

Line attribution on main · 64 line(s)

6e1fca51
Keenan Nemetz
6d ago
1 package agent
2
3 import (
4 "strings"
5 "testing"
6 )
7
8 // The job pushes to the instance it was leased from, so the known_hosts entry has to
9 // name the same host and port. OpenSSH brackets a non-default port, and getting that
10 // wrong turns every push into a host-key failure.
11 func TestKnownHostsLineNamesTheLeaseHost(t *testing.T) {
12 t.Parallel()
13 const key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIexample"
14 cases := []struct{ remote, want string }{
15 {"ssh://git@forge.example.com/demo", "forge.example.com " + key + "\n"},
16 {"ssh://git@forge.example.com:22/demo", "forge.example.com " + key + "\n"},
17 {"ssh://git@127.0.0.1:2222/vole", "[127.0.0.1]:2222 " + key + "\n"},
18 }
19 for _, c := range cases {
20 got, err := knownHostsLine(c.remote, key)
21 if err != nil {
22 t.Fatalf("%s: %v", c.remote, err)
23 }
24 if got != c.want {
25 t.Fatalf("%s: known_hosts = %q, want %q", c.remote, got, c.want)
26 }
27 }
28 // A job that cannot be told how to verify the host must not run with the key: a
29 // credential pointed at an unverified host is worse than no credential.
30 if _, err := knownHostsLine("https://forge.example.com/demo", key); err == nil {
31 t.Fatal("a non-ssh remote was accepted")
32 }
33 if _, err := knownHostsLine("ssh://git@forge.example.com/demo", ""); err == nil {
34 t.Fatal("an empty host key was accepted")
35 }
36 }
37
38 // The environment points git and the CLI at the key without a step doing anything,
39 // and it does not edit the run's pinned environment.
40 func TestCredentialEnvironmentPointsAtTheKey(t *testing.T) {
41 t.Parallel()
42 pinned := map[string]string{"GOCACHE": "/cache/go-build"}
43 job := &runnerJob{ID: "01M6FFY0BARR9YDQ3KK64VQ0MG", Repo: "demo"}
44 env, err := withCredentialEnv(pinned, "ssh://git@forge.example.com:2222/demo", job)
45 if err != nil {
46 t.Fatal(err)
47 }
48 if env["VOLE_RUN_ID"] != job.ID || env["VOLE_REPO"] != "demo" {
49 t.Fatalf("run identity = %q/%q", env["VOLE_RUN_ID"], env["VOLE_REPO"])
50 }
51 if !strings.Contains(env["GIT_SSH_COMMAND"], "-i "+RunCredentialPath) ||
52 !strings.Contains(env["GIT_SSH_COMMAND"], RunKnownHostsPath) {
53 t.Fatalf("GIT_SSH_COMMAND = %q", env["GIT_SSH_COMMAND"])
54 }
55 if env["GIT_SSH_COMMAND"] != env["VOLE_SSH_COMMAND"] {
56 t.Fatalf("the CLI and git disagree: %q vs %q", env["VOLE_SSH_COMMAND"], env["GIT_SSH_COMMAND"])
57 }
58 if env["GOCACHE"] != "/cache/go-build" {
59 t.Fatalf("the job's own environment was lost: %q", env["GOCACHE"])
60 }
61 if _, ok := pinned["VOLE_RUN_ID"]; ok {
62 t.Fatal("the pinned environment was edited in place")
63 }
64 }