9
9
"context"
10
10
"fmt"
11
11
"strings"
12
+ "sync"
12
13
13
14
secretmanager "cloud.google.com/go/secretmanager/apiv1"
14
15
gax "github.com/googleapis/gax-go/v2"
@@ -28,6 +29,7 @@ const (
28
29
)
29
30
30
31
type provider struct {
32
+ mu sync.Mutex
31
33
client secretsManagerClient
32
34
}
33
35
@@ -41,24 +43,24 @@ func newProvider(confmap.ProviderSettings) confmap.Provider {
41
43
42
44
func (p * provider ) Retrieve (ctx context.Context , uri string , _ confmap.WatcherFunc ) (* confmap.Retrieved , error ) {
43
45
if ! strings .HasPrefix (uri , schemeName + ":" ) {
44
- return nil , fmt .Errorf ("%q uri is not supported by %q provider " , uri , schemeName )
46
+ return nil , fmt .Errorf ("%q uri is not supported by Google Secret Manager Provider " , uri )
45
47
}
46
48
secretName := strings .TrimPrefix (uri , schemeName + ":" )
47
-
49
+ p . mu . Lock ()
48
50
if p .client == nil {
49
51
client , err := secretmanager .NewClient (ctx )
50
52
if err != nil {
53
+ p .mu .Unlock ()
51
54
return nil , fmt .Errorf ("failed to create a Google secret manager client: %w" , err )
52
55
}
53
- defer client .Close ()
54
56
p .client = client
57
+ p .mu .Unlock ()
55
58
}
56
59
req := & secretmanagerpb.AccessSecretVersionRequest {
57
60
Name : secretName ,
58
61
}
59
62
resp , err := p .client .AccessSecretVersion (ctx , req )
60
63
if err != nil {
61
- var apiErr * apierror.APIError
62
64
apiErr , ok := apierror .FromError (err )
63
65
errorMsg := "failed to access secret version"
64
66
if ! ok {
@@ -73,6 +75,12 @@ func (*provider) Scheme() string {
73
75
return schemeName
74
76
}
75
77
76
- func (* provider ) Shutdown (context.Context ) error {
78
+ func (p * provider ) Shutdown (context.Context ) error {
79
+ p .mu .Lock ()
80
+ if p .client != nil {
81
+ p .client .Close ()
82
+ p .client = nil
83
+ }
84
+ p .mu .Unlock ()
77
85
return nil
78
86
}
0 commit comments