88 "github.com/microsoft/go-sqlcmd/cmd/modern/sqlconfig"
99 "github.com/microsoft/go-sqlcmd/internal/pal"
1010 "os"
11+ "runtime"
1112
1213 "github.com/microsoft/go-sqlcmd/internal/cmdparser"
1314 "github.com/microsoft/go-sqlcmd/internal/config"
@@ -18,35 +19,48 @@ import (
1819type AddUser struct {
1920 cmdparser.Cmd
2021
21- name string
22- authType string
23- username string
24- encryptPassword bool
22+ name string
23+ authType string
24+ username string
25+ passwordEncryption string
2526}
2627
2728func (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 ()),
3936 },
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 ()) ,
4744 },
4845 },
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 }
5064
5165 c .Cmd .DefineCommand (options )
5266
@@ -70,14 +84,19 @@ func (c *AddUser) DefineCommand(...cmdparser.CommandOptions) {
7084 Usage : "The username (provide password in SQLCMD_PASSWORD or SQLCMDPASSWORD environment variable)" ,
7185 })
7286
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+ })
7493}
7594
7695// run a user to the configuration. It sets the user's name and
7796// authentication type, and, if the authentication type is 'basic', it sets the
7897// 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
81100// authentication type is not 'basic', an error is thrown. If the authentication
82101// type is 'basic' but the username or password is not provided, an error is thrown.
83102// If the username is provided but the password is not, an error is thrown.
@@ -90,11 +109,17 @@ func (c *AddUser) run() {
90109 "Authentication type '' is not valid %v'" , c .authType )
91110 }
92111
93- if c .authType != "basic" && c .encryptPassword {
112+ if c .authType != "basic" && c .passwordEncryption != "" {
94113 output .FatalWithHints ([]string {
95- "Remove the --encrypt- password flag" ,
114+ "Remove the --password-encryption flag" ,
96115 "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'" )
98123 }
99124
100125 user := sqlconfig.User {
@@ -117,6 +142,12 @@ func (c *AddUser) run() {
117142 "Username not provided" )
118143 }
119144
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+
120151 if os .Getenv ("SQLCMD_PASSWORD" ) != "" &&
121152 os .Getenv ("SQLCMDPASSWORD" ) != "" {
122153 output .FatalWithHints ([]string {
@@ -129,12 +160,12 @@ func (c *AddUser) run() {
129160 password = os .Getenv ("SQLCMDPASSWORD" )
130161 }
131162 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 ),
135166 }
136167 }
137168
138- config .AddUser (user )
139- output .Infof ("User '%v' added" , user . Name )
169+ uniqueUserName := config .AddUser (user )
170+ output .Infof ("User '%v' added" , uniqueUserName )
140171}
0 commit comments