Skip to content

Commit

Permalink
cli, sql: allow usernames to start with digits
Browse files Browse the repository at this point in the history
Allows CockroachDB usernames to start with digits.

Fixes cockroachdb#42155.
  • Loading branch information
georgebuckerfield committed Nov 13, 2019
1 parent 3d3432c commit 8ac00fd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 18 additions & 8 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,7 @@ func Example_user() {
c.Run("user set f,oo")
c.Run("user set foo,")
c.Run("user set 0foo")
c.Run("user set 0123")
c.Run("user set foo0")
c.Run("user set f0oo")
c.Run("user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof")
Expand Down Expand Up @@ -1483,16 +1484,19 @@ func Example_user() {
// CREATE USER 1
// user set ,foo
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// pq: username ",foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// pq: username ",foo" invalid; usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// user set f,oo
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// pq: username "f,oo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// pq: username "f,oo" invalid; usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// user set foo,
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// pq: username "foo," invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// pq: username "foo," invalid; usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// user set 0foo
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// pq: username "0foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// CREATE USER 1
// user set 0123
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// CREATE USER 1
// user set foo0
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// CREATE USER 1
Expand All @@ -1501,7 +1505,7 @@ func Example_user() {
// CREATE USER 1
// user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// pq: username "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// pq: username "foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof" invalid; usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// user set foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo
// warning: This command is deprecated. Use CREATE USER or ALTER USER ... WITH PASSWORD ... in a SQL session.
// CREATE USER 1
Expand All @@ -1518,6 +1522,8 @@ func Example_user() {
// warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
// user_name
// +-----------------------------------------------------------------+
// 0123
// 0foo
// _foo
// and
// f0oo
Expand All @@ -1529,14 +1535,16 @@ func Example_user() {
// root
// table
// ομηρος
// (11 rows)
// (13 rows)
// user rm foo
// warning: This command is deprecated. Use DROP USER or DROP ROLE in a SQL session.
// DROP USER 1
// user ls --format=table
// warning: This command is deprecated. Use SHOW USERS or SHOW ROLES in a SQL session.
// user_name
// +-----------------------------------------------------------------+
// 0123
// 0foo
// _foo
// and
// f0oo
Expand All @@ -1547,7 +1555,7 @@ func Example_user() {
// root
// table
// ομηρος
// (10 rows)
// (12 rows)
// sql -e drop database defaultdb
// DROP DATABASE
// user set foo
Expand All @@ -1562,12 +1570,14 @@ func Example_cert() {
c.RunWithCAArgs([]string{"cert", "create-client", "foo"})
c.RunWithCAArgs([]string{"cert", "create-client", "Ομηρος"})
c.RunWithCAArgs([]string{"cert", "create-client", "0foo"})
c.RunWithCAArgs([]string{"cert", "create-client", ",foo"})

// Output:
// cert create-client foo
// cert create-client Ομηρος
// cert create-client 0foo
// failed to generate client certificate and key: username "0foo" invalid; usernames are case insensitive, must start with a letter or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
// cert create-client ,foo
// failed to generate client certificate and key: username ",foo" invalid; usernames are case insensitive, must start with a letter, digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters
}

// TestFlagUsage is a basic test to make sure the fragile
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/create_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ func (*CreateUserNode) Close(context.Context) {}
// FastPathResults implements the planNodeFastPath interface.
func (n *CreateUserNode) FastPathResults() (int, bool) { return n.run.rowsAffected, true }

const usernameHelp = "usernames are case insensitive, must start with a letter " +
"or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters"
const usernameHelp = "usernames are case insensitive, must start with a letter, " +
"digit or underscore, may contain letters, digits, dashes, or underscores, and must not exceed 63 characters"

var usernameRE = regexp.MustCompile(`^[\p{Ll}_][\p{Ll}0-9_-]{0,62}$`)
var usernameRE = regexp.MustCompile(`^[\p{Ll}0-9_][\p{Ll}0-9_-]{0,62}$`)

var blacklistedUsernames = map[string]struct{}{
security.NodeUser: {},
Expand Down

0 comments on commit 8ac00fd

Please sign in to comment.