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

mendersoftware / mender-server / 1495380963

14 Oct 2024 03:35PM UTC coverage: 70.373% (-2.5%) from 72.904%
1495380963

Pull #101

gitlab-ci

mineralsfree
feat: tenant list added

Ticket: MEN-7568
Changelog: None

Signed-off-by: Mikita Pilinka <mikita.pilinka@northern.tech>
Pull Request #101: feat: tenant list added

4406 of 6391 branches covered (68.94%)

Branch coverage included in aggregate %.

88 of 183 new or added lines in 10 files covered. (48.09%)

2623 existing lines in 65 files now uncovered.

36673 of 51982 relevant lines covered (70.55%)

31.07 hits per line

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

0.0
/backend/services/useradm/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/urfave/cli"
23

24
        "github.com/mendersoftware/mender-server/pkg/config"
25
        "github.com/mendersoftware/mender-server/pkg/log"
26

27
        . "github.com/mendersoftware/mender-server/services/useradm/config"
28
        "github.com/mendersoftware/mender-server/services/useradm/model"
29
        "github.com/mendersoftware/mender-server/services/useradm/store/mongo"
30
)
31

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

UNCOV
38
func doMain(args []string) error {
×
UNCOV
39
        var debug bool
×
UNCOV
40
        var configPath string
×
UNCOV
41

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

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

×
UNCOV
122
                        Action: runMigrate,
×
UNCOV
123
                },
×
UNCOV
124
        }
×
UNCOV
125

×
UNCOV
126
        app.Action = runServer
×
UNCOV
127
        app.Before = func(args *cli.Context) error {
×
UNCOV
128
                log.Setup(debug)
×
UNCOV
129

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

137
                // Enable setting conig values by environment variables
UNCOV
138
                config.Config.SetEnvPrefix("USERADM")
×
UNCOV
139
                config.Config.AutomaticEnv()
×
UNCOV
140

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

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

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

×
168
        l.Print("User Administration Service starting up")
×
169

×
170
        ctx := context.Background()
×
171

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

179
        if args.Bool("automigrate") {
×
180
                db = db.WithAutomigrate()
×
181
        }
×
182

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

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

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

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

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