Repository navigation
Expand file tree
/
Copy pathsecret_test.go
More file actions
80 lines (69 loc) · 2.09 KB
/
Copy pathsecret_test.go
File metadata and controls
80 lines (69 loc) · 2.09 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
75
76
77
78
79
80
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"context"
"net"
"testing"
secretmanager "cloud.google.com/go/secretmanager/apiv1"
secretmanagerpb "cloud.google.com/go/secretmanager/apiv1/secretmanagerpb"
"github.com/mozilla-services/yaml"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type fakeServer struct {
secretmanagerpb.UnimplementedSecretManagerServiceServer
}
func (f *fakeServer) AccessSecretVersion(ctx context.Context, req *secretmanagerpb.AccessSecretVersionRequest) (*secretmanagerpb.AccessSecretVersionResponse, error) {
fakeData, _ := yaml.Marshal(map[string]string{
"privatekey": "privatekey",
"passphrase": "passphrase",
"issuerprivkey": "issuerprivkey",
})
resp := &secretmanagerpb.AccessSecretVersionResponse{
Name: req.Name,
Payload: &secretmanagerpb.SecretPayload{
Data: fakeData,
},
}
return resp, nil
}
func TestSecretMapRead(t *testing.T) {
ctx := context.Background()
fake := &fakeServer{}
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
gsrv := grpc.NewServer()
secretmanagerpb.RegisterSecretManagerServiceServer(gsrv, fake)
fakeAddr := l.Addr().String()
go func() {
if err := gsrv.Serve(l); err != nil {
panic(err)
}
}()
client, err := secretmanager.NewClient(ctx,
option.WithEndpoint(fakeAddr),
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
)
if err != nil {
t.Fatal(err)
}
testMap, err := getSecretMap(client, ctx, "fake-project", "fake-secret")
if err != nil {
t.Fatal(err)
}
if testMap["privatekey"] != "privatekey" {
t.Error("Unable to retrieve privatekey from secret")
}
if testMap["passphrase"] != "passphrase" {
t.Error("Unable to retrieve passphrase from secret")
}
if testMap["issuerprivkey"] != "issuerprivkey" {
t.Error("Unable to retrieve issuerprivkey from secret")
}
}