Skip to content

Commit

Permalink
*: add support for --initialize-secure and --initialize-insecure (#28487
Browse files Browse the repository at this point in the history
)
  • Loading branch information
morgo authored Oct 27, 2021
1 parent 1d1f1d2 commit da76e34
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
7 changes: 4 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,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 @@ -1700,10 +1701,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)
} 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

0 comments on commit da76e34

Please sign in to comment.