• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / deviceauth / 1284672998

09 May 2024 01:19PM UTC coverage: 81.658% (-1.1%) from 82.796%
1284672998

Pull #715

gitlab-ci

alfrunes
test(acceptance/os): :broom: Remove unused fixtures

Signed-off-by: Alf-Rune Siqveland <alf.rune@northern.tech>
Pull Request #715: Acceptance test fixup

4808 of 5888 relevant lines covered (81.66%)

51.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.99
/main.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package main
15

16
import (
17
        "context"
18
        "fmt"
19
        "os"
20
        "time"
21

22
        "github.com/mendersoftware/go-lib-micro/config"
23
        "github.com/mendersoftware/go-lib-micro/log"
24
        "github.com/pkg/errors"
25
        "github.com/urfave/cli"
26

27
        cinv "github.com/mendersoftware/deviceauth/client/inventory"
28
        "github.com/mendersoftware/deviceauth/client/orchestrator"
29
        "github.com/mendersoftware/deviceauth/client/tenant"
30
        "github.com/mendersoftware/deviceauth/cmd"
31
        dconfig "github.com/mendersoftware/deviceauth/config"
32
        "github.com/mendersoftware/deviceauth/store/mongo"
33
)
34

35
const (
36
        cliDefaultRateLimit = 50
37
)
38

39
func main() {
×
40
        doMain(os.Args)
×
41
}
×
42

