Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change Password #461

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions cmd/sqlcmd/sqlcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strconv"
"strings"

mssql "github.com/microsoft/go-mssqldb"
"github.com/microsoft/go-mssqldb/azuread"
"github.com/microsoft/go-sqlcmd/internal/localizer"
"github.com/microsoft/go-sqlcmd/pkg/console"
Expand Down Expand Up @@ -70,7 +71,9 @@ type SQLCmdArguments struct {
RemoveControlCharacters *int
EchoInput bool
QueryTimeout int
EnableColumnEnryption bool
EnableColumnEncryption bool
ChangePassword string
ChangePasswordAndExit string
// Keep Help at the end of the list
Help bool
}
Expand Down Expand Up @@ -423,7 +426,9 @@ func setFlags(rootCmd *cobra.Command, args *SQLCmdArguments) {
_ = 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]"))
rootCmd.Flags().BoolVarP(&args.EchoInput, "echo-input", "e", false, localizer.Sprintf("Echo input"))
rootCmd.Flags().IntVarP(&args.QueryTimeout, "query-timeout", "t", 0, "Query timeout")
rootCmd.Flags().BoolVarP(&args.EnableColumnEnryption, "enable-column-encryption", "g", false, localizer.Sprintf("Enable column encryption"))
rootCmd.Flags().BoolVarP(&args.EnableColumnEncryption, "enable-column-encryption", "g", false, localizer.Sprintf("Enable column encryption"))
rootCmd.Flags().StringVarP(&args.ChangePassword, "change-password", "z", "", localizer.Sprintf("New password"))
rootCmd.Flags().StringVarP(&args.ChangePasswordAndExit, "change-password-exit", "Z", "", localizer.Sprintf("New password and exit"))
}

func setScriptVariable(v string) string {
Expand Down Expand Up @@ -682,11 +687,17 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
connect.ExitOnError = args.ExitOnError
connect.ErrorSeverityLevel = args.ErrorSeverityLevel
connect.DedicatedAdminConnection = args.DedicatedAdminConnection
connect.EnableColumnEnryption = args.EnableColumnEnryption
connect.EnableColumnEncryption = args.EnableColumnEncryption
if len(args.ChangePassword) > 0 {
connect.ChangePassword = args.ChangePassword
}
if len(args.ChangePasswordAndExit) > 0 {
connect.ChangePassword = args.ChangePasswordAndExit
}
}

func isConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) bool {
iactive := args.InputFile == nil && args.Query == ""
iactive := args.InputFile == nil && args.Query == "" && len(args.ChangePasswordAndExit) == 0
return iactive || connect.RequiresPassword()
}

Expand Down Expand Up @@ -749,8 +760,23 @@ func run(vars *sqlcmd.Variables, args *SQLCmdArguments) (int, error) {
// connect using no overrides
err = s.ConnectDb(nil, line == nil)
if err != nil {
s.WriteError(s.GetError(), err)
return 1, err
switch e := err.(type) {
// 18488 == password must be changed on connection
case mssql.Error:
if e.Number == 18488 && line != nil && len(args.Password) == 0 && len(args.ChangePassword) == 0 && len(args.ChangePasswordAndExit) == 0 {
b, _ := line.ReadPassword(localizer.Sprintf("Enter new password:"))
s.Connect.ChangePassword = string(b)
err = s.ConnectDb(nil, true)
}
}
if err != nil {
s.WriteError(s.GetError(), err)
return 1, err
}
}

if len(args.ChangePasswordAndExit) > 0 {
return 0, nil
}

script := vars.StartupScriptFile()
Expand Down
4 changes: 2 additions & 2 deletions cmd/sqlcmd/sqlcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
{[]string{"-i", `"comma,text.sql"`}, func(args SQLCmdArguments) bool {
return args.InputFile[0] == "comma,text.sql"
}},
{[]string{"-k", "-X", "-r"}, func(args SQLCmdArguments) bool {
return args.warnOnBlockedCmd() && !args.useEnvVars() && args.getControlCharacterBehavior() == sqlcmd.ControlRemove && *args.ErrorsToStderr == 0
{[]string{"-k", "-X", "-r", "-z", "something"}, func(args SQLCmdArguments) bool {
return args.warnOnBlockedCmd() && !args.useEnvVars() && args.getControlCharacterBehavior() == sqlcmd.ControlRemove && *args.ErrorsToStderr == 0 && args.ChangePassword == "something"
}},
}

Expand Down
114 changes: 65 additions & 49 deletions internal/translations/catalog.go

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions internal/translations/locales/de-DE/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
28 changes: 28 additions & 0 deletions internal/translations/locales/en-US/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3016,6 +3016,20 @@
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "New password",
"message": "New password",
"translation": "New password",
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": "New password and exit",
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3257,6 +3271,13 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": "Enter new password:",
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3549,6 +3570,13 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": "Password:",
"translatorComment": "Copied from source.",
"fuzzy": true
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
20 changes: 20 additions & 0 deletions internal/translations/locales/es-ES/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
20 changes: 20 additions & 0 deletions internal/translations/locales/fr-FR/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
20 changes: 20 additions & 0 deletions internal/translations/locales/it-IT/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
20 changes: 20 additions & 0 deletions internal/translations/locales/ja-JP/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
20 changes: 20 additions & 0 deletions internal/translations/locales/ko-KR/out.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -3002,6 +3002,16 @@
"message": "Enable column encryption",
"translation": ""
},
{
"id": "New password",
"message": "New password",
"translation": ""
},
{
"id": "New password and exit",
"message": "New password and exit",
"translation": ""
},
{
"id": "Sets the sqlcmd scripting variable {V}",
"message": "Sets the sqlcmd scripting variable {V}",
Expand Down Expand Up @@ -3243,6 +3253,11 @@
],
"fuzzy": true
},
{
"id": "Enter new password:",
"message": "Enter new password:",
"translation": ""
},
{
"id": "Sqlcmd: Error:",
"message": "Sqlcmd: Error:",
Expand Down Expand Up @@ -3533,6 +3548,11 @@
],
"fuzzy": true
},
{
"id": "Password:",
"message": "Password:",
"translation": ""
},
{
"id": "Invalid variable identifier {Name}",
"message": "Invalid variable identifier {Name}",
Expand Down
Loading