This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
write_test.go
184 lines (138 loc) · 5.24 KB
/
write_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package keysync
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func testConfig(t *testing.T) Config {
dir, err := ioutil.TempDir("", "keysyncWriteTest")
assert.NoError(t, err, "Error making tempdir")
return Config{
SecretsDir: dir,
ChownFiles: false,
}
}
func testClientConfig(name string) ClientConfig {
return ClientConfig{
Key: fmt.Sprintf("fixtures/clients/%s.key", name),
Cert: fmt.Sprintf("fixtures/clients/%s.crt", name),
DirName: name,
}
}
func testSecret(name string) Secret {
return Secret{
Name: name,
Content: []byte("my secret content"),
Checksum: "0ABC",
}
}
func testFixture(t *testing.T) (Config, ClientConfig, OutputDirCollection, Output) {
c := testConfig(t)
testlogger := testLogger()
odc := OutputDirCollection{Config: &c}
cc := testClientConfig("client 1")
out, err := odc.NewOutput(cc, testlogger)
assert.NoError(t, err)
return c, cc, odc, out
}
func testLogger() *logrus.Entry {
return logrus.NewEntry(logrus.New())
}
// Test the basic secret lifecycle:
// Calls all methods in the OutputCollection and Output interface
func TestBasicLifecycle(t *testing.T) {
c, _, odc, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
odc.Cleanup(map[string]struct{}{"client 1": {}}, testLogger())
name := "secret 1"
s := testSecret(name)
state, err := out.Write(&s)
assert.NoError(t, err)
deleted, err := out.Cleanup(map[string]Secret{name: {}})
assert.NoError(t, err)
assert.Zero(t, deleted)
assert.True(t, out.Validate(&s, *state), "Expected just-written secret to be valid")
filecontents, err := ioutil.ReadFile(filepath.Join(c.SecretsDir, "client 1", name))
assert.NoError(t, err)
assert.Equal(t, s.Content, content(filecontents))
assert.NoError(t, out.Remove(name))
assert.False(t, out.Validate(&s, *state), "Expected secret invalid after deletion")
deleted, err = out.RemoveAll()
assert.NoError(t, err)
assert.EqualValues(t, 1, deleted)
}
// Test that if ChownFiles is set, we fail to write out files (since we're not root)
// While this isn't a super-great test, it makes sure ChownFiles = true does something.
func TestChownFiles(t *testing.T) {
c, _, _, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
// Easier to modify this after-the-fact so we can share test fixture setup
out.(*OutputDir).ChownFiles = true
secret := testSecret("secret")
_, err := out.Write(&secret)
assert.Error(t, err, "Expected error writing file. Maybe you're testing as root?")
}
// This tests enforcing filesystems. We set an EnforceFilesystem value that won't correspond with any filesystem
// we might run tests on, and thus we should never succeed in writing files.
func TestEnforceFS(t *testing.T) {
c, _, _, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
// Easier to modify this after-the-fact so we can share test fixture setup
// This value is Linux's /proc filesystem, arbitrarily chosen as a filesystem we're not going to be writing to,
// so that out.Write is guaranteed to fail.
out.(*OutputDir).EnforceFilesystem = 0x9fa0
secret := testSecret("secret")
_, err := out.Write(&secret)
assert.Contains(t, err.Error(), "unexpected filesystem")
}
// Make sure any stray files and directories are cleaned up by Keysync.
func TestCleanup(t *testing.T) {
c, cc, odc, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
junkdir := filepath.Join(c.SecretsDir, "junk client")
assert.NoError(t, os.MkdirAll(junkdir, 0400))
_, err := os.Stat(junkdir)
assert.NoError(t, err, "Expected junkdir to exist before cleanup")
_, errs := odc.Cleanup(map[string]struct{}{cc.DirName: {}}, testLogger())
assert.Equal(t, 0, len(errs), "Expected no errors cleaning up")
_, err = os.Stat(junkdir)
assert.Error(t, err, "Expected junkdir to be gone after cleanup")
junkfile := filepath.Join(c.SecretsDir, cc.DirName, "junk file")
assert.NoError(t, ioutil.WriteFile(junkfile, []byte("my data"), 0400))
deleted, err := out.Cleanup(map[string]Secret{"secret 1": {}})
assert.NoError(t, err)
assert.EqualValues(t, 1, deleted)
_, err = os.Stat(junkfile)
assert.Error(t, err, "Expected file to be gone after cleanup")
}
// TestCustomFilename makes sure we honor the "filename" attribute when writing out files.
func TestCustomFilename(t *testing.T) {
c, _, _, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
secret := testSecret("secret_name")
filename := "override_filename"
secret.FilenameOverride = &filename
state, err := out.Write(&secret)
assert.NoError(t, err)
deleted, err := out.Cleanup(map[string]Secret{filename: {}})
assert.NoError(t, err)
assert.Zero(t, deleted)
assert.True(t, out.Validate(&secret, *state), "Expected override_filename secret to be valid after cleanup")
assert.NoError(t, out.Remove(filename))
assert.False(t, out.Validate(&secret, *state), "Expected secret to be removed")
}
// TestCustomFilenameAsFilepath makes sure we fail to write a file if the "filename" is actually a filepath
func TestCustomFilenameAsFilepath(t *testing.T) {
c, _, _, out := testFixture(t)
defer os.RemoveAll(c.SecretsDir)
secret := testSecret("secret_name")
filename := "../override_filename"
secret.FilenameOverride = &filename
state, err := out.Write(&secret)
assert.Error(t, err)
assert.Nil(t, state)
}