Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(hive): support different kerberos realms (#12898)
Azusainb1c09814980d
diff --git a/backend/plugin/db/hive/hive.go b/backend/plugin/db/hive/hive.goindex e2a9fc6f68..404fd36bae 100644--- a/backend/plugin/db/hive/hive.go+++ b/backend/plugin/db/hive/hive.go@@ -61,13 +61,6 @@ func (d *Driver) Open(_ context.Context, _ storepb.Engine, config db.ConnectionCif !config.SASLConfig.Check() {return nil, errors.New("SASL settings error")}-- db.KrbEnvLock()- defer db.KrbEnvUnlock()-- if err := config.SASLConfig.InitEnv(); err != nil {- return nil, errors.Wrapf(err, "failed to init SASL environment")- }pool, err := CreateHiveConnPool(numMaxConn, &config)if err != nil {return nil, err@@ -165,10 +158,6 @@ func (d *Driver) RunStatement(ctx context.Context, _ *sql.Conn, statementsStr streturn nil, errors.Wrapf(err, "failed to split statements")}- if err := SetRole(ctx, d.conn, "admin"); err != nil {- return nil, err- }-for _, statement := range statements {statementStr := strings.TrimRight(statement.Text, ";")diff --git a/backend/plugin/db/hive/hive_conn_pool.go b/backend/plugin/db/hive/hive_conn_pool.goindex 47a5f1caba..0c21a7b8a4 100644--- a/backend/plugin/db/hive/hive_conn_pool.go+++ b/backend/plugin/db/hive/hive_conn_pool.go@@ -19,10 +19,9 @@ type ConnPool interface {}type FixedConnPool struct {- Host string- Port int+ BasicConfig *db.ConnectionConfigHiveConfig *gohive.ConnectConfiguration- SASLConfig db.SASLConfig+ Port intRWMutex sync.RWMutexConnections chan *gohive.ConnectionIsActivated bool@@ -38,31 +37,38 @@ func CreateHiveConnPool(*FixedConnPool, error,) {conns := make(chan *gohive.Connection, numMaxConn)-hiveConfig := gohive.NewConnectConfiguration()hiveConfig.Database = config.Database- port, err := strconv.Atoi(config.Port)- if err != nil {- return nil, errors.Errorf("conversion failure for 'port' [string -> int]")- }-- // SASL settings.switch t := config.SASLConfig.(type) {case *db.KerberosConfig:- // Kerberos.hiveConfig.Hostname = t.InstancehiveConfig.Service = t.Primary+case *db.PlainSASLConfig:- // Plain.hiveConfig.Username = t.UsernamehiveConfig.Password = t.Password+default:return nil, errors.Errorf("invalid SASL config")}+ port, err := strconv.Atoi(config.Port)+ if err != nil {+ return nil, errors.Errorf("conversion failure for 'port' [string -> int]")+ }++ saslTypName := config.SASLConfig.GetTypeName()+ if saslTypName == db.SASLTypeKerberos {+ db.KrbEnvLock()+ defer db.KrbEnvUnlock()+ if err := config.SASLConfig.InitEnv(); err != nil {+ return nil, errors.Wrapf(err, "failed to init SASL environment")+ }+ }+for i := 0; i < numMaxConn; i++ {- conn, err := gohive.Connect(config.Host, port, config.SASLConfig.GetTypeName(), hiveConfig)+ conn, err := gohive.Connect(config.Host, port, string(saslTypName), hiveConfig)// release resources if err.if err != nil || conn == nil {if err == nil {@@ -86,14 +92,13 @@ func CreateHiveConnPool(}return &FixedConnPool{- Host: config.Host,- Port: port,- HiveConfig: hiveConfig,- SASLConfig: config.SASLConfig,RWMutex: sync.RWMutex{},Connections: conns,IsActivated: true,NumMaxConns: numMaxConn,+ BasicConfig: config,+ HiveConfig: hiveConfig,+ Port: port,},nil}@@ -112,8 +117,16 @@ func (pool *FixedConnPool) Get(dbName string) (*gohive.Connection, error) {case conn = <-pool.Connections:default:+ saslTypName := pool.BasicConfig.SASLConfig.GetTypeName()+ if saslTypName == db.SASLTypeKerberos {+ db.KrbEnvLock()+ defer db.KrbEnvUnlock()+ if err := pool.BasicConfig.SASLConfig.InitEnv(); err != nil {+ return nil, errors.Wrapf(err, "failed to init SASL environment")+ }+ }var err error- conn, err = gohive.Connect(pool.Host, pool.Port, pool.SASLConfig.GetTypeName(), pool.HiveConfig)+ conn, err = gohive.Connect(pool.BasicConfig.Host, pool.Port, string(saslTypName), pool.HiveConfig)if err != nil {return nil, errors.Wrapf(err, "failed to get Hive connection")}diff --git a/backend/plugin/db/sasl.go b/backend/plugin/db/sasl.goindex 679baca090..bc8ac68c12 100644--- a/backend/plugin/db/sasl.go+++ b/backend/plugin/db/sasl.go@@ -4,17 +4,34 @@ import ("fmt""os""os/exec"+ "runtime""strings""sync""github.com/pkg/errors")+// 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))+ }+}++type SASLType string++const (+ SASLTypeNone SASLType = "NONE"+ SASLTypeKerberos SASLType = "KERBEROS"+)+type SASLConfig interface {InitEnv() errorCheck() bool// used for gohive.- GetTypeName() string+ GetTypeName() SASLType}type Realm struct {@@ -35,17 +52,16 @@ type KerberosEnv struct {KrbConfPath stringKeytabPath stringKinitBinPath string- realms map[string]Realm+ CurrRealm *Realm}var (_ SASLConfig = &KerberosConfig{}singletonEnv = KerberosEnv{- KrbConfPath: "/tmp/krb5.conf",- KeytabPath: "/tmp/tmp.keytab",+ KrbConfPath: DftKrbConfPath,+ KeytabPath: DftKeytabPath,// be careful of your environment variables.- KinitBinPath: "kinit",- realms: map[string]Realm{},+ KinitBinPath: DftKinitBinPath,krbEnvMutex: &sync.Mutex{},}@@ -53,15 +69,21 @@ var (// 'root/[email protected]' or '[email protected]'.PrincipalWithoutInstanceFmt = "%s@%s"PrincipalWithInstanceFmt = "%s/%s@%s"- // KrbConfRealmFmt is the content format for krb5.conf, for example:+ // content format of a krb5.conf file:+ // [libdefaults]+ // default_realm = {realm}// [realms]// {realm} = {// kdc = {transport_protocol}/{host}:{port}// }- KrbConfRealmKeyword = "[realms]"- KrbConfRealmFmt = "\t%s = {\n\t\tkdc = %s%s:%s\n\t}\n"+ KrbConfLibDftKeyword = "[libdefaults]\n"+ KrbConfDftRealmFmt = "\tdefault_realm = %s\n"+ KrbConfRealmKeyword = "[realms]\n"+ KrbConfRealmFmt = "\t%s = {\n\t\tkdc = %s%s:%s\n\t}\n"// We have to specify the path of 'krb5.conf' for the 'kinit' command.- KrbConfEnvVarFmt = "KRB5_CONFIG=%s"+ DftKrbConfPath = "/tmp/krb5.conf"+ DftKeytabPath = "/tmp/tmp.keytab"+ DftKinitBinPath = "kinit")// let users manually solve the resource competition problem.@@ -74,13 +96,8 @@ func KrbEnvUnlock() {}func (krbConfig *KerberosConfig) InitEnv() error {- // KDCs can use either 'tcp' or 'udp' as their transport protocol.- if krbConfig.Realm.KDCTransportProtocol != "udp" && krbConfig.Realm.KDCTransportProtocol != "tcp" {- return errors.Errorf("invalid transport protocol for KDC connection: %s", krbConfig.Realm.KDCTransportProtocol)- }-// Create tmp krb5.conf.- if err := singletonEnv.AddRealm(krbConfig.Realm); err != nil {+ if err := singletonEnv.SetRealm(krbConfig.Realm); err != nil {return err}@@ -108,8 +125,7 @@ func (krbConfig *KerberosConfig) InitEnv() error {principal = fmt.Sprintf(PrincipalWithInstanceFmt, krbConfig.Primary, krbConfig.Instance, krbConfig.Realm.Name)}args := []string{- fmt.Sprintf(KrbConfEnvVarFmt, singletonEnv.KrbConfPath),- "kinit",+ singletonEnv.KinitBinPath,"-kt",singletonEnv.KeytabPath,principal,@@ -123,7 +139,15 @@ func (krbConfig *KerberosConfig) InitEnv() error {return nil}-func (*KerberosEnv) AddRealm(realm Realm) error {+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+ }+// Create a krb5.conf file.file, err := os.Create(singletonEnv.KrbConfPath)if err != nil {@@ -132,14 +156,21 @@ func (*KerberosEnv) AddRealm(realm Realm) error {defer file.Close()// Write configurations.- if _, err = file.WriteString(fmt.Sprintf("%s\n", KrbConfRealmKeyword)); err != nil {+ 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}var kdcConnStr string- if realm.KDCTransportProtocol == "tcp" {- // This will force kinit client to communicate with KDC over tcp as Mac+ 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)@@ -153,14 +184,18 @@ func (*KerberosEnv) AddRealm(realm Realm) error {// 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}return true}-func (*KerberosConfig) GetTypeName() string {- return "KERBEROS"+func (*KerberosConfig) GetTypeName() SASLType {+ return SASLTypeKerberos}type PlainSASLConfig struct {@@ -174,8 +209,8 @@ func (*PlainSASLConfig) Check() bool {return true}-func (*PlainSASLConfig) GetTypeName() string {- return "NONE"+func (*PlainSASLConfig) GetTypeName() SASLType {+ return SASLTypeNone}func (*PlainSASLConfig) InitEnv() error {diff --git a/scripts/Dockerfile b/scripts/Dockerfileindex b27a9d6465..9cc63dda66 100644--- a/scripts/Dockerfile+++ b/scripts/Dockerfile@@ -100,7 +100,7 @@ RUN mkdir -p /var/opt/bytebaseRUN mkdir -p /var/opt/bytebase/pgdata-demo# Our HEALTHCHECK instruction in dockerfile needs curl.# Install psmisc to use killall command in demo.sh used by render.com.-RUN apt-get update && apt-get install -y locales curl psmisc procps libncurses5+RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales curl psmisc procps libncurses5 krb5-user# Generate en_US.UTF-8 locale which is needed to start postgres server.# Fix the posgres server issue (invalid value for parameter "lc_messages": "en_US.UTF-8").RUN echo "en_US.UTF-8 UTF-8" > /etc/locale.gen && locale-gen