/
connection_create.go
252 lines (193 loc) · 5.53 KB
/
connection_create.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package command
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
structs "github.com/seashell/drago/drago/structs"
cli "github.com/seashell/drago/pkg/cli"
"github.com/spf13/pflag"
)
// ConnectionCreateCommand :
type ConnectionCreateCommand struct {
UI cli.UI
Command
// Parsed flags
json bool
persistentKeepalive int
allowAll bool
}
func (c *ConnectionCreateCommand) FlagSet() *pflag.FlagSet {
flags := c.Command.FlagSet(c.Name())
flags.Usage = func() { c.UI.Output("\n" + c.Help() + "\n") }
// General options
flags.BoolVar(&c.json, "json", false, "")
flags.IntVar(&c.persistentKeepalive, "keepalive", 0, "")
flags.BoolVar(&c.allowAll, "allow-all", false, "")
return flags
}
// Name :
func (c *ConnectionCreateCommand) Name() string {
return "connection create"
}
// Synopsis :
func (c *ConnectionCreateCommand) Synopsis() string {
return "Create a new connection"
}
// Run :
func (c *ConnectionCreateCommand) Run(ctx context.Context, args []string) int {
flags := c.FlagSet()
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if len(args) != 3 {
c.UI.Error("This command takes three arguments: <src_node_id> <dst_node_id> <network>")
c.UI.Error(`For additional help, try 'drago connection create --help'`)
return 1
}
networkID := ""
networkAddressRange := ""
nodeIDs := []string{args[0], args[1]}
networkName := args[2]
// Get the HTTP client
api, err := c.Command.APIClient()
if err != nil {
c.UI.Error(fmt.Sprintf("Error setting up API client: %s", err))
return 1
}
// Resolve network name
networks, err := api.Networks().List()
if err != nil {
c.UI.Error(fmt.Sprintf("Error getting networks: %s", err))
return 1
}
for _, n := range networks {
if n.Name == networkName {
networkID = n.ID
networkAddressRange = n.AddressRange
break
}
}
if networkID == "" {
c.UI.Error("Error: network not found")
return 1
}
conn := &structs.Connection{
NetworkID: networkID,
PersistentKeepalive: &c.persistentKeepalive,
PeerSettings: []*structs.PeerSettings{},
}
for idx, nodeID := range nodeIDs {
filters := map[string][]string{
"node": {nodeID},
}
interfaces, err := api.Interfaces().List(filters)
if err != nil {
c.UI.Error(fmt.Sprintf("Error getting node interfaces: %s", err))
return 1
}
// Find interface in node which is connected to the target network,
// and add it to the connection struct together with default settings.
for _, iface := range interfaces {
if iface.NetworkID == networkID {
conn.PeerSettings = append(conn.PeerSettings, &structs.PeerSettings{
InterfaceID: iface.ID,
RoutingRules: &structs.RoutingRules{
AllowedIPs: []string{},
},
})
// Allow all traffic if allowAll is set
if c.allowAll {
conn.PeerSettingsByInterfaceID(iface.ID).RoutingRules.AllowedIPs = []string{networkAddressRange}
}
break
}
}
if len(conn.PeerSettings) < idx+1 {
c.UI.Error(fmt.Sprintf("Error: node %s does not have any interface in network %s", nodeID, networkID))
return 1
}
}
err = api.Connections().Create(conn)
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating connection: %s", err))
return 1
}
//c.UI.Output(c.formatConnection(connection))
return 0
}
// Help :
func (c *ConnectionCreateCommand) Help() string {
h := `
Usage: drago connection create <src_node_id> <dst_node_id> <network> [options]
Create is used to create a new connection between two nodes that have interfaces in the same network.
If ACLs are enabled, this option requires a token with the 'connection:write' capability.
General Options:
` + GlobalOptions() + `
ACL Token Create Options:
--json
Enable JSON output.
--allow-all
Enables routing of all traffic in this connection.
--keepalive=<seconds>
Time interval between persistent keepalive packets. Defaults to 0, which disables the feature.
`
return strings.TrimSpace(h)
}
func (c *ConnectionCreateCommand) formatConnection(connection *structs.Connection) string {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"id": connection.ID,
"networkId": connection.NetworkID,
"persistentKeepalive": connection.PersistentKeepalive,
"peerSettings": c.formatPeerSettings(connection.PeerSettings),
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}
func (c *ConnectionCreateCommand) formatPeerSettings(peerSettings []*structs.PeerSettings) string {
var b bytes.Buffer
for _, peer := range peerSettings {
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"nodeId": peer.NodeID,
"interfaceID": peer.InterfaceID,
"routingRules": c.formatRoutingRules(peer.RoutingRules),
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}
func (c *ConnectionCreateCommand) formatRoutingRules(rules *structs.RoutingRules) string {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
formatted := map[string]interface{}{
"allowedIps": rules.AllowedIPs,
}
if err := enc.Encode(formatted); err != nil {
c.UI.Error(fmt.Sprintf("Error formatting JSON output: %s", err))
}
s := b.String()
if c.json {
return s
}
return cleanJSONString(s)
}