|
| 1 | +package s3_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/PowerDNS/simpleblob/backends/s3" |
| 12 | + "github.com/PowerDNS/simpleblob/backends/s3/s3testing" |
| 13 | + "github.com/PowerDNS/simpleblob/tester" |
| 14 | + "github.com/minio/minio-go/v7" |
| 15 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 16 | +) |
| 17 | + |
| 18 | +func TestFileSecretsCredentials(t *testing.T) { |
| 19 | + tempDir := t.TempDir() |
| 20 | + |
| 21 | + access, secret := secretsPaths(tempDir) |
| 22 | + |
| 23 | + // Instanciate provider (what we're testing). |
| 24 | + provider := &s3.FileSecretsCredentials{ |
| 25 | + AccessKeyFile: access, |
| 26 | + SecretKeyFile: secret, |
| 27 | + } |
| 28 | + |
| 29 | + ctx := context.Background() |
| 30 | + ctx, cancel := context.WithTimeout(ctx, 10*time.Second) |
| 31 | + defer cancel() |
| 32 | + |
| 33 | + // Create server |
| 34 | + addr, stop, err := s3testing.ServeMinio(ctx, tempDir) |
| 35 | + if errors.Is(err, s3testing.ErrMinioNotFound) { |
| 36 | + t.Skip("minio binary not found locally, make sure it is in PATH") |
| 37 | + } |
| 38 | + if err != nil { |
| 39 | + t.Fatal(err) |
| 40 | + } |
| 41 | + defer func() { _ = stop() }() |
| 42 | + |
| 43 | + // Create minio client, using our provider. |
| 44 | + creds := credentials.New(provider) |
| 45 | + clt, err := minio.New(addr, &minio.Options{ |
| 46 | + Creds: creds, |
| 47 | + Region: "us-east-1", |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + |
| 53 | + assertClientSuccess := func(want bool, when string) { |
| 54 | + _, err = clt.BucketExists(ctx, "doesnotmatter") |
| 55 | + s := "fail" |
| 56 | + if want { |
| 57 | + s = "succeed" |
| 58 | + } |
| 59 | + ok := (err == nil) == want |
| 60 | + if !ok { |
| 61 | + t.Fatalf("expected call to %s %s", s, when) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 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 | + |
| 70 | + // The files do not hold the right values, |
| 71 | + // so a call to the server should fail. |
| 72 | + assertClientSuccess(false, "just after init") |
| 73 | + |
| 74 | + // Write the right keys to the files. |
| 75 | + // We're not testing expiry here, |
| 76 | + // and forcing credentials cache to update. |
| 77 | + writeSecrets(t, tempDir, s3testing.AdminUserOrPassword) |
| 78 | + creds.Expire() |
| 79 | + assertClientSuccess(true, "after changing files content") |
| 80 | + |
| 81 | + // Change content of the files. |
| 82 | + writeSecrets(t, tempDir, "badcredentials") |
| 83 | + creds.Expire() |
| 84 | + assertClientSuccess(false, "after changing again, to bad credentials") |
| 85 | +} |
| 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