@@ -4,24 +4,21 @@ import (
44 "context"
55 "crypto/rsa"
66 "fmt"
7- "net/http"
8- "os"
9- "strconv"
10-
7+ "github.com/actiontech/dms/pkg/dms-common/dmsobject"
118 "github.com/actiontech/sqle/sqle/errors"
129 "github.com/actiontech/sqle/sqle/utils"
1310 "github.com/go-git/go-git/v5/config"
1411 "github.com/go-git/go-git/v5/plumbing"
12+ "net/http"
13+ "os"
1514
1615 "github.com/actiontech/sqle/sqle/api/controller"
16+ "github.com/actiontech/sqle/sqle/dms"
1717 "github.com/actiontech/sqle/sqle/driver"
1818
19- "github.com/actiontech/sqle/sqle/model"
20-
2119 "github.com/go-git/go-git/v5"
2220 "github.com/go-git/go-git/v5/plumbing/transport"
2321 goGitTransport "github.com/go-git/go-git/v5/plumbing/transport/http"
24-
2522 sshTransport "github.com/go-git/go-git/v5/plumbing/transport/ssh"
2623 "github.com/labstack/echo/v4"
2724 "golang.org/x/crypto/ssh"
@@ -98,53 +95,6 @@ type UpdateSystemVariablesReqV1 struct {
9895 CbOperationLogsExpiredHours * int `json:"cb_operation_logs_expired_hours" form:"cb_operation_logs_expired_hours" example:"2160"`
9996}
10097
101- // @Summary 修改系统变量
102- // @Description update system variables
103- // @Accept json
104- // @Id updateSystemVariablesV1
105- // @Tags configuration
106- // @Security ApiKeyAuth
107- // @Param instance body v1.UpdateSystemVariablesReqV1 true "update system variables request"
108- // @Success 200 {object} controller.BaseRes
109- // @router /v1/configurations/system_variables [patch]
110- func UpdateSystemVariables (c echo.Context ) error {
111- req := new (UpdateSystemVariablesReqV1 )
112- if err := controller .BindAndValidateReq (c , req ); err != nil {
113- return err
114- }
115-
116- s := model .GetStorage ()
117-
118- var systemVariables []model.SystemVariable
119-
120- if req .OperationRecordExpiredHours != nil {
121- systemVariables = append (systemVariables , model.SystemVariable {
122- Key : model .SystemVariableOperationRecordExpiredHours ,
123- Value : strconv .Itoa (* req .OperationRecordExpiredHours ),
124- })
125- }
126-
127- if req .CbOperationLogsExpiredHours != nil {
128- systemVariables = append (systemVariables , model.SystemVariable {
129- Key : model .SystemVariableCbOperationLogsExpiredHours ,
130- Value : strconv .Itoa (* req .CbOperationLogsExpiredHours ),
131- })
132- }
133-
134- if req .Url != nil {
135- systemVariables = append (systemVariables , model.SystemVariable {
136- Key : model .SystemVariableSqleUrl ,
137- Value : * req .Url ,
138- })
139- }
140-
141- if err := s .PathSaveSystemVariables (systemVariables ); err != nil {
142- return controller .JSONBaseErrorReq (c , err )
143- }
144-
145- return controller .JSONBaseErrorReq (c , nil )
146- }
147-
14898type GetSystemVariablesResV1 struct {
14999 controller.BaseRes
150100 Data SystemVariablesResV1 `json:"data"`
@@ -156,39 +106,6 @@ type SystemVariablesResV1 struct {
156106 CbOperationLogsExpiredHours int `json:"cb_operation_logs_expired_hours"`
157107}
158108
159- // @Summary 获取系统变量
160- // @Description get system variables
161- // @Id getSystemVariablesV1
162- // @Tags configuration
163- // @Security ApiKeyAuth
164- // @Success 200 {object} v1.GetSystemVariablesResV1
165- // @router /v1/configurations/system_variables [get]
166- func GetSystemVariables (c echo.Context ) error {
167- s := model .GetStorage ()
168- systemVariables , err := s .GetAllSystemVariables ()
169- if err != nil {
170- return controller .JSONBaseErrorReq (c , err )
171- }
172- operationRecordExpiredHours , err := strconv .Atoi (systemVariables [model .SystemVariableOperationRecordExpiredHours ].Value )
173- if err != nil {
174- return controller .JSONBaseErrorReq (c , err )
175- }
176-
177- cbOperationLogsExpiredHours , err := strconv .Atoi (systemVariables [model .SystemVariableCbOperationLogsExpiredHours ].Value )
178- if err != nil {
179- return controller .JSONBaseErrorReq (c , err )
180- }
181-
182- return c .JSON (http .StatusOK , & GetSystemVariablesResV1 {
183- BaseRes : controller .NewBaseReq (nil ),
184- Data : SystemVariablesResV1 {
185- Url : systemVariables [model .SystemVariableSqleUrl ].Value ,
186- OperationRecordExpiredHours : operationRecordExpiredHours ,
187- CbOperationLogsExpiredHours : cbOperationLogsExpiredHours ,
188- },
189- })
190- }
191-
192109type GetDriversResV1 struct {
193110 controller.BaseRes
194111 Data DriversResV1 `json:"data"`
@@ -498,15 +415,14 @@ func getGitAuthMethod(url, username, password string) (transport.AuthMethod, err
498415 // 2. 查看公钥
499416 // 3. 仓库配置密钥
500417 // 不支持该步骤,用户手动执行
501- storage := model .GetStorage ()
502- privateKey , exists , err := storage .GetSystemVariableByKey (model .SystemVariableSSHPrimaryKey )
418+ systemVariable , err := dmsobject .GetSystemVariables (context .TODO (), dms .GetDMSServerAddress ())
503419 if err != nil {
504420 return nil , err
505421 }
506- if ! exists {
422+ if systemVariable . Code != 0 {
507423 return nil , errors .New (errors .DataNotExist , fmt .Errorf ("git ssh private key not found" ))
508424 }
509- publicKeys , err := sshTransport .NewPublicKeys ("git" , []byte (privateKey . Value ), "" )
425+ publicKeys , err := sshTransport .NewPublicKeys ("git" , []byte (systemVariable . Data . SystemVariableSSHPrimaryKey ), "" )
510426 if err != nil {
511427 return nil , fmt .Errorf ("failed to load SSH key: %w" , err )
512428 }
@@ -630,20 +546,19 @@ type SSHPublicKeyInfo struct {
630546// @Success 200 {object} v1.SSHPublicKeyInfoV1Rsp
631547// @Router /v1/configurations/ssh_key [get]
632548func GetSSHPublicKey (c echo.Context ) error {
633- storage := model .GetStorage ()
634- systemVariables , exists , err := storage .GetSystemVariableByKey (model .SystemVariableSSHPrimaryKey )
549+ systemVariable , err := dmsobject .GetSystemVariables (context .TODO (), dms .GetDMSServerAddress ())
635550 if err != nil {
636551 return controller .JSONBaseErrorReq (c , err )
637552 }
638- if ! exists {
553+ if systemVariable . Code != 0 {
639554 return c .JSON (http .StatusOK , SSHPublicKeyInfoV1Rsp {
640555 BaseRes : controller .NewBaseReq (nil ),
641556 Data : SSHPublicKeyInfo {
642557 PublicKey : "" ,
643558 },
644559 })
645560 }
646- privateKey , err := ssh .ParseRawPrivateKey ([]byte (systemVariables . Value ))
561+ privateKey , err := ssh .ParseRawPrivateKey ([]byte (systemVariable . Data . SystemVariableSSHPrimaryKey ))
647562 if err != nil {
648563 return controller .JSONBaseErrorReq (c , err )
649564 }
@@ -673,14 +588,11 @@ func GetSSHPublicKey(c echo.Context) error {
673588// @Success 200 {object} controller.BaseRes
674589// @Router /v1/configurations/ssh_key [post]
675590func GenSSHKey (c echo.Context ) error {
676- storage := model .GetStorage ()
677- _ , exists , err := storage .GetSystemVariableByKey (model .SystemVariableSSHPrimaryKey )
591+ systemVariable , err := dmsobject .GetSystemVariables (context .TODO (), dms .GetDMSServerAddress ())
678592 if err != nil {
679593 return controller .JSONBaseErrorReq (c , err )
680594 }
681-
682- // 如果已存在密钥,则不重新生成
683- if exists {
595+ if systemVariable .Data .SystemVariableSSHPrimaryKey != "" {
684596 return controller .JSONBaseErrorReq (c , nil )
685597 }
686598
@@ -691,12 +603,7 @@ func GenSSHKey(c echo.Context) error {
691603 }
692604
693605 // 保存密钥到系统变量
694- err = storage .PathSaveSystemVariables ([]model.SystemVariable {
695- {
696- Key : model .SystemVariableSSHPrimaryKey ,
697- Value : primaryKey ,
698- },
699- })
606+ err = dms .UpdateSystemVariables (c .Request ().Context (), dms .SystemVariableSSHPrimaryKey , primaryKey )
700607 if err != nil {
701608 return controller .JSONBaseErrorReq (c , err )
702609 }
0 commit comments