-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
202 lines (190 loc) · 4.95 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/foundriesio/fioconfig/internal"
"github.com/urfave/cli/v2"
)
func NewApp(c *cli.Context) (*internal.App, error) {
app, err := internal.NewApp(c.StringSlice("config"), c.String("secrets-dir"), c.Bool("unsafe-handlers"), false)
if err != nil {
return nil, err
}
if c.Command.Name == "renew-cert" {
return app, nil
}
stateFile := filepath.Join(app.StorageDir, "cert-rotation.state")
handler := internal.RestoreCertRotationHandler(app, stateFile)
if handler != nil {
online := c.Command.Name != "extract"
err = handler.ResumeRotation(online)
}
return app, err
}
func extract(c *cli.Context) error {
app, err := NewApp(c)
if err != nil {
return err
}
if _, err := os.Stat(app.SecretsDir); os.IsNotExist(err) {
log.Printf("Creating secrets directory: %s", app.SecretsDir)
if err := os.Mkdir(app.SecretsDir, 0750); err != nil {
return err
}
}
log.Printf("Extracting keys from %s to %s", app.EncryptedConfig, app.SecretsDir)
if err := app.Extract(); err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Println("Encrypted config does not exist")
} else {
return err
}
}
return nil
}
func checkin(c *cli.Context) error {
app, err := NewApp(c)
if err != nil {
return err
}
log.Print("Checking in with server")
if err := app.CheckIn(); err != nil && !errors.Is(err, internal.NotModifiedError) {
return err
}
return nil
}
func daemon(c *cli.Context) error {
interval := time.Second * time.Duration(c.Int("interval"))
app, err := NewApp(c)
if err != nil {
return err
}
log.Printf("Running as daemon with interval %d seconds", c.Int("interval"))
for {
log.Print("Checking in with server")
if err := app.CheckIn(); err != nil && !errors.Is(err, internal.NotModifiedError) {
log.Println(err)
}
time.Sleep(interval)
}
}
func renewCert(c *cli.Context) error {
app, err := NewApp(c)
if err != nil {
return err
}
if c.NArg() != 1 && c.NArg() != 2 {
cli.ShowCommandHelpAndExit(c, "renew-cert", 1)
}
server := c.Args().Get(0)
stateFile := filepath.Join(app.StorageDir, "cert-rotation.state")
handler := internal.NewCertRotationHandler(app, stateFile, server)
idsStr := c.String("pkcs11-key-ids")
handler.State.PkeySlotIds = strings.Split(idsStr, ",")
idsStr = c.String("pkcs11-cert-ids")
handler.State.CertSlotIds = strings.Split(idsStr, ",")
if c.NArg() == 2 {
handler.State.CorrelationId = c.Args().Get(1)
}
log.Printf("Performing certificate renewal")
if err = handler.Rotate(); err == nil {
log.Print("Certificate rotation sequence complete")
}
return err
}
func main() {
app := &cli.App{
Name: "fioconfig",
Usage: "A daemon to handle configuration management for devices in a Foundries Factory",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "config",
Aliases: []string{"c"},
Value: cli.NewStringSlice(internal.DEF_CONFIG_ORDER...),
Usage: "Aktualizr config paths",
EnvVars: []string{"SOTA_DIR"},
},
&cli.StringFlag{
Name: "secrets-dir",
Aliases: []string{"s"},
Value: "/var/run/secrets",
Usage: "Location to extract configuration to",
EnvVars: []string{"SECRETS_DIR"},
},
&cli.BoolFlag{
Name: "unsafe-handlers",
Usage: "Enable running on-changed handlers defined outside of /usr/share/fioconfig/handlers/",
EnvVars: []string{"UNSAFE_CALLBACKS"},
},
},
Commands: []*cli.Command{
{
Name: "extract",
Usage: "Extract the current encrypted configuration to secrets directory",
Action: func(c *cli.Context) error {
return extract(c)
},
},
{
Name: "check-in",
Usage: "Check in with the server and update the local config",
Action: func(c *cli.Context) error {
return checkin(c)
},
},
{
Name: "daemon",
Usage: "Run check-in's with the server in an endless loop",
Action: func(c *cli.Context) error {
return daemon(c)
},
Flags: []cli.Flag{
&cli.IntFlag{
Name: "interval",
Aliases: []string{"i"},
Value: 300,
Usage: "Interval in seconds for checking in for updates",
EnvVars: []string{"DAEMON_INTERVAL"},
},
},
},
{
Name: "renew-cert",
HelpName: "renew-cert <EST Server> [<rotation-id>]",
Usage: "Renew device's TLS keypair used with device-gateway",
Action: func(c *cli.Context) error {
return renewCert(c)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "pkcs11-key-ids",
Value: "01,07",
Usage: "The two pkcs11 slot IDs to use for private keys",
},
&cli.StringFlag{
Name: "pkcs11-cert-ids",
Value: "03,09",
Usage: "The two pkcs11 slot IDs to use for client certificates",
},
},
},
{
Name: "version",
Usage: "Display version of this command",
Action: func(c *cli.Context) error {
fmt.Println(internal.Commit)
return nil
},
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}