-
Notifications
You must be signed in to change notification settings - Fork 4
/
path_config.go
166 lines (152 loc) · 4.53 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
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
package wireguard
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
const (
configPath = "config"
)
func (b *backend) pathConfig() []*framework.Path {
return []*framework.Path{
{
Pattern: configPath,
Fields: map[string]*framework.FieldSchema{
"port": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "Port of the wireguard server.",
Default: 51820,
},
"public_endpoint": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Address of the wireguard server.",
Required: true,
},
"client_persistent_keepalive": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "",
Default: 25,
},
"save_config": &framework.FieldSchema{
Type: framework.TypeBool,
Description: "",
Default: true,
},
"post_up_script": &framework.FieldSchema{
Type: framework.TypeString,
Description: ".",
Default: "sysctl -w net.ipv4.ip_forward=1; iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE",
},
"post_down_script": &framework.FieldSchema{
Type: framework.TypeString,
Description: ".",
Default: "iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE",
},
"server_cidr": &framework.FieldSchema{
Type: framework.TypeString,
Description: ".",
Default: "10.29.0.1/24",
},
"webhook_address": &framework.FieldSchema{
Type: framework.TypeString,
Description: ".",
},
"webhook_secret": &framework.FieldSchema{
Type: framework.TypeString,
Description: ".",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.configCreateUpdateOperation,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.configCreateUpdateOperation,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.configReadOperation,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.configDeleteOperation,
},
},
HelpSynopsis: configHelpSynopsis,
HelpDescription: configHelpDescription,
},
}
}
func (b *backend) configCreateUpdateOperation(ctx context.Context, req *logical.Request, fieldData *framework.FieldData) (*logical.Response, error) {
err := fieldData.Validate()
if err != nil {
return nil, errors.New("Failing validation: " + err.Error())
}
// Host must be provided all the time
host := fieldData.Get("public_endpoint").(string)
if host == "" {
return nil, errors.New("public_endpoint is required")
}
// _, err = url.Parse(host)
// if err != nil {
// return nil, errors.New("host is not formatted correctly: " + err.Error())
// }
port := fieldData.Get("port").(int)
config := &config{
Port: port,
PublicEndpoint: host,
}
entry, err := logical.StorageEntryJSON(configPath, config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// Respond with a 204.
return nil, nil
}
func (b *backend) configReadOperation(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
entry, err := req.Storage.Get(ctx, configPath)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := &config{}
if err := entry.DecodeJSON(config); err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
var configMap map[string]interface{}
err = mapstructure.Decode(config, &configMap)
if err != nil {
return nil, err
}
resp := &logical.Response{
Data: configMap,
}
return resp, nil
}
func (b *backend) configDeleteOperation(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(ctx, configPath); err != nil {
return nil, err
}
return nil, nil
}
// config represent a wireguard/config
type config struct {
Port int `mapstructure:"port" `
PrivateKey string `mapstructure:"-"`
PublicKey string `mapstructure:"publickey"`
PublicEndpoint string `mapstructure:"public_endpoint"`
ClientPersistentKeepalive string `mapstructure:"client_persistent_keepalive"`
}
const configHelpSynopsis = `
Configure the Wireguard secret engine plugin.
`
const configHelpDescription = `
`