Skip to content

Commit 992aa2c

Browse files
authored
Backward compat support for -N (#498)
1 parent ecec84e commit 992aa2c

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed

NOTICE.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ Apache License
587587
## github.com/docker/docker
588588

589589
* Name: github.com/docker/docker
590-
* Version: v24.0.6
591-
* License: [Apache-2.0](https://github.com/docker/docker/blob/v24.0.6/LICENSE)
590+
* Version: v24.0.7
591+
* License: [Apache-2.0](https://github.com/docker/docker/blob/v24.0.7/LICENSE)
592592

593593
```
594594

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ The `sqlcmd` project aims to be a complete port of the original ODBC sqlcmd to t
127127
The following switches have different behavior in this version of `sqlcmd` compared to the original ODBC based `sqlcmd`.
128128
- `-R` switch is ignored. The go runtime does not provide access to user locale information, and it's not readily available through syscall on all supported platforms.
129129
- `-I` switch is ignored; quoted identifiers are always set on. To disable quoted identifier behavior, add `SET QUOTED IDENTIFIER OFF` in your scripts.
130-
- `-N` now takes a string value that can be one of `strict`,`true`,`mandatory`,`yes`,`1`,`t`, `optional`, `no`, `0`, `f`, `false`, or `disable` to specify the encryption choice.
130+
- `-N` now takes an optional string value that can be one of `s[trict]`,`t[rue]`,`m[andatory]`, `yes`,`1`, `o[ptional]`,`no`, `0`, `f[alse]`, or `disable` to specify the encryption choice.
131+
- If `-N` is passed but no value is provided, `true` is used.
131132
- If `-N` and `-C` are not provided, sqlcmd will negotiate authentication with the server without validating the server certificate.
132133
- If `-N` is provided but `-C` is not, sqlcmd will require validation of the server certificate. Note that a `false` value for encryption could still lead to encryption of the login packet.
133134
- `-C` has no effect when `strict` value is specified for `-N`.

cmd/sqlcmd/sqlcmd.go

+13-3
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ func checkDefaultValue(args []string, i int) (val string) {
314314
'k': "0",
315315
'L': "|", // | is the sentinel for no value since users are unlikely to use it. It's "reserved" in most shells
316316
'X': "0",
317+
'N': "true",
317318
}
318319
if isFlag(args[i]) && (len(args) == i+1 || args[i+1][0] == '-') {
319320
if v, ok := flags[rune(args[i][1])]; ok {
@@ -463,10 +464,10 @@ func normalizeFlags(cmd *cobra.Command) error {
463464
case encryptConnection:
464465
value := strings.ToLower(v)
465466
switch value {
466-
case "mandatory", "yes", "1", "t", "true", "disable", "optional", "no", "0", "f", "false", "strict":
467+
case "mandatory", "yes", "1", "t", "true", "disable", "optional", "no", "0", "f", "false", "strict", "m", "s", "o":
467468
return pflag.NormalizedName(name)
468469
default:
469-
err = invalidParameterError("-N", v, "mandatory", "yes", "1", "t", "true", "disable", "optional", "no", "0", "f", "false", "strict")
470+
err = invalidParameterError("-N", v, "m[andatory]", "yes", "1", "t[rue]", "disable", "o[ptional]", "no", "0", "f[alse]", "s[trict]")
470471
return pflag.NormalizedName("")
471472
}
472473
case format:
@@ -688,7 +689,16 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq
688689
connect.DisableVariableSubstitution = args.DisableVariableSubstitution
689690
connect.ApplicationIntent = args.ApplicationIntent
690691
connect.LoginTimeoutSeconds = args.LoginTimeout
691-
connect.Encrypt = args.EncryptConnection
692+
switch args.EncryptConnection {
693+
case "s":
694+
connect.Encrypt = "strict"
695+
case "o":
696+
connect.Encrypt = "optional"
697+
case "m":
698+
connect.Encrypt = "mandatory"
699+
default:
700+
connect.Encrypt = args.EncryptConnection
701+
}
692702
connect.PacketSize = args.PacketSize
693703
connect.WorkstationName = args.WorkstationName
694704
connect.LogLevel = args.DriverLoggingLevel

cmd/sqlcmd/sqlcmd_test.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ func TestValidCommandLineToArgsConversion(t *testing.T) {
9999
{[]string{"-k", "-X", "-r", "-z", "something"}, func(args SQLCmdArguments) bool {
100100
return args.warnOnBlockedCmd() && !args.useEnvVars() && args.getControlCharacterBehavior() == sqlcmd.ControlRemove && *args.ErrorsToStderr == 0 && args.ChangePassword == "something"
101101
}},
102+
{[]string{"-N"}, func(args SQLCmdArguments) bool {
103+
return args.EncryptConnection == "true"
104+
}},
105+
{[]string{"-N", "m"}, func(args SQLCmdArguments) bool {
106+
return args.EncryptConnection == "m"
107+
}},
102108
}
103109

104110
for _, test := range commands {
@@ -150,7 +156,7 @@ func TestInvalidCommandLine(t *testing.T) {
150156
{[]string{"-P"}, "'-P': Missing argument. Enter '-?' for help."},
151157
{[]string{"-;"}, "';': Unknown Option. Enter '-?' for help."},
152158
{[]string{"-t", "-2"}, "'-t -2': value must be greater than or equal to 0 and less than or equal to 65534."},
153-
{[]string{"-N", "invalid"}, "'-N invalid': Unexpected argument. Argument value has to be one of [mandatory yes 1 t true disable optional no 0 f false strict]."},
159+
{[]string{"-N", "invalid"}, "'-N invalid': Unexpected argument. Argument value has to be one of [m[andatory] yes 1 t[rue] disable o[ptional] no 0 f[alse] s[trict]]."},
154160
}
155161

156162
for _, test := range commands {
@@ -530,6 +536,42 @@ func TestConvertOsArgs(t *testing.T) {
530536
}
531537
}
532538

539+
func TestEncryptionOptions(t *testing.T) {
540+
type test struct {
541+
input string
542+
output string
543+
}
544+
tests := []test{
545+
{
546+
"s",
547+
"strict",
548+
},
549+
{
550+
"m",
551+
"mandatory",
552+
},
553+
{
554+
"o",
555+
"optional",
556+
},
557+
{
558+
"mandatory",
559+
"mandatory",
560+
},
561+
}
562+
for _, c := range tests {
563+
t.Run(c.input, func(t *testing.T) {
564+
args := newArguments()
565+
args.EncryptConnection = c.input
566+
vars := sqlcmd.InitializeVariables(false)
567+
setVars(vars, &args)
568+
var connectConfig sqlcmd.ConnectSettings
569+
setConnect(&connectConfig, &args, vars)
570+
assert.Equal(t, c.output, connectConfig.Encrypt, "Incorrect connect.Encrypt")
571+
})
572+
}
573+
}
574+
533575
// Assuming public Azure, use AAD when SQLCMDUSER environment variable is not set
534576
func canTestAzureAuth() bool {
535577
server := os.Getenv(sqlcmd.SQLCMDSERVER)

0 commit comments

Comments
 (0)