Skip to content

Commit 45cd020

Browse files
authored
feat: Change Password (#461)
1 parent 0a4cd19 commit 45cd020

File tree

16 files changed

+349
-72
lines changed

16 files changed

+349
-72
lines changed

cmd/sqlcmd/sqlcmd.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"strconv"
1313
"strings"
1414

15+
mssql "github.com/microsoft/go-mssqldb"
1516
"github.com/microsoft/go-mssqldb/azuread"
1617
"github.com/microsoft/go-sqlcmd/internal/localizer"
1718
"github.com/microsoft/go-sqlcmd/pkg/console"
@@ -70,7 +71,9 @@ type SQLCmdArguments struct {
7071
RemoveControlCharacters *int
7172
EchoInput bool
7273
QueryTimeout int
73-
EnableColumnEnryption bool
74+
EnableColumnEncryption bool
75+
ChangePassword string
76+
ChangePasswordAndExit string
7477
// Keep Help at the end of the list
7578
Help bool
7679
}
@@ -423,7 +426,9 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {
423426
_ = rootCmd.Flags().IntP(removeControlCharacters, "k", 0, localizer.Sprintf("%s Remove control characters from output. Pass 1 to substitute a space per character, 2 for a space per consecutive characters", "-k [1|2]"))
424427
rootCmd.Flags().BoolVarP(&args.EchoInput, "echo-input", "e", false, localizer.Sprintf("Echo input"))
425428
rootCmd.Flags().IntVarP(&args.QueryTimeout, "query-timeout", "t", 0, "Query timeout")
426-
rootCmd.Flags().BoolVarP(&args.EnableColumnEnryption, "enable-column-encryption", "g", false, localizer.Sprintf("Enable column encryption"))
429+
rootCmd.Flags().BoolVarP(&args.EnableColumnEncryption, "enable-column-encryption", "g", false, localizer.Sprintf("Enable column encryption"))
430+
rootCmd.Flags().StringVarP(&args.ChangePassword, "change-password", "z", "", localizer.Sprintf("New password"))
431+
rootCmd.Flags().StringVarP(&args.ChangePasswordAndExit, "change-password-exit", "Z", "", localizer.Sprintf("New password and exit"))
427432
}
428433

429434
func setScriptVariable(v string) string {
@@ -682,11 +687,17 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
682687
connect.ExitOnError = args.ExitOnError
683688
connect.ErrorSeverityLevel = args.ErrorSeverityLevel
684689
connect.DedicatedAdminConnection = args.DedicatedAdminConnection
685-
connect.EnableColumnEnryption = args.EnableColumnEnryption
690+
connect.EnableColumnEncryption = args.EnableColumnEncryption
691+
if len(args.ChangePassword) > 0 {
692+
connect.ChangePassword = args.ChangePassword
693+
}
694+
if len(args.ChangePasswordAndExit) > 0 {
695+
connect.ChangePassword = args.ChangePasswordAndExit
696+
}
686697
}
687698

688699
func isConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) bool {
689-
iactive := args.InputFile == nil && args.Query == ""
700+
iactive := args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0
690701
return iactive || connect.RequiresPassword()
691702
}
692703

@@ -749,8 +760,23 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
749760
// connect using no overrides
750761
err = s.ConnectDb(nil, line == nil)
751762
if err != nil {
752-
s.WriteError(s.GetError(), err)
753-
return 1, err
763+
switch e := err.(type) {
764+
// 18488 == password must be changed on connection
765+
case mssql.Error:
766+
if e.Number == 18488 && line != nil && len(args.Password) == 0 && len(args.ChangePassword) == 0 && len(args.ChangePasswordAndExit) == 0 {
767+
b, _ := line.ReadPassword(localizer.Sprintf("Enter new password:"))
768+
s.Connect.ChangePassword = string(b)
769+
err = s.ConnectDb(nil, true)
770+
}
771+
}
772+
if err != nil {
773+
s.WriteError(s.GetError(), err)
774+
return 1, err
775+
}
776+
}
777+
778+
if len(args.ChangePasswordAndExit) > 0 {
779+
return 0, nil
754780
}
755781

