Skip to content

Commit cd27a2f

Browse files
committed
Merge branch 'stuartpa/fix-msi-path-order' of https://github.com/microsoft/go-sqlcmd into stuartpa/fix-msi-path-order
2 parents 809174b + 05a61bd commit cd27a2f

97 files changed

Lines changed: 1377 additions & 646 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

SECURITY.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).
66

7-
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)), please report it to us as described below.
7+
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/previous-versions/tn-archive/cc751383(v=technet.10)), please report it to us as described below.
88

99
## Reporting Security Issues
1010

1111
**Please do not report security vulnerabilities through public GitHub issues.**
1212

1313
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report).
1414

15-
If you prefer to submit without logging in, send email to [[email protected]](mailto:[email protected]). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc).
15+
If you prefer to submit without logging in, send email to [[email protected]](mailto:[email protected]). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/msrc/pgp-key-msrc).
1616

1717
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
1818

@@ -36,6 +36,6 @@ We prefer all communications to be in English.
3636

3737
## Policy
3838

39-
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd).
39+
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/msrc/cvd).
4040

4141
<!-- END MICROSOFT SECURITY.MD BLOCK -->

build/azure-pipelines/build-common.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,19 @@ steps:
5050
displayName: 'Go: build sqlcmd'
5151
inputs:
5252
command: 'build'
53-
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }} -ldflags="-X main.version=${{ parameters.VersionTag }}"'
53+
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }} -ldflags="-s -w -X main.version=${{ parameters.VersionTag }}"'
54+
workingDirectory: '$(Build.SourcesDirectory)/cmd/modern'
55+
env:
56+
GOOS: ${{ parameters.OS }}
57+
GOARCH: ${{ parameters.Arch }}
58+
GOBIN: $(Build.SourcesDirectory)
59+
CGO_ENABLED: 0 # Enables Docker image based off 'scratch'
60+
61+
- task: Go@0
62+
displayName: 'Go: build sqlcmd with debug symbols'
63+
inputs:
64+
command: 'build'
65+
arguments: '-o $(Build.BinariesDirectory)/${{ parameters.BinaryName }}_debug -ldflags="-X main.version=${{ parameters.VersionTag }}"'
5466
workingDirectory: '$(Build.SourcesDirectory)/cmd/modern'
5567
env:
5668
GOOS: ${{ parameters.OS }}

cmd/modern/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,32 @@ func main() {
4545
cmdparser.Initialize(initializeCallback)
4646
rootCmd.Execute()
4747
} else {
48+
initializeEnvVars()
4849
legacyCmd.Execute(version)
4950
}
5051
}
5152

