This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
paths_peers.go
212 lines (165 loc) · 5.37 KB
/
paths_peers.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"bytes"
"context"
"fmt"
"net/netip"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
type wireguardPeer struct {
AllowedIPs []string `json:"allowed_ips" mapstructure:"allowed_ips"`
Hostname string `json:"hostname" mapstructure:"hostname"`
Name string `json:"name" mapstructure:"name"`
Port int `json:"port" mapstructure:"port"`
PrivateKey string `json:"private_key" mapstructure:"private_key"`
PublicKey string `json:"public_key" mapstructure:"public_key"`
}
func getPeer(ctx context.Context, s logical.Storage, groupname, name string) (*wireguardPeer, error) {
if groupname == "" {
return nil, fmt.Errorf("missing group name")
}
if name == "" {
return nil, fmt.Errorf("missing name")
}
entry, err := s.Get(ctx, "groups/"+groupname+"/"+name)
if err != nil {
return nil, fmt.Errorf("error retrieving peer: %w", err)
}
if entry == nil {
return nil, nil
}
var peer wireguardPeer
if err := entry.DecodeJSON(&peer); err != nil {
return nil, fmt.Errorf("error decoding peer data: %w", err)
}
return &peer, nil
}
func (b *wireguardBackend) pathPeersList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "groups/"+data.Get("name").(string)+"/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func (b *wireguardBackend) pathPeersDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
groupname := data.Get("group_name").(string)
b.lock.Lock()
if err := req.Storage.Delete(ctx, "groups/"+groupname+"/"+data.Get("name").(string)); err != nil {
b.lock.Unlock()
return nil, err
}
b.lock.Unlock()
return b.updateGroupPeers(ctx, req.Storage, groupname)
}
func (b *wireguardBackend) pathPeersRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
peer, err := getPeer(ctx, req.Storage, data.Get("group_name").(string), data.Get("name").(string))
if err != nil {
return nil, err
}
if peer == nil {
return nil, nil
}
var groupMap map[string]interface{}
err = mapstructure.Decode(peer, &groupMap)
if err != nil {
return nil, err
}
return &logical.Response{
Data: groupMap,
}, nil
}
func (b *wireguardBackend) pathPeersWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
groupname := data.Get("group_name").(string)
if groupname == "" {
return logical.ErrorResponse("missing group name"), nil
}
name := data.Get("name").(string)
if name == "" {
return logical.ErrorResponse("missing name"), nil
}
group, err := getGroup(ctx, req.Storage, groupname)
if err != nil || group == nil {
return logical.ErrorResponse("missing group"), err
}
peer, err := getPeer(ctx, req.Storage, groupname, name)
if err != nil {
return logical.ErrorResponse("missing peer"), err
}
if peer == nil {
peer = &wireguardPeer{}
}
peer.Name = name
if allowedIPs, ok := data.GetOk("allowed_ips"); ok {
prefixes := []string{}
for _, ip := range allowedIPs.([]string) {
prefix, err := netip.ParsePrefix(ip)
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("error parsing allowed_ips %s: %e", ip, err)), err
}
prefixes = append(prefixes, prefix.String())
}
peer.AllowedIPs = prefixes
}
if hostname, ok := data.GetOk("hostname"); ok && hostname != "" {
peer.Hostname = hostname.(string)
} else {
peer.Hostname = name
}
if port, ok := data.GetOk("port"); ok {
peer.Port = port.(int)
}
if privateKey, ok := data.GetOk("private_key"); ok {
key, err := wgtypes.ParseKey(privateKey.(string))
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("error parsing private_key: %e", err)), err
}
peer.PrivateKey = privateKey.(string)
peer.PublicKey = key.PublicKey().String()
}
if publicKey, ok := data.GetOk("public_key"); ok {
peer.PublicKey = publicKey.(string)
}
if peer.PrivateKey == "" && peer.PublicKey == "" {
key, err := wgtypes.GeneratePrivateKey()
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("error generating private_key: %e", err)), err
}
peer.PrivateKey = key.String()
peer.PublicKey = key.PublicKey().String()
}
if err := b.put(ctx, req.Storage, "groups/"+groupname+"/"+name, peer); err != nil {
return nil, err
}
return b.updateGroupPeers(ctx, req.Storage, groupname)
}
func (b *wireguardBackend) pathPeersWGQuickRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
groupname := data.Get("group_name").(string)
if groupname == "" {
return logical.ErrorResponse("missing group name"), nil
}
name := data.Get("name").(string)
if name == "" {
return logical.ErrorResponse("missing name"), nil
}
group, err := getGroup(ctx, req.Storage, groupname)
if err != nil || group == nil {
return logical.ErrorResponse("unable to find group"), nil
}
var config bytes.Buffer
if err := wgQuickTemplate.Execute(&config, wgQuickValues{
Group: group,
Name: name,
}); err != nil {
return logical.ErrorResponse("error rendering config: %w", err), nil
}
return &logical.Response{
Data: map[string]interface{}{
"config": config.String(),
"max_ttl": group.MaxTTL,
"ttl": group.TTL,
},
}, nil
}