756782
script := vars.StartupScriptFile()

cmd/sqlcmd/sqlcmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
9696
{[]string{"-i", `"comma,text.sql"`}, func(args SQLCmdArguments) bool {
9797
return args.InputFile[0] == "comma,text.sql"
9898
}},
99-
{[]string{"-k", "-X", "-r"}, func(args SQLCmdArguments) bool {
100-
return args.warnOnBlockedCmd() && !args.useEnvVars() && args.getControlCharacterBehavior() == sqlcmd.ControlRemove && *args.ErrorsToStderr == 0
99+
{[]string{"-k", "-X", "-r", "-z", "something"}, func(args SQLCmdArguments) bool {
100+
return args.warnOnBlockedCmd() && !args.useEnvVars() && args.getControlCharacterBehavior() == sqlcmd.ControlRemove && *args.ErrorsToStderr == 0 && args.ChangePassword == "something"
101101
}},
102102
}
103103

internal/translations/catalog.go

Lines changed: 65 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/translations/locales/de-DE/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

internal/translations/locales/en-US/out.gotext.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,20 @@
30163016
"translatorComment": "Copied from source.",
30173017
"fuzzy": true
30183018
},
3019+
{
3020+
"id": "New password",
3021+
"message": "New password",
3022+
"translation": "New password",
3023+
"translatorComment": "Copied from source.",
3024+
"fuzzy": true
3025+
},
3026+
{
3027+
"id": "New password and exit",
3028+
"message": "New password and exit",
3029+
"translation": "New password and exit",
3030+
"translatorComment": "Copied from source.",
3031+
"fuzzy": true
3032+
},
30193033
{
30203034
"id": "Sets the sqlcmd scripting variable {V}",
30213035
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3257,6 +3271,13 @@
32573271
],
32583272
"fuzzy": true
32593273
},
3274+
{
3275+
"id": "Enter new password:",
3276+
"message": "Enter new password:",
3277+
"translation": "Enter new password:",
3278+
"translatorComment": "Copied from source.",
3279+
"fuzzy": true
3280+
},
32603281
{
32613282
"id": "Sqlcmd: Error:",
32623283
"message": "Sqlcmd: Error:",
@@ -3549,6 +3570,13 @@
35493570
],
35503571
"fuzzy": true
35513572
},
3573+
{
3574+
"id": "Password:",
3575+
"message": "Password:",
3576+
"translation": "Password:",
3577+
"translatorComment": "Copied from source.",
3578+
"fuzzy": true
3579+
},
35523580
{
35533581
"id": "Invalid variable identifier {Name}",
35543582
"message": "Invalid variable identifier {Name}",

internal/translations/locales/es-ES/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

internal/translations/locales/fr-FR/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

internal/translations/locales/it-IT/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

internal/translations/locales/ja-JP/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

internal/translations/locales/ko-KR/out.gotext.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,16 @@
30023002
"message": "Enable column encryption",
30033003
"translation": ""
30043004
},
3005+
{
3006+
"id": "New password",
3007+
"message": "New password",
3008+
"translation": ""
3009+
},
3010+
{
3011+
"id": "New password and exit",
3012+
"message": "New password and exit",
3013+
"translation": ""
3014+
},
30053015
{
30063016
"id": "Sets the sqlcmd scripting variable {V}",
30073017
"message": "Sets the sqlcmd scripting variable {V}",
@@ -3243,6 +3253,11 @@
32433253
],
32443254
"fuzzy": true
32453255
},
3256+
{
3257+
"id": "Enter new password:",
3258+
"message": "Enter new password:",
3259+
"translation": ""
3260+
},
32463261
{
32473262
"id": "Sqlcmd: Error:",
32483263
"message": "Sqlcmd: Error:",
@@ -3533,6 +3548,11 @@
35333548
],
35343549
"fuzzy": true
35353550
},
3551+
{
3552+
"id": "Password:",
3553+
"message": "Password:",
3554+
"translation": ""
3555+
},
35363556
{
35373557
"id": "Invalid variable identifier {Name}",
35383558
"message": "Invalid variable identifier {Name}",

0 commit comments

Comments
 (0)