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

mongodb / mongodb-atlas-cli / 26228926989

21 May 2026 01:27PM UTC coverage: 22.63% (-41.6%) from 64.198%
26228926989

push

github

apix-bot[bot]
Update compliance report for v1.55.0

8987 of 39713 relevant lines covered (22.63%)

0.25 hits per line

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

23.53
/internal/cli/setup/dbuser_setup.go
1
// Copyright 2021 MongoDB Inc
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

15
package setup
16

17
import (
18
        "fmt"
19

20
        "github.com/AlecAivazis/survey/v2"
21
        "github.com/mongodb/atlas-cli-core/config"
22
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert"
23
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
24
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/randgen"
25
        "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/telemetry"
26
        atlasv2 "go.mongodb.org/atlas-sdk/v20250312018/admin"
27
)
28

29
func (opts *Opts) createDatabaseUser() error {
1✔
30
        if _, err := opts.store.CreateDatabaseUser(opts.newDatabaseUser()); err != nil {
1✔
31
                return err
×
32
        }
×
33

34
        return nil
1✔
35
}
36

37
func (opts *Opts) askDBUserOptions() error {
×
38
        var qs []*survey.Question
×
39

×
40
        if opts.DBUsername == "" {
×
41
                opts.DBUsername = opts.defaultName
×
42
        }
×
43

44
        if opts.shouldAskForValue(flag.Username) {
×
45
                qs = append(qs, newDBUsernameQuestion(opts.DBUsername, opts.validateUniqueUsername))
×
46
        }
×
47

48
        if opts.shouldAskForValue(flag.Password) {
×
49
                if opts.DBUserPassword == "" {
×
50
                        pwd, err := generatePassword()
×
51
                        if err != nil {
×
52
                                return err
×
53
                        }
×
54
                        opts.DBUserPassword = pwd
×
55
                }
56

57
                minLength := 10
×
58
                if config.Service() == config.CloudGovService {
×
59
                        minLength = 12
×
60
                }
×
61
                message := fmt.Sprintf(" [You must include >%d characters. Press Enter to use an auto-generated password]", minLength)
×
62

×
63
                qs = append(qs, newDBUserPasswordQuestion(opts.DBUserPassword, message))
×
64
        }
65

66
        if len(qs) == 0 {
×
67
                return nil
×
68
        }
×
69

70
        fmt.Print(`
×
71
[Set up your database authentication access details. Store them in a secure location.]
×
72
`)
×
73
        return telemetry.TrackAsk(qs, opts)
×
74
}
75

76
func (opts *Opts) validateUniqueUsername(val any) error {
×
77
        username, ok := val.(string)
×
78
        if !ok {
×
79
                return fmt.Errorf("the username %s is not valid", username)
×
80
        }
×
81

82
        _, err := opts.store.DatabaseUser(convert.AdminDB, opts.ConfigProjectID(), username)
×
83
        if err != nil {
×
84
                target, ok := atlasv2.AsError(err)
×
85
                if ok && target.GetErrorCode() == "USERNAME_NOT_FOUND" {
×
86
                        return nil
×
87
                }
×
88
                return err
×
89
        }
90
        return fmt.Errorf("a user with this username %s already exists", username)
×
91
}
92

93
func (opts *Opts) newDatabaseUser() *atlasv2.CloudDatabaseUser {
1✔
94
        var none = "NONE"
1✔
95
        return &atlasv2.CloudDatabaseUser{
1✔
96
                Roles:        convert.BuildAtlasRoles([]string{atlasAdmin}),
1✔
97
                GroupId:      opts.ConfigProjectID(),
1✔
98
                Password:     &opts.DBUserPassword,
1✔
99
                X509Type:     &none,
1✔
100
                AwsIAMType:   &none,
1✔
101
                LdapAuthType: &none,
1✔
102
                DatabaseName: convert.AdminDB,
1✔
103
                Username:     opts.DBUsername,
1✔
104
        }
1✔
105
}
1✔
106

107
func generatePassword() (string, error) {
×
108
        const passwordLength = 12
×
109
        pwd, err := randgen.GenerateRandomBase64String(passwordLength)
×
110
        if err != nil {
×
111
                return "", err
×
112
        }
×
113

114
        return pwd, nil
×
115
}
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

© 2026 Coveralls, Inc