-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.go
223 lines (186 loc) · 6.16 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Package atlasvault integrates GlobalSign Atlas with Hashicorp Vault using the plugin Interface.
package atlasvault
import (
"context"
"crypto/tls"
"fmt"
"os"
"strconv"
"strings"
"github.com/globalsign/atlas-hashicorp-vault/pkg/atlas"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/errutil"
"github.com/hashicorp/vault/sdk/logical"
)
// Factory configures and returns the GlobalSign Atlas Vault Plugin Backend.
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
var err error
b := NewBackend(conf, nil)
if conf != nil && len(conf.Config) != 0 {
b.issuanceOptions, err = parseIssuanceOptions(conf.Config)
if err != nil {
return nil, err
}
}
b.Backend.Setup(ctx, conf)
return b, nil
}
type atlasConstructor func(*atlas.ClientConfig) (atlas.Client, error)
// Backend returns a new Backend framework struct
func NewBackend(conf *logical.BackendConfig, clientConstructor atlasConstructor) *Backend {
if clientConstructor == nil {
clientConstructor = atlas.New
}
b := &Backend{
clientConstructor: clientConstructor,
tidyCASGuard: new(uint32),
}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(atlasHelp),
BackendType: logical.TypeLogical,
PathsSpecial: &logical.Paths{
SealWrapStorage: []string{
"config/authn",
},
},
Secrets: []*framework.Secret{
secretCerts(b),
},
}
b.Backend.Paths = append(b.Backend.Paths, b.paths()...)
return b
}
func parseIssuanceOptions(opts map[string]string) (out *atlas.CertRequestOptions, err error) {
out = &atlas.CertRequestOptions{}
if v, found := opts["OverrideSignatureAlgorithm"]; found {
out.OverrideSignatureAlgorithm = &v
}
if v, found := opts["OverrideSignatureHashAlgorithm"]; found {
out.OverrideSignatureHashAlgorithm = &v
}
if v, found := opts["OverrideDisableKeyUsageExtensions"]; found {
out.OverrideDisableKeyUsageExtensions, err = strconv.ParseBool(v)
if err != nil {
return nil, errutil.UserError{Err: "Config 'OverrideDisableKeyUsageExtensions': " + err.Error()}
}
}
if v, found := opts["OverrideDisableExtendedKeyUsageExtensions"]; found {
out.OverrideDisableExtendedKeyUsageExtensions, err = strconv.ParseBool(v)
if err != nil {
return nil, errutil.UserError{Err: "Config 'OverrideDisableExtendedKeyUsageExtensions': " + err.Error()}
}
}
return out, nil
}
// Backend wraps the Backend framework and adds a map for storing key value pairs
type Backend struct {
*framework.Backend
// cachedAtlasClient should not be used directly, please call the getter method getAtlasClient
cachedAtlasClient atlas.Client
clientConstructor atlasConstructor
// issuanceOptions overrides behavior of x509 cert template to Atlas request on a plugin level.
issuanceOptions *atlas.CertRequestOptions
tidyCASGuard *uint32
}
const (
// storageSystemPrefix is used to store things like module configuration
storageSystemPrefix = "sys/"
// Configuration value locations, stored under system prefix.
storageAtlasAPIKey = "atlas/api_key"
storageAtlasAPISecret = "atlas/api_secret"
storageAtlasAPICert = "atlas/cert_pem"
)
// getSystemValue gets the vault stored state under the system prefix using the provided logical storage.
func (b *Backend) getSystemValue(ctx context.Context, storage logical.Storage, key string) ([]byte, error) {
sysVal, err := storage.Get(ctx, storageSystemPrefix+key)
if err != nil {
return nil, err
}
if sysVal == nil {
return nil, fmt.Errorf("System Value Not Found '%s'", key)
}
return sysVal.Value, nil
}
// setSystemValue persists a value in the provided vault logical storage Backend using the system prefix.
func (b *Backend) setSystemValue(ctx context.Context, storage logical.Storage, key string, value []byte) error {
return storage.Put(ctx, &logical.StorageEntry{
Key: storageSystemPrefix + key,
Value: value,
SealWrap: true,
})
}
// getAtlasClient gets the cached atlas client or will lazily generate one based on stored paramters.
func (b *Backend) GetAtlasClient(ctx context.Context, storage logical.Storage) (atlas.Client, error) {
// Check cached version
if b.cachedAtlasClient != nil {
return b.cachedAtlasClient, nil
}
// Lazy construct using stored paramters
apiKey, err := b.getSystemValue(ctx, storage, storageAtlasAPIKey)
if err != nil {
return nil, err
}
apiSecret, err := b.getSystemValue(ctx, storage, storageAtlasAPISecret)
if err != nil {
return nil, err
}
apiCert, err := b.getSystemValue(ctx, storage, storageAtlasAPICert)
if err != nil {
return nil, err
}
// Parse the cert into a key pair object
// Note: Cert value is conjoined blob, this is done for the sake of simplicity
cert, err := tls.X509KeyPair(apiCert, apiCert)
if err != nil {
return nil, err
}
HVCAUrl := os.Getenv("HVCAURL")
if len(HVCAUrl) == 0 {
HVCAUrl = "https://emea.api.hvca.globalsign.com:8443"
}
// Assumption: Constructor will throw error if atlas is misconfigured.
hclient, err := b.clientConstructor(&atlas.ClientConfig{
// Cast []byte to *string
HVCAUrl: HVCAUrl,
APIKey: atlas.String(string(apiKey)),
APISecret: atlas.String(string(apiSecret)),
Certificate: &cert,
})
if err != nil {
return nil, err
}
// Make a call with the provided parameters to tie an error to the configuration if necessary.
if err := hclient.Login(ctx); err != nil {
return nil, err
}
// Set cached version to avoid future reconstruction
b.cachedAtlasClient = hclient
return b.cachedAtlasClient, nil
}
func (b *Backend) paths() []*framework.Path {
return []*framework.Path{
pathConfigureAuthn(b),
pathListRoles(b),
pathRoles(b),
pathSign(b),
pathIssue(b),
pathFetchCA(b),
pathFetchCAChain(b),
pathRevoke(b),
pathFetchValid(b),
pathFetchListCerts(b),
pathTidy(b),
}
}
func (b *Backend) handleExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
// Invokes storage engine
out, err := req.Storage.Get(ctx, req.Path)
if err != nil {
return false, errwrap.Wrapf("existence check failed: {{err}}", err)
}
return out != nil, nil
}
const atlasHelp = `
Atlas enables you to Issue and Manage certificates using your GlobalSign Atlas Instance.
`