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_groups.go
218 lines (172 loc) · 5.57 KB
/
paths_groups.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
213
214
215
216
217
218
package main
import (
"context"
"fmt"
"net/netip"
"strings"
"time"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
type wireguardGroup struct {
Name string `json:"name" mapstructure:"name"`
Network netip.Prefix `json:"network" mapstructure:"network"`
Peers []wireguardGroupPeer `json:"peers" mapstructure:"peers"`
PersistentKeepalive int `json:"persistent_keepalive" mapstructure:"persistent_keepalive"`
TTL int `json:"ttl" mapstructure:"ttl"`
MaxTTL int `json:"max_ttl" mapstructure:"max_ttl"`
}
type wireguardGroupPeer struct {
AllowedIPs string `json:"allowed_ips"`
Hostname string `json:"hostname"`
IP string `json:"ip"`
Name string `json:"name"`
PersistentKeepalive int `json:"persistent_keepalive"`
Port int `json:"port"`
PrivateKey string `json:"private_key"`
PublicKey string `json:"public_key"`
}
func getGroup(ctx context.Context, s logical.Storage, name string) (*wireguardGroup, error) {
if name == "" {
return nil, fmt.Errorf("missing group name")
}
entry, err := s.Get(ctx, "groups/"+name)
if err != nil {
return nil, fmt.Errorf("error retrieving group: %w", err)
}
if entry == nil {
return nil, nil
}
var group wireguardGroup
if err := entry.DecodeJSON(&group); err != nil {
return nil, fmt.Errorf("error decoding group data: %w", err)
}
return &group, nil
}
func (b *wireguardBackend) updateGroupPeers(ctx context.Context, s logical.Storage, name string) (*logical.Response, error) {
group, err := getGroup(ctx, s, name)
if err != nil {
return nil, err
}
peerNames, err := s.List(ctx, "groups/"+name+"/")
if err != nil {
return logical.ErrorResponse("no peers in group"), err
}
ip := group.Network.Addr()
group.Peers = make([]wireguardGroupPeer, len(peerNames))
for i := range peerNames {
ip = ip.Next()
addr := fmt.Sprintf("%s/%d", ip, group.Network.Bits())
allow := ip.String()
if ip.Is4() {
allow += "/32"
} else {
allow += "/128"
}
p, err := getPeer(ctx, s, name, peerNames[i])
if err != nil {
return nil, err
}
peer := wireguardGroupPeer{
AllowedIPs: strings.Join(append([]string{allow}, p.AllowedIPs...), ","),
IP: addr,
Hostname: p.Hostname,
Name: p.Name,
Port: p.Port,
PrivateKey: p.PrivateKey,
PublicKey: p.PublicKey,
}
if peer.Port == 0 {
peer.PersistentKeepalive = group.PersistentKeepalive
}
group.Peers[i] = peer
}
err = b.put(ctx, s, "groups/"+name, group)
return nil, err
}
func (b *wireguardBackend) pathGroupsList(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "groups/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}
func (b *wireguardBackend) pathGroupsDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
groupname := data.Get("name").(string)
b.lock.Lock()
defer b.lock.Unlock()
if err := req.Storage.Delete(ctx, "groups/"+groupname); err != nil {
return nil, err
}
peerNames, err := req.Storage.List(ctx, "groups/"+groupname+"/")
if err != nil {
return logical.ErrorResponse("no peers in group"), err
}
for i := range peerNames {
if err := req.Storage.Delete(ctx, "groups/"+groupname+"/"+peerNames[i]); err != nil {
return nil, err
}
}
return nil, nil
}
func (b *wireguardBackend) pathGroupsRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
group, err := getGroup(ctx, req.Storage, data.Get("name").(string))
if err != nil {
return nil, err
}
if group == nil {
return nil, nil
}
var groupMap map[string]interface{}
err = mapstructure.Decode(group, &groupMap)
if err != nil {
return nil, err
}
delete(groupMap, "peers")
groupMap["network"] = group.Network.String()
return &logical.Response{
Data: groupMap,
}, nil
}
func (b *wireguardBackend) pathGroupsWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
if name == "" {
return logical.ErrorResponse("missing group name"), nil
}
group, err := getGroup(ctx, req.Storage, name)
if err != nil {
return nil, err
}
if group == nil {
group = &wireguardGroup{}
}
group.Name = name
create := (req.Operation == logical.CreateOperation)
if network, ok := data.GetOk("network"); ok {
prefix, err := netip.ParsePrefix(network.(string))
if err != nil {
return logical.ErrorResponse(fmt.Sprintf("error parsing network: %e", err)), nil
}
group.Network = prefix
} else if create {
return logical.ErrorResponse("missing network field"), nil
}
if ttl, ok := data.GetOk("ttl"); ok && ttl.(int) != 0 {
group.TTL = int((time.Duration(ttl.(int)) * time.Second).Seconds())
} else {
group.TTL = int(time.Duration(60 * time.Second).Seconds())
}
if maxTTL, ok := data.GetOk("ttl"); ok && maxTTL.(int) != 0 {
group.MaxTTL = int((time.Duration(maxTTL.(int)) * time.Second).Seconds())
} else {
group.MaxTTL = int(time.Duration(60 * time.Second).Seconds())
}
if persistentKeepalive, ok := data.GetOk("persistent_keepalive"); ok {
group.PersistentKeepalive = persistentKeepalive.(int)
}
if err := b.put(ctx, req.Storage, "groups/"+name, group); err != nil {
return nil, err
}
return b.updateGroupPeers(ctx, req.Storage, group.Name)
}