43
func doMain(args []string) {
1✔
44
        var configPath string
1✔
45
        var debug bool
1✔
46

1✔
47
        app := cli.NewApp()
1✔
48
        app.Usage = "Device Authentication Service"
1✔
49

1✔
50
        app.Flags = []cli.Flag{
1✔
51
                cli.StringFlag{
1✔
52
                        Name: "config",
1✔
53
                        Usage: "Configuration `FILE`." +
1✔
54
                                " Supports JSON, TOML, YAML and HCL formatted configs.",
1✔
55
                        Destination: &configPath,
1✔
56
                },
1✔
57
                cli.BoolFlag{
1✔
58
                        Name:        "debug",
1✔
59
                        Usage:       "Enable debug logging",
1✔
60
                        Destination: &debug,
1✔
61
                },
1✔
62
        }
1✔
63

1✔
64
        app.Commands = []cli.Command{
1✔
65
                {
1✔
66
                        Name:  "server",
1✔
67
                        Usage: "Run the service as a server",
1✔
68
                        Flags: []cli.Flag{
1✔
69
                                cli.BoolFlag{
1✔
70
                                        Name:  "automigrate",
1✔
71
                                        Usage: "Run database migrations before starting.",
1✔
72
                                },
1✔
73
                        },
1✔
74

1✔
75
                        Action: cmdServer,
1✔
76
                },
1✔
77
                {
1✔
78
                        Name:  "migrate",
1✔
79
                        Usage: "Run migrations and exit",
1✔
80
                        Flags: []cli.Flag{
1✔
81
                                cli.StringFlag{
1✔
82
                                        Name:  "tenant",
1✔
83
                                        Usage: "Tenant ID (optional).",
1✔
84
                                },
1✔
85
                                cli.BoolFlag{
1✔
86
                                        Name:  "list-tenants",
1✔
87
                                        Usage: "List Tenant IDs. Not performing migrations.",
1✔
88
                                },
1✔
89
                        },
1✔
90

1✔
91
                        Action: cmdMigrate,
1✔
92
                },
1✔
93
                {
1✔
94
                        Name:  "propagate-inventory-statuses",
1✔
95
                        Usage: "Push device statuses to inventory",
1✔
96
                        Flags: []cli.Flag{
1✔
97
                                cli.StringFlag{
1✔
98
                                        Name:  "tenant_id",
1✔
99
                                        Usage: "Tenant ID (optional) - propagate for just a single tenant.",
1✔
100
                                },
1✔
101
                                cli.StringFlag{
1✔
102
                                        Name:  "force-set-migration",
1✔
103
                                        Usage: "Migration version to be stored in migration_info collection.",
1✔
104
                                },
1✔
105
                                cli.BoolFlag{
1✔
106
                                        Name: "dry-run",
1✔
107
                                        Usage: "Do not perform any inventory modifications," +
1✔
108
                                                " just scan and print devices.",
1✔
109
                                },
1✔
110
                        },
1✔
111

1✔
112
                        Action: cmdPropagateStatusesInventory,
1✔
113
                },
1✔
114
                {
1✔
115
                        Name:  "propagate-inventory-id-data",
1✔
116
                        Usage: "Push device id_data to inventory",
1✔
117
                        Flags: []cli.Flag{
1✔
118
                                cli.StringFlag{
1✔
119
                                        Name:  "tenant_id",
1✔
120
                                        Usage: "Tenant ID (optional) - propagate for just a single tenant.",
1✔
121
                                },
1✔
122
                                cli.BoolFlag{
1✔
123
                                        Name: "dry-run",
1✔
124
                                        Usage: "Do not perform any inventory modifications," +
1✔
125
                                                " just scan and print devices.",
1✔
126
                                },
1✔
127
                        },
1✔
128

1✔
129
                        Action: cmdPropagateIdDataInventory,
1✔
130
                },
1✔
131
                {
1✔
132
                        Name:  "propagate-reporting",
1✔
133
                        Usage: "Trigger a reindex of all the devices in the reporting services ",
1✔
134
                        Flags: []cli.Flag{
1✔
135
                                cli.StringFlag{
1✔
136
                                        Name:  "tenant_id",
1✔
137
                                        Usage: "Tenant ID (optional) - propagate for just a single tenant.",
1✔
138
                                },
1✔
139
                                cli.UintFlag{
1✔
140
                                        Name:  "rate-limit",
1✔
141
                                        Usage: "`N`umber of reindexing batch requests per second.",
1✔
142
                                        Value: cliDefaultRateLimit,
1✔
143
                                },
1✔
144
                                cli.BoolFlag{
1✔
145
                                        Name: "dry-run",
1✔
146
                                        Usage: "Do not perform any inventory modifications," +
1✔
147
                                                " just scan and print devices.",
1✔
148
                                },
1✔
149
                        },
1✔
150

1✔
151
                        Action: cmdPropagateReporting,
1✔
152
                },
1✔
153
                {
1✔
154
                        Name:  "maintenance",
1✔
155
                        Usage: "Run maintenance operations and exit",
1✔
156
                        Flags: []cli.Flag{
1✔
157
                                cli.BoolFlag{
1✔
158
                                        Name:  "decommissioning-cleanup",
1✔
159
                                        Usage: "Cleanup devauth database from leftovers after failed decommissioning",
1✔
160
                                },
1✔
161
                                cli.StringFlag{
1✔
162
                                        Name:  "tenant",
1✔
163
                                        Usage: "Tenant ID (optional).",
1✔
164
                                },
1✔
165
                                cli.BoolFlag{
1✔
166
                                        Name: "dry-run",
1✔
167
                                        Usage: "Do not perform any modifications and serves" +
1✔
168
                                                " only as a way to inspect changes and detect if any are necessary",
1✔
169
                                },
1✔
170
                        },
1✔
171

1✔
172
                        Action: cmdMaintenance,
1✔
173
                }, {
1✔
174
                        Name:  "check-device-limits",
1✔
175
                        Usage: "Warn users if user is approaching device limit",
1✔
176
                        Description: "Loops through all tenant databases and " +
1✔
177
                                "checks if the number of devices is over a " +
1✔
178
                                "threshold of the allowed limit and sends an " +
1✔
179
                                "email asking the user to upgrade or decomission" +
1✔
180
                                "unused devices.",
1✔
181
                        Flags: []cli.Flag{
1✔
182
                                cli.Float64Flag{
1✔
183
                                        Name:  "threshold, t",
1✔
184
                                        Value: 90.0,
1✔
185
                                        Usage: "Threshold in percent (%) of " +
1✔
186
                                                "device limit that trigger " +
1✔
187
                                                "email event.",
1✔
188
                                },
1✔
189
                        },
1✔
190
                        Action: cmdCheckDeviceLimits,
1✔
191
                },
1✔
192
        }
1✔
193

1✔
194
        app.Action = cmdServer
1✔
195
        app.Before = func(args *cli.Context) error {
2✔
196
                log.Setup(debug)
1✔
197

1✔
198
                err := config.FromConfigFile(configPath, dconfig.Defaults)
1✔
199
                if err != nil {
1✔
200
                        return cli.NewExitError(
×
201
                                fmt.Sprintf("error loading configuration: %s", err),
×
202
                                1)
×
203
                }
×
204

205
                // Enable setting config values by environment variables
206
                config.Config.SetEnvPrefix("DEVICEAUTH")
1✔
207
                config.Config.AutomaticEnv()
1✔
208

1✔
209
                return nil
1✔
210
        }
211

212
        _ = app.Run(args)
1✔
213
}
214

215
func cmdServer(args *cli.Context) error {
1✔
216
        l := log.New(log.Ctx{})
1✔
217

1✔
218
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
1✔
219
        if err != nil {
1✔
220
                return cli.NewExitError(
×
221
                        fmt.Sprintf("failed to connect to db: %v", err),
×
222
                        2)
×
223
        }
×
224

225
        if args.Bool("automigrate") {
2✔
226
                db = db.WithAutomigrate().(*mongo.DataStoreMongo)
1✔
227
        }
1✔
228

229
        if config.Config.Get(dconfig.SettingTenantAdmAddr) != "" {
1✔
230
                db = db.WithMultitenant()
×
231
        }
×
232

233
        ctx := context.Background()
1✔
234
        err = db.Migrate(ctx, mongo.DbVersion)
1✔
235
        if err != nil {
1✔
236
                return cli.NewExitError(
×
237
                        fmt.Sprintf("failed to run migrations: %v", err),
×
238
                        3)
×
239
        }
×
240

241
        l.Print("Device Authentication Service starting up")
1✔
242

1✔
243
        err = RunServer(config.Config)
1✔
244
        if err != nil {
1✔
245
                return cli.NewExitError(err.Error(), 4)
×
246
        }
×
247

248
        return nil
×
249
}
250

