Repository navigation
Expand file tree
/
Copy pathauth_source_test.go
More file actions
74 lines (69 loc) · 2.68 KB
/
Copy pathauth_source_test.go
File metadata and controls
74 lines (69 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func envOf(m map[string]string) func(string) string { return func(k string) string { return m[k] } }
func TestResolveAuthSources(t *testing.T) {
dir := t.TempDir()
pwFile := filepath.Join(dir, "pw")
os.WriteFile(pwFile, []byte("alice-pw\nbob-pw\n"), 0600)
cases := []struct {
name string
args []string
env map[string]string
want map[string]string
cliPw bool
wantErr string
}{
{"none", nil, nil, nil, false, ""},
{"cli", []string{"user=a", "passwd=x"}, nil, map[string]string{"a": "x"}, true, ""},
{"env", nil, map[string]string{"FOGPING_USER": "a", "FOGPING_PASSWD": "x"}, map[string]string{"a": "x"}, false, ""},
{"cli user + env password", []string{"user=a"}, map[string]string{"FOGPING_PASSWD": "x"}, map[string]string{"a": "x"}, false, ""},
{"passwd-file", []string{"user=alice,bob", "passwd-file=" + pwFile}, nil, map[string]string{"alice": "alice-pw", "bob": "bob-pw"}, false, ""},
{"env file", nil, map[string]string{"FOGPING_USER": "alice,bob", "FOGPING_PASSWD_FILE": pwFile}, map[string]string{"alice": "alice-pw", "bob": "bob-pw"}, false, ""},
{"cli beats env", []string{"user=a", "passwd=x"}, map[string]string{"FOGPING_USER": "b", "FOGPING_PASSWD": "y"}, map[string]string{"a": "x"}, true, ""},
{"both env", nil, map[string]string{"FOGPING_USER": "a", "FOGPING_PASSWD": "x", "FOGPING_PASSWD_FILE": pwFile}, nil, false, "not both"},
{"both cli", []string{"user=a", "passwd=x", "passwd-file=" + pwFile}, nil, nil, true, "not both"},
{"missing file", []string{"user=a", "passwd-file=" + filepath.Join(dir, "nope")}, nil, nil, false, "passwd-file"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, cliPw, err := resolveAuth(c.args, envOf(c.env))
if c.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), c.wantErr) {
t.Fatalf("err = %v, want %q", err, c.wantErr)
}
return
}
if err != nil {
t.Fatal(err)
}
if cliPw != c.cliPw {
t.Errorf("cliPasswd = %v, want %v", cliPw, c.cliPw)
}
if len(got) != len(c.want) {
t.Fatalf("users = %v, want %v", got, c.want)
}
for k, v := range c.want {
if got[k] != v {
t.Fatalf("users = %v, want %v", got, c.want)
}
}
})
}
}
// The container entrypoint must not turn the password into an argument.
func TestEntrypointKeepsPasswordOutOfArgv(t *testing.T) {
b, err := os.ReadFile("docker/entrypoint.sh")
if err != nil {
t.Fatal(err)
}
for _, line := range strings.Split(string(b), "\n") {
if !strings.HasPrefix(strings.TrimSpace(line), "#") && strings.Contains(line, "passwd=") {
t.Fatalf("entrypoint puts the password on the command line: %s", line)
}
}
}