Skip to content

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

refactor: kbr boot (#15474)

Danny Xudf47490857f9
diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.go
index c4a45f7d31..faf32d41de 100644
--- a/backend/plugin/db/driver.go
+++ b/backend/plugin/db/driver.go
@@ -170,32 +170,6 @@ type QueryContext struct {
Option *v1pb.QueryOption
}
-// DatabaseRoleMessage is the API message for database role.
-type DatabaseRoleMessage struct {
- // The role unique name.
- Name string
- // The connection count limit for this role.
- ConnectionLimit int32
- // The expiration for the role's password.
- ValidUntil *string
- // The role attribute.
- Attribute *string
-}
-
-// DatabaseRoleUpsertMessage is the API message for upserting a database role.
-type DatabaseRoleUpsertMessage struct {
- // The role unique name.
- Name string
- // A password is only significant if the client authentication method requires the user to supply a password when connecting to the database.
- Password *string
- // Connection limit can specify how many concurrent connections a role can make. -1 (the default) means no limit.
- ConnectionLimit *int32
- // The VALID UNTIL clause sets a date and time after which the role's password is no longer valid. If this clause is omitted the password will be valid for all time.
- ValidUntil *string
- // The role attribute.
- Attribute *string
-}
-
// Driver is the interface for database driver.
type Driver interface {
// General execution
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.go
index fd106a616d..b977abc1e0 100644
--- a/backend/plugin/db/hive/hive.go
+++ b/backend/plugin/db/hive/hive.go
@@ -60,20 +60,13 @@ func (d *Driver) Open(ctx context.Context, _ storepb.Engine, config db.Connectio
var authConnParam = "NONE"
hiveConfig := gohive.NewConnectConfiguration()
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 {
+ // Kerberos environment mutex.
+ db.Lock.Lock()
+ defer db.Lock.Unlock()
+
+ hiveConfig.Hostname = t.KrbConfig.Instance
+ hiveConfig.Service = t.KrbConfig.Primary
+ if err := db.BootKerberosEnv(t); err != nil {
return nil, errors.Wrapf(err, "failed to init SASL environment")
}
authConnParam = "KERBEROS"
diff --git a/backend/plugin/db/sasl.go b/backend/plugin/db/sasl.go
index ab28ff825c..8e24812e15 100644
--- a/backend/plugin/db/sasl.go
+++ b/backend/plugin/db/sasl.go
@@ -5,147 +5,107 @@ import (
"os"
"os/exec"
"runtime"
- "strings"
"sync"
"github.com/pkg/errors"
+
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// There are two places where the KRB5_CONFIG is needed:
// 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", tmpKrbConfPath); err != nil {
+ panic(fmt.Sprintf("failed to set env %s: %s", "KRB5_CONFIG", tmpKrbConfPath))
}
}
-type Realm struct {
- Name string
- KDCHost string
- KDCPort string
- KDCTransportProtocol string
-}
-type KerberosConfig struct {
- Primary string
- Instance string
- Realm Realm
- Keytab []byte
-}
-
-type KerberosEnv struct {
- krbConfPath string
- KinitBinPath string
- CurrRealm *Realm
-}
-
var (
- lock = sync.Mutex{}
- singletonEnv = KerberosEnv{
- krbConfPath: dftKrbConfPath,
- }
+ Lock = sync.Mutex{}
// The principal is in {primary}/{instance}@{realm} or {primary}@{realm} format, for example:
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"
+ tmpKrbConfPath = "/tmp/krb5.conf"
+ tmpKeytabPath = "/tmp/tmp.keytab"
)
-func (krbConfig *KerberosConfig) InitEnv() error {
- lock.Lock()
- defer lock.Unlock()
-
- // Create tmp krb5.conf.
- if err := singletonEnv.SetRealm(krbConfig.Realm); err != nil {
+func BootKerberosEnv(cfg *storepb.SASLConfig_KrbConfig) error {
+ // Create a krb5.conf file.
+ // content format of a krb5.conf file:
+ // [libdefaults]
+ // default_realm = {realm}
+ // [realms]
+ // {realm} = {
+ // kdc = {transport_protocol}/{host}:{port}
+ // }
+ 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 cfg.KrbConfig.KdcTransportProtocol == "tcp" && runtime.GOOS == "darwin" {
+ protocol = "tcp/"
+ }
+ cfgContent := fmt.Sprintf(`[libdefaults]
+ default_realm = %s
+[realms]
+ %s = {
+ kdc = %s%s:%s
+ }
+`,
+ cfg.KrbConfig.Realm,
+ cfg.KrbConfig.Realm,
+ protocol, cfg.KrbConfig.KdcHost, cfg.KrbConfig.KdcPort,
+ )
+ if err := func() error {
+ f, err := os.Create(tmpKrbConfPath)
+ if err != nil {
+ return err
+ }
+ defer f.Close()
+ if _, err = f.WriteString(cfgContent); err != nil {
+ return err
+ }
+ return nil
+ }(); err != nil {
return err
}
- // Save .keytab file as a temporary file.
+ // Create a tmp .keytab.
if err := func() error {
- keytabFile, err := os.Create(dftKeytabPath)
+ f, err := os.Create(tmpKeytabPath)
if err != nil {
return err
}
- defer keytabFile.Close()
- if n, err := keytabFile.Write(krbConfig.Keytab); err != nil || n != len(krbConfig.Keytab) {
+ defer f.Close()
+ if _, err := f.Write(cfg.KrbConfig.Keytab); err != nil {
return err
}
- return keytabFile.Sync()
+ return nil
}(); err != nil {
return err
}
// kinit -kt {keytab file path} {principal}.
- var cmd *exec.Cmd
var principal string
- if krbConfig.Instance == "" {
- principal = fmt.Sprintf(principalWithoutInstanceFmt, krbConfig.Primary, krbConfig.Realm.Name)
+ if cfg.KrbConfig.Instance == "" {
+ principal = fmt.Sprintf(principalWithoutInstanceFmt, cfg.KrbConfig.Primary, cfg.KrbConfig.Realm)
} else {
- principal = fmt.Sprintf(principalWithInstanceFmt, krbConfig.Primary, krbConfig.Instance, krbConfig.Realm.Name)
+ principal = fmt.Sprintf(principalWithInstanceFmt, cfg.KrbConfig.Primary, cfg.KrbConfig.Instance, cfg.KrbConfig.Realm)
}
args := []string{
"kinit",
"-kt",
- dftKeytabPath,
+ tmpKeytabPath,
principal,
}
- cmd = exec.Command("bash", "-c", strings.Join(args, " "))
-
+ cmd := exec.Command("kinit", args...)
if output, err := cmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "failed to execute command kinit: %s", output)
}
return nil
}
-
-func isRealmSettingsEqual(a, b Realm) bool {
- return a.KDCHost == b.KDCHost && a.KDCTransportProtocol == b.KDCTransportProtocol && a.KDCPort == b.KDCPort && a.Name == b.Name
-}
-
-func (e *KerberosEnv) SetRealm(realm Realm) error {
- if e.CurrRealm != nil && isRealmSettingsEqual(*e.CurrRealm, realm) {
- return nil
- }
-
- // content format of a krb5.conf file:
- // [libdefaults]
- // default_realm = {realm}
- // [realms]
- // {realm} = {
- // kdc = {transport_protocol}/{host}:{port}
- // }
-
- 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/"
- }
-
- content := fmt.Sprintf(`[libdefaults]
- default_realm = %s
-[realms]
- %s = {
- kdc = %s%s:%s
- }
-`,
- 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()
-
- if _, err = f.WriteString(content); err != nil {
- return err
- }
- return nil
-}