-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.go
38 lines (30 loc) · 963 Bytes
/
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
package gsuitemdm
//
// GSuiteMDM GCP Secret Manager related code
//
import (
"context"
"errors"
secretmanager "cloud.google.com/go/secretmanager/apiv1beta1"
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1beta1"
)
// Get a secret from Secret Manager
func GetSecret(ctx context.Context, sid string) (string, error) {
// Create a Secret Manager client
client, err := secretmanager.NewClient(ctx)
if err != nil {
return "", errors.New("Error creating Secret Manager client: " + err.Error())
}
// Build the Secret Manager request
smreq := &secretmanagerpb.AccessSecretVersionRequest{
Name: sid + "/versions/latest",
}
// Call the Secret Manager API and get the requested secret using its ID
smres, err := client.AccessSecretVersion(ctx, smreq)
if err != nil {
return "", errors.New("Error retrieving secret: " + err.Error())
}
// Return the specified secret
return string(smres.Payload.Data), nil
}
// EOF