/
serve.go
79 lines (69 loc) · 2.02 KB
/
serve.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
package serve
import (
"errors"
"fmt"
"time"
"github.com/urfave/cli/v2"
action "github.com/IPA-CyberLab/kmgm/action"
"github.com/IPA-CyberLab/kmgm/action/serve"
"github.com/IPA-CyberLab/kmgm/structflags"
"github.com/IPA-CyberLab/kmgm/wcrypto"
)
var IssueHttpDefaultShutdown = 5 * time.Minute
var Command = &cli.Command{
Name: "serve",
Usage: "Start Komagome PKI HTTPS/gRPC API Server",
Aliases: []string{"s", "srv"},
Flags: append(structflags.MustPopulateFlagsFromStruct(serve.Config{}),
&cli.BoolFlag{
Name: "bootstrap",
Usage: "enable node bootstrapping via (generated) token.",
},
&cli.StringFlag{
Name: "bootstrap-token",
Usage: "enable node bootstrapping using the specified token.",
},
&cli.PathFlag{
Name: "bootstrap-token-file",
Usage: "enable node bootstrapping using the specified token file.",
},
),
Action: func(c *cli.Context) error {
env := action.GlobalEnvironment
cfg, err := serve.DefaultConfig()
if err != nil {
return err
}
if err := structflags.PopulateStructFromCliContext(cfg, c); err != nil {
return err
}
if !c.IsSet("auto-shutdown") && cfg.IssueHttp > 0 {
cfg.AutoShutdown = IssueHttpDefaultShutdown
}
if c.IsSet("bootstrap") || c.IsSet("bootstrap-token") {
if c.IsSet("bootstrap-token-file") {
return errors.New("--bootstrap-token-file can't be specified when --bootstrap,--bootstrap-token is specified.")
}
token := c.String("bootstrap-token")
if token == "" {
token, err = wcrypto.GenBase64Token(env.Randr, env.Logger)
if err != nil {
return err
}
}
cfg.Bootstrap = &serve.FixedTokenAuthProvider{
Token: token,
NotAfter: time.Now().Add(15 * time.Minute),
Logger: env.Logger,
}
} else if c.IsSet("bootstrap-token-file") {
ta, err := serve.NewTokenFileAuthProvider(
c.String("bootstrap-token-file"), env.Logger)
if err != nil {
return fmt.Errorf("Failed to initialize tokenFileAuthProvider: %w", err)
}
cfg.Bootstrap = ta
}
return serve.Run(c.Context, env, cfg)
},
}