Skip to content

Commit 3845a8d

Browse files
committed
s3: also test backend with secrets
1 parent c49bfd4 commit 3845a8d

File tree

1 file changed

+94
-31
lines changed

1 file changed

+94
-31
lines changed

backends/s3/credentials_test.go

Lines changed: 94 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,27 @@ package s3_test
33
import (
44
"context"
55
"errors"
6-
"io"
76
"os"
87
"path/filepath"
98
"testing"
109
"time"
1110

1211
"github.com/PowerDNS/simpleblob/backends/s3"
1312
"github.com/PowerDNS/simpleblob/backends/s3/s3testing"
13+
"github.com/PowerDNS/simpleblob/tester"
1414
"github.com/minio/minio-go/v7"
1515
"github.com/minio/minio-go/v7/pkg/credentials"
1616
)
1717

1818
func TestFileSecretsCredentials(t *testing.T) {
1919
tempDir := t.TempDir()
2020

21+
access, secret := secretsPaths(tempDir)
22+
2123
// Instanciate provider (what we're testing).
2224
provider := &s3.FileSecretsCredentials{
23-
AccessKeyFile: filepath.Join(tempDir, "access-key"),
24-
SecretKeyFile: filepath.Join(tempDir, "secret-key"),
25-
}
26-
27-
// writeFiles creates or overwrites provider files
28-
// with the same content.
29-
writeFiles := func(content string) {
30-
writeContent := func(filename string) {
31-
f, err := os.Create(filename)
32-
if err != nil {
33-
t.Fatal(err)
34-
}
35-
defer f.Close()
36-
if content == "" {
37-
return
38-
}
39-
_, err = io.WriteString(f, content)
40-
if err != nil {
41-
t.Fatal(err)
42-
}
43-
}
44-
writeContent(provider.AccessKeyFile)
45-
writeContent(provider.SecretKeyFile)
25+
AccessKeyFile: access,
26+
SecretKeyFile: secret,
4627
}
4728

4829
ctx := context.Background()
@@ -59,11 +40,6 @@ func TestFileSecretsCredentials(t *testing.T) {
5940
}
6041
defer func() { _ = stop() }()
6142

62-
// First credential files creation.
63-
// Keep them empty for now,
64-
// so that calls to the server will fail.
65-
writeFiles("")
66-
6743
// Create minio client, using our provider.
6844
creds := credentials.New(provider)
6945
clt, err := minio.New(addr, &minio.Options{
@@ -86,19 +62,106 @@ func TestFileSecretsCredentials(t *testing.T) {
8662
}
8763
}
8864

65+
// First credential files creation.
66+
// Keep them empty for now,
67+
// so that calls to the server will fail.
68+
writeSecrets(t, tempDir, "")
69+
8970
// The files do not hold the right values,
9071
// so a call to the server should fail.
9172
assertClientSuccess(false, "just after init")
9273

9374
// Write the right keys to the files.
9475
// We're not testing expiry here,
9576
// and forcing credentials cache to update.
96-
writeFiles(s3testing.AdminUserOrPassword)
77+
writeSecrets(t, tempDir, s3testing.AdminUserOrPassword)
9778
creds.Expire()
9879
assertClientSuccess(true, "after changing files content")
9980

10081
// Change content of the files.
101-
writeFiles("badcredentials")
82+
writeSecrets(t, tempDir, "badcredentials")
10283
creds.Expire()
10384
assertClientSuccess(false, "after changing again, to bad credentials")
10485
}
86+
87+
func TestBackendWithSecrets(t *testing.T) {
88+
tempDir := t.TempDir()
89+
90+
ctx := context.Background()
91+
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
92+
defer cancel()
93+
94+
addr, stop, err := s3testing.ServeMinio(ctx, tempDir)
95+
if errors.Is(err, s3testing.ErrMinioNotFound) {
96+
t.Skip("minio binary not found locally, make sure it is in PATH")
97+
}
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
defer func() { _ = stop() }()
102+
103+
// Prepare backend options to reuse.
104+
// These will not change.
105+
access, secret := secretsPaths(tempDir)
106+
opt := s3.Options{
107+
AccessKeyFile: access,
108+
SecretKeyFile: secret,
109+
Region: "us-east-1",
110+
Bucket: "test-bucket",
111+
CreateBucket: true,
112+
EndpointURL: "http://" + addr,
113+
}
114+
115+
// Backend should not start if secrets files do not exist.
116+
_, err = s3.New(ctx, opt)
117+
if !errors.Is(err, os.ErrNotExist) {
118+
t.Fatal("backend should not start without credentials")
119+
}
120+
121+
// Now write files, but with bad content.
122+
writeSecrets(t, tempDir, "")
123+
_, err = s3.New(ctx, opt)
124+
if err == nil || err.Error() != "Access Denied." {
125+
t.Fatal("backend should not start with bad credentials")
126+
}
127+
128+
// Write the good content.
129+
// Now the backend should start and be able to perform a request.
130+
writeSecrets(t, tempDir, s3testing.AdminUserOrPassword)
131+
132+
backend, err := s3.New(ctx, opt)
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
_, err = backend.List(ctx, "")
137+
if err != nil {
138+
t.Fatal(err)
139+
}
140+
141+
// Finally, the whole test suite should succeed.
142+
tester.DoBackendTests(t, backend)
143+
}
144+
145+
// secretsPaths returns the file paths for the access key
146+
// and the secret key, respectively.
147+
// For a same dir, the returned values will always be the same.
148+
func secretsPaths(dir string) (access, secret string) {
149+
access = filepath.Join(dir, "access-key")
150+
secret = filepath.Join(dir, "secret-key")
151+
return
152+
}
153+
154+
// writeSecrets writes content to files called "access-key" and "secret-key"
155+
// in dir.
156+
// It returns
157+
func writeSecrets(t testing.TB, dir, content string) {
158+
access, secret := secretsPaths(dir)
159+
err := os.WriteFile(access, []byte(content), 0666)
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
err = os.WriteFile(secret, []byte(content), 0666)
164+
if err != nil {
165+
t.Fatal(err)
166+
}
167+
}

0 commit comments

Comments
 (0)