| 1 |
// Copyright 2019-2025 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package secrets |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"errors" |
| 20 |
"net/url" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
"gocloud.dev/gcerrors" |
| 26 |
"gocloud.dev/internal/gcerr" |
| 27 |
"gocloud.dev/internal/testing/oteltest" |
| 28 |
"gocloud.dev/secrets/driver" |
| 29 |
) |
| 30 |
|
| 31 |
var errFake = errors.New("fake") |
| 32 |
|
| 33 |
type erroringKeeper struct { |
| 34 |
driver.Keeper |
| 35 |
} |
| 36 |
|
| 37 |
func (k *erroringKeeper) Decrypt(ctx context.Context, b []byte) ([]byte, error) { |
| 38 |
return nil, errFake |
| 39 |
} |
| 40 |
|
| 41 |
func (k *erroringKeeper) Encrypt(ctx context.Context, b []byte) ([]byte, error) { |
| 42 |
return nil, errFake |
| 43 |
} |
| 44 |
|
| 45 |
func (k *erroringKeeper) Close() error { return errFake } |
| 46 |
func (k *erroringKeeper) ErrorCode(error) gcerrors.ErrorCode { return gcerrors.Internal } |
| 47 |
|
| 48 |
func TestErrorsAreWrapped(t *testing.T) { |
| 49 |
ctx := context.Background() |
| 50 |
k := NewKeeper(&erroringKeeper{}) |
| 51 |
|
| 52 |
// verifyWrap ensures that err is wrapped exactly once. |
| 53 |
verifyWrap := func(description string, err error) { |
| 54 |
if err == nil { |
| 55 |
t.Errorf("%s: got nil error, wanted non-nil", description) |
| 56 |
} else if unwrapped, ok := err.(*gcerr.Error); !ok { |
| 57 |
t.Errorf("%s: not wrapped: %v", description, err) |
| 58 |
} else if du, ok := unwrapped.Unwrap().(*gcerr.Error); ok { |
| 59 |
t.Errorf("%s: double wrapped: %v", description, du) |
| 60 |
} |
| 61 |
if s := err.Error(); !strings.HasPrefix(s, "secrets ") { |
| 62 |
t.Errorf("%s: Error() for wrapped error doesn't start with secrets: prefix: %s", description, s) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
_, err := k.Decrypt(ctx, nil) |
| 67 |
verifyWrap("Decrypt", err) |
| 68 |
|
| 69 |
_, err = k.Encrypt(ctx, nil) |
| 70 |
verifyWrap("Encrypt", err) |
| 71 |
|
| 72 |
err = k.Close() |
| 73 |
verifyWrap("Close", err) |
| 74 |
} |
| 75 |
|
| 76 |
// TestKeeperIsClosed tests that Keeper functions return an error when the |
| 77 |
// Keeper is closed. |
| 78 |
func TestKeeperIsClosed(t *testing.T) { |
| 79 |
ctx := context.Background() |
| 80 |
k := NewKeeper(&erroringKeeper{}) |
| 81 |
k.Close() |
| 82 |
|
| 83 |
if _, err := k.Decrypt(ctx, nil); err != errClosed { |
| 84 |
t.Error(err) |
| 85 |
} |
| 86 |
if _, err := k.Encrypt(ctx, nil); err != errClosed { |
| 87 |
t.Error(err) |
| 88 |
} |
| 89 |
if err := k.Close(); err != errClosed { |
| 90 |
t.Error(err) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestOpenTelemetry(t *testing.T) { |
| 95 |
|
| 96 |
ctx := context.Background() |
| 97 |
|
| 98 |
te := oteltest.NewTestExporter(t, OpenTelemetryViews) |
| 99 |
|
| 100 |
k := NewKeeper(&erroringKeeper{}) |
| 101 |
defer k.Close() |
| 102 |
_, _ = k.Encrypt(ctx, nil) |
| 103 |
|
| 104 |
_, _ = k.Decrypt(ctx, nil) |
| 105 |
_, _ = k.Decrypt(ctx, nil) |
| 106 |
|
| 107 |
// Check collected spans. |
| 108 |
spanStubs := te.GetSpans() |
| 109 |
metrics := te.GetMetrics(ctx) |
| 110 |
diff := oteltest.Diff(spanStubs.Snapshots(), metrics, pkgName, "gocloud.dev/secrets", []oteltest.Call{ |
| 111 |
{Method: "Encrypt", Code: gcerrors.Internal}, |
| 112 |
{Method: "Decrypt", Code: gcerrors.Internal}, |
| 113 |
{Method: "Decrypt", Code: gcerrors.Internal}, |
| 114 |
}) |
| 115 |
if diff != "" { |
| 116 |
t.Error(diff) |
| 117 |
} |
| 118 |
|
| 119 |
if err := te.Shutdown(ctx); err != nil { |
| 120 |
// Just log and continue - not failing the test on shutdown errors. |
| 121 |
t.Logf("OpenTelemetry shutdown error (non-fatal): %v", err) |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
func TestURLMux(t *testing.T) { |
| 127 |
ctx := context.Background() |
| 128 |
|
| 129 |
mux := new(URLMux) |
| 130 |
fake := &fakeOpener{} |
| 131 |
mux.RegisterKeeper("foo", fake) |
| 132 |
mux.RegisterKeeper("err", fake) |
| 133 |
|
| 134 |
if diff := cmp.Diff(mux.KeeperSchemes(), []string{"err", "foo"}); diff != "" { |
| 135 |
t.Errorf("Schemes: %s", diff) |
| 136 |
} |
| 137 |
if !mux.ValidKeeperScheme("foo") || !mux.ValidKeeperScheme("err") { |
| 138 |
t.Errorf("ValidKeeperScheme didn't return true for valid scheme") |
| 139 |
} |
| 140 |
if mux.ValidKeeperScheme("foo2") || mux.ValidKeeperScheme("http") { |
| 141 |
t.Errorf("ValidKeeperScheme didn't return false for invalid scheme") |
| 142 |
} |
| 143 |
|
| 144 |
for _, tc := range []struct { |
| 145 |
name string |
| 146 |
url string |
| 147 |
wantErr bool |
| 148 |
}{ |
| 149 |
{ |
| 150 |
name: "empty URL", |
| 151 |
wantErr: true, |
| 152 |
}, |
| 153 |
{ |
| 154 |
name: "invalid URL", |
| 155 |
url: ":foo", |
| 156 |
wantErr: true, |
| 157 |
}, |
| 158 |
{ |
| 159 |
name: "invalid URL no scheme", |
| 160 |
url: "foo", |
| 161 |
wantErr: true, |
| 162 |
}, |
| 163 |
{ |
| 164 |
name: "unregistered scheme", |
| 165 |
url: "bar://mykeeper", |
| 166 |
wantErr: true, |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "func returns error", |
| 170 |
url: "err://mykeeper", |
| 171 |
wantErr: true, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "no query options", |
| 175 |
url: "foo://mykeeper", |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "empty query options", |
| 179 |
url: "foo://mykeeper?", |
| 180 |
}, |
| 181 |
{ |
| 182 |
name: "query options", |
| 183 |
url: "foo://mykeeper?aAa=bBb&cCc=dDd", |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "multiple query options", |
| 187 |
url: "foo://mykeeper?x=a&x=b&x=c", |
| 188 |
}, |
| 189 |
{ |
| 190 |
name: "fancy keeper name", |
| 191 |
url: "foo:///foo/bar/baz", |
| 192 |
}, |
| 193 |
{ |
| 194 |
name: "using api scheme prefix", |
| 195 |
url: "secrets+foo://mykeeper", |
| 196 |
}, |
| 197 |
{ |
| 198 |
name: "using api+type scheme prefix", |
| 199 |
url: "secrets+keeper+foo://mykeeper", |
| 200 |
}, |
| 201 |
} { |
| 202 |
t.Run(tc.name, func(t *testing.T) { |
| 203 |
keeper, gotErr := mux.OpenKeeper(ctx, tc.url) |
| 204 |
if (gotErr != nil) != tc.wantErr { |
| 205 |
t.Fatalf("got err %v, want error %v", gotErr, tc.wantErr) |
| 206 |
} |
| 207 |
if gotErr != nil { |
| 208 |
return |
| 209 |
} |
| 210 |
defer keeper.Close() |
| 211 |
if got := fake.u.String(); got != tc.url { |
| 212 |
t.Errorf("got %q want %q", got, tc.url) |
| 213 |
} |
| 214 |
// Repeat with OpenKeeperURL. |
| 215 |
parsed, err := url.Parse(tc.url) |
| 216 |
if err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
keeper, gotErr = mux.OpenKeeperURL(ctx, parsed) |
| 220 |
if gotErr != nil { |
| 221 |
t.Fatalf("got err %v, want nil", gotErr) |
| 222 |
} |
| 223 |
defer keeper.Close() |
| 224 |
if got := fake.u.String(); got != tc.url { |
| 225 |
t.Errorf("got %q want %q", got, tc.url) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
type fakeOpener struct { |
| 232 |
u *url.URL // last url passed to OpenKeeperURL. |
| 233 |
} |
| 234 |
|
| 235 |
func (o *fakeOpener) OpenKeeperURL(ctx context.Context, u *url.URL) (*Keeper, error) { |
| 236 |
if u.Scheme == "err" { |
| 237 |
return nil, errors.New("fail") |
| 238 |
} |
| 239 |
o.u = u |
| 240 |
return NewKeeper(&erroringKeeper{}), nil |
| 241 |
} |