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

playground: auto tiproxy sign certs #2372

Merged
merged 3 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions components/playground/instance/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ type TiDBInstance struct {
instance
pds []*PDInstance
Process
enableBinlog bool
isDisaggMode bool
tiproxyCertDir string
enableBinlog bool
isDisaggMode bool
}

// NewTiDBInstance return a TiDBInstance
func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int, pds []*PDInstance, enableBinlog bool, isDisaggMode bool) *TiDBInstance {
func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int, pds []*PDInstance, tiproxyCertDir string, enableBinlog bool, isDisaggMode bool) *TiDBInstance {
if port <= 0 {
port = 4000
}
Expand All @@ -48,9 +49,10 @@ func NewTiDBInstance(binPath string, dir, host, configPath string, id, port int,
StatusPort: utils.MustGetFreePort("0.0.0.0", 10080),
ConfigPath: configPath,
},
pds: pds,
enableBinlog: enableBinlog,
isDisaggMode: isDisaggMode,
tiproxyCertDir: tiproxyCertDir,
pds: pds,
enableBinlog: enableBinlog,
isDisaggMode: isDisaggMode,
}
}

Expand Down
14 changes: 14 additions & 0 deletions components/playground/instance/tidb_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

package instance

import (
"os"
"path/filepath"
)

func (inst *TiDBInstance) getConfig() map[string]any {
config := make(map[string]any)
config["security.auto-tls"] = true
Expand All @@ -22,5 +27,14 @@ func (inst *TiDBInstance) getConfig() map[string]any {
config["disaggregated-tiflash"] = true
}

tiproxyCrtPath := filepath.Join(inst.tiproxyCertDir, "tiproxy.crt")
tiproxyKeyPath := filepath.Join(inst.tiproxyCertDir, "tiproxy.key")
_, err1 := os.Stat(tiproxyCrtPath)
_, err2 := os.Stat(tiproxyKeyPath)
if err1 == nil && err2 == nil {
config["security.session-token-signing-cert"] = tiproxyCrtPath
config["security.session-token-signing-key"] = tiproxyKeyPath
}

return config
}
33 changes: 33 additions & 0 deletions components/playground/instance/tiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ package instance

import (
"context"
"encoding/pem"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/pingcap/tiup/pkg/cluster/spec"
"github.com/pingcap/tiup/pkg/crypto"
tiupexec "github.com/pingcap/tiup/pkg/exec"
"github.com/pingcap/tiup/pkg/utils"
)
Expand All @@ -35,6 +37,37 @@ type TiProxy struct {

var _ Instance = &TiProxy{}

// GenTiProxySessionCerts will create a self-signed certs for TiProxy session migration. NOTE that this cert is directly used by TiDB.
func GenTiProxySessionCerts(dir string) error {
if _, err := os.Stat(filepath.Join(dir, "tiproxy.crt")); err == nil {
return nil
}

ca, err := crypto.NewCA("tiproxy")
if err != nil {
return err
}
privKey, err := crypto.NewKeyPair(crypto.KeyTypeRSA, crypto.KeySchemeRSASSAPSSSHA256)
if err != nil {
return err
}
csr, err := privKey.CSR("tiproxy", "tiproxy", nil, nil)
if err != nil {
return err
}
cert, err := ca.Sign(csr)
if err != nil {
return err
}
if err := utils.SaveFileWithBackup(filepath.Join(dir, "tiproxy.key"), privKey.Pem(), ""); err != nil {
return err
}
return utils.SaveFileWithBackup(filepath.Join(dir, "tiproxy.crt"), pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert,
}), "")
}

// NewTiProxy create a TiProxy instance.
func NewTiProxy(binPath string, dir, host, configPath string, id int, port int, pds []*PDInstance) *TiProxy {
if port <= 0 {
Expand Down
5 changes: 4 additions & 1 deletion components/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ func (p *Playground) addInstance(componentID string, pdRole instance.PDRole, tif
p.rms = append(p.rms, inst)
}
case spec.ComponentTiDB:
inst := instance.NewTiDBInstance(cfg.BinPath, dir, host, cfg.ConfigPath, id, cfg.Port, p.pds, p.enableBinlog(), p.bootOptions.Mode == "tidb-disagg")
inst := instance.NewTiDBInstance(cfg.BinPath, dir, host, cfg.ConfigPath, id, cfg.Port, p.pds, dataDir, p.enableBinlog(), p.bootOptions.Mode == "tidb-disagg")
ins = inst
p.tidbs = append(p.tidbs, inst)
case spec.ComponentTiKV:
Expand All @@ -744,6 +744,9 @@ func (p *Playground) addInstance(componentID string, pdRole instance.PDRole, tif
ins = inst
p.tiflashs = append(p.tiflashs, inst)
case spec.ComponentTiProxy:
if err := instance.GenTiProxySessionCerts(dataDir); err != nil {
return nil, err
}
inst := instance.NewTiProxy(cfg.BinPath, dir, host, cfg.ConfigPath, id, cfg.Port, p.pds)
ins = inst
p.tiproxys = append(p.tiproxys, inst)
Expand Down
Loading