53+
// initializeEnvVars intializes SQLCMDSERVER, SQLCMDUSER and SQLCMDPASSWORD
54+
// if the currentContext is set and if these env vars are not already set.
55+
// In terms of precedence, command line switches/flags take higher precedence
56+
// than env variables and env variables take higher precedence over config
57+
// file info.
58+
func initializeEnvVars() {
59+
initializeCallback()
60+
if config.CurrentContextName() != "" {
61+
server, username, password := config.GetCurrentContextInfo()
62+
if os.Getenv("SQLCMDSERVER") == "" {
63+
os.Setenv("SQLCMDSERVER", server)
64+
}
65+
if os.Getenv("SQLCMDUSER") == "" {
66+
os.Setenv("SQLCMDUSER", username)
67+
}
68+
if os.Getenv("SQLCMDPASSWORD") == "" {
69+
os.Setenv("SQLCMDPASSWORD", password)
70+
}
71+
}
72+
}
73+
5274
// isFirstArgModernCliSubCommand is TEMPORARY code, to be removed when
5375
// we remove the Kong based CLI
5476
func isFirstArgModernCliSubCommand() (isNewCliCommand bool) {

cmd/modern/main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ func TestDisplayHints(t *testing.T) {
3737
}
3838

3939
func TestCheckErr(t *testing.T) {
40+
rootCmd = cmdparser.New[*Root](dependency.Options{})
41+
rootCmd.loggingLevel = 4
42+
checkErr(nil)
4043
assert.Panics(t, func() {
41-
rootCmd = cmdparser.New[*Root](dependency.Options{})
42-
rootCmd.loggingLevel = 4
43-
checkErr(nil)
4444
checkErr(errors.New("test error"))
4545
})
4646
}

cmd/modern/root.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
package main
55

66
import (
7+
"runtime"
8+
79
"github.com/microsoft/go-sqlcmd/cmd/modern/root"
810
"github.com/microsoft/go-sqlcmd/internal/cmdparser"
911
"github.com/microsoft/go-sqlcmd/internal/config"
10-
"runtime"
1112
)
1213

1314
// Root type implements the very top-level command for sqlcmd (which contains
@@ -27,16 +28,19 @@ func (c *Root) DefineCommand(...cmdparser.CommandOptions) {
2728
// Example usage steps
2829
steps := []string{"sqlcmd create mssql --accept-eula --using https://aka.ms/AdventureWorksLT.bak"}
2930

30-
if runtime.GOOS == "windows" {
31+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
3132
steps = append(steps, "sqlcmd open ads")
3233
}
3334

34-
steps = append(steps, `sqlcmd query "SELECT @version"`)
35+
steps = append(steps, `sqlcmd query "SELECT @@version"`)
3536
steps = append(steps, "sqlcmd delete")
3637

37-
examples := []cmdparser.ExampleOptions{{
38-
Description: "Install/Create, Query, Uninstall SQL Server",
39-
Steps: steps}}
38+
examples := []cmdparser.ExampleOptions{
39+
{Description: "Install/Create, Query, Uninstall SQL Server",
40+
Steps: steps},
41+
{Description: "View configuration information and connection strings",
42+
Steps: []string{"sqlcmd config view", "sqlcmd config cs"}},
43+
}
4044

4145
commandOptions := cmdparser.CommandOptions{
4246
Use: "sqlcmd",
@@ -66,8 +70,8 @@ func (c *Root) SubCommands() []cmdparser.Command {
6670
cmdparser.New[*root.Uninstall](dependencies),
6771
}
6872

69-
// BUG:(stuartpa) - Add Mac / Linux support
70-
if runtime.GOOS == "windows" {
73+
// BUG(stuartpa): - Add Linux support
74+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
7175
subCommands = append(subCommands, cmdparser.New[*root.Open](dependencies))
7276
}
7377

@@ -90,7 +94,7 @@ func (c *Root) IsValidSubCommand(command string) bool {
9094

9195
func (c *Root) addGlobalFlags() {
9296

93-
// BUG:(stuartpa) - This is a temporary flag until we have migrated
97+
// BUG(stuartpa): - This is a temporary flag until we have migrated
9498
// the kong impl to cobra. sqlcmd -? will show the kong help (all the back-compat
9599
// flags), sqlcmd --? will show the kong "did you mean one of" help.
96100
var unused bool
@@ -101,14 +105,23 @@ func (c *Root) addGlobalFlags() {
101105
Usage: "help for backwards compatibility flags (-S, -U, -E etc.)",
102106
})
103107

108+
// BUG(stuartpa): - This is a temporary flag until we have migrated
109+
// the kong impl to cobra. The implementation of --version is coming from
110+
// kong, but we need to add it to the list of flags so that it shows up in the --help
111+
c.AddFlag(cmdparser.FlagOptions{
112+
Bool: &unused,
113+
Name: "version",
114+
Usage: "print version of sqlcmd",
115+
})
116+
104117
c.AddFlag(cmdparser.FlagOptions{
105118
String: &c.configFilename,
106119
DefaultString: config.DefaultFileName(),
107120
Name: "sqlconfig",
108-
Usage: "Configuration file",
121+
Usage: "configuration file",
109122
})
110123

111-
/* BUG:(stuartpa) - At the moment this is a top level flag, but it doesn't
124+
/* BUG(stuartpa): - At the moment this is a top level flag, but it doesn't
112125
work with all sub-commands (e.g. query), so removing for now.
113126
c.AddFlag(cmdparser.FlagOptions{
114127
String: &c.outputType,
@@ -123,6 +136,6 @@ func (c *Root) addGlobalFlags() {
123136
Int: (*int)(&c.loggingLevel),
124137
DefaultInt: 2,
125138
Name: "verbosity",
126-
Usage: "Log level, error=0, warn=1, info=2, debug=3, trace=4",
139+
Usage: "log level, error=0, warn=1, info=2, debug=3, trace=4",
127140
})
128141
}

cmd/modern/root/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ func (c *Config) DefineCommand(...cmdparser.CommandOptions) {
2424
SubCommands: c.SubCommands(),
2525
Examples: []cmdparser.ExampleOptions{
2626
{
27-
Description: "Add context for existing endpoint and user",
27+
Description: "Add context for existing endpoint and user (use SQLCMD_PASSWORD or SQLCMDPASSWORD)",
2828
Steps: []string{
29-
fmt.Sprintf("%s SQLCMD_PASSWORD=<password>", pal.CreateEnvVarKeyword()),
29+
fmt.Sprintf("%s SQLCMD_PASSWORD=<placeholderpassword>", pal.CreateEnvVarKeyword()),
3030
"sqlcmd config add-user --name sa1434 --username sa",
3131
fmt.Sprintf("%s SQLCMD_PASSWORD=", pal.CreateEnvVarKeyword()),
3232
"sqlcmd config add-endpoint --name ep1434 --address localhost --port 1434",

cmd/modern/root/config/add-context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ func TestAddContext(t *testing.T) {
1616
}
1717

1818
func TestNegAddContext(t *testing.T) {
19+
cmdparser.TestSetup(t)
1920
assert.Panics(t, func() {
20-
cmdparser.TestSetup(t)
2121
cmdparser.TestCmd[*AddContext]("--endpoint does-not-exist")
2222
})
2323
}
2424

2525
func TestNegAddContext2(t *testing.T) {
26+
cmdparser.TestSetup(t)
27+
cmdparser.TestCmd[*AddEndpoint]()
2628
assert.Panics(t, func() {
27-
cmdparser.TestSetup(t)
28-
cmdparser.TestCmd[*AddEndpoint]()
2929
cmdparser.TestCmd[*AddContext]("--endpoint endpoint --user does-not-exist")
3030
})
3131
}

cmd/modern/root/config/add-user.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ func (c *AddUser) DefineCommand(...cmdparser.CommandOptions) {
3030
Short: "Add a user",
3131
Examples: []cmdparser.ExampleOptions{
3232
{
33-
Description: "Add a user",
33+
Description: "Add a user (using the SQLCMD_PASSWORD environment variable)",
3434
Steps: []string{
35-
fmt.Sprintf(`%s SQLCMD_PASSWORD=<password>`, pal.CreateEnvVarKeyword()),
35+
fmt.Sprintf(`%s SQLCMD_PASSWORD=<placeholderpassword>`, pal.CreateEnvVarKeyword()),
3636
"sqlcmd config add-user --name my-user --username user1",
3737
fmt.Sprintf(`%s SQLCMD_PASSWORD=`, pal.CreateEnvVarKeyword()),
3838
},
3939
},
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+
},
47+
},
4048
},
4149
Run: c.run}
4250

@@ -59,7 +67,7 @@ func (c *AddUser) DefineCommand(...cmdparser.CommandOptions) {
5967
c.AddFlag(cmdparser.FlagOptions{
6068
String: &c.username,
6169
Name: "username",
62-
Usage: "The username (provide password in SQLCMD_PASSWORD environment variable)",
70+
Usage: "The username (provide password in SQLCMD_PASSWORD or SQLCMDPASSWORD environment variable)",
6371
})
6472

6573
c.encryptPasswordFlag()
@@ -95,24 +103,35 @@ func (c *AddUser) run() {
95103
}
96104

97105
if c.authType == "basic" {
98-
if os.Getenv("SQLCMD_PASSWORD") == "" {
106+
if os.Getenv("SQLCMD_PASSWORD") == "" && os.Getenv("SQLCMDPASSWORD") == "" {
99107
output.FatalWithHints([]string{
100-
"Provide password in the SQLCMD_PASSWORD environment variable"},
108+
"Provide password in the SQLCMD_PASSWORD (or SQLCMDPASSWORD) environment variable"},
101109
"Authentication Type 'basic' requires a password")
102110
}
103111

104112
if c.username == "" {
105113
output.FatalfWithHintExamples([][]string{
106114
{"Provide a username with the --username flag",
107-
"sqlcmd config add-user --username stuartpa"},
115+
"sqlcmd config add-user --username sa"},
108116
},
109-
"Username not provider")
117+
"Username not provided")
110118
}
111119

120+
if os.Getenv("SQLCMD_PASSWORD") != "" &&
121+
os.Getenv("SQLCMDPASSWORD") != "" {
122+
output.FatalWithHints([]string{
123+
"Unset one of the environment variables SQLCMD_PASSWORD or SQLCMDPASSWORD"},
124+
"Both environment variables SQLCMD_PASSWORD and SQLCMDPASSWORD are set. ")
125+
}
126+
127+
password := os.Getenv("SQLCMD_PASSWORD")
128+
if password == "" {
129+
password = os.Getenv("SQLCMDPASSWORD")
130+
}
112131
user.BasicAuth = &sqlconfig.BasicAuthDetails{
113132
Username: c.username,
114133
PasswordEncrypted: c.encryptPassword,
115-
Password: secret.Encode(os.Getenv("SQLCMD_PASSWORD"), c.encryptPassword),
134+
Password: secret.Encode(password, c.encryptPassword),
116135
}
117136
}
118137

cmd/modern/root/config/add-user_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,57 @@ import (
1010
"testing"
1111
)
1212

13+
// TestAddUser tests that the `sqlcmd config add-user` command
14+
// works as expected
1315
func TestAddUser(t *testing.T) {
14-
os.Setenv("SQLCMD_PASSWORD", "it's-a-secret")
16+
17+
// SQLCMDPASSWORD is already set in the build pipelines, so don't
18+
// overwrite it here.
19+
if os.Getenv("SQLCMDPASSWORD") == "" {
20+
os.Setenv("SQLCMDPASSWORD", "it's-a-secret")
21+
}
1522
cmdparser.TestSetup(t)
1623
cmdparser.TestCmd[*AddUser]("--username user1")
1724
}
1825

26+
// TestNegAddUser tests that the `sqlcmd config add-user` command
27+
// fails when the auth-type is invalid
1928
func TestNegAddUser(t *testing.T) {
29+
cmdparser.TestSetup(t)
2030
assert.Panics(t, func() {
21-
cmdparser.TestSetup(t)
2231
cmdparser.TestCmd[*AddUser]("--username user1 --auth-type bad-bad")
2332
})
2433
}
2534

35+
// TestNegAddUser2 tests that the `sqlcmd config add-user` command
36+
// fails when the auth-type is not basic and --encrypt-password is set
2637
func TestNegAddUser2(t *testing.T) {
38+
cmdparser.TestSetup(t)
2739
assert.Panics(t, func() {
28-
cmdparser.TestSetup(t)
2940
cmdparser.TestCmd[*AddUser]("--username user1 --auth-type other --encrypt-password")
3041
})
3142
}
3243

44+
// TestNegAddUser3 tests that the `sqlcmd config add-user` command
45+
// fails when the SQLCMD_PASSWORD environment variable is not set
3346
func TestNegAddUser3(t *testing.T) {
34-
assert.Panics(t, func() {
35-
36-
os.Setenv("SQLCMD_PASSWORD", "")
47+
if os.Getenv("SQLCMDPASSWORD") != "" {
48+
os.Setenv("SQLCMDPASSWORD", "")
49+
}
3750

38-
cmdparser.TestSetup(t)
51+
cmdparser.TestSetup(t)
52+
assert.Panics(t, func() {
3953
cmdparser.TestCmd[*AddUser]("--username user1")
4054
})
4155
}
4256

57+
// TestNegAddUser4 tests that the `sqlcmd config add-user` command
58+
// when username is not set
4359
func TestNegAddUser4(t *testing.T) {
44-
assert.Panics(t, func() {
45-
46-
os.Setenv("SQLCMD_PASSWORD", "whatever")
60+
os.Setenv("SQLCMD_PASSWORD", "whatever")
4761

48-
cmdparser.TestSetup(t)
62+
cmdparser.TestSetup(t)
63+
assert.Panics(t, func() {
4964
cmdparser.TestCmd[*AddUser]()
5065
})
5166
}

0 commit comments

Comments
 (0)