-
Notifications
You must be signed in to change notification settings - Fork 4
/
secrets.go
66 lines (53 loc) · 1.36 KB
/
secrets.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
package secrets
import (
"context"
"fmt"
"google.golang.org/api/option"
vkit "cloud.google.com/go/secretmanager/apiv1"
pb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)
type Client struct {
pbc *vkit.Client
}
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
pbc, err := vkit.NewClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("secretmanager: %w", err)
}
return &Client{pbc}, nil
}
type secretOpt struct {
version string
}
type SecretOption func(s *secretOpt)
func WithVersion(v string) SecretOption {
return func(s *secretOpt) {
s.version = v
}
}
func (c *Client) Close() error {
return c.pbc.Close()
}
func (c *Client) GetSecret(ctx context.Context, project, secretName string, opts ...SecretOption) ([]byte, error) {
so := secretOpt{
version: "latest",
}
for _, o := range opts {
o(&so)
}
req := &pb.AccessSecretVersionRequest{
Name: fmt.Sprintf("projects/%s/secrets/%s/versions/%s", project, secretName, so.version),
}
resp, err := c.pbc.AccessSecretVersion(ctx, req)
if err != nil {
return nil, err
}
return resp.GetPayload().GetData(), nil
}
func GetSecret(ctx context.Context, project, secretName string, opts ...SecretOption) ([]byte, error) {
cl, err := NewClient(ctx)
if err != nil {
return nil, err
}
return cl.GetSecret(ctx, project, secretName, opts...)
}