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

mendersoftware / useradm / 1290251377

14 May 2024 01:33PM UTC coverage: 85.226% (-1.6%) from 86.874%
1290251377

Pull #423

gitlab-ci

alfrunes
test(acceptance): Expose Mongo URL as pytest argument

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

2792 of 3276 relevant lines covered (85.23%)

49.04 hits per line

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

81.42
/main.go
1
// Copyright 2022 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
        "strings"
21

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

26
        . "github.com/mendersoftware/useradm/config"
27
        "github.com/mendersoftware/useradm/model"
28
        "github.com/mendersoftware/useradm/store/mongo"
29
)
30

31
func main() {
×
32
        if err := doMain(os.Args); err != nil {
×
33
                os.Exit(1)
×
34
        }
×
35
}
36

37
func doMain(args []string) error {
190✔
38
        var debug bool
190✔
39
        var configPath string
190✔
40

190✔
41
        app := cli.NewApp()
190✔
42
        app.Usage = "user administration service"
190✔
43
        app.Flags = []cli.Flag{
190✔
44
                cli.StringFlag{
190✔
45
                        Name: "config",
190✔
46
                        Usage: "Configuration `FILE`." +
190✔
47
                                " Supports JSON, TOML, YAML and HCL formatted configs.",
190✔
48
                        Destination: &configPath,
190✔
49
                },
190✔
50
                cli.BoolFlag{
190✔
51
                        Name:        "debug",
190✔
52
                        Usage:       "Enable debug logging",
190✔
53
                        Destination: &debug,
190✔
54
                },
190✔
55
        }
190✔
56
        app.Commands = []cli.Command{
190✔
57
                {
190✔
58
                        Name:  "server",
190✔
59
                        Usage: "Run the service as a server",
190✔
60
                        Flags: []cli.Flag{
190✔
61
                                cli.BoolFlag{
190✔
62
                                        Name:  "automigrate",
190✔
63
                                        Usage: "Run database migrations before starting.",
190✔
64
                                },
190✔
65
                        },
190✔
66

190✔
67
                        Action: runServer,
190✔
68
                },
190✔
69
                {
190✔
70
                        Name:  "create-user",
190✔
71
                        Usage: "Create user",
190✔
72
                        Flags: []cli.Flag{
190✔
73
                                cli.StringFlag{
190✔
74
                                        Name:  "username",
190✔
75
                                        Usage: "Name of created user, must be an email address",
190✔
76
                                },
190✔
77
                                cli.StringFlag{
190✔
78
                                        Name:  "password",
190✔
79
                                        Usage: "User's password, leave empty to have it read from stdin",
190✔
80
                                },
190✔
81
                                cli.StringFlag{
190✔
82
                                        Name:  "user-id",
190✔
83
                                        Usage: "User ID, if already generated/available (optional).",
190✔
84
                                },
190✔
85
                                cli.StringFlag{
190✔
86
                                        Name:  "tenant-id",
190✔
87
                                        Usage: "Tenant ID, if running a multitenant setup (optional).",
190✔
88
                                },
190✔
89
                        },
190✔
90
                        Action: runCreateUser,
190✔
91
                },
190✔
92
                {
190✔
93
                        Name:  "set-password",
190✔
94
                        Usage: "Set password",
190✔
95
                        Flags: []cli.Flag{
190✔
96
                                cli.StringFlag{
190✔
97
                                        Name:  "username",
190✔
98
                                        Usage: "Name of created user, must be an email address",
190✔
99
                                },
190✔
100
                                cli.StringFlag{
190✔
101
                                        Name:  "password",
190✔
102
                                        Usage: "User's password, leave empty to have it read from stdin",
190✔
103
                                },
190✔
104
                                cli.StringFlag{
190✔
105
                                        Name:  "tenant-id",
190✔
106
                                        Usage: "Tenant ID, if running a multitenant setup (optional).",
190✔
107
                                },
190✔
108
                        },
190✔
109
                        Action: runSetPassword,
190✔
110
                },
190✔
111
                {
190✔
112
                        Name:  "migrate",
190✔
113
                        Usage: "Run migrations",
190✔
114
                        Flags: []cli.Flag{
190✔
115
                                cli.StringFlag{
190✔
116
                                        Name:  "tenant",
190✔
117
                                        Usage: "Takes ID of specific tenant to migrate.",
190✔
118
                                },
190✔
119
                        },
190✔
120

190✔
121
                        Action: runMigrate,
190✔
122
                },
190✔
123
        }
190✔
124

190✔
125
        app.Action = runServer
190✔
126
        app.Before = func(args *cli.Context) error {
380✔
127
                log.Setup(debug)
190✔
128

190✔
129
                err := config.FromConfigFile(configPath, ConfigDefaults)
190✔
130
                if err != nil {
190✔
131
                        return cli.NewExitError(
×
132
                                fmt.Sprintf("error loading configuration: %s", err),
×
133
                                1)
×
134
                }
×
135

136
                // Enable setting conig values by environment variables
137
                config.Config.SetEnvPrefix("USERADM")
190✔
138
                config.Config.AutomaticEnv()
190✔
139

190✔
140
                if config.Config.IsSet(SettingPlanDefinitions) {
380✔
141
                        log.NewEmpty().Info(
190✔
142
                                "loading plan definitions from configuration",
190✔
143
                        )
190✔
144
                        err := model.LoadPlans(
190✔
145
                                config.Config.GetString(SettingPlanDefinitions),
190✔
146
                        )
190✔
147
                        if err != nil {
190✔
148
                                return fmt.Errorf(
×
149
                                        "failed to load Plans from config: %w",
×
150
                                        err,
×
151
                                )
×
152
                        }
×
153
                }
154

155
                return nil
190✔
156
        }
157
        err := app.Run(args)
190✔
158
        if err != nil {
190✔
159
                log.NewEmpty().Fatal(err)
×
160
        }
×
161
        return err
189✔
162
}
163

