-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_config.go
122 lines (108 loc) · 3.21 KB
/
path_config.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
package artifactory
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathConfig(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
"address": {
Type: framework.TypeString,
Description: "Artifactory server address",
},
"api_key": {
Type: framework.TypeString,
Description: "API Key to use to create access tokens",
},
"username": {
Type: framework.TypeString,
Description: "Username which will be used to create access tokens",
},
"password": {
Type: framework.TypeString,
Description: "Password of the user which will be used to create access tokens",
},
"tls_verify": {
Type: framework.TypeBool,
Description: "Disable TLS verification of Artifactory server",
Default: true,
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathConfigRead,
logical.UpdateOperation: b.pathConfigWrite,
},
HelpSynopsis: pathConfigRootHelpSyn,
}
}
func (b *backend) readConfig(ctx context.Context, storage logical.Storage) (*accessConfig, error) {
entry, err := storage.Get(ctx, "config")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
conf := &accessConfig{}
if err := entry.DecodeJSON(conf); err != nil {
return nil, fmt.Errorf("error reading artifactory configuration: %v", err)
}
return conf, nil
}
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
conf, err := b.readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
return nil, fmt.Errorf("No artifactory configuration found")
}
return &logical.Response{
Data: map[string]interface{}{
"address": conf.Address,
},
}, nil
}
func (b *backend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config := accessConfig{
Address: data.Get("address").(string),
ApiKey: data.Get("api_key").(string),
Username: data.Get("username").(string),
Password: data.Get("password").(string),
TlsVerify: data.Get("tls_verify").(bool),
}
if config.Address == "" {
return logical.ErrorResponse("address must be set"), nil
}
if config.ApiKey != "" && config.Username != "" {
return logical.ErrorResponse("provide either api_key or username, not both"), nil
}
if config.Username != "" {
if config.Password == "" {
return logical.ErrorResponse("must provide password with username"), nil
}
} else if config.ApiKey == "" {
return logical.ErrorResponse("api_key must be set"), nil
}
entry, err := logical.StorageEntryJSON("config", config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
type accessConfig struct {
Address string `json:"address"`
ApiKey string `json:"api_key"`
Username string `json:"username"`
Password string `json:"password"`
TlsVerify bool `json:"tls_verify"`
}
const pathConfigRootHelpSyn = `
Configure the address and API key to access the Artifactory server.
`