Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: support list role OpenAPI for "bytebase_database_role" data source in provider (#3939)
ecmadao304a5f540e1d
diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.goindex 043559ad3c..51fef75aad 100644--- a/plugin/advisor/utils_for_tests.go+++ b/plugin/advisor/utils_for_tests.go@@ -333,6 +333,11 @@ func (*MockDriver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, erreturn nil, nil}+// ListRole lists the role.+func (*MockDriver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, nil+}+// DeleteRole deletes the role by name.func (*MockDriver) DeleteRole(_ context.Context, _ string) error {return nildiff --git a/plugin/db/clickhouse/role.go b/plugin/db/clickhouse/role.goindex 5b66fa6a4d..bed731f225 100644--- a/plugin/db/clickhouse/role.go+++ b/plugin/db/clickhouse/role.go@@ -23,6 +23,11 @@ func (*Driver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, error)return nil, errors.Errorf("find role for ClickHouse is not implemented yet")}+// ListRole lists the role.+func (*Driver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, errors.Errorf("list role for ClickHouse is not implemented yet")+}+// DeleteRole deletes the role by name.func (*Driver) DeleteRole(_ context.Context, _ string) error {return errors.Errorf("delete role for ClickHouse is not implemented yet")diff --git a/plugin/db/driver.go b/plugin/db/driver.goindex 4db49dcc10..1cc4eb90db 100644--- a/plugin/db/driver.go+++ b/plugin/db/driver.go@@ -507,6 +507,8 @@ type Driver interface {UpdateRole(ctx context.Context, roleName string, upsert *v1pb.DatabaseRoleUpsert) (*v1pb.DatabaseRole, error)// FindRole finds the role by name.FindRole(ctx context.Context, roleName string) (*v1pb.DatabaseRole, error)+ // ListRole lists the role.+ ListRole(ctx context.Context) ([]*v1pb.DatabaseRole, error)// DeleteRole deletes the role by name.DeleteRole(ctx context.Context, roleName string) errordiff --git a/plugin/db/mongodb/role.go b/plugin/db/mongodb/role.goindex 3259513e17..27d0f47f4e 100644--- a/plugin/db/mongodb/role.go+++ b/plugin/db/mongodb/role.go@@ -23,6 +23,11 @@ func (*Driver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, error)return nil, errors.Errorf("find role for MongoDB is not implemented yet")}+// ListRole lists the role.+func (*Driver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, errors.Errorf("list role for MongoDB is not implemented yet")+}+// DeleteRole deletes the role by name.func (*Driver) DeleteRole(_ context.Context, _ string) error {return errors.Errorf("delete role for MongoDB is not implemented yet")diff --git a/plugin/db/mysql/role.go b/plugin/db/mysql/role.goindex b2ef4942ea..63283879da 100644--- a/plugin/db/mysql/role.go+++ b/plugin/db/mysql/role.go@@ -23,6 +23,11 @@ func (*Driver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, error)return nil, errors.Errorf("find role for MySQL is not implemented yet")}+// ListRole lists the role.+func (*Driver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, errors.Errorf("list role for MySQL is not implemented yet")+}+// DeleteRole deletes the role by name.func (*Driver) DeleteRole(_ context.Context, _ string) error {return errors.Errorf("delete role for MySQL is not implemented yet")diff --git a/plugin/db/pg/role.go b/plugin/db/pg/role.goindex 60627be8ea..929f4ef90b 100644--- a/plugin/db/pg/role.go+++ b/plugin/db/pg/role.go@@ -37,6 +37,10 @@ func (a RoleAttribute) ToString() string {return string(a)}+type roleFind struct {+ Name *string+}+// CreateRole will create the PG role.func (driver *Driver) CreateRole(ctx context.Context, upsert *v1pb.DatabaseRoleUpsert) (*v1pb.DatabaseRole, error) {txn, err := driver.db.BeginTx(ctx, nil)@@ -49,16 +53,21 @@ func (driver *Driver) CreateRole(ctx context.Context, upsert *v1pb.DatabaseRoleUreturn nil, err}- role, err := getRoleImpl(ctx, txn, upsert.Name)+ roles, err := findRoleImpl(ctx, txn, &roleFind{+ Name: &upsert.Name,+ })if err != nil {return nil, err}+ if len(roles) == 0 {+ return nil, common.Errorf(common.NotFound, fmt.Sprintf("cannot find the role %s", upsert.Name))+ }if err := txn.Commit(); err != nil {return nil, err}- return role, nil+ return roles[0], nil}// UpdateRole will alter the PG role.@@ -73,16 +82,21 @@ func (driver *Driver) UpdateRole(ctx context.Context, roleName string, upsert *vreturn nil, err}- role, err := getRoleImpl(ctx, txn, upsert.Name)+ roles, err := findRoleImpl(ctx, txn, &roleFind{+ Name: &upsert.Name,+ })if err != nil {return nil, err}+ if len(roles) == 0 {+ return nil, common.Errorf(common.NotFound, fmt.Sprintf("cannot find the role %s", upsert.Name))+ }if err := txn.Commit(); err != nil {return nil, err}- return role, nil+ return roles[0], nil}// FindRole will find the PG role by name.@@ -93,16 +107,41 @@ func (driver *Driver) FindRole(ctx context.Context, roleName string) (*v1pb.Data}defer txn.Rollback()- role, err := getRoleImpl(ctx, txn, roleName)+ roles, err := findRoleImpl(ctx, txn, &roleFind{+ Name: &roleName,+ })if err != nil {return nil, err}+ if len(roles) == 0 {+ return nil, common.Errorf(common.NotFound, fmt.Sprintf("cannot find the role %s", roleName))+ }if err := txn.Commit(); err != nil {return nil, err}- return role, nil+ return roles[0], nil+}++// ListRole lists the role.+func (driver *Driver) ListRole(ctx context.Context) ([]*v1pb.DatabaseRole, error) {+ txn, err := driver.db.BeginTx(ctx, nil)+ if err != nil {+ return nil, err+ }+ defer txn.Rollback()++ roles, err := findRoleImpl(ctx, txn, &roleFind{})+ if err != nil {+ return nil, err+ }++ if err := txn.Commit(); err != nil {+ return nil, err+ }++ return roles, nil}// DeleteRole will drop the PG role by name.@@ -115,7 +154,15 @@ func (driver *Driver) DeleteRole(ctx context.Context, roleName string) error {return nil}-func getRoleImpl(ctx context.Context, txn *sql.Tx, roleName string) (*v1pb.DatabaseRole, error) {+func findRoleImpl(ctx context.Context, txn *sql.Tx, find *roleFind) ([]*v1pb.DatabaseRole, error) {+ where := []string{}+ if v := find.Name; v != nil {+ where = append(where, fmt.Sprintf("r.rolname = '%s'", *v))+ }+ if len(where) == 0 {+ where = append(where, "r.rolname !~ '^pg_'")+ }+statement := fmt.Sprintf(`SELECTr.rolname,@@ -129,37 +176,46 @@ func getRoleImpl(ctx context.Context, txn *sql.Tx, roleName string) (*v1pb.Databr.rolvaliduntil,r.rolconnlimitFROM pg_catalog.pg_roles r- WHERE r.rolname = '%s';- `, roleName)-- role := &v1pb.DatabaseRole{- Name: roleName,- Attribute: &v1pb.DatabaseRoleAttribute{},- }-- inherit := false-- if err := txn.QueryRowContext(ctx, statement).Scan(- &role.Name,- &role.Attribute.SuperUser,- &inherit,- &role.Attribute.CreateRole,- &role.Attribute.CreateDb,- &role.Attribute.CanLogin,- &role.Attribute.Replication,- &role.Attribute.BypassRls,- &role.ValidUntil,- &role.ConnectionLimit,- ); err != nil {- if err == sql.ErrNoRows {- return nil, common.Errorf(common.NotFound, fmt.Sprintf("query %q returned empty row", statement))- }+ WHERE %s;+ `, strings.Join(where, " AND "))++ rows, err := txn.QueryContext(ctx, statement)+ if err != nil {return nil, util.FormatErrorWithQuery(err, statement)}+ defer rows.Close()- role.Attribute.NoInherit = !inherit+ var roleList []*v1pb.DatabaseRole- return role, nil+ for rows.Next() {+ inherit := false+ role := &v1pb.DatabaseRole{+ Attribute: &v1pb.DatabaseRoleAttribute{},+ }++ if err := rows.Scan(+ &role.Name,+ &role.Attribute.SuperUser,+ &inherit,+ &role.Attribute.CreateRole,+ &role.Attribute.CreateDb,+ &role.Attribute.CanLogin,+ &role.Attribute.Replication,+ &role.Attribute.BypassRls,+ &role.ValidUntil,+ &role.ConnectionLimit,+ ); err != nil {+ return nil, util.FormatErrorWithQuery(err, statement)+ }++ role.Attribute.NoInherit = !inherit+ roleList = append(roleList, role)+ }++ if err := rows.Err(); err != nil {+ return nil, err+ }+ return roleList, nil}func createRoleImpl(ctx context.Context, txn *sql.Tx, upsert *v1pb.DatabaseRoleUpsert) error {diff --git a/plugin/db/snowflake/role.go b/plugin/db/snowflake/role.goindex 44023294f1..73a967ee09 100644--- a/plugin/db/snowflake/role.go+++ b/plugin/db/snowflake/role.go@@ -23,6 +23,11 @@ func (*Driver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, error)return nil, errors.Errorf("find role for Snowflake is not implemented yet")}+// ListRole lists the role.+func (*Driver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, errors.Errorf("list role for Snowflake is not implemented yet")+}+// DeleteRole deletes the role by name.func (*Driver) DeleteRole(_ context.Context, _ string) error {return errors.Errorf("delete role for Snowflake is not implemented yet")diff --git a/plugin/db/sqlite/role.go b/plugin/db/sqlite/role.goindex dbb66dbf59..36f20bb5e1 100644--- a/plugin/db/sqlite/role.go+++ b/plugin/db/sqlite/role.go@@ -23,6 +23,11 @@ func (*Driver) FindRole(_ context.Context, _ string) (*v1pb.DatabaseRole, error)return nil, errors.Errorf("find role for SQLite is not implemented yet")}+// ListRole lists the role.+func (*Driver) ListRole(_ context.Context) ([]*v1pb.DatabaseRole, error) {+ return nil, errors.Errorf("list role for SQLite is not implemented yet")+}+// DeleteRole deletes the role by name.func (*Driver) DeleteRole(_ context.Context, _ string) error {return errors.Errorf("delete role for SQLite is not implemented yet")diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex 8aa2e97915..a17d832667 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -8,6 +8,7 @@ import ("database/sql""fmt""io"+ "regexp""strconv""strings""time"@@ -23,6 +24,9 @@ import (// FormatErrorWithQuery will format the error with failed query.func FormatErrorWithQuery(err error, query string) error {+ if regexp.MustCompile("does not exist").MatchString(err.Error()) {+ return common.Wrapf(err, common.NotFound, "failed to execute query %q", query)+ }return common.Wrapf(err, common.DbExecutionError, "failed to execute query %q", query)}diff --git a/proto/generated-go/v1/database_role.pb.go b/proto/generated-go/v1/database_role.pb.goindex 65a01578d4..40960d64f3 100644--- a/proto/generated-go/v1/database_role.pb.go+++ b/proto/generated-go/v1/database_role.pb.go@@ -208,6 +208,54 @@ func (x *DatabaseRole) GetAttribute() *DatabaseRoleAttribute {return nil}+// DatabaseRoleList is the API message for role list.+type DatabaseRoleList struct {+ state protoimpl.MessageState+ sizeCache protoimpl.SizeCache+ unknownFields protoimpl.UnknownFields++ RoleList []*DatabaseRole `protobuf:"bytes,1,rep,name=role_list,json=roleList,proto3" json:"role_list,omitempty"`+}++func (x *DatabaseRoleList) Reset() {+ *x = DatabaseRoleList{}+ if protoimpl.UnsafeEnabled {+ mi := &file_v1_database_role_proto_msgTypes[2]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+ }+}++func (x *DatabaseRoleList) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*DatabaseRoleList) ProtoMessage() {}++func (x *DatabaseRoleList) ProtoReflect() protoreflect.Message {+ mi := &file_v1_database_role_proto_msgTypes[2]+ if protoimpl.UnsafeEnabled && x != nil {+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ if ms.LoadMessageInfo() == nil {+ ms.StoreMessageInfo(mi)+ }+ return ms+ }+ return mi.MessageOf(x)+}++// Deprecated: Use DatabaseRoleList.ProtoReflect.Descriptor instead.+func (*DatabaseRoleList) Descriptor() ([]byte, []int) {+ return file_v1_database_role_proto_rawDescGZIP(), []int{2}+}++func (x *DatabaseRoleList) GetRoleList() []*DatabaseRole {+ if x != nil {+ return x.RoleList+ }+ return nil+}+// DatabaseRoleUpsert is the API message for upserting a database role.type DatabaseRoleUpsert struct {state protoimpl.MessageState@@ -229,7 +277,7 @@ type DatabaseRoleUpsert struct {func (x *DatabaseRoleUpsert) Reset() {*x = DatabaseRoleUpsert{}if protoimpl.UnsafeEnabled {- mi := &file_v1_database_role_proto_msgTypes[2]+ mi := &file_v1_database_role_proto_msgTypes[3]ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))ms.StoreMessageInfo(mi)}@@ -242,7 +290,7 @@ func (x *DatabaseRoleUpsert) String() string {func (*DatabaseRoleUpsert) ProtoMessage() {}func (x *DatabaseRoleUpsert) ProtoReflect() protoreflect.Message {- mi := &file_v1_database_role_proto_msgTypes[2]+ mi := &file_v1_database_role_proto_msgTypes[3]if protoimpl.UnsafeEnabled && x != nil {ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))if ms.LoadMessageInfo() == nil {@@ -255,7 +303,7 @@ func (x *DatabaseRoleUpsert) ProtoReflect() protoreflect.Message {// Deprecated: Use DatabaseRoleUpsert.ProtoReflect.Descriptor instead.func (*DatabaseRoleUpsert) Descriptor() ([]byte, []int) {- return file_v1_database_role_proto_rawDescGZIP(), []int{2}+ return file_v1_database_role_proto_rawDescGZIP(), []int{3}}func (x *DatabaseRoleUpsert) GetName() string {@@ -328,27 +376,32 @@ var file_v1_database_role_proto_rawDesc = []byte{0x31, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x41, 0x74,0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x52, 0x09, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75,0x74, 0x65, 0x42, 0x0e, 0x0a, 0x0c, 0x5f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x5f, 0x75, 0x6e, 0x74,- 0x69, 0x6c, 0x22, 0xa6, 0x02, 0x0a, 0x12, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52,- 0x6f, 0x6c, 0x65, 0x55, 0x70, 0x73, 0x65, 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d,- 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a,- 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48,- 0x00, 0x52, 0x08, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x88, 0x01, 0x01, 0x12, 0x2e,- 0x0a, 0x10, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6c, 0x69, 0x6d,- 0x69, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x48, 0x01, 0x52, 0x0f, 0x63, 0x6f, 0x6e, 0x6e,- 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x88, 0x01, 0x01, 0x12, 0x24,- 0x0a, 0x0b, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x5f, 0x75, 0x6e, 0x74, 0x69, 0x6c, 0x18, 0x04, 0x20,- 0x01, 0x28, 0x09, 0x48, 0x02, 0x52, 0x0a, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x55, 0x6e, 0x74, 0x69,- 0x6c, 0x88, 0x01, 0x01, 0x12, 0x45, 0x0a, 0x09, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74,- 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61,- 0x73, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x6f,- 0x6c, 0x65, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x48, 0x03, 0x52, 0x09, 0x61,- 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x88, 0x01, 0x01, 0x42, 0x0b, 0x0a, 0x09, 0x5f,- 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x63, 0x6f, 0x6e,- 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x42, 0x0e, 0x0a,- 0x0c, 0x5f, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x5f, 0x75, 0x6e, 0x74, 0x69, 0x6c, 0x42, 0x0c, 0x0a,- 0x0a, 0x5f, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x42, 0x11, 0x5a, 0x0f, 0x67,- 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x76, 0x31, 0x62, 0x06,- 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,+ 0x69, 0x6c, 0x22, 0x4a, 0x0a, 0x10, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x6f,+ 0x6c, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x36, 0x0a, 0x09, 0x72, 0x6f, 0x6c, 0x65, 0x5f, 0x6c,+ 0x69, 0x73, 0x74, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x19, 0x2e, 0x62, 0x79, 0x74, 0x65,+ 0x62, 0x61, 0x73, 0x65, 0x2e, 0x76, 0x31, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65,+ 0x52, 0x6f, 0x6c, 0x65, 0x52, 0x08, 0x72, 0x6f, 0x6c, 0x65, 0x4c, 0x69, 0x73, 0x74, 0x22, 0xa6,+ 0x02, 0x0a, 0x12, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x55,+ 0x70, 0x73, 0x65, 0x72, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x01, 0x20,+ 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x1f, 0x0a, 0x08, 0x70, 0x61, 0x73,+ 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x08, 0x70,+ 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x88, 0x01, 0x01, 0x12, 0x2e, 0x0a, 0x10, 0x63, 0x6f,+ 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x03,+ 0x20, 0x01, 0x28, 0x05, 0x48, 0x01, 0x52, 0x0f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69,+ 0x6f, 0x6e, 0x4c, 0x69, 0x6d, 0x69, 0x74, 0x88, 0x01, 0x01, 0x12, 0x24, 0x0a, 0x0b, 0x76, 0x61,+ 0x6c, 0x69, 0x64, 0x5f, 0x75, 0x6e, 0x74, 0x69, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x48,+ 0x02, 0x52, 0x0a, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x55, 0x6e, 0x74, 0x69, 0x6c, 0x88, 0x01, 0x01,+ 0x12, 0x45, 0x0a, 0x09, 0x61, 0x74, 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x18, 0x05, 0x20,+ 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x76,+ 0x31, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x6f, 0x6c, 0x65, 0x41, 0x74,+ 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x48, 0x03, 0x52, 0x09, 0x61, 0x74, 0x74, 0x72, 0x69,+ 0x62, 0x75, 0x74, 0x65, 0x88, 0x01, 0x01, 0x42, 0x0b, 0x0a, 0x09, 0x5f, 0x70, 0x61, 0x73, 0x73,+ 0x77, 0x6f, 0x72, 0x64, 0x42, 0x13, 0x0a, 0x11, 0x5f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74,+ 0x69, 0x6f, 0x6e, 0x5f, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x42, 0x0e, 0x0a, 0x0c, 0x5f, 0x76, 0x61,+ 0x6c, 0x69, 0x64, 0x5f, 0x75, 0x6e, 0x74, 0x69, 0x6c, 0x42, 0x0c, 0x0a, 0x0a, 0x5f, 0x61, 0x74,+ 0x74, 0x72, 0x69, 0x62, 0x75, 0x74, 0x65, 0x42, 0x11, 0x5a, 0x0f, 0x67, 0x65, 0x6e, 0x65, 0x72,+ 0x61, 0x74, 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,+ 0x6f, 0x33,}var (@@ -363,20 +416,22 @@ func file_v1_database_role_proto_rawDescGZIP() []byte {return file_v1_database_role_proto_rawDescData}-var file_v1_database_role_proto_msgTypes = make([]protoimpl.MessageInfo, 3)+var file_v1_database_role_proto_msgTypes = make([]protoimpl.MessageInfo, 4)var file_v1_database_role_proto_goTypes = []interface{}{(*DatabaseRoleAttribute)(nil), // 0: bytebase.v1.DatabaseRoleAttribute(*DatabaseRole)(nil), // 1: bytebase.v1.DatabaseRole- (*DatabaseRoleUpsert)(nil), // 2: bytebase.v1.DatabaseRoleUpsert+ (*DatabaseRoleList)(nil), // 2: bytebase.v1.DatabaseRoleList+ (*DatabaseRoleUpsert)(nil), // 3: bytebase.v1.DatabaseRoleUpsert}var file_v1_database_role_proto_depIdxs = []int32{0, // 0: bytebase.v1.DatabaseRole.attribute:type_name -> bytebase.v1.DatabaseRoleAttribute- 0, // 1: bytebase.v1.DatabaseRoleUpsert.attribute:type_name -> bytebase.v1.DatabaseRoleAttribute- 2, // [2:2] is the sub-list for method output_type- 2, // [2:2] is the sub-list for method input_type- 2, // [2:2] is the sub-list for extension type_name- 2, // [2:2] is the sub-list for extension extendee- 0, // [0:2] is the sub-list for field type_name+ 1, // 1: bytebase.v1.DatabaseRoleList.role_list:type_name -> bytebase.v1.DatabaseRole+ 0, // 2: bytebase.v1.DatabaseRoleUpsert.attribute:type_name -> bytebase.v1.DatabaseRoleAttribute+ 3, // [3:3] is the sub-list for method output_type+ 3, // [3:3] is the sub-list for method input_type+ 3, // [3:3] is the sub-list for extension type_name+ 3, // [3:3] is the sub-list for extension extendee+ 0, // [0:3] is the sub-list for field type_name}func init() { file_v1_database_role_proto_init() }@@ -410,6 +465,18 @@ func file_v1_database_role_proto_init() {}}file_v1_database_role_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {+ switch v := v.(*DatabaseRoleList); i {+ case 0:+ return &v.state+ case 1:+ return &v.sizeCache+ case 2:+ return &v.unknownFields+ default:+ return nil+ }+ }+ file_v1_database_role_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {switch v := v.(*DatabaseRoleUpsert); i {case 0:return &v.state@@ -423,14 +490,14 @@ func file_v1_database_role_proto_init() {}}file_v1_database_role_proto_msgTypes[1].OneofWrappers = []interface{}{}- file_v1_database_role_proto_msgTypes[2].OneofWrappers = []interface{}{}+ file_v1_database_role_proto_msgTypes[3].OneofWrappers = []interface{}{}type x struct{}out := protoimpl.TypeBuilder{File: protoimpl.DescBuilder{GoPackagePath: reflect.TypeOf(x{}).PkgPath(),RawDescriptor: file_v1_database_role_proto_rawDesc,NumEnums: 0,- NumMessages: 3,+ NumMessages: 4,NumExtensions: 0,NumServices: 0,},diff --git a/proto/v1/database_role.proto b/proto/v1/database_role.protoindex 16783b4334..6c2e43bfea 100644--- a/proto/v1/database_role.proto+++ b/proto/v1/database_role.proto@@ -45,6 +45,11 @@ message DatabaseRole {DatabaseRoleAttribute attribute = 5;}+// DatabaseRoleList is the API message for role list.+message DatabaseRoleList {+ repeated DatabaseRole role_list = 1;+}+// DatabaseRoleUpsert is the API message for upserting a database role.message DatabaseRoleUpsert {// The role unique name.diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csvindex 435d867f3d..3099cbb866 100644--- a/server/acl_casbin_policy_dba.csv+++ b/server/acl_casbin_policy_dba.csv@@ -45,6 +45,7 @@ p, DBA, /instance/{instanceID}/migration, POSTp, DBA, /instance/{instanceID}/migration/status, GETp, DBA, /instance/{instanceID}/migration/history, GETp, DBA, /instance/{instanceID}/migration/history/{historyID}, GET+p, DBA, /instance/{instanceID}/role, GETp, DBA, /instance/{instanceID}/role, POSTp, DBA, /instance/{instanceID}/role/{roleName}, GETp, DBA, /instance/{instanceID}/role/{roleName}, PATCHdiff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csvindex 272d183739..8b45fdf634 100644--- a/server/acl_casbin_policy_developer.csv+++ b/server/acl_casbin_policy_developer.csv@@ -36,6 +36,8 @@ p, DEVELOPER, /instance/{instanceID}/migration/status, GETp, DEVELOPER, /instance/{instanceID}/migration/history, GETp, DEVELOPER, /instance/{instanceID}/migration/history/{historyID}, GETp, DEVELOPER, /instance/{instanceID}, GET+p, DEVELOPER, /instance/{instanceID}/role, GET+p, DEVELOPER, /instance/{instanceID}/role/{roleName}, GETp, DEVELOPER, /database, GETp, DEVELOPER, /database/{databaseID}, GETp, DEVELOPER, /database/{databaseID}, PATCHdiff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csvindex c1dba21502..900e66466b 100644--- a/server/acl_casbin_policy_owner.csv+++ b/server/acl_casbin_policy_owner.csv@@ -48,6 +48,7 @@ p, OWNER, /instance/{instanceID}/migration, POSTp, OWNER, /instance/{instanceID}/migration/status, GETp, OWNER, /instance/{instanceID}/migration/history, GETp, OWNER, /instance/{instanceID}/migration/history/{historyID}, GET+p, OWNER, /instance/{instanceID}/role, GETp, OWNER, /instance/{instanceID}/role, POSTp, OWNER, /instance/{instanceID}/role/{roleName}, GETp, OWNER, /instance/{instanceID}/role/{roleName}, PATCHdiff --git a/server/openapi_instance.go b/server/openapi_instance.goindex 1b031ad16d..489090b8d3 100644--- a/server/openapi_instance.go+++ b/server/openapi_instance.go@@ -26,6 +26,7 @@ func (s *Server) registerOpenAPIRoutesForInstance(g *echo.Group) {g.GET("/instance/:instanceID", s.getInstanceByID)g.PATCH("/instance/:instanceID", s.updateInstanceByOpenAPI)g.DELETE("/instance/:instanceID", s.deleteInstanceByOpenAPI)+ g.GET("/instance/:instanceID/role", s.listDatabaseRole)g.POST("/instance/:instanceID/role", s.createDatabaseRole)g.GET("/instance/:instanceID/role/:roleName", s.getDatabaseRole)g.PATCH("/instance/:instanceID/role/:roleName", s.updateDatabaseRole)@@ -193,6 +194,55 @@ func (s *Server) getInstanceByID(c echo.Context) error {return c.JSON(http.StatusOK, convertToOpenAPIInstance(instance))}+func (s *Server) listDatabaseRole(c echo.Context) error {+ ctx := c.Request().Context()+ instance, err := s.validateInstance(ctx, c)+ if err != nil {+ return err+ }++ roleList, err := func() ([]*v1pb.DatabaseRole, error) {+ driver, err := s.dbFactory.GetAdminDatabaseDriver(ctx, instance, "" /* database name */)+ if err != nil {+ return nil, err+ }+ defer driver.Close(ctx)++ roleList, err := driver.ListRole(ctx)+ if err != nil {+ return nil, err+ }++ return roleList, nil+ }()+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)+ }++ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)++ response := &v1pb.DatabaseRoleList{}+ for _, role := range roleList {+ response.RoleList = append(response.RoleList, &v1pb.DatabaseRole{+ Name: role.Name,+ InstanceId: int32(instance.ID),+ ConnectionLimit: role.ConnectionLimit,+ ValidUntil: role.ValidUntil,+ Attribute: role.Attribute,+ })+ }++ metadataBytes, err := protojson.Marshal(response)+ if err != nil {+ return err+ }+ if _, err := c.Response().Write(metadataBytes); err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to write schema response for role list").SetInternal(err)+ }++ return nil+}+func (s *Server) getDatabaseRole(c echo.Context) error {ctx := c.Request().Context()roleName := c.Param("roleName")@@ -323,6 +373,9 @@ func (s *Server) updateDatabaseRole(c echo.Context) error {return role, nil}()if err != nil {+ if common.ErrorCode(err) == common.NotFound {+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Cannot found the role %s in instance %d", rawName, instance.ID))+ }return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)}