-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.go
161 lines (136 loc) · 3.85 KB
/
backend.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package ibmcloudauth
import (
"context"
"errors"
"sync"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := Backend(conf)
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
// IBM Cloud Auth Backend
type ibmCloudAuthBackend struct {
*framework.Backend
adminTokenLock sync.RWMutex
adminToken string
adminTokenExpiry time.Time
iamHelperLock sync.RWMutex
iamHelper iamHelper
}
func Backend(c *logical.BackendConfig) *ibmCloudAuthBackend {
b := &ibmCloudAuthBackend{}
b.Backend = &framework.Backend{
AuthRenew: b.pathLoginRenew,
BackendType: logical.TypeCredential, // This means it's an auth plugin
Help: backendHelp,
PathsSpecial: &logical.Paths{
Unauthenticated: []string{"login"}, // the '/login' path in not authenticated
SealWrapStorage: []string{
"config",
},
},
Paths: framework.PathAppend(
[]*framework.Path{
pathLogin(b),
pathConfig(b),
pathConfigRotateRoot(b),
},
pathsRole(b),
),
Clean: b.cleanup,
Invalidate: b.invalidate,
}
return b
}
func (b *ibmCloudAuthBackend) invalidate(ctx context.Context, key string) {
switch key {
case "config":
b.reset()
}
}
func (b *ibmCloudAuthBackend) reset() {
b.adminTokenLock.Lock()
unlockFunc := b.adminTokenLock.Unlock
defer func() { unlockFunc() }()
b.adminTokenExpiry = time.Now()
b.adminToken = ""
unlockIAMFunc := b.iamHelperLock.Unlock
defer func() { unlockIAMFunc() }()
b.iamHelperLock.Lock()
if b.iamHelper != nil {
b.iamHelper.Cleanup()
b.iamHelper = nil
}
}
func (b *ibmCloudAuthBackend) cleanup(_ context.Context) {
b.reset()
}
func (b *ibmCloudAuthBackend) getAdminToken(ctx context.Context, s logical.Storage) (string, error) {
b.adminTokenLock.RLock()
unlockFunc := b.adminTokenLock.RUnlock
defer func() { unlockFunc() }()
if b.adminToken != "" && (time.Until(b.adminTokenExpiry).Minutes() > adminTokenRenewBeforeExpirationMinutes) {
return b.adminToken, nil
}
b.adminTokenLock.RUnlock()
b.adminTokenLock.Lock()
unlockFunc = b.adminTokenLock.Unlock
if b.adminToken != "" && (time.Until(b.adminTokenExpiry).Minutes() > adminTokenRenewBeforeExpirationMinutes) {
return b.adminToken, nil
}
config, err := b.config(ctx, s)
if err != nil {
b.Logger().Error("failed to load configuration")
return "", err
}
if config == nil || config.APIKey == "" {
return "", errors.New("no API key was set in the configuration. Token login requires the auth plugin to be configured with an API key")
}
iam, resp := b.getIAMHelper(ctx, s)
if resp != nil {
b.Logger().Error("failed to retrieve an IAM helper", "error", resp.Error())
return "", resp.Error()
}
token, err := iam.ObtainToken(config.APIKey)
if err != nil {
b.Logger().Error("failed to obtain the access token using the configured API key configuration", "error", err)
return "", err
}
adminTokenInfo, resp := iam.VerifyToken(ctx, token)
if resp != nil {
return "", resp.Error()
}
b.adminToken = token
b.adminTokenExpiry = adminTokenInfo.Expiry
return b.adminToken, nil
}
func (b *ibmCloudAuthBackend) getIAMHelper(ctx context.Context, s logical.Storage) (iamHelper, *logical.Response) {
b.iamHelperLock.RLock()
unlockFunc := b.iamHelperLock.RUnlock
defer func() { unlockFunc() }()
if b.iamHelper != nil {
return b.iamHelper, nil
}
b.iamHelperLock.RUnlock()
b.iamHelperLock.Lock()
unlockFunc = b.iamHelperLock.Unlock
if b.iamHelper != nil {
return b.iamHelper, nil
}
config, resp := b.getConfig(ctx, s)
if resp != nil {
return nil, resp
}
b.iamHelper = new(ibmCloudHelper)
b.iamHelper.Init(config.IAMEndpoint, config.UserManagementEndpoint)
return b.iamHelper, nil
}
const backendHelp = `
The IBM Cloud backend plugin allows authentication for IBM Public Cloud.
`