• 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

61.36
/commands.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

21
        "github.com/mendersoftware/go-lib-micro/config"
22
        "github.com/mendersoftware/go-lib-micro/identity"
23
        "github.com/mendersoftware/go-lib-micro/log"
24
        "github.com/pkg/errors"
25
        "golang.org/x/term"
26

27
        "github.com/mendersoftware/useradm/client/tenant"
28
        . "github.com/mendersoftware/useradm/config"
29
        "github.com/mendersoftware/useradm/model"
30
        "github.com/mendersoftware/useradm/store/mongo"
31
        useradm "github.com/mendersoftware/useradm/user"
32
)
33

34
// safeReadPassword reads a user password from a terminal in a safe way (without
35
// echoing the characters input by the user)
36
func safeReadPassword() (string, error) {
×
37
        stdinfd := int(os.Stdin.Fd())
×
38

×
39
        if !term.IsTerminal(stdinfd) {
×
40
                return "", errors.New("stdin is not a terminal")
×
41
        }
×
42

43
        fmt.Fprintf(os.Stderr, "Enter password: ")
×
44
        raw, err := term.ReadPassword(int(os.Stdin.Fd()))
×
45
        if err != nil {
×
46
                return "", errors.Wrap(err, "failed to read password")
×
47
        }
×
48
        fmt.Fprintf(os.Stderr, "\n")
×
49

×
50
        return string(raw), nil
×
51
}
52

53
func commandCreateUser(
54
        c config.Reader,
55
        username model.Email,
56
        password, userId, tenantId string,
57
) error {
96✔
58
        ctx := context.Background()
96✔
59
        l := log.NewEmpty()
96✔
60

96✔
61
        l.Debugf("create user '%s'", username)
96✔
62

96✔
63
        if password == "" {
96✔
64
                var err error
×
65
                if password, err = safeReadPassword(); err != nil {
×
66
                        return err
×
67
                }
×
68
        }
69

70
        u := model.User{
96✔
71
                Email:    username,
96✔
72
                Password: password,
96✔
73
        }
96✔
74

96✔
75
        if userId != "" {
97✔
76
                u.ID = userId
1✔
77
        }
1✔
78

79
        if err := u.Validate(); err != nil {
97✔
80
                return errors.Wrap(err, "user validation failed")
1✔
81
        }
1✔
82

83
        db, err := mongo.GetDataStoreMongo(dataStoreMongoConfigFromAppConfig(c))
95✔
84
        if err != nil {
96✔
85
                return errors.Wrap(err, "database connection failed")
1✔
86
        }
1✔
87

88
        ua := useradm.NewUserAdm(nil, db, useradm.Config{})
94✔
89
        if tadmAddr := c.GetString(SettingTenantAdmAddr); tadmAddr != "" {
94✔
90
                l.Infof("setting up tenant verification")
×
91

×
92
                tc := tenant.NewClient(tenant.Config{
×
93
                        TenantAdmAddr: tadmAddr,
×
94
                })
×
95

×
96
                ua = ua.WithTenantVerification(tc)
×
97
                ctx = identity.WithContext(ctx, &identity.Identity{
×
98
                        Tenant: tenantId,
×
99
                })
×
100
        }
×
101

102
        if err := ua.CreateUser(ctx, &u); err != nil {
94✔
103
                return errors.Wrap(err, "creating user failed")
×
104
        }
×
105

106
        fmt.Printf("%s\n", u.ID)
94✔
107

94✔
108
        return nil
94✔
109
}
110

111
func getTenantContext(tenantId string) context.Context {
1✔
112
        ctx := context.Background()
1✔
113
        if tenantId != "" {
1✔
114
                id := &identity.Identity{
×
115
                        Tenant: tenantId,
×
116
                }
×
117

×
118
                ctx = identity.WithContext(ctx, id)
×
119
        }
×
120

121
        return ctx
1✔
122
}
123

124
func commandMigrate(c config.Reader, tenantId string) error {
94✔
125
        l := log.New(log.Ctx{})
94✔
126

94✔
127
        l.Printf("User Administration Service starting up")
94✔
128

94✔
129
        if tenantId != "" {
156✔
130
                l.Printf("migrating tenant %v", tenantId)
62✔
131
        } else {
94✔
132
                l.Printf("migrating all the tenants")
32✔
133
        }
32✔
134

135
        db, err := mongo.NewDataStoreMongo(dataStoreMongoConfigFromAppConfig(c))
94✔
136

94✔
137
        if err != nil {
94✔
138
                return errors.Wrap(err, "database connection failed")
×
139
        }
×
140

141
        // we want to apply migrations
142
        db = db.WithAutomigrate()
94✔
143

94✔
144
        ctx := context.Background()
94✔
145

94✔
146
        if tenantId != "" {
156✔
147
                err = db.MigrateTenant(ctx, mongo.DbVersion, tenantId)
62✔
148
        } else {
94✔
149
                err = db.Migrate(ctx, mongo.DbVersion)
32✔
150
        }
32✔
151
        if err != nil {
94✔
152
                return errors.Wrap(err, "failed to run migrations")
×
153
        }
×
154

155
        return nil
94✔
156

157
}
158

159
func commandSetPassword(
160
        c config.Reader,
161
        username model.Email,
162
        password, tenantId string,
163
) error {
1✔
164
        l := log.NewEmpty()
1✔
165

1✔
166
        l.Debugf("set password for '%s'", username)
1✔
167

1✔
168
        if password == "" {
1✔
169
                var err error
×
170
                if password, err = safeReadPassword(); err != nil {
×
171
                        return err
×
172
                }
×
173
        }
174

175
        db, err := mongo.GetDataStoreMongo(dataStoreMongoConfigFromAppConfig(c))
1✔
176
        if err != nil {
1✔
177
                return errors.Wrap(err, "database connection failed")
×
178
        }
×
179

180
        ua := useradm.NewUserAdm(nil, db, useradm.Config{})
1✔
181

1✔
182
        u := model.User{
1✔
183
                Email:    username,
1✔
184
                Password: password,
1✔
185
        }
1✔
186

1✔
187
        if err := u.Validate(); err != nil {
1✔
188
                return errors.Wrap(err, "user validation failed")
×
189
        }
×
190

191
        ctx := getTenantContext(tenantId)
1✔
192

1✔
193
        uu := model.UserUpdate{
1✔
194
                Email:    username,
1✔
195
                Password: password,
1✔
196
        }
1✔
197

1✔
198
        if err := ua.SetPassword(ctx, uu); err != nil {
1✔
199
                return errors.Wrap(err, "setting password failed")
×
200
        }
×
201

202
        return nil
1✔
203
}
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