Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: update principal store with MFA config (#4981)
boojackc0ee77c74e0b
diff --git a/backend/store/principal.go b/backend/store/principal.goindex e229158c6d..bb078b961c 100644--- a/backend/store/principal.go+++ b/backend/store/principal.go@@ -7,9 +7,11 @@ import ("strings""github.com/pkg/errors"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/common"api "github.com/bytebase/bytebase/backend/legacyapi"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")// GetPrincipalList gets a list of Principal instances.@@ -83,6 +85,7 @@ type UpdateUserMessage struct {PasswordHash *stringRole *api.RoleDelete *bool+ MFAConfig *storepb.MFAConfig}// UserMessage is the message for an user.@@ -95,6 +98,7 @@ type UserMessage struct {PasswordHash stringRole api.RoleMemberDeleted bool+ MFAConfig *storepb.MFAConfig}// GetUser gets an user.@@ -195,51 +199,109 @@ func (*Store) listUserImpl(ctx context.Context, tx *Tx, find *FindUserMessage) (}var userMessages []*UserMessage- rows, err := tx.QueryContext(ctx, `- SELECT- principal.id AS user_id,- principal.email,- principal.name,- principal.type,- principal.password_hash,- member.role,- member.row_status AS row_status- FROM principal- LEFT JOIN member ON principal.id = member.principal_id- WHERE `+strings.Join(where, " AND "),- args...,- )- if err != nil {- return nil, FormatError(err)- }- defer rows.Close()- for rows.Next() {- var userMessage UserMessage- var role, rowStatus sql.NullString- if err := rows.Scan(- &userMessage.ID,- &userMessage.Email,- &userMessage.Name,- &userMessage.Type,- &userMessage.PasswordHash,- &role,- &rowStatus,- ); err != nil {+ if common.FeatureFlag(common.FeatureFlagNoop) {+ rows, err := tx.QueryContext(ctx, `+ SELECT+ principal.id AS user_id,+ principal.email,+ principal.name,+ principal.type,+ principal.password_hash,+ principal.mfa_config,+ member.role,+ member.row_status AS row_status+ FROM principal+ LEFT JOIN member ON principal.id = member.principal_id+ WHERE `+strings.Join(where, " AND "),+ args...,+ )+ if err != nil {return nil, FormatError(err)}- if role.Valid {- userMessage.Role = api.Role(role.String)- } else if userMessage.ID == api.SystemBotID {- userMessage.Role = api.Owner- } else {- userMessage.Role = api.Developer+ defer rows.Close()+ for rows.Next() {+ var userMessage UserMessage+ var role, rowStatus sql.NullString+ var mfaConfigBytes []byte+ if err := rows.Scan(+ &userMessage.ID,+ &userMessage.Email,+ &userMessage.Name,+ &userMessage.Type,+ &userMessage.PasswordHash,+ &mfaConfigBytes,+ &role,+ &rowStatus,+ ); err != nil {+ return nil, FormatError(err)+ }+ if role.Valid {+ userMessage.Role = api.Role(role.String)+ } else if userMessage.ID == api.SystemBotID {+ userMessage.Role = api.Owner+ } else {+ userMessage.Role = api.Developer+ }+ if rowStatus.Valid {+ userMessage.MemberDeleted = convertRowStatusToDeleted(rowStatus.String)+ } else if userMessage.ID != api.SystemBotID {+ userMessage.MemberDeleted = true+ }+ var mfaConfig storepb.MFAConfig+ decoder := protojson.UnmarshalOptions{DiscardUnknown: true}+ if err := decoder.Unmarshal(mfaConfigBytes, &mfaConfig); err != nil {+ return nil, err+ }+ userMessage.MFAConfig = &mfaConfig+ userMessages = append(userMessages, &userMessage)}- if rowStatus.Valid {- userMessage.MemberDeleted = convertRowStatusToDeleted(rowStatus.String)- } else if userMessage.ID != api.SystemBotID {- userMessage.MemberDeleted = true+ } else {+ rows, err := tx.QueryContext(ctx, `+ SELECT+ principal.id AS user_id,+ principal.email,+ principal.name,+ principal.type,+ principal.password_hash,+ member.role,+ member.row_status AS row_status+ FROM principal+ LEFT JOIN member ON principal.id = member.principal_id+ WHERE `+strings.Join(where, " AND "),+ args...,+ )+ if err != nil {+ return nil, FormatError(err)+ }+ defer rows.Close()+ for rows.Next() {+ var userMessage UserMessage+ var role, rowStatus sql.NullString+ if err := rows.Scan(+ &userMessage.ID,+ &userMessage.Email,+ &userMessage.Name,+ &userMessage.Type,+ &userMessage.PasswordHash,+ &role,+ &rowStatus,+ ); err != nil {+ return nil, FormatError(err)+ }+ if role.Valid {+ userMessage.Role = api.Role(role.String)+ } else if userMessage.ID == api.SystemBotID {+ userMessage.Role = api.Owner+ } else {+ userMessage.Role = api.Developer+ }+ if rowStatus.Valid {+ userMessage.MemberDeleted = convertRowStatusToDeleted(rowStatus.String)+ } else if userMessage.ID != api.SystemBotID {+ userMessage.MemberDeleted = true+ }+ userMessages = append(userMessages, &userMessage)}- userMessages = append(userMessages, &userMessage)}return userMessages, nil}@@ -342,6 +404,13 @@ func (s *Store) UpdateUser(ctx context.Context, userID int, patch *UpdateUserMesif v := patch.PasswordHash; v != nil {principalSet, principalArgs = append(principalSet, fmt.Sprintf("password_hash = $%d", len(principalArgs)+1)), append(principalArgs, *v)}+ if v := patch.MFAConfig; v != nil {+ mfaConfigBytes, err := protojson.Marshal(v)+ if err != nil {+ return nil, err+ }+ principalSet, principalArgs = append(principalSet, fmt.Sprintf("mfa_config = $%d", len(principalArgs)+1)), append(principalArgs, mfaConfigBytes)+ }principalArgs = append(principalArgs, userID)memberSet, memberArgs := []string{"updater_id = $1"}, []interface{}{fmt.Sprintf("%d", updaterID)}diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 0942a13b36..a2451aa27d 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -26,6 +26,9 @@- [store/setting.proto](#store_setting-proto)- [WorkspaceProfileSetting](#bytebase-store-WorkspaceProfileSetting)+- [store/user.proto](#store_user-proto)+ - [MFAConfig](#bytebase-store-MFAConfig)+- [Scalar Value Types](#scalar-value-types)@@ -384,6 +387,38 @@ The external URL is used for: 1. Constructing the correct callback URL when conf+<a name="store_user-proto"></a>+<p align="right"><a href="#top">Top</a></p>++## store/user.proto++++<a name="bytebase-store-MFAConfig"></a>++### MFAConfig+MFAConfig is the MFA configuration for a user.+++| Field | Type | Label | Description |+| ----- | ---- | ----- | ----------- |+| otp_secret | [string](#string) | | The otp_secret is the secret key used to validate the OTP code. |+| recovery_codes | [string](#string) | repeated | The recovery_codes are the codes that can be used to recover the account. |+++++++++++++++## Scalar Value Types| .proto Type | Notes | C++ | Java | Python | Go | C# | PHP | Ruby |diff --git a/proto/generated-go/store/user.pb.go b/proto/generated-go/store/user.pb.gonew file mode 100644index 0000000000..736e6a4868--- /dev/null+++ b/proto/generated-go/store/user.pb.go@@ -0,0 +1,157 @@+// Code generated by protoc-gen-go. DO NOT EDIT.+// versions:+// protoc-gen-go v1.28.1+// protoc (unknown)+// source: store/user.proto++package store++import (+ protoreflect "google.golang.org/protobuf/reflect/protoreflect"+ protoimpl "google.golang.org/protobuf/runtime/protoimpl"+ reflect "reflect"+ sync "sync"+)++const (+ // Verify that this generated code is sufficiently up-to-date.+ _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)+ // Verify that runtime/protoimpl is sufficiently up-to-date.+ _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)+)++// MFAConfig is the MFA configuration for a user.+type MFAConfig struct {+ state protoimpl.MessageState+ sizeCache protoimpl.SizeCache+ unknownFields protoimpl.UnknownFields++ // The otp_secret is the secret key used to validate the OTP code.+ OtpSecret string `protobuf:"bytes,1,opt,name=otp_secret,json=otpSecret,proto3" json:"otp_secret,omitempty"`+ // The recovery_codes are the codes that can be used to recover the account.+ RecoveryCodes []string `protobuf:"bytes,2,rep,name=recovery_codes,json=recoveryCodes,proto3" json:"recovery_codes,omitempty"`+}++func (x *MFAConfig) Reset() {+ *x = MFAConfig{}+ if protoimpl.UnsafeEnabled {+ mi := &file_store_user_proto_msgTypes[0]+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))+ ms.StoreMessageInfo(mi)+ }+}++func (x *MFAConfig) String() string {+ return protoimpl.X.MessageStringOf(x)+}++func (*MFAConfig) ProtoMessage() {}++func (x *MFAConfig) ProtoReflect() protoreflect.Message {+ mi := &file_store_user_proto_msgTypes[0]+ 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 MFAConfig.ProtoReflect.Descriptor instead.+func (*MFAConfig) Descriptor() ([]byte, []int) {+ return file_store_user_proto_rawDescGZIP(), []int{0}+}++func (x *MFAConfig) GetOtpSecret() string {+ if x != nil {+ return x.OtpSecret+ }+ return ""+}++func (x *MFAConfig) GetRecoveryCodes() []string {+ if x != nil {+ return x.RecoveryCodes+ }+ return nil+}++var File_store_user_proto protoreflect.FileDescriptor++var file_store_user_proto_rawDesc = []byte{+ 0x0a, 0x10, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f,+ 0x74, 0x6f, 0x12, 0x0e, 0x62, 0x79, 0x74, 0x65, 0x62, 0x61, 0x73, 0x65, 0x2e, 0x73, 0x74, 0x6f,+ 0x72, 0x65, 0x22, 0x51, 0x0a, 0x09, 0x4d, 0x46, 0x41, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12,+ 0x1d, 0x0a, 0x0a, 0x6f, 0x74, 0x70, 0x5f, 0x73, 0x65, 0x63, 0x72, 0x65, 0x74, 0x18, 0x01, 0x20,+ 0x01, 0x28, 0x09, 0x52, 0x09, 0x6f, 0x74, 0x70, 0x53, 0x65, 0x63, 0x72, 0x65, 0x74, 0x12, 0x25,+ 0x0a, 0x0e, 0x72, 0x65, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x79, 0x5f, 0x63, 0x6f, 0x64, 0x65, 0x73,+ 0x18, 0x02, 0x20, 0x03, 0x28, 0x09, 0x52, 0x0d, 0x72, 0x65, 0x63, 0x6f, 0x76, 0x65, 0x72, 0x79,+ 0x43, 0x6f, 0x64, 0x65, 0x73, 0x42, 0x14, 0x5a, 0x12, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,+ 0x65, 0x64, 0x2d, 0x67, 0x6f, 0x2f, 0x73, 0x74, 0x6f, 0x72, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f,+ 0x74, 0x6f, 0x33,+}++var (+ file_store_user_proto_rawDescOnce sync.Once+ file_store_user_proto_rawDescData = file_store_user_proto_rawDesc+)++func file_store_user_proto_rawDescGZIP() []byte {+ file_store_user_proto_rawDescOnce.Do(func() {+ file_store_user_proto_rawDescData = protoimpl.X.CompressGZIP(file_store_user_proto_rawDescData)+ })+ return file_store_user_proto_rawDescData+}++var file_store_user_proto_msgTypes = make([]protoimpl.MessageInfo, 1)+var file_store_user_proto_goTypes = []interface{}{+ (*MFAConfig)(nil), // 0: bytebase.store.MFAConfig+}+var file_store_user_proto_depIdxs = []int32{+ 0, // [0:0] is the sub-list for method output_type+ 0, // [0:0] is the sub-list for method input_type+ 0, // [0:0] is the sub-list for extension type_name+ 0, // [0:0] is the sub-list for extension extendee+ 0, // [0:0] is the sub-list for field type_name+}++func init() { file_store_user_proto_init() }+func file_store_user_proto_init() {+ if File_store_user_proto != nil {+ return+ }+ if !protoimpl.UnsafeEnabled {+ file_store_user_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {+ switch v := v.(*MFAConfig); i {+ case 0:+ return &v.state+ case 1:+ return &v.sizeCache+ case 2:+ return &v.unknownFields+ default:+ return nil+ }+ }+ }+ type x struct{}+ out := protoimpl.TypeBuilder{+ File: protoimpl.DescBuilder{+ GoPackagePath: reflect.TypeOf(x{}).PkgPath(),+ RawDescriptor: file_store_user_proto_rawDesc,+ NumEnums: 0,+ NumMessages: 1,+ NumExtensions: 0,+ NumServices: 0,+ },+ GoTypes: file_store_user_proto_goTypes,+ DependencyIndexes: file_store_user_proto_depIdxs,+ MessageInfos: file_store_user_proto_msgTypes,+ }.Build()+ File_store_user_proto = out.File+ file_store_user_proto_rawDesc = nil+ file_store_user_proto_goTypes = nil+ file_store_user_proto_depIdxs = nil+}diff --git a/proto/store/user.proto b/proto/store/user.protonew file mode 100644index 0000000000..f7a6a1462d--- /dev/null+++ b/proto/store/user.proto@@ -0,0 +1,14 @@+syntax = "proto3";++package bytebase.store;++option go_package = "generated-go/store";++// MFAConfig is the MFA configuration for a user.+message MFAConfig {+ // The otp_secret is the secret key used to validate the OTP code.+ string otp_secret = 1;++ // The recovery_codes are the codes that can be used to recover the account.+ repeated string recovery_codes = 2;+}