This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.go
86 lines (68 loc) · 1.79 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
package artifactory
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
type backend struct {
*framework.Backend
configMutex sync.RWMutex
rolesMutex sync.RWMutex
httpClient *http.Client
}
// Factory configures and returns Artifactory secrets backends.
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
if conf == nil {
return nil, fmt.Errorf("configuration passed into backend is nil")
}
b, err := Backend(conf)
if err != nil {
return nil, err
}
if err := b.Backend.Setup(ctx, conf); err != nil {
return nil, err
}
return b, nil
}
func Backend(_ *logical.BackendConfig) (*backend, error) {
b := &backend{
httpClient: http.DefaultClient,
}
b.Backend = &framework.Backend{
Help: strings.TrimSpace(artifactoryHelp),
PathsSpecial: &logical.Paths{
SealWrapStorage: []string{"config/admin"},
},
BackendType: logical.TypeLogical,
}
b.Backend.Secrets = append(b.Backend.Secrets, b.secretAccessToken())
b.Backend.Paths = append(b.Backend.Paths,
b.pathListRoles(),
b.pathRoles(),
b.pathTokenCreate(),
b.pathConfig())
return b, nil
}
// fetchAdminConfiguration will return nil,nil if there's no configuration
func (b *backend) fetchAdminConfiguration(ctx context.Context, storage logical.Storage) (*adminConfiguration, error) {
var config adminConfiguration
// Read in the backend configuration
entry, err := storage.Get(ctx, "config/admin")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
if err := entry.DecodeJSON(&config); err != nil {
return nil, err
}
return &config, nil
}
const artifactoryHelp = `
The Artifactory secrets backend provides Artifactory access tokens based on configured roles.
`