Skip to content
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

*: add support for --initialize-secure and --initialize-insecure #28487

Merged
merged 5 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ type Security struct {
// EnableSEM prevents SUPER users from having full access.
EnableSEM bool `toml:"enable-sem" json:"enable-sem"`
// Allow automatic TLS certificate generation
AutoTLS bool `toml:"auto-tls" json:"auto-tls"`
MinTLSVersion string `toml:"tls-version" json:"tls-version"`
RSAKeySize int `toml:"rsa-key-size" json:"rsa-key-size"`
AutoTLS bool `toml:"auto-tls" json:"auto-tls"`
MinTLSVersion string `toml:"tls-version" json:"tls-version"`
RSAKeySize int `toml:"rsa-key-size" json:"rsa-key-size"`
SecureBootstrap bool `toml:"secure-bootstrap" json:"secure-bootstrap"`
}

// The ErrConfigValidationFailed error is used so that external callers can do a type assertion
Expand Down
17 changes: 14 additions & 3 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/hex"
"flag"
"fmt"
osuser "os/user"
"runtime/debug"
"strconv"
"strings"
Expand Down Expand Up @@ -1688,10 +1689,20 @@ func doDDLWorks(s Session) {
// TODO: sanitize.
func doDMLWorks(s Session) {
mustExecute(s, "BEGIN")

// Insert a default user with empty password.
mustExecute(s, `INSERT HIGH_PRIORITY INTO mysql.user VALUES
if config.GetGlobalConfig().Security.SecureBootstrap {
// If secure bootstrap is enabled, we create a root@localhost account which can login with auth_socket.
// i.e. mysql -S /tmp/tidb.sock -uroot
// The auth_socket plugin will validate that the user matches $USER.
u, err := osuser.Current()
if err != nil {
logutil.BgLogger().Fatal("failed to read current user. unable to secure bootstrap.", zap.Error(err))
}
mustExecute(s, `INSERT HIGH_PRIORITY INTO mysql.user VALUES
("localhost", "root", %?, "auth_socket", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`, u.Username)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Windows? there "auth_socket" won't work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a check for windows and refuse to use secure bootstrap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dveeden I've added validation to the option so it is not supported on Windows. PTAL again.

} else {
mustExecute(s, `INSERT HIGH_PRIORITY INTO mysql.user VALUES
("%", "root", "", "mysql_native_password", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "Y", "N", "Y", "Y", "Y", "Y", "Y", "Y", "Y")`)
}

// Init global system variables table.
values := make([]string, 0, len(variable.GetSysVars()))
Expand Down
29 changes: 29 additions & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ const (
nmProxyProtocolNetworks = "proxy-protocol-networks"
nmProxyProtocolHeaderTimeout = "proxy-protocol-header-timeout"
nmAffinityCPU = "affinity-cpus"

nmInitializeSecure = "initialize-secure"
nmInitializeInsecure = "initialize-insecure"
)

var (
Expand Down Expand Up @@ -152,6 +155,10 @@ var (
// PROXY Protocol
proxyProtocolNetworks = flag.String(nmProxyProtocolNetworks, "", "proxy protocol networks allowed IP or *, empty mean disable proxy protocol support")
proxyProtocolHeaderTimeout = flag.Uint(nmProxyProtocolHeaderTimeout, 5, "proxy protocol header read timeout, unit is second.")

// Security
initializeSecure = flagBoolean(nmInitializeSecure, false, "bootstrap tidb-server in secure mode")
initializeInsecure = flagBoolean(nmInitializeInsecure, true, "bootstrap tidb-server in insecure mode")
)

func main() {
Expand Down Expand Up @@ -505,6 +512,28 @@ func overrideConfig(cfg *config.Config) {
if actualFlags[nmProxyProtocolHeaderTimeout] {
cfg.ProxyProtocol.HeaderTimeout = *proxyProtocolHeaderTimeout
}

// Sanity check: can't specify both options
if actualFlags[nmInitializeSecure] && actualFlags[nmInitializeInsecure] {
err = fmt.Errorf("the options --initialize-insecure and --initialize-secure are mutually exclusive")
terror.MustNil(err)
}
// The option --initialize-secure=true ensures that a secure bootstrap is used.
if actualFlags[nmInitializeSecure] {
cfg.Security.SecureBootstrap = *initializeSecure
}
// The option --initialize-insecure=true/false was used.
// Store the inverted value of this to the secure bootstrap cfg item
if actualFlags[nmInitializeInsecure] {
cfg.Security.SecureBootstrap = !*initializeInsecure
}
// Secure bootstrap initializes with Socket authentication
// which is not supported on windows. Only the insecure bootstrap
// method is supported.
if runtime.GOOS == "windows" && cfg.Security.SecureBootstrap {
err = fmt.Errorf("the option --initialize-secure is not supported on Windows")
terror.MustNil(err)
}
}

func setGlobalVars() {
Expand Down