forked from martezr/vault-plugin-auth-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_config.go
126 lines (103 loc) · 3.06 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
123
124
125
126
package vsphere
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
// pathConfig returns the path configuration for CRUD operations on the backend
// configuration.
func pathConfig(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
"vauth_url": {
Type: framework.TypeString,
Description: "vAuth URL address (https://vauth.grt.local)",
},
},
ExistenceCheck: b.pathConfigExistCheck,
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathConfigCreateOrUpdate,
logical.CreateOperation: b.pathConfigCreateOrUpdate,
logical.ReadOperation: b.pathConfigRead,
},
HelpSynopsis: pathConfigSyn,
}
}
// config contains the URL of the vAuth server used for VM validation
type config struct {
VAuthURL string `json:"vauth_url"`
}
// pathConfigExistCheck checks for the existence of a configuration
func (b *backend) pathConfigExistCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
config, err := b.Config(ctx, req.Storage)
if err != nil {
return false, err
}
if config == nil {
return false, nil
}
return true, nil
}
// pathConfigCreateOrUpdate handles create and update commands to the config
func (b *backend) pathConfigCreateOrUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
cfg, err := b.Config(ctx, req.Storage)
if err != nil {
return nil, err
}
if cfg == nil {
cfg = &config{}
}
val, ok := data.GetOk("vauth_url")
if ok {
cfg.VAuthURL = val.(string)
} else if req.Operation == logical.CreateOperation {
cfg.VAuthURL = data.Get("vauth_url").(string)
}
if cfg.VAuthURL == "" {
return logical.ErrorResponse("config parameter `vauth_url` cannot be empty"), nil
}
entry, err := logical.StorageEntryJSON("config", cfg)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
return nil, nil
}
// pathConfigWrite handles create and update commands to the config
func (b *backend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := b.Config(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
resp := &logical.Response{
Data: map[string]interface{}{
"vauth_url": config.VAuthURL,
},
}
return resp, nil
}
// Config returns the configuration for this backend.
func (b *backend) Config(ctx context.Context, s logical.Storage) (*config, error) {
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, err
}
var result config
if entry != nil {
if err := entry.DecodeJSON(&result); err != nil {
return nil, fmt.Errorf("error reading configuration: %s", err)
}
return &result, nil
}
return nil, nil
}
const pathConfigSyn = `
This path allows you to configure the VMware vSphere auth provider to interact with the vAuth Identity Platform
for authenticating virtual machines.`