-
Notifications
You must be signed in to change notification settings - Fork 2
/
command_ops.go
302 lines (258 loc) · 7.63 KB
/
command_ops.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
299
300
301
302
package main
import (
"encoding/json"
"fmt"
"os"
"gopkg.in/resty.v0"
"github.com/1and1/soma/internal/help"
"github.com/1and1/soma/internal/adm"
"github.com/1and1/soma/internal/cmpl"
"github.com/1and1/soma/lib/auth"
"github.com/1and1/soma/lib/proto"
"github.com/codegangsta/cli"
)
func registerOps(app cli.App) *cli.App {
app.Commands = append(app.Commands,
[]cli.Command{
{
Name: "ops",
Usage: "SUBCOMMANDS for SOMA administration",
Subcommands: []cli.Command{
{
Name: "bootstrap",
Usage: "Bootstrap authenticate to a new installation",
Description: help.CmdOpsBootstrap,
Action: boottime(cmdOpsBootstrap),
},
{
Name: "dumptoken",
Usage: "Print the currently active token for a user",
Description: help.CmdOpsDumpToken,
Action: runtime(cmdOpsDumpToken),
},
{
Name: `shutdown`,
Usage: `Controlled shutdown of a running SOMA instance`,
Description: help.CmdOpsShutdown,
Action: runtime(cmdOpsShutdown),
},
{
Name: `repository`,
Usage: `SUBCOMMANDS for repository TreeKeeper maintenance`,
Subcommands: []cli.Command{
{
Name: `stop`,
Usage: `Stop the TreeKeeper for a specific repository`,
Description: help.CmdOpsRepoStop,
Action: runtime(cmdOpsRepoStop),
},
{
Name: `rebuild`,
Usage: `Rebuild dynamic objects in a specific repository`,
Action: runtime(cmdOpsRepoRebuild),
Description: help.CmdOpsRepoRebuild,
BashComplete: cmpl.OpsRepoRebuild,
},
{
Name: `restart`,
Usage: `Restart the TreeKeeper for a specific repository`,
Description: help.CmdOpsRepoRestart,
Action: runtime(cmdOpsRepoRestart),
},
},
},
// -> settings loglevel/opendoor/...
// -> metrics?
// -> termui?
},
},
}...,
)
return &app
}
func cmdOpsBootstrap(c *cli.Context) error {
// no command line arguments
utl.ValidateCliArgumentCount(c, 0)
var (
err error
happy bool
password, token string
kex, peer *auth.Kex
resp *resty.Response
tCred *auth.Token
)
jBytes := &[]byte{}
cipher := &[]byte{}
plain := &[]byte{}
fmt.Println(`
Welcome to SOMA!
This dialogue will guide you to set up the system's root account of
your new instance.
As first step, enter the root password you want to set.
`)
password_read:
password = adm.ReadVerified(`password`)
if happy, err = adm.EvaluatePassword(3, password, `root`, `soma`); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if !happy {
password = ""
goto password_read
}
fmt.Println(`
Very good. Now enter the bootstrap token printed by somadbctl at the
end of the schema installation.
`)
token = adm.ReadVerified(`password`)
fmt.Println(`
Alright. Let's sully that pristine database. Here we go! `)
fmt.Printf("\nGenerating keypair: ")
kex = auth.NewKex()
fmt.Println(adm.GREEN+adm.SUCCESS+adm.CLEAR, ` OK`)
fmt.Printf(`Initiating key exchange: `)
if resp, err = Client.R().SetBody(kex).Post(`/authenticate/`); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
if resp.StatusCode() != 200 {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, resp.Status())
os.Exit(1)
}
peer = &auth.Kex{}
if err = json.Unmarshal(resp.Body(), peer); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
kex.SetPeerKey(peer.PublicKey())
if err = kex.SetRequestUUID(peer.Request.String()); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
fmt.Println(adm.GREEN+adm.SUCCESS+adm.CLEAR, ` OK`)
tCred = &auth.Token{
UserName: `root`,
Password: password,
Token: token,
}
fmt.Printf(`Sending bootstrap request: `)
if *jBytes, err = json.Marshal(tCred); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
kex.SetTimeUTC()
if err = kex.EncryptAndEncode(jBytes, cipher); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
if resp, err = Client.R().
SetHeader(`Content-Type`, `application/octet-stream`).
SetBody(*cipher).
Put(fmt.Sprintf(
"/authenticate/bootstrap/%s", kex.Request.String())); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
if resp.StatusCode() != 200 {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR+` FAILED`, resp.Status())
os.Exit(1)
}
b := resp.Body()
if err = kex.DecodeAndDecrypt(&b, plain); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
if err = json.Unmarshal(*plain, tCred); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
fmt.Println(adm.GREEN+adm.SUCCESS+adm.CLEAR, ` OK`)
fmt.Printf(`Validating received token: `)
if resp, err = Client.R().
SetBasicAuth(`root`, tCred.Token).
Get(`/authenticate/validate/`); err != nil || resp.StatusCode() != 204 {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR+` FAILED`, resp.String())
os.Exit(1)
}
fmt.Printf(`Writing token to local cache: `)
if err = store.SaveToken(
`root`,
tCred.ValidFrom,
tCred.ExpiresAt,
tCred.Token,
); err != nil {
fmt.Fprintln(os.Stderr, adm.RED+adm.FAILURE+adm.CLEAR, err)
os.Exit(1)
}
fmt.Println(adm.GREEN+adm.SUCCESS+adm.CLEAR, ` OK`)
fmt.Println(`
All done. Thank you for flying with SOMA.
Suggested next steps:
- create system_admin permission
- create your team
- create your user
- grant system_admin to your user
- activate your user
- switch to using your user instead of root
`)
return nil
}
func cmdOpsDumpToken(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
// this is running wrapped in runtime(), there _is_ a token
token, _ := store.GetActiveToken(Cfg.Auth.User)
fmt.Println(token)
store.Close()
return nil
}
func cmdOpsRepoStop(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
req := proto.NewSystemOperationRequest()
req.SystemOperation.Request = `stop_repository`
return cmdOpsRepo(c, req)
}
func cmdOpsRepoRestart(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 1)
req := proto.NewSystemOperationRequest()
req.SystemOperation.Request = `restart_repository`
return cmdOpsRepo(c, req)
}
func cmdOpsRepoRebuild(c *cli.Context) error {
opts := utl.ParseVariadicArguments(
[]string{}, // more than once
[]string{`level`}, // at most once
[]string{`level`}, // at least once
c.Args().Tail())
// validate requested rebuild level
switch opts[`level`][0] {
case `checks`, `instances`:
default:
return fmt.Errorf(`Only rebuild levels 'checks' and 'instances' are supported`)
}
req := proto.NewSystemOperationRequest()
req.SystemOperation.Request = `rebuild_repository`
req.SystemOperation.RebuildLevel = opts[`level`][0]
return cmdOpsRepo(c, req)
}
func cmdOpsRepo(c *cli.Context, req proto.Request) error {
// lookup requested repository
repoId := utl.TryGetRepositoryByUUIDOrName(
Client, c.Args().First())
req.SystemOperation.RepositoryId = repoId
if resp, err := adm.PostReqBody(req, `/system/`); err != nil {
return err
} else {
return adm.FormatOut(c, resp, `system`)
}
}
func cmdOpsShutdown(c *cli.Context) error {
utl.ValidateCliArgumentCount(c, 0)
req := proto.NewSystemOperationRequest()
req.SystemOperation.Request = `shutdown`
if resp, err := adm.PostReqBody(req, `/system/`); err != nil {
return err
} else {
return adm.FormatOut(c, resp, `system`)
}
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix