-
-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable SQL migrated CLI commands to authenticate
Enable SQL migrated commands authentication in the following ways: * New Global Flag --password * DOLT_CLI_PASSWORD environment variable * Ask for a password with a prompt. * Automatic authentication to a server using the secret in the sql-server.lock file One significant change in behavior is that previously if a user presented a non-sense username/password, we'd accept it as a super user identity. If a real user was specified, we would promote that user to a super user - regardless of if the password was correct. Now, if a --user flag is presented, the user must present a password by flag, env var or prompt. If the user/pwd combination is not a known user, the command will fail. This applies to both local and remote mode. Important to call out that this isn't about security. If you want to be a super user when using your local instance, you can just not provide a --user. This behavior is to enable consistent behavior of client applications where they need to test permissions. Related: #3922
- Loading branch information
Showing
21 changed files
with
652 additions
and
197 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2023 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/crypto/ssh/terminal" | ||
|
||
"github.com/dolthub/dolt/go/libraries/utils/argparser" | ||
) | ||
|
||
type UserPassword struct { | ||
Username string | ||
Password string | ||
Specified bool // If true, the user and password were provided by the user. | ||
} | ||
|
||
const DOLT_ENV_PWD = "DOLT_CLI_PASSWORD" | ||
const DOLT_SILENCE_USER_REQ_FOR_TESTING = "DOLT_SILENCE_USER_REQ_FOR_TESTING" | ||
|
||
// BuildUserPasswordPrompt builds a UserPassword struct from the parsed args. The user is prompted for a password if one | ||
// is not provided. If a username is not provided, the default is "root" (which will not be allowed is a password is | ||
// provided). A new instances of ArgParseResults is returned which does not contain the user or password flags. | ||
func BuildUserPasswordPrompt(parsedArgs *argparser.ArgParseResults) (newParsedArgs *argparser.ArgParseResults, credentials *UserPassword, err error) { | ||
userId, hasUserId := parsedArgs.GetValue(UserFlag) | ||
password, hasPassword := parsedArgs.GetValue(PasswordFlag) | ||
|
||
if !hasPassword { | ||
envPassword, hasEnvPassword := os.LookupEnv(DOLT_ENV_PWD) | ||
if hasEnvPassword { | ||
password = envPassword | ||
hasPassword = true | ||
} | ||
} | ||
|
||
newParsedArgs = parsedArgs.DropValue(UserFlag) | ||
newParsedArgs = newParsedArgs.DropValue(PasswordFlag) | ||
|
||
if !hasUserId && !hasPassword { | ||
// Common "out of box" behavior. | ||
return newParsedArgs, &UserPassword{Username: "root", Password: "", Specified: false}, nil | ||
} | ||
|
||
if hasUserId && hasPassword { | ||
return newParsedArgs, &UserPassword{Username: userId, Password: password, Specified: true}, nil | ||
} | ||
|
||
if hasUserId && !hasPassword { | ||
password = "" | ||
val, hasVal := os.LookupEnv(DOLT_ENV_PWD) | ||
if hasVal { | ||
password = val | ||
} else { | ||
Printf("Enter password: ") | ||
passwordBytes, err := terminal.ReadPassword(int(os.Stdin.Fd())) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
password = string(passwordBytes) // Assuming UTF-8 for time being. This may not work forever. | ||
} | ||
return newParsedArgs, &UserPassword{Username: userId, Password: password, Specified: true}, nil | ||
} | ||
|
||
testOverride, hasTestOverride := os.LookupEnv(DOLT_SILENCE_USER_REQ_FOR_TESTING) | ||
if hasTestOverride && testOverride == "Y" { | ||
// Used for BATS testing only. Typical usage will not hit this path, but we have many legacy tests which | ||
// do not provide a user, and the DOLT_ENV_PWD is set to avoid the prompt. | ||
return newParsedArgs, &UserPassword{Specified: false}, nil | ||
} | ||
|
||
return nil, nil, fmt.Errorf("When a password is provided, a user must also be provided. Use the --user flag to provide a username") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2023 Dolthub, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package cli | ||
|
||
// Constants for command line flags names. These tend to be used in multiple places, so defining | ||
// them low in the package dependency tree makes sense. | ||
const ( | ||
AbortParam = "abort" | ||
AllFlag = "all" | ||
AllowEmptyFlag = "allow-empty" | ||
AmendFlag = "amend" | ||
AuthorParam = "author" | ||
BranchParam = "branch" | ||
CachedFlag = "cached" | ||
CheckoutCoBranch = "b" | ||
CommitFlag = "commit" | ||
CopyFlag = "copy" | ||
DateParam = "date" | ||
DecorateFlag = "decorate" | ||
DeleteFlag = "delete" | ||
DeleteForceFlag = "D" | ||
DryRunFlag = "dry-run" | ||
ForceFlag = "force" | ||
HardResetParam = "hard" | ||
ListFlag = "list" | ||
MergesFlag = "merges" | ||
MessageArg = "message" | ||
MinParentsFlag = "min-parents" | ||
MoveFlag = "move" | ||
NoCommitFlag = "no-commit" | ||
NoEditFlag = "no-edit" | ||
NoFFParam = "no-ff" | ||
NoPrettyFlag = "no-pretty" | ||
NotFlag = "not" | ||
NumberFlag = "number" | ||
OneLineFlag = "oneline" | ||
OursFlag = "ours" | ||
OutputOnlyFlag = "output-only" | ||
ParentsFlag = "parents" | ||
PasswordFlag = "password" | ||
RemoteParam = "remote" | ||
SetUpstreamFlag = "set-upstream" | ||
ShallowFlag = "shallow" | ||
ShowIgnoredFlag = "ignored" | ||
SkipEmptyFlag = "skip-empty" | ||
SoftResetParam = "soft" | ||
SquashParam = "squash" | ||
TheirsFlag = "theirs" | ||
TrackFlag = "track" | ||
UpperCaseAllFlag = "ALL" | ||
UserFlag = "user" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.