8
8
"github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
9
9
"github.com/microsoft/go-sqlcmd/internal/pal"
10
10
"os"
11
+ "runtime"
11
12
12
13
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
13
14
"github.com/microsoft/go-sqlcmd/internal/config"
@@ -18,35 +19,48 @@ import (
18
19
type AddUser struct {
19
20
cmdparser.Cmd
20
21
21
- name string
22
- authType string
23
- username string
24
- encryptPassword bool
22
+ name string
23
+ authType string
24
+ username string
25
+ passwordEncryption string
25
26
}
26
27
27
28
func (c * AddUser ) DefineCommand (... cmdparser.CommandOptions ) {
28
- options := cmdparser.CommandOptions {
29
- Use : "add-user" ,
30
- Short : "Add a user" ,
31
- Examples : []cmdparser.ExampleOptions {
32
- {
33
- Description : "Add a user (using the SQLCMD_PASSWORD environment variable)" ,
34
- Steps : []string {
35
- fmt .Sprintf (`%s SQLCMD_PASSWORD=<placeholderpassword>` , pal .CreateEnvVarKeyword ()),
36
- "sqlcmd config add-user --name my-user --username user1" ,
37
- fmt .Sprintf (`%s SQLCMD_PASSWORD=` , pal .CreateEnvVarKeyword ()),
38
- },
29
+ examples := []cmdparser.ExampleOptions {
30
+ {
31
+ Description : "Add a user (using the SQLCMD_PASSWORD environment variable)" ,
32
+ Steps : []string {
33
+ fmt .Sprintf (`%s SQLCMD_PASSWORD=<placeholderpassword>` , pal .CreateEnvVarKeyword ()),
34
+ "sqlcmd config add-user --name my-user --username user1 --password-encryption none" ,
35
+ fmt .Sprintf (`%s SQLCMD_PASSWORD=` , pal .CreateEnvVarKeyword ()),
39
36
},
40
- {
41
- Description : "Add a user (using the SQLCMDPASSWORD environment variable)" ,
42
- Steps : [] string {
43
- fmt . Sprintf ( `%s SQLCMDPASSWORD=<placeholderpassword>` , pal . CreateEnvVarKeyword ()),
44
- "sqlcmd config add-user --name my-user --username user1" ,
45
- fmt . Sprintf ( `%s SQLCMDPASSWORD=` , pal . CreateEnvVarKeyword ()) ,
46
- } ,
37
+ },
38
+ {
39
+ Description : "Add a user (using the SQLCMDPASSWORD environment variable)" ,
40
+ Steps : [] string {
41
+ fmt . Sprintf ( `%s SQLCMDPASSWORD=<placeholderpassword>` , pal . CreateEnvVarKeyword ()) ,
42
+ "sqlcmd config add-user --name my-user --username user1 --password-encryption none" ,
43
+ fmt . Sprintf ( `%s SQLCMDPASSWORD=` , pal . CreateEnvVarKeyword ()) ,
47
44
},
48
45
},
49
- Run : c .run }
46
+ }
47
+
48
+ if runtime .GOOS == "windows" {
49
+ examples = append (examples , cmdparser.ExampleOptions {
50
+ Description : "Add a user using Windows Data Protection API to encrypt password in sqlconfig" ,
51
+ Steps : []string {
52
+ fmt .Sprintf (`%s SQLCMD_PASSWORD=<placeholderpassword>` , pal .CreateEnvVarKeyword ()),
53
+ "sqlcmd config add-user --name my-user --username user1 --password-encryption dpapi" ,
54
+ fmt .Sprintf (`%s SQLCMD_PASSWORD=` , pal .CreateEnvVarKeyword ()),
55
+ },
56
+ })
57
+ }
58
+
59
+ options := cmdparser.CommandOptions {
60
+ Use : "add-user" ,
61
+ Short : "Add a user" ,
62
+ Examples : examples ,
63
+ Run : c .run }
50
64
51
65
c .Cmd .DefineCommand (options )
52
66
@@ -70,14 +84,19 @@ func (c *AddUser) DefineCommand(...cmdparser.CommandOptions) {
70
84
Usage : "The username (provide password in SQLCMD_PASSWORD or SQLCMDPASSWORD environment variable)" ,
71
85
})
72
86
73
- c .encryptPasswordFlag ()
87
+ c .AddFlag (cmdparser.FlagOptions {
88
+ String : & c .passwordEncryption ,
89
+ Name : "password-encryption" ,
90
+ Usage : fmt .Sprintf ("Password encryption method (%s) in sqlconfig file" ,
91
+ secret .EncryptionMethodsForUsage ()),
92
+ })
74
93
}
75
94
76
95
// run a user to the configuration. It sets the user's name and
77
96
// authentication type, and, if the authentication type is 'basic', it sets the
78
97
// user's username and password (either in plain text or encrypted, depending
79
- // on the --encrypt- password flag). If the user's authentication type is not 'basic'
80
- // or 'other', an error is thrown. If the --encrypt- password flag is set but the
98
+ // on the --password-encryption flag). If the user's authentication type is not 'basic'
99
+ // or 'other', an error is thrown. If the --password-encryption flag is set but the
81
100
// authentication type is not 'basic', an error is thrown. If the authentication
82
101
// type is 'basic' but the username or password is not provided, an error is thrown.
83
102
// If the username is provided but the password is not, an error is thrown.
@@ -90,11 +109,17 @@ func (c *AddUser) run() {
90
109
"Authentication type '' is not valid %v'" , c .authType )
91
110
}
92
111
93
- if c .authType != "basic" && c .encryptPassword {
112
+ if c .authType != "basic" && c .passwordEncryption != "" {
94
113
output .FatalWithHints ([]string {
95
- "Remove the --encrypt- password flag" ,
114
+ "Remove the --password-encryption flag" ,
96
115
"Pass in the --auth-type basic" },
97
- "The --encrypt-password flag can only be used when authentication type is 'basic'" )
116
+ "The --password-encryption flag can only be used when authentication type is 'basic'" )
117
+ }
118
+
119
+ if c .authType == "basic" && c .passwordEncryption == "" {
120
+ output .FatalWithHints ([]string {
121
+ "Add the --password-encryption flag" },
122
+ "The --password-encryption flag must be set when authentication type is 'basic'" )
98
123
}
99
124
100
125
user := sqlconfig.User {
@@ -117,6 +142,12 @@ func (c *AddUser) run() {
117
142
"Username not provided" )
118
143
}
119
144
145
+ if ! secret .IsValidEncryptionMethod (c .passwordEncryption ) {
146
+ output .FatalfWithHints ([]string {
147
+ fmt .Sprintf ("Provide a valid encryption method (%s) with the --password-encryption flag" , secret .EncryptionMethodsForUsage ())},
148
+ "Encryption method '%v' is not valid" , c .passwordEncryption )
149
+ }
150
+
120
151
if os .Getenv ("SQLCMD_PASSWORD" ) != "" &&
121
152
os .Getenv ("SQLCMDPASSWORD" ) != "" {
122
153
output .FatalWithHints ([]string {
@@ -129,12 +160,12 @@ func (c *AddUser) run() {
129
160
password = os .Getenv ("SQLCMDPASSWORD" )
130
161
}
131
162
user .BasicAuth = & sqlconfig.BasicAuthDetails {
132
- Username : c .username ,
133
- PasswordEncrypted : c .encryptPassword ,
134
- Password : secret .Encode (password , c .encryptPassword ),
163
+ Username : c .username ,
164
+ PasswordEncryption : c .passwordEncryption ,
165
+ Password : secret .Encode (password , c .passwordEncryption ),
135
166
}
136
167
}
137
168
138
- config .AddUser (user )
139
- output .Infof ("User '%v' added" , user . Name )
169
+ uniqueUserName := config .AddUser (user )
170
+ output .Infof ("User '%v' added" , uniqueUserName )
140
171
}
0 commit comments