164
func runServer(args *cli.Context) error {
1✔
165
        l := log.New(log.Ctx{})
1✔
166

1✔
167
        l.Print("User Administration Service starting up")
1✔
168

1✔
169
        ctx := context.Background()
1✔
170

1✔
171
        db, err := mongo.NewDataStoreMongo(dataStoreMongoConfigFromAppConfig(config.Config))
1✔
172
        if err != nil {
1✔
173
                return cli.NewExitError(
×
174
                        fmt.Sprintf("failed to connect to db: %v", err),
×
175
                        2)
×
176
        }
×
177

178
        if args.Bool("automigrate") {
2✔
179
                db = db.WithAutomigrate()
1✔
180
        }
1✔
181

182
        if config.Config.Get(SettingTenantAdmAddr) != "" {
1✔
183
                db = db.WithMultitenant()
×
184
        }
×
185
        err = db.Migrate(ctx, mongo.DbVersion)
1✔
186
        if err != nil {
1✔
187
                return cli.NewExitError(
×
188
                        fmt.Sprintf("failed to run migrations: %v", err),
×
189
                        3)
×
190
        }
×
191

192
        err = RunServer(config.Config)
1✔
193
        if err != nil {
1✔
194
                return cli.NewExitError(err.Error(), 4)
×
195
        }
×
196
        return nil
×
197
}
198

199
func runCreateUser(args *cli.Context) error {
94✔
200
        email := model.Email(strings.ToLower(args.String("username")))
94✔
201
        err := commandCreateUser(
94✔
202
                config.Config,
94✔
203
                email,
94✔
204
                args.String("password"),
94✔
205
                args.String("user-id"),
94✔
206
                args.String("tenant-id"),
94✔
207
        )
94✔
208
        if err != nil {
94✔
209
                return cli.NewExitError(err.Error(), 5)
×
210
        }
×
211
        return nil
94✔
212
}
213

214
func runMigrate(args *cli.Context) error {
94✔
215
        err := commandMigrate(config.Config, args.String("tenant"))
94✔
216
        if err != nil {
94✔
217
                return cli.NewExitError(err.Error(), 6)
×
218
        }
×
219
        return nil
94✔
220
}
221

222
func runSetPassword(args *cli.Context) error {
1✔
223
        email := model.Email(strings.ToLower(args.String("username")))
1✔
224
        err := commandSetPassword(
1✔
225
                config.Config, email,
1✔
226
                args.String("password"), args.String("tenant-id"),
1✔
227
        )
1✔
228
        if err != nil {
1✔
229
                return cli.NewExitError(err.Error(), 7)
×
230
        }
×
231
        return nil
1✔
232
}
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