-
Notifications
You must be signed in to change notification settings - Fork 65
Initialize console if -U is specified #121
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
Conversation
In absence of console or when console reference line is set to nil, noPwd is set to true which disables password prompt and when console is not initialized, password cannot be read from user. This is why when -i is specified, console is not initialized and we do not see password prompt. This commit initializes console even if -U is specified so that password can be read interactively from user.
pls add a test that fails without the fix. |
cmd/sqlcmd/main_test.go
Outdated
@@ -162,8 +162,8 @@ func TestUnicodeOutput(t *testing.T) { | |||
} | |||
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn) | |||
setVars(vars, &args) | |||
|
|||
exitCode, err := run(vars, &args) | |||
sqlcmdConsole := getSqlcmdConsole(args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's go with a model that doesn't require so many changes.
Just define a type like
type allocConsole func(historyFile string) sqlcmd.Console
And create a field whose default assignment is console.NewConsole. The test can override it by assigning it a new func.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unit test now no more relies on the console allocator function hence have removed it.
The unit test will validate console initialization and conditions for password prompt based on the parameters supplied.
cmd/sqlcmd/main.go
Outdated
var line sqlcmd.Console = nil | ||
if iactive { | ||
line = console.NewConsole("") | ||
if connectConfig.RequiresPassword() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't you still need the iactive check? How are you testing this before pushing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad. I missed that locally it is only using SQL authentication.
Adding testcases for AAD modes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the iactive check.
cad1251
to
c634d1f
Compare
cmd/sqlcmd/main.go
Outdated
@@ -201,22 +201,28 @@ func setConnect(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments, vars *sq | |||
connect.ErrorSeverityLevel = args.ErrorSeverityLevel | |||
} | |||
|
|||
func IsConsoleInitializationRequired(connect *sqlcmd.ConnectSettings, args *SQLCmdArguments) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cmd/sqlcmd/main_test.go
Outdated
vars := sqlcmd.InitializeVariables(!args.DisableCmdAndWarn) | ||
setVars(vars, &args) | ||
var connectConfig sqlcmd.ConnectSettings | ||
setConnect(&connectConfig, &args, vars) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not use setVars and setConnect in this test. Their behavior depends on the values of environment variables set in the pipeline.
Just set the specific combinations of authentication type and user name/password in each struct explicitly and validate the right values for isConsoleInitializationRequired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setConnect only seems to set password from env variable and that too if DisableCmdAndWarn
is false.
servername and usernamre is fetched from vars.get() vars is obtained from InitializeVariables() which has an argument to indicate if values should be fetched from end. In this case DisableCmdAndWarn is set to true and value of DisableCmdAndWarn is passed to ensure that we don't fetch anything from env.
setVars() doesn't seems to fetch any anything from env.
I will, however, parameterize the test to add more scenarios.
@@ -167,7 +167,7 @@ func TestConnectCommand(t *testing.T) { | |||
err := connectCommand(s, []string{"someserver -U someuser"}, 1) | |||
assert.NoError(t, err, "connectCommand with valid arguments doesn't return an error on connect failure") | |||
assert.True(t, prompted, "connectCommand with user name and no password should prompt for password") | |||
assert.NotEqual(t, "someserver", s.Connect.ServerName, "On error, sqlCmd.Connect does not copy inputs") | |||
assert.Equal(t, "someserver", s.Connect.ServerName, "servername should match with the input parameter") | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this changing? s.Connect.ServerName being changed implies that the connection attempt succeeded when it should have failed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the context. This was a side effect of changing type of connect from ConnectSettings to a pointer to ConnectSettings in sqlcmd struct.
In connectCommand() we copy s.Connect to local connect object to establish connection.
I will fix this in my next commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if the message in the assert didn't provide sufficient context, please update the assert message to be clearer.
connectConfig.AuthenticationMethod = testcase.authenticationMethod | ||
connectConfig.Password = testcase.pwd | ||
assert.Equal(t, testcase.expectedResult, isConsoleInitializationRequired(&connectConfig, &args), "Unexpected test result encountered for console initialization") | ||
assert.Equal(t, testcase.expectedResult, connectConfig.RequiresPassword() && connectConfig.Password == "", "Unexpected test result encountered for password prompt conditions") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This was to avoid the conditional check and to assert the same condition for password prompt that we use in connectDb()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In absence of console or when console reference line is set
to nil, noPwd is set to true which disables password prompt
and when console is not initialized, password cannot be read
from user. This is why when -i is specified, console is not
initialized and we do not see password prompt.
This commit initializes console even if -U is specified so
that password can be read interactively from user.
Resolves #115
Validation: