forked from mendersoftware/deviceauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (168 loc) · 4.7 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
// Copyright 2018 Northern.tech AS
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/mendersoftware/go-lib-micro/config"
"github.com/mendersoftware/go-lib-micro/log"
"github.com/urfave/cli"
"github.com/mendersoftware/deviceauth/cmd"
dconfig "github.com/mendersoftware/deviceauth/config"
"github.com/mendersoftware/deviceauth/store/mongo"
)
func main() {
doMain(os.Args)
}
func doMain(args []string) {
var configPath string
var debug bool
app := cli.NewApp()
app.Usage = "Device Authentication Service"
app.Version = CreateVersionString()
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config",
Usage: "Configuration `FILE`. Supports JSON, TOML, YAML and HCL formatted configs.",
Destination: &configPath,
},
cli.BoolFlag{
Name: "dev",
Usage: "Use development setup",
},
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug logging",
Destination: &debug,
},
}
app.Commands = []cli.Command{
{
Name: "server",
Usage: "Run the service as a server",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "automigrate",
Usage: "Run database migrations before starting.",
},
},
Action: cmdServer,
},
{
Name: "migrate",
Usage: "Run migrations and exit",
Flags: []cli.Flag{
cli.StringFlag{
Name: "tenant",
Usage: "Tenant ID (optional).",
},
cli.BoolFlag{
Name: "list-tenants",
Usage: "List Tenant IDs. Not performing migrations.",
},
},
Action: cmdMigrate,
},
{
Name: "maintenance",
Usage: "Run maintenance operations and exit",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "decommissioning-cleanup",
Usage: "Cleanup devauth database from leftovers after failed decommissioning",
},
cli.StringFlag{
Name: "tenant",
Usage: "Tenant ID (optional).",
},
cli.BoolFlag{
Name: "dry-run",
Usage: "Do not perform any modifications and serves only as a way to inspect changes and detect if any are necessary",
},
},
Action: cmdMaintenance,
},
}
app.Action = cmdServer
app.Before = func(args *cli.Context) error {
log.Setup(debug)
err := config.FromConfigFile(configPath, dconfig.Defaults)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("error loading configuration: %s", err),
1)
}
// Enable setting config values by environment variables
config.Config.SetEnvPrefix("DEVICEAUTH")
config.Config.AutomaticEnv()
return nil
}
app.Run(args)
}
func cmdServer(args *cli.Context) error {
devSetup := args.GlobalBool("dev")
l := log.New(log.Ctx{})
if devSetup {
l.Infof("setting up development configuration")
config.Config.Set(dconfig.SettingMiddleware, EnvDev)
}
db, err := mongo.NewDataStoreMongo(
mongo.DataStoreMongoConfig{
ConnectionString: config.Config.GetString(dconfig.SettingDb),
SSL: config.Config.GetBool(dconfig.SettingDbSSL),
SSLSkipVerify: config.Config.GetBool(dconfig.SettingDbSSLSkipVerify),
Username: config.Config.GetString(dconfig.SettingDbUsername),
Password: config.Config.GetString(dconfig.SettingDbPassword),
})
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to connect to db: %v", err),
2)
}
if args.Bool("automigrate") {
db = db.WithAutomigrate().(*mongo.DataStoreMongo)
}
if config.Config.Get(dconfig.SettingTenantAdmAddr) != "" {
db = db.WithMultitenant()
}
ctx := context.Background()
err = db.Migrate(ctx, mongo.DbVersion)
if err != nil {
return cli.NewExitError(
fmt.Sprintf("failed to run migrations: %v", err),
3)
}
l.Printf("Device Authentication Service, version %s starting up",
CreateVersionString())
err = RunServer(config.Config)
if err != nil {
return cli.NewExitError(err.Error(), 4)
}
return nil
}
func cmdMigrate(args *cli.Context) error {
err := cmd.Migrate(config.Config, args.String("tenant"), args.Bool("list-tenants"))
if err != nil {
return cli.NewExitError(err, 5)
}
return nil
}
func cmdMaintenance(args *cli.Context) error {
err := cmd.Maintenance(args.Bool("decommissioning-cleanup"), args.String("tenant"), args.Bool("dry-run"))
if err != nil {
return cli.NewExitError(err, 6)
}
return nil
}