forked from kula/vault-plugin-secrets-backblazeb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb2_client.go
67 lines (52 loc) · 1.79 KB
/
b2_client.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
67
package vault_plugin_secrets_backblazeb2
import (
"context"
b2client "github.com/Backblaze/blazer/b2"
"github.com/hashicorp/vault/sdk/logical"
)
// Call this to set a new b2client in the backend.
func (b *backblazeB2Backend) newB2Client(ctx context.Context, applicationKeyID string, applicationKey string) error {
b.Logger().Debug("newB2Client", "applicationKeyID", applicationKeyID)
client, err := b2client.NewClient(ctx, applicationKeyID, applicationKey)
if err != nil {
b.Logger().Error("Error getting new b2 client", "error", err)
return err
}
b.Logger().Debug("Getting clientMutex.Lock")
b.lock.Lock()
defer b.lock.Unlock()
b.client = client
b.Logger().Debug("Set new b.client, unlocking and returning")
return nil
}
// Convenience function to get the b2client
func (b *backblazeB2Backend) getB2Client(ctx context.Context, s logical.Storage) (*b2client.Client, error) {
b.Logger().Debug("getB2Client, getting clientMutex.RLock")
b.lock.RLock()
if b.client != nil {
b.Logger().Debug("have client already, unlocking and returning")
b.lock.RUnlock()
return b.client, nil
}
b.lock.RUnlock()
// We don't have a current client, look up the id and key
// from the current configuration and create a new client
b.Logger().Info("Getting new b2 client, fetching config")
c, err := b.getConfig(ctx, s)
if err != nil {
b.Logger().Error("Error fetching configuration to make new b2client", "error", err)
return nil, err
}
if c.ApplicationKeyId == "" {
b.Logger().Error("KeyID not set when trying to create new client")
return nil, err
}
if c.ApplicationKey == "" {
b.Logger().Error("Key not set when trying to create new client")
return nil, err
}
if err := b.newB2Client(ctx, c.ApplicationKeyId, c.ApplicationKey); err != nil {
return nil, err
}
return b.client, nil
}