-
Notifications
You must be signed in to change notification settings - Fork 0
/
border.go
298 lines (243 loc) · 7.02 KB
/
border.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"context"
"errors"
"flag"
"fmt"
"net"
"os"
"os/signal"
"time"
"github.com/erikh/border/pkg/api"
"github.com/erikh/border/pkg/config"
"github.com/erikh/border/pkg/controlclient"
"github.com/erikh/border/pkg/josekit"
"github.com/erikh/border/pkg/launcher"
"github.com/erikh/go-hashchain"
"github.com/ghodss/yaml"
"github.com/go-jose/go-jose/v3"
"github.com/peterbourgon/ff/ffcli"
"golang.org/x/sys/unix"
)
var (
appFlagSet = flag.NewFlagSet("border", flag.ExitOnError)
serveFlagSet = flag.NewFlagSet("border serve", flag.ExitOnError)
clientFlagSet = flag.NewFlagSet("border client", flag.ExitOnError)
keyGenerateFlagSet = flag.NewFlagSet("border keygenerate", flag.ExitOnError)
configFile = appFlagSet.String("c", "/etc/border/config.yaml", "configuration file path")
clientConfigFile = appFlagSet.String("client", "/etc/border/client.yaml", "client configuration file path")
)
func main() {
app := &ffcli.Command{
Usage: "border [flags] <subcommand> [args]",
FlagSet: appFlagSet,
Subcommands: []*ffcli.Command{
{
Name: "serve",
Usage: "border serve",
ShortHelp: "Start the border service",
FlagSet: serveFlagSet,
Exec: serve,
},
{
Name: "client",
Usage: "border client --help",
ShortHelp: "Talk to the border service",
FlagSet: clientFlagSet,
Subcommands: []*ffcli.Command{
{
Name: "authcheck",
Usage: "border client authcheck",
ShortHelp: "Validates that auth is working without doing anything else",
Exec: clientAuthCheck,
},
{
Name: "addpeer",
Usage: "border client addpeer <YAML JWK keyfile> <ip (repeating)>",
ShortHelp: "Add a peer to the quorum. Use keygenerate to generate a keyfile.",
Exec: clientAddPeer,
},
{
Name: "updateconfig",
Usage: "border client updateconfig <config file>",
ShortHelp: "Update the configuration remotely",
Exec: clientUpdateConfig,
},
{
Name: "reloadconfig",
Usage: "border client reloadconfig",
ShortHelp: "Force a reload of the configuration",
Exec: clientReloadConfig,
},
{
Name: "identifypublisher",
Usage: "border client identifypublisher",
ShortHelp: "Produce the name of the current elected publisher peer",
Exec: clientIdentifyPublisher,
},
},
},
{
Name: "keygenerate",
Usage: "border keygenerate <name>",
ShortHelp: "Generate a new authentication key for use in border",
FlagSet: keyGenerateFlagSet,
Exec: keyGenerate,
},
},
}
if len(os.Args) == 1 {
fmt.Fprintln(os.Stderr, ffcli.DefaultUsageFunc(app))
}
if err := app.Run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func serve(args []string) error {
if len(args) != 1 {
return fmt.Errorf("Please provide a peer name to serve from")
}
c := config.New(hashchain.New(nil))
c, err := c.FromDisk(*configFile, config.LoadYAML)
if err != nil {
return err
}
// save and reload to initialize our internal JSON configuration. This is
// used when exchanging configuration between peers, so this is a bootstrap
// and only needs to be performed once.
if err := c.Save(); err != nil {
return err
}
if err := c.Reload(); err != nil {
return err
}
<-c.ReloadChan()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
server := &launcher.Server{}
if err := server.Launch(args[0], c); err != nil {
return err
}
sigChan := make(chan os.Signal, 1)
go func() {
<-sigChan
sw := c.ShutdownWait
if sw == 0 {
sw = time.Second
}
innerCtx, innerCancel := context.WithTimeout(ctx, sw)
defer cancel()
defer innerCancel()
server.Shutdown(innerCtx) // nolint:errcheck
}()
signal.Notify(sigChan, unix.SIGTERM, unix.SIGINT)
<-ctx.Done()
fmt.Fprintln(os.Stderr, "terminating")
return nil
}
func clientAuthCheck(args []string) error {
client, err := controlclient.Load(*clientConfigFile)
if err != nil {
return fmt.Errorf("Could not load client configuration at %q: %w", *clientConfigFile, err)
}
authCheck := make(api.AuthCheck, api.NonceSize)
if _, err := client.SendRequest(&authCheck, false); err != nil {
return fmt.Errorf("Authentication failed: %w", err)
}
fmt.Println("OK")
return nil
}
func clientAddPeer(args []string) error {
client, err := controlclient.Load(*clientConfigFile)
if err != nil {
return fmt.Errorf("Could not load client configuration at %q: %w", *clientConfigFile, err)
}
if len(args) < 2 {
return errors.New("Please provide a key file and list of peer IPs")
}
var jwk jose.JSONWebKey
byt, err := os.ReadFile(args[0])
if err != nil {
return fmt.Errorf("Could not read key file: %w", err)
}
if err := yaml.Unmarshal(byt, &jwk); err != nil {
return fmt.Errorf("Could not unmarshal JWK YAML: %w", err)
}
ips := []net.IP{}
for _, ipstr := range args[1:] {
ip := net.ParseIP(ipstr)
if ip == nil {
return fmt.Errorf("IP %q is not a valid IP address", ipstr)
}
ips = append(ips, ip)
}
peer := &config.Peer{
Key: &jwk,
IPs: ips,
}
req := &api.PeerRegistrationRequest{
Peer: peer,
}
_, err = client.Exchange(req, false)
return err
}
func clientReloadConfig(args []string) error {
client, err := controlclient.Load(*clientConfigFile)
if err != nil {
return fmt.Errorf("Could not load client configuration at %q: %w", *clientConfigFile, err)
}
if len(args) != 0 {
return errors.New("Invalid Arguments")
}
if _, err := client.Exchange(&api.ConfigReloadRequest{}, false); err != nil {
return fmt.Errorf("Error updating configuration: %w", err)
}
fmt.Println("OK")
return nil
}
func clientUpdateConfig(args []string) error {
client, err := controlclient.Load(*clientConfigFile)
if err != nil {
return fmt.Errorf("Could not load client configuration at %q: %w", *clientConfigFile, err)
}
if len(args) != 1 {
return errors.New("Please provide a config file to load")
}
c := config.New(hashchain.New(nil))
c, err = c.FromDisk(args[0], config.LoadYAML)
if err != nil {
return err
}
if _, err := client.Exchange(&api.ConfigUpdateRequest{Config: c}, false); err != nil {
return fmt.Errorf("Error updating configuration: %w", err)
}
fmt.Println("OK")
return nil
}
func clientIdentifyPublisher(args []string) error {
client, err := controlclient.Load(*clientConfigFile)
if err != nil {
return fmt.Errorf("Could not load client configuration at %q: %w", *clientConfigFile, err)
}
resp, err := client.Exchange(&api.IdentifyPublisherRequest{}, false)
if err != nil {
return fmt.Errorf("Error identifying publisher: %w", err)
}
fmt.Println("Publisher:", resp.(*api.IdentifyPublisherResponse).Publisher)
return nil
}
func keyGenerate(args []string) error {
if len(args) != 1 {
return errors.New("Please provide a key id as an argument")
}
key, err := josekit.MakeKey(args[0])
if err != nil {
return err
}
byt, err := yaml.Marshal(key)
if err != nil {
return err
}
fmt.Println(string(byt))
return nil
}