-
Notifications
You must be signed in to change notification settings - Fork 1
/
secretmanager.go
46 lines (38 loc) · 1.03 KB
/
secretmanager.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
package secretstring
import (
"context"
"fmt"
secretmanager "cloud.google.com/go/secretmanager/apiv1beta1"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1beta1"
)
type secretRetrieval interface {
getSecretString(project, name string) (string, error)
}
const (
secretResourceName = `projects/%s/secrets/%s/versions/latest`
)
var (
secretManagerClient secretRetrieval
)
type secretClient struct {
secretClient *secretmanager.Client
}
func (s *secretClient) getSecretString(project, desiredSecretName string) (string, error) {
ctx := context.Background()
name := fmt.Sprintf(secretResourceName, project, desiredSecretName)
req := &secretmanagerpb.AccessSecretVersionRequest{
Name: name,
}
resp, err := s.secretClient.AccessSecretVersion(ctx, req)
if err != nil {
return "", err
}
return string(resp.Payload.Data), nil
}
func setupSecretClient() (err error) {
var sc secretClient
ctx := context.Background()
sc.secretClient, err = secretmanager.NewClient(ctx)
secretManagerClient = &sc
return
}