Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: kbr config (#15466)

Danny Xuee43aa13eb84
diff --git a/backend/component/dbfactory/dbfactory.go b/backend/component/dbfactory/dbfactory.go
index 993cec0556..fda9c7c69b 100644
--- a/backend/component/dbfactory/dbfactory.go
+++ b/backend/component/dbfactory/dbfactory.go
@@ -50,23 +50,6 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, instance *store.Ins
if err != nil {
return nil, err
}
- var dbSaslConfig db.SASLConfig
- switch t := dataSource.GetSaslConfig().GetMechanism().(type) {
- case *storepb.SASLConfig_KrbConfig:
- dbSaslConfig = &db.KerberosConfig{
- Primary: t.KrbConfig.Primary,
- Instance: t.KrbConfig.Instance,
- Realm: db.Realm{
- Name: t.KrbConfig.Realm,
- KDCHost: t.KrbConfig.KdcHost,
- KDCPort: t.KrbConfig.KdcPort,
- KDCTransportProtocol: t.KrbConfig.KdcTransportProtocol,
- },
- Keytab: t.KrbConfig.Keytab,
- }
- default:
- dbSaslConfig = nil
- }
connectionContext.InstanceID = instance.ResourceID
connectionContext.EngineVersion = instance.Metadata.GetVersion()
@@ -82,7 +65,6 @@ func (d *DBFactory) GetDataSourceDriver(ctx context.Context, instance *store.Ins
ConnectionContext: connectionContext,
Password: password,
- SASLConfig: dbSaslConfig,
MaximumSQLResultSize: maximumSQLResultSize,
},
)
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index c3bd41b2d3..c4a45f7d31 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -136,10 +136,6 @@ type ConnectionConfig struct {
DataSource *storepb.DataSource
ConnectionContext ConnectionContext
Password string
-
- // Only used for Hive.
- SASLConfig SASLConfig
-
// The maximum number of bytes for sql results in response body.
MaximumSQLResultSize int64
}
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.go
index c85dc9d340..fd106a616d 100644
--- a/backend/plugin/db/hive/hive.go
+++ b/backend/plugin/db/hive/hive.go
@@ -59,19 +59,24 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
var authConnParam = "NONE"
hiveConfig := gohive.NewConnectConfiguration()
- switch t := config.SASLConfig.(type) {
- case *db.KerberosConfig:
- hiveConfig.Hostname = t.Instance
- hiveConfig.Service = t.Primary
- db.KrbEnvLock()
- defer db.KrbEnvUnlock()
- if err := config.SASLConfig.InitEnv(); err != nil {
+ if t, ok := config.DataSource.GetSaslConfig().GetMechanism().(*storepb.SASLConfig_KrbConfig); ok {
+ saslConfig := &db.KerberosConfig{
+ Primary: t.KrbConfig.Primary,
+ Instance: t.KrbConfig.Instance,
+ Realm: db.Realm{
+ Name: t.KrbConfig.Realm,
+ KDCHost: t.KrbConfig.KdcHost,
+ KDCPort: t.KrbConfig.KdcPort,
+ KDCTransportProtocol: t.KrbConfig.KdcTransportProtocol,
+ },
+ Keytab: t.KrbConfig.Keytab,
+ }
+ hiveConfig.Hostname = saslConfig.Instance
+ hiveConfig.Service = saslConfig.Primary
+ if err := saslConfig.InitEnv(); err != nil {
return nil, errors.Wrapf(err, "failed to init SASL environment")
}
authConnParam = "KERBEROS"
- case *db.PlainSASLConfig:
- hiveConfig.Username = t.Username
- hiveConfig.Password = t.Password
}
conn, err := gohive.Connect(config.DataSource.Host, port, authConnParam, hiveConfig)
diff --git a/backend/plugin/db/sasl.go b/backend/plugin/db/sasl.go
index d2bba6c60f..ab28ff825c 100644
--- a/backend/plugin/db/sasl.go
+++ b/backend/plugin/db/sasl.go
@@ -15,25 +15,11 @@ import (
// 1. The 'kinit' command in the subprocess.
// 2. gohive.Connect() in the same process.
func init() {
- if err := os.Setenv("KRB5_CONFIG", DftKrbConfPath); err != nil {
- panic(fmt.Sprintf("failed to set env %s: %s", "KRB5_CONFIG", DftKrbConfPath))
+ if err := os.Setenv("KRB5_CONFIG", dftKrbConfPath); err != nil {
+ panic(fmt.Sprintf("failed to set env %s: %s", "KRB5_CONFIG", dftKrbConfPath))
}
}
-type SASLType string
-
-const (
- SASLTypeNone SASLType = "NONE"
- SASLTypeKerberos SASLType = "KERBEROS"
-)
-
-type SASLConfig interface {
- InitEnv() error
- Check() bool
- // used for gohive.
- GetTypeName() SASLType
-}
-
type Realm struct {
Name string
KDCHost string
@@ -48,54 +34,30 @@ type KerberosConfig struct {
}
type KerberosEnv struct {
- krbEnvMutex *sync.Mutex
- KrbConfPath string
- KeytabPath string
+ krbConfPath string
KinitBinPath string
CurrRealm *Realm
}
var (
- _ SASLConfig = &KerberosConfig{}
- singletonEnv = KerberosEnv{
- KrbConfPath: DftKrbConfPath,
- KeytabPath: DftKeytabPath,
- // be careful of your environment variables.
- KinitBinPath: DftKinitBinPath,
- krbEnvMutex: &sync.Mutex{},
+ lock = sync.Mutex{}
+ singletonEnv = KerberosEnv{
+ krbConfPath: dftKrbConfPath,
}
// The principal is in {primary}/{instance}@{realm} or {primary}@{realm} format, for example:
- PrincipalWithoutInstanceFmt = "%s@%s"
- PrincipalWithInstanceFmt = "%s/%s@%s"
- // content format of a krb5.conf file:
- // [libdefaults]
- // default_realm = {realm}
- // [realms]
- // {realm} = {
- // kdc = {transport_protocol}/{host}:{port}
- // }
- KrbConfLibDftKeyword = "[libdefaults]\n"
- KrbConfDftRealmFmt = "\tdefault_realm = %s\n"
- KrbConfRealmKeyword = "[realms]\n"
- KrbConfRealmFmt = "\t%s = {\n\t\tkdc = %s%s:%s\n\t}\n"
+ principalWithoutInstanceFmt = "%s@%s"
+ principalWithInstanceFmt = "%s/%s@%s"
// We have to specify the path of 'krb5.conf' for the 'kinit' command.
- DftKrbConfPath = "/tmp/krb5.conf"
- DftKeytabPath = "/tmp/tmp.keytab"
- DftKinitBinPath = "kinit"
+ dftKrbConfPath = "/tmp/krb5.conf"
+ dftKeytabPath = "/tmp/tmp.keytab"
)
-// let users manually solve the resource competition problem.
-func KrbEnvLock() {
- singletonEnv.krbEnvMutex.Lock()
-}
-
-func KrbEnvUnlock() {
- singletonEnv.krbEnvMutex.Unlock()
-}
-
func (krbConfig *KerberosConfig) InitEnv() error {
+ lock.Lock()
+ defer lock.Unlock()
+
// Create tmp krb5.conf.
if err := singletonEnv.SetRealm(krbConfig.Realm); err != nil {
return err
@@ -103,7 +65,7 @@ func (krbConfig *KerberosConfig) InitEnv() error {
// Save .keytab file as a temporary file.
if err := func() error {
- keytabFile, err := os.Create(singletonEnv.KeytabPath)
+ keytabFile, err := os.Create(dftKeytabPath)
if err != nil {
return err
}
@@ -120,14 +82,14 @@ func (krbConfig *KerberosConfig) InitEnv() error {
var cmd *exec.Cmd
var principal string
if krbConfig.Instance == "" {
- principal = fmt.Sprintf(PrincipalWithoutInstanceFmt, krbConfig.Primary, krbConfig.Realm.Name)
+ principal = fmt.Sprintf(principalWithoutInstanceFmt, krbConfig.Primary, krbConfig.Realm.Name)
} else {
- principal = fmt.Sprintf(PrincipalWithInstanceFmt, krbConfig.Primary, krbConfig.Instance, krbConfig.Realm.Name)
+ principal = fmt.Sprintf(principalWithInstanceFmt, krbConfig.Primary, krbConfig.Instance, krbConfig.Realm.Name)
}
args := []string{
- singletonEnv.KinitBinPath,
+ "kinit",
"-kt",
- singletonEnv.KeytabPath,
+ dftKeytabPath,
principal,
}
cmd = exec.Command("bash", "-c", strings.Join(args, " "))
@@ -148,71 +110,42 @@ func (e *KerberosEnv) SetRealm(realm Realm) error {
return nil
}
- // Create a krb5.conf file.
- file, err := os.Create(singletonEnv.KrbConfPath)
- if err != nil {
- return err
- }
- defer file.Close()
+ // content format of a krb5.conf file:
+ // [libdefaults]
+ // default_realm = {realm}
+ // [realms]
+ // {realm} = {
+ // kdc = {transport_protocol}/{host}:{port}
+ // }
- // Write configurations.
- if _, err = file.WriteString(KrbConfLibDftKeyword); err != nil {
- return err
- }
- if _, err = file.WriteString(fmt.Sprintf(KrbConfDftRealmFmt, realm.Name)); err != nil {
- return err
- }
- if _, err = file.WriteString(KrbConfRealmKeyword); err != nil {
- return err
+ protocol := ""
+ // This will force kinit client to communicate with KDC over tcp as Darwin
+ // doesn't has fall-down mechanism if it fails to communicate over udp.
+ // However, Linux doesn't need this.
+ if realm.KDCTransportProtocol == "tcp" && runtime.GOOS == "darwin" {
+ protocol = "tcp/"
}
- var kdcConnStr string
- if realm.KDCTransportProtocol == "tcp" && runtime.GOOS == "darwin" {
- // This will force kinit client to communicate with KDC over tcp as Darwin
- // doesn't has fall-down mechanism if it fails to communicate over udp.
- // However, Linux doesn't need this.
- kdcConnStr = fmt.Sprintf(KrbConfRealmFmt, realm.Name, "tcp/", realm.KDCHost, realm.KDCPort)
- } else {
- kdcConnStr = fmt.Sprintf(KrbConfRealmFmt, realm.Name, "", realm.KDCHost, realm.KDCPort)
+ content := fmt.Sprintf(`[libdefaults]
+ default_realm = %s
+[realms]
+ %s = {
+ kdc = %s%s:%s
}
- if _, err = file.WriteString(kdcConnStr); err != nil {
+`,
+ realm.Name,
+ realm.Name,
+ protocol, realm.KDCHost, realm.KDCPort,
+ )
+ // Create a krb5.conf file.
+ f, err := os.Create(singletonEnv.krbConfPath)
+ if err != nil {
return err
}
+ defer f.Close()
- return file.Sync()
-}
-
-// check whether Kerberos is enabled and its settings are valid.
-func (krbConfig *KerberosConfig) Check() bool {
- // KDCs can use either 'tcp' or 'udp' as their transport protocol.
- if krbConfig.Realm.KDCTransportProtocol != "udp" && krbConfig.Realm.KDCTransportProtocol != "tcp" {
- return false
- }
- if krbConfig.Primary == "" || krbConfig.Realm.Name == "" || len(krbConfig.Keytab) == 0 || krbConfig.Realm.KDCTransportProtocol == "" {
- return false
+ if _, err = f.WriteString(content); err != nil {
+ return err
}
- return true
-}
-
-func (*KerberosConfig) GetTypeName() SASLType {
- return SASLTypeKerberos
-}
-
-type PlainSASLConfig struct {
- Username string
- Password string
-}
-
-var _ SASLConfig = &PlainSASLConfig{}
-
-func (*PlainSASLConfig) Check() bool {
- return true
-}
-
-func (*PlainSASLConfig) GetTypeName() SASLType {
- return SASLTypeNone
-}
-
-func (*PlainSASLConfig) InitEnv() error {
return nil
}
diff --git a/backend/tests/testcontainer.go b/backend/tests/testcontainer.go
index b51ad2cc44..f1df122e29 100644
--- a/backend/tests/testcontainer.go
+++ b/backend/tests/testcontainer.go
@@ -132,14 +132,18 @@ func getPgContainer(ctx context.Context) (retC *Container, retErr error) {
}
func waitDBPing(ctx context.Context, db *sql.DB) error {
+ started := time.Now()
ticker := time.NewTicker(3 * time.Second)
defer ticker.Stop()
- timeout := time.After(2 * time.Minute)
+ timeout := time.After(10 * time.Minute)
outerLoop:
for {
select {
case <-ticker.C:
if err := db.PingContext(ctx); err == nil {
+ if time.Since(started) > 1*time.Minute {
+ fmt.Printf("Total wait time: %s\n", time.Since(started))
+ }
break outerLoop
}
case <-timeout: