-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbackend.go
More file actions
131 lines (113 loc) · 3.25 KB
/
backend.go
File metadata and controls
131 lines (113 loc) · 3.25 KB
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
/*
* Copyright 2024 Keyfactor
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package kfbackend
import (
"context"
"errors"
"fmt"
"strings"
"sync"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
const (
operationPrefixKeyfactor string = "keyfactor"
)
// Factory configures and returns backend
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
b := backend()
if err := b.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
// // Store certificates by serial number
type keyfactorBackend struct {
*framework.Backend
configLock sync.RWMutex
cachedConfig *keyfactorConfig
client *keyfactorClient
}
// keyfactorBackend defines the target API keyfactorBackend
// for Vault. It must include each path
// and the secrets it will store.
func backend() *keyfactorBackend {
var b = keyfactorBackend{}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(keyfactorHelp),
PathsSpecial: &logical.Paths{
LocalStorage: []string{},
SealWrapStorage: []string{
"config",
"role/*",
},
},
Paths: framework.PathAppend(
pathConfig(&b),
pathRoles(&b),
pathCA(&b),
pathCerts(&b),
),
Secrets: []*framework.Secret{},
BackendType: logical.TypeLogical,
Invalidate: b.invalidate,
InitializeFunc: b.Initialize,
}
return &b
}
// reset clears any client configuration for a new
// backend to be configured
func (b *keyfactorBackend) reset() {
b.configLock.RLock()
defer b.configLock.RUnlock()
b.cachedConfig = nil
b.client = nil
}
func (b *keyfactorBackend) Initialize(ctx context.Context, req *logical.InitializationRequest) error {
b.configLock.RLock()
defer b.configLock.RUnlock()
if req == nil {
return fmt.Errorf("initialization request is nil")
}
return nil
}
// invalidate clears an existing client configuration in
// the backend
func (b *keyfactorBackend) invalidate(ctx context.Context, key string) {
if key == "config" {
b.reset()
}
}
// getClient locks the backend as it configures and creates a
// a new client for the target API
func (b *keyfactorBackend) getClient(ctx context.Context, s logical.Storage) (*keyfactorClient, error) {
b.configLock.RLock()
defer b.configLock.RUnlock()
if b.client != nil {
b.Logger().Debug("closing idle connections before returning existing client")
b.client.httpClient.CloseIdleConnections()
return b.client, nil
}
// get configuration
config, err := b.fetchConfig(ctx, s)
if err != nil {
return nil, err
}
if config == nil {
return nil, errors.New("configuration is empty")
}
b.client, err = newClient(config, b)
if err != nil {
return nil, err
}
return b.client, nil
}
const keyfactorHelp = `
The Keyfactor backend is a pki service that issues and manages certificates.
`