251
func cmdMigrate(args *cli.Context) error {
1✔
252
        err := cmd.Migrate(config.Config, args.String("tenant"), args.Bool("list-tenants"))
1✔
253
        if err != nil {
1✔
254
                return cli.NewExitError(err, 5)
×
255
        }
×
256
        return nil
1✔
257
}
258

259
func cmdMaintenance(args *cli.Context) error {
×
260
        err := cmd.Maintenance(
×
261
                args.Bool("decommissioning-cleanup"),
×
262
                args.String("tenant"),
×
263
                args.Bool("dry-run"),
×
264
        )
×
265
        if err != nil {
×
266
                return cli.NewExitError(err, 6)
×
267
        }
×
268
        return nil
×
269
}
270

271
func cmdPropagateStatusesInventory(args *cli.Context) error {
×
272
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
×
273
        if err != nil {
×
274
                return err
×
275
        }
×
276

277
        inv := config.Config.GetString(dconfig.SettingInventoryAddr)
×
278
        c := cinv.NewClient(inv, false)
×
279

×
280
        err = cmd.PropagateStatusesInventory(db,
×
281
                c,
×
282
                args.String("tenant_id"),
×
283
                args.String("force-set-migration"),
×
284
                args.Bool("dry-run"))
×
285
        if err != nil {
×
286
                return cli.NewExitError(err, 7)
×
287
        }
×
288
        return nil
×
289
}
290

291
func cmdPropagateIdDataInventory(args *cli.Context) error {
×
292
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
×
293
        if err != nil {
×
294
                return err
×
295
        }
×
296

297
        inv := config.Config.GetString(dconfig.SettingInventoryAddr)
×
298
        c := cinv.NewClient(inv, false)
×
299

×
300
        err = cmd.PropagateIdDataInventory(db,
×
301
                c,
×
302
                args.String("tenant_id"),
×
303
                args.Bool("dry-run"))
×
304
        if err != nil {
×
305
                return cli.NewExitError(err, 7)
×
306
        }
×
307
        return nil
×
308
}
309

310
func cmdPropagateReporting(args *cli.Context) error {
×
311
        if !config.Config.GetBool(dconfig.SettingEnableReporting) {
×
312
                return cli.NewExitError(errors.New("reporting support not enabled"), 1)
×
313
        }
×
314

315
        db, err := mongo.NewDataStoreMongo(makeDataStoreConfig())
×
316
        if err != nil {
×
317
                return err
×
318
        }
×
319

320
        wflows := orchestrator.NewClient(orchestrator.Config{
×
321
                OrchestratorAddr: config.Config.GetString(
×
322
                        dconfig.SettingOrchestratorAddr,
×
323
                ),
×
324
        })
×
325

×
326
        var requestPeriod time.Duration
×
327
        if rateLimit := args.Uint("rate-limit"); rateLimit > 0 {
×
328
                requestPeriod = time.Second / time.Duration(rateLimit)
×
329
        }
×
330

331
        err = cmd.PropagateReporting(
×
332
                db,
×
333
                wflows,
×
334
                args.String("tenant_id"),
×
335
                requestPeriod,
×
336
                args.Bool("dry-run"),
×
337
        )
×
338
        if err != nil {
×
339
                return cli.NewExitError(err, 7)
×
340
        }
×
341
        return nil
×
342
}
343

344
func makeDataStoreConfig() mongo.DataStoreMongoConfig {
1✔
345
        return mongo.DataStoreMongoConfig{
1✔
346
                ConnectionString: config.Config.GetString(dconfig.SettingDb),
1✔
347

1✔
348
                SSL:           config.Config.GetBool(dconfig.SettingDbSSL),
1✔
349
                SSLSkipVerify: config.Config.GetBool(dconfig.SettingDbSSLSkipVerify),
1✔
350

1✔
351
                Username: config.Config.GetString(dconfig.SettingDbUsername),
1✔
352
                Password: config.Config.GetString(dconfig.SettingDbPassword),
1✔
353
        }
1✔
354

1✔
355
}
1✔
356

357
func cmdCheckDeviceLimits(args *cli.Context) error {
×
358
        mgoConf := makeDataStoreConfig()
×
359
        ds, err := mongo.NewDataStoreMongo(mgoConf)
×
360
        if err != nil {
×
361
                return errors.Wrap(err, "cmd: failed to initialize DataStore client")
×
362
        }
×
363
        // Initialize tenantadm and workflows clients.
364
        tadm := tenant.NewClient(tenant.Config{
×
365
                TenantAdmAddr: config.Config.GetString(
×
366
                        dconfig.SettingTenantAdmAddr,
×
367
                ),
×
368
        })
×
369
        wflows := orchestrator.NewClient(orchestrator.Config{
×
370
                OrchestratorAddr: config.Config.GetString(
×
371
                        dconfig.SettingOrchestratorAddr,
×
372
                ),
×
373
        })
×
374
        return cmd.CheckDeviceLimits(
×
375
                args.Float64("threshold"),
×
376
                ds, tadm, wflows,
×
377
        )
×
378
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc