Skip to content

Commit 4e8f392

Browse files
LeeEircibuler
authored andcommitted
feat: mariadb的支持
1 parent 5344302 commit 4e8f392

9 files changed

Lines changed: 31 additions & 24 deletions

File tree

pkg/handler/app_mysql.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313
"github.com/jumpserver/koko/pkg/utils"
1414
)
1515

16-
func (u *UserSelectHandler) retrieveRemoteMySQL(reqParam model.PaginationParam) []map[string]interface{} {
17-
res, err := u.h.jmsService.GetUserPermsMySQL(u.user.ID, reqParam)
16+
func (u *UserSelectHandler) retrieveRemoteMySQLAndMariadb(reqParam model.PaginationParam) []map[string]interface{} {
17+
res, err := u.h.jmsService.GetUserPermsMySQLAndMariadb(u.user.ID, reqParam)
1818
if err != nil {
1919
logger.Errorf("Ger user perm MySQL failed: %s", err)
2020
}

pkg/handler/select_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (u *UserSelectHandler) Proxy(target map[string]interface{}) {
234234
}
235235
u.proxyK8s(app)
236236
case TypeMySQL:
237-
app, err := u.h.jmsService.GetMySQLApplicationById(targetId)
237+
app, err := u.h.jmsService.GetMySQLOrMariadbApplicationById(targetId)
238238
if err != nil || app.ID == "" {
239239
logger.Errorf("Select MySQL %s not found", targetId)
240240
return
@@ -330,7 +330,7 @@ func (u *UserSelectHandler) retrieveFromRemote(pageSize, offset int, searches ..
330330
}
331331
switch u.currentType {
332332
case TypeMySQL:
333-
return u.retrieveRemoteMySQL(reqParam)
333+
return u.retrieveRemoteMySQLAndMariadb(reqParam)
334334
case TypeK8s:
335335
return u.retrieveRemoteK8s(reqParam)
336336
case TypeNodeAsset:

pkg/httpd/tty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (h *tty) handleTerminalMessage(msg *Message) {
148148
func (h *tty) getApp() bool {
149149
switch h.getAppType() {
150150
case AppTypeDB:
151-
databaseAsset, err := h.jmsService.GetMySQLApplicationById(h.targetId)
151+
databaseAsset, err := h.jmsService.GetMySQLOrMariadbApplicationById(h.targetId)
152152
if err != nil {
153153
logger.Errorf("Get MySQL App failed; %s", err)
154154
return false

pkg/jms-sdk-go/service/jms.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ type JMService struct {
4848
authClient *httplib.Client
4949

5050
sync.Mutex
51-
remoteConfig *model.TerminalConfig
5251
}
5352

5453
func (s *JMService) GetUserById(userID string) (user *model.User, err error) {

pkg/jms-sdk-go/service/jms_application.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ func (s *JMService) GetMySQLApplicationById(appId string) (app model.DatabaseApp
1010
return
1111
}
1212

13+
func (s *JMService) GetMySQLOrMariadbApplicationById(appId string) (app model.DatabaseApplication, err error) {
14+
err = s.getApplicationById(appId, &app)
15+
return
16+
}
17+
1318
func (s *JMService) GetK8sApplicationById(appId string) (app model.K8sApplication, err error) {
1419
err = s.getApplicationById(appId, &app)
1520
return

pkg/jms-sdk-go/service/jms_perm_application.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func (s *JMService) GetAllUserPermMySQLs(userId string) ([]map[string]interface{}, error) {
1212
var param model.PaginationParam
13-
res, err := s.GetUserPermsMySQL(userId, param)
13+
res, err := s.GetUserPermsMySQLAndMariadb(userId, param)
1414
if err != nil {
1515
return nil, err
1616
}
@@ -31,6 +31,11 @@ func (s *JMService) GetUserPermsMySQL(userId string, param model.PaginationParam
3131
return s.getPaginationResult(reqUrl, param)
3232
}
3333

34+
func (s *JMService) GetUserPermsMySQLAndMariadb(userId string, param model.PaginationParam) (resp model.PaginationResponse, err error) {
35+
reqUrl := fmt.Sprintf(UserPermsMySQLMariadbURL, userId)
36+
return s.getPaginationResult(reqUrl, param)
37+
}
38+
3439
func (s *JMService) GetUserPermsK8s(userId string, param model.PaginationParam) (resp model.PaginationResponse, err error) {
3540
reqUrl := fmt.Sprintf(UserPermsApplicationsURL, userId, model.AppTypeK8s)
3641
return s.getPaginationResult(reqUrl, param)

pkg/jms-sdk-go/service/url.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,22 @@ const (
3737
UserPermsApplicationSystemUsersURL = "/api/v1/perms/users/%s/applications/%s/system-users/"
3838
ValidateUserAssetPermissionURL = "/api/v1/perms/asset-permissions/user/validate/"
3939
ValidateApplicationPermissionURL = "/api/v1/perms/application-permissions/user/validate/"
40+
41+
UserPermsMySQLMariadbURL = "/api/v1/perms/users/%s/applications/?type=mysql&type=mariadb"
4042
)
4143

4244
// 系统用户密码相关API
4345
const (
4446
SystemUserAuthURL = "/api/v1/assets/system-users/%s/auth-info/"
45-
SystemUserAppAuthURL = "/api/v1/assets/system-users/%s/applications/%s/auth-info/" // 该系统用户对某资产的授权
46-
SystemUserAssetAuthURL = "/api/v1/assets/system-users/%s/assets/%s/auth-info/" // 该系统用户对某资产的授权
47+
SystemUserAppAuthURL = "/api/v1/assets/system-users/%s/applications/%s/auth-info/" // 该系统用户对某应用的授权
48+
SystemUserAssetAuthURL = "/api/v1/assets/system-users/%s/assets/%s/auth-info/" // 该系统用户对某资产的授权
4749
)
4850

4951
// 各资源详情相关API
5052
const (
5153
UserDetailURL = "/api/v1/users/users/%s/"
5254
AssetDetailURL = "/api/v1/assets/assets/%s/"
5355
AssetPlatFormURL = "/api/v1/assets/assets/%s/platform/"
54-
AssetGatewaysURL = "/api/v1/assets/assets/%s/gateways/"
5556
SystemUserDetailURL = "/api/v1/assets/system-users/%s/"
5657
ApplicationDetailURL = "/api/v1/applications/applications/%s/"
5758

@@ -64,10 +65,6 @@ const (
6465
NotificationCommandURL = "/api/v1/terminal/commands/insecure-command/"
6566
)
6667

67-
const (
68-
StatURL = "/api/v1/terminal/components/state/"
69-
)
70-
7168
const (
7269
PermissionURL = "/api/v1/perms/asset-permissions/user/actions/"
7370

pkg/proxy/server.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (opts *ConnectionOptions) TerminalTitle() string {
8484
opts.ProtocolType,
8585
opts.systemUser.Username,
8686
opts.asset.IP)
87-
case srvconn.ProtocolMySQL:
87+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
8888
title = fmt.Sprintf("%s://%s@%s",
8989
opts.ProtocolType,
9090
opts.systemUser.Username,
@@ -103,7 +103,7 @@ func (opts *ConnectionOptions) ConnectMsg() string {
103103
case srvconn.ProtocolTELNET,
104104
srvconn.ProtocolSSH:
105105
msg = fmt.Sprintf(i18n.T("Connecting to %s@%s"), opts.systemUser.Name, opts.asset.IP)
106-
case srvconn.ProtocolMySQL:
106+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
107107
msg = fmt.Sprintf(i18n.T("Connecting to Database %s"), opts.dbApp)
108108
case srvconn.ProtocolK8s:
109109
msg = fmt.Sprintf(i18n.T("Connecting to Kubernetes %s"), opts.k8sApp.Attrs.Cluster)
@@ -244,7 +244,7 @@ func NewServer(conn UserConnection, jmsService *service.JMService, opts ...Conne
244244
AssetID: connOpts.k8sApp.ID,
245245
OrgID: connOpts.k8sApp.OrgID,
246246
}
247-
case srvconn.ProtocolMySQL:
247+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
248248
if !IsInstalledMysqlClient() {
249249
msg := i18n.T("Database %s protocol client not installed.")
250250
msg = fmt.Sprintf(msg, connOpts.dbApp.TypeName)
@@ -417,7 +417,7 @@ func (s *Server) GenerateCommandItem(input, output string,
417417
DateCreated: createdDate.UTC(),
418418
}
419419

420-
case srvconn.ProtocolMySQL:
420+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
421421
return &model.Command{
422422
SessionID: s.ID,
423423
OrgID: s.connOpts.dbApp.OrgID,
@@ -492,7 +492,7 @@ func (s *Server) checkRequiredAuth() error {
492492
utils.IgnoreErrWriteString(s.UserConn, msg)
493493
return errors.New("no auth token")
494494
}
495-
case srvconn.ProtocolMySQL, srvconn.ProtocolTELNET:
495+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb, srvconn.ProtocolTELNET:
496496
if err := s.getUsernameIfNeed(); err != nil {
497497
msg := utils.WrapperWarn(i18n.T("Get auth username failed"))
498498
utils.IgnoreErrWriteString(s.UserConn, msg)
@@ -782,7 +782,7 @@ func (s *Server) getServerConn(proxyAddr *net.TCPAddr) (srvconn.ServerConnection
782782
return s.getTelnetConn()
783783
case srvconn.ProtocolK8s:
784784
return s.getK8sConConn(proxyAddr)
785-
case srvconn.ProtocolMySQL:
785+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
786786
return s.getMysqlConn(proxyAddr)
787787
default:
788788
return nil, ErrUnMatchProtocol
@@ -816,7 +816,7 @@ func (s *Server) checkLoginConfirm() bool {
816816
targetId string
817817
)
818818
switch s.connOpts.ProtocolType {
819-
case srvconn.ProtocolMySQL:
819+
case srvconn.ProtocolMySQL, srvconn.ProtocolMariadb:
820820
targetType = model.AppType
821821
targetId = s.connOpts.dbApp.ID
822822
case srvconn.ProtocolK8s:
@@ -872,7 +872,7 @@ func (s *Server) Proxy() {
872872
var proxyAddr *net.TCPAddr
873873
if s.domainGateways != nil && len(s.domainGateways.Gateways) != 0 {
874874
switch s.connOpts.ProtocolType {
875-
case srvconn.ProtocolMySQL, srvconn.ProtocolK8s:
875+
case srvconn.ProtocolMySQL, srvconn.ProtocolK8s, srvconn.ProtocolMariadb:
876876
dGateway, err := s.createAvailableGateWay(s.domainGateways)
877877
if err != nil {
878878
msg := i18n.T("Start domain gateway failed %s")

pkg/srvconn/conn.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ const (
2121
ProtocolTELNET = "telnet"
2222
ProtocolK8s = "k8s"
2323
ProtocolMySQL = "mysql"
24-
)
2524

25+
ProtocolMariadb = "mariadb"
26+
)
2627

2728
var (
2829
ErrUnSupportedProtocol = errors.New("unsupported protocol")
29-
)
30+
)

0 commit comments

Comments
 (0)