-
Notifications
You must be signed in to change notification settings - Fork 68
/
create.go
124 lines (105 loc) · 3.68 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbusers
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/convert"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
"go.mongodb.org/ops-manager/atmcfg"
"go.mongodb.org/ops-manager/opsmngr"
)
const scramSHA1 = "SCRAM-SHA-1"
type CreateOpts struct {
cli.GlobalOpts
username string
password string
authDB string
roles []string
mechanisms []string
store store.AutomationPatcher
}
func (opts *CreateOpts) initStore() error {
var err error
opts.store, err = store.New(config.Default())
return err
}
func (opts *CreateOpts) Run() error {
current, err := opts.store.GetAutomationConfig(opts.ConfigProjectID())
if err != nil {
return err
}
dbuser := opts.newDBUser()
if err := atmcfg.ConfigureScramCredentials(dbuser, opts.password); err != nil {
return err
}
atmcfg.AddUser(current, dbuser)
if err := opts.store.UpdateAutomationConfig(opts.ConfigProjectID(), current); err != nil {
return err
}
fmt.Print(cli.DeploymentStatus(config.OpsManagerURL(), opts.ConfigProjectID()))
return nil
}
func (opts *CreateOpts) newDBUser() *opsmngr.MongoDBUser {
return &opsmngr.MongoDBUser{
Database: opts.authDB,
Username: opts.username,
Roles: convert.BuildOMRoles(opts.roles),
AuthenticationRestrictions: []string{},
Mechanisms: opts.mechanisms,
}
}
func (opts *CreateOpts) Prompt() error {
if opts.password != "" {
return nil
}
prompt := &survey.Password{
Message: "Password:",
}
return survey.AskOne(prompt, &opts.password)
}
// mongocli atlas dbuser(s) create --username username --password password --role roleName@dbName [--projectId projectId]
func CreateBuilder() *cobra.Command {
opts := &CreateOpts{}
cmd := &cobra.Command{
Use: "create",
Short: CreateDBUser,
Example: `
Create a user with readWriteAnyDatabase and clusterMonitor access
$ mongocli om dbuser create --username <username> --role readWriteAnyDatabase,clusterMonitor --mechanisms SCRAM-SHA-256 --projectId <projectId>`,
Args: cobra.NoArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(opts.ValidateProjectID, opts.initStore)
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := opts.Prompt(); err != nil {
return err
}
return opts.Run()
},
}
cmd.Flags().StringVarP(&opts.username, flag.Username, flag.UsernameShort, "", usage.DBUsername)
cmd.Flags().StringVarP(&opts.password, flag.Password, flag.PasswordShort, "", usage.Password)
cmd.Flags().StringVar(&opts.authDB, flag.AuthDB, convert.AdminDB, usage.AuthDB)
cmd.Flags().StringSliceVar(&opts.roles, flag.Role, []string{}, usage.Roles)
cmd.Flags().StringSliceVar(&opts.mechanisms, flag.Mechanisms, []string{scramSHA1}, usage.Mechanisms)
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
_ = cmd.MarkFlagRequired(flag.Username)
return cmd
}