-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsql_workbench_client.go
More file actions
1135 lines (994 loc) · 43.7 KB
/
Copy pathsql_workbench_client.go
File metadata and controls
1135 lines (994 loc) · 43.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package client
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"net/http"
"regexp"
"strings"
"time"
config "github.com/actiontech/dms/internal/sql_workbench/config"
utilLog "github.com/actiontech/dms/pkg/dms-common/pkg/log"
)
// SqlWorkbenchClient SQL工作台HTTP客户端
type SqlWorkbenchClient struct {
cfg *config.SqlWorkbenchOpts
log *utilLog.Helper
httpClient *http.Client
baseURL string
}
// NewSqlWorkbenchClient 创建SQL工作台客户端
func NewSqlWorkbenchClient(cfg *config.SqlWorkbenchOpts, logger utilLog.Logger) *SqlWorkbenchClient {
baseURL := fmt.Sprintf("http://%s:%s", cfg.Host, cfg.Port)
return &SqlWorkbenchClient{
cfg: cfg,
log: utilLog.NewHelper(logger, utilLog.WithMessageKey("sql_workbench.client")),
httpClient: &http.Client{
Timeout: 30 * time.Second,
},
baseURL: baseURL,
}
}
// GetPublicKey 获取RSA公钥
func (c *SqlWorkbenchClient) GetPublicKey() (string, error) {
c.log.Infof("Attempting to get public key")
// 构建获取公钥URL
publicKeyURL := fmt.Sprintf("%s/api/v2/encryption/publicKey", c.baseURL)
// 创建GET请求
req, err := http.NewRequest("GET", publicKeyURL, nil)
if err != nil {
c.log.Errorf("Failed to create get public key request: %v", err)
return "", fmt.Errorf("failed to create get public key request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send get public key request: %v", err)
return "", fmt.Errorf("failed to send get public key request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read get public key response: %v", err)
return "", fmt.Errorf("failed to read get public key response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Get public key failed with status code: %d, response: %s", resp.StatusCode, string(body))
return "", fmt.Errorf("get public key failed with status code: %d", resp.StatusCode)
}
// 解析响应
var publicKeyResp GetPublicKeyResponse
if err := json.Unmarshal(body, &publicKeyResp); err != nil {
c.log.Errorf("Failed to parse get public key response: %v", err)
return "", fmt.Errorf("failed to parse get public key response: %v", err)
}
// 检查业务状态
if !publicKeyResp.Successful {
errorMsg := "get public key failed"
if publicKeyResp.Message != nil {
errorMsg = *publicKeyResp.Message
}
c.log.Errorf("Get public key failed: %s", errorMsg)
return "", fmt.Errorf("get public key failed: %s", errorMsg)
}
c.log.Infof("Successfully retrieved public key")
return publicKeyResp.Data, nil
}
// Login 登录到SQL工作台
func (c *SqlWorkbenchClient) Login(username, password, publicKey string) (*LoginResponse, error) {
c.log.Infof("Attempting to login to SQL workbench for user: %s", username)
// 加密密码
encryptedPassword, err := c.EncryptPasswordWithRSA(password, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt password: %v", err)
return nil, fmt.Errorf("failed to encrypt password: %v", err)
}
// 构建登录URL
loginURL := fmt.Sprintf("%s/api/v2/iam/login?currentOrganizationId=&ignoreError=true", c.baseURL)
// 准备multipart表单数据
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// 添加表单字段,使用CreateFormField确保UTF-8编码正确
usernameField, err := writer.CreateFormField("username")
if err != nil {
c.log.Errorf("Failed to create username field: %v", err)
return nil, fmt.Errorf("failed to create username field: %v", err)
}
if _, err := usernameField.Write([]byte(username)); err != nil {
c.log.Errorf("Failed to write username field: %v", err)
return nil, fmt.Errorf("failed to write username field: %v", err)
}
passwordField, err := writer.CreateFormField("password")
if err != nil {
c.log.Errorf("Failed to create password field: %v", err)
return nil, fmt.Errorf("failed to create password field: %v", err)
}
if _, err := passwordField.Write([]byte(encryptedPassword)); err != nil {
c.log.Errorf("Failed to write password field: %v", err)
return nil, fmt.Errorf("failed to write password field: %v", err)
}
if err := writer.Close(); err != nil {
c.log.Errorf("Failed to close multipart writer: %v", err)
return nil, fmt.Errorf("failed to close multipart writer: %v", err)
}
// 创建请求
req, err := http.NewRequest("POST", loginURL, &buf)
if err != nil {
c.log.Errorf("Failed to create login request: %v", err)
return nil, fmt.Errorf("failed to create login request: %v", err)
}
// 设置请求头,显式指定 charset=utf-8 确保中文字符正确编码
contentType := writer.FormDataContentType() + "; charset=utf-8"
req.Header.Set("Content-Type", contentType)
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send login request: %v", err)
return nil, fmt.Errorf("failed to send login request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read login response: %v", err)
return nil, fmt.Errorf("failed to read login response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Login failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("login failed with status code: %d", resp.StatusCode)
}
// 解析响应
var loginResp LoginResponse
if err := json.Unmarshal(body, &loginResp); err != nil {
c.log.Errorf("Failed to parse login response: %v", err)
return nil, fmt.Errorf("failed to parse login response: %v", err)
}
// 检查业务状态
if !loginResp.Successful {
errorMsg := "login failed"
if loginResp.Message != nil {
errorMsg = *loginResp.Message
}
c.log.Errorf("Login failed: %s", errorMsg)
return nil, fmt.Errorf("login failed: %s", errorMsg)
}
loginResp.Cookie = resp.Header.Get("Set-Cookie")
c.log.Infof("Successfully logged in to SQL workbench for user: %s", username)
return &loginResp, nil
}
// GetOrganizations 获取组织信息
func (c *SqlWorkbenchClient) GetOrganizations(cookie string) (*GetOrganizationsResponse, error) {
c.log.Infof("Attempting to get organizations")
// 构建获取组织URL
organizationsURL := fmt.Sprintf("%s/api/v2/iam/users/me/organizations?currentOrganizationId=", c.baseURL)
// 创建GET请求
req, err := http.NewRequest("GET", organizationsURL, nil)
if err != nil {
c.log.Errorf("Failed to create get organizations request: %v", err)
return nil, fmt.Errorf("failed to create get organizations request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send get organizations request: %v", err)
return nil, fmt.Errorf("failed to send get organizations request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read get organizations response: %v", err)
return nil, fmt.Errorf("failed to read get organizations response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Get organizations failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("get organizations failed with status code: %d", resp.StatusCode)
}
// 解析响应
var organizationsResp GetOrganizationsResponse
if err := json.Unmarshal(body, &organizationsResp); err != nil {
c.log.Errorf("Failed to parse get organizations response: %v", err)
return nil, fmt.Errorf("failed to parse get organizations response: %v", err)
}
organizationsResp.XsrfToken = resp.Header.Get("Set-Cookie")
// 检查业务状态
if !organizationsResp.Successful {
errorMsg := "get organizations failed"
if organizationsResp.Message != nil {
errorMsg = *organizationsResp.Message
}
c.log.Errorf("Get organizations failed: %s", errorMsg)
return nil, fmt.Errorf("get organizations failed: %s", errorMsg)
}
c.log.Infof("Successfully retrieved %d organizations", len(organizationsResp.Data.Contents))
return &organizationsResp, nil
}
// Environment 环境信息
type Environment struct {
BuiltIn bool `json:"builtIn"`
CreateTime int64 `json:"createTime"`
Creator *User `json:"creator"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
ID int64 `json:"id"`
LastModifier *User `json:"lastModifier"`
Name string `json:"name"`
OrganizationID int64 `json:"organizationId"`
OriginalName string `json:"originalName"`
RulesetID int64 `json:"rulesetId"`
RulesetName *string `json:"rulesetName"`
Style string `json:"style"`
UpdateTime int64 `json:"updateTime"`
}
// GetEnvironmentsResponse 获取环境列表响应
type GetEnvironmentsResponse struct {
Data struct {
Contents []Environment `json:"contents"`
} `json:"data"`
DurationMillis int64 `json:"durationMillis"`
HTTPStatus string `json:"httpStatus"`
RequestID string `json:"requestId"`
Server string `json:"server"`
Successful bool `json:"successful"`
Timestamp float64 `json:"timestamp"`
TraceID string `json:"traceId"`
}
// GetEnvironments 获取环境列表
func (c *SqlWorkbenchClient) GetEnvironments(organizationID int64, cookie string) (*GetEnvironmentsResponse, error) {
c.log.Infof("Attempting to get environments for organization %d", organizationID)
// 构建获取环境列表URL
environmentsURL := fmt.Sprintf("%s/api/v2/collaboration/environments?currentOrganizationId=%d&enabled=true", c.baseURL, organizationID)
// 创建请求
req, err := http.NewRequest("GET", environmentsURL, nil)
if err != nil {
c.log.Errorf("Failed to create get environments request: %v", err)
return nil, fmt.Errorf("failed to create get environments request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send get environments request: %v", err)
return nil, fmt.Errorf("failed to send get environments request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read get environments response: %v", err)
return nil, fmt.Errorf("failed to read get environments response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Get environments failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("get environments failed with status code: %d", resp.StatusCode)
}
// 解析响应
var environmentsResp GetEnvironmentsResponse
if err := json.Unmarshal(body, &environmentsResp); err != nil {
c.log.Errorf("Failed to parse get environments response: %v", err)
return nil, fmt.Errorf("failed to parse get environments response: %v", err)
}
// 检查业务状态
if !environmentsResp.Successful {
c.log.Errorf("Get environments failed: %s", environmentsResp.HTTPStatus)
return nil, fmt.Errorf("get environments failed: %s", environmentsResp.HTTPStatus)
}
c.log.Infof("Successfully got %d environments", len(environmentsResp.Data.Contents))
return &environmentsResp, nil
}
// CreateDatasources 创建数据源
func (c *SqlWorkbenchClient) CreateDatasources(datasource CreateDatasourceRequest, publicKey string, cookie string, organizationID int64) (*CreateDatasourceResponse, error) {
c.log.Infof("Attempting to create datasource: %s", datasource.Name)
// 加密密码
encryptedPassword, err := c.EncryptPasswordWithRSA(datasource.Password, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt password for datasource %s: %v", datasource.Name, err)
return nil, fmt.Errorf("failed to encrypt password for datasource %s: %v", datasource.Name, err)
}
datasource.Password = encryptedPassword
// 构建创建数据源URL
createDatasourceURL := fmt.Sprintf("%s/api/v2/datasource/datasources?currentOrganizationId=%d&wantCatchError=false&holdErrorTip=true", c.baseURL, organizationID)
// 准备JSON请求体
jsonData, err := json.Marshal(datasource)
if err != nil {
c.log.Errorf("Failed to marshal datasource data: %v", err)
return nil, fmt.Errorf("failed to marshal datasource data: %v", err)
}
// 创建请求
req, err := http.NewRequest("POST", createDatasourceURL, bytes.NewBuffer(jsonData))
if err != nil {
c.log.Errorf("Failed to create create datasource request: %v", err)
return nil, fmt.Errorf("failed to create create datasource request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send create datasource request: %v", err)
return nil, fmt.Errorf("failed to send create datasource request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read create datasource response: %v", err)
return nil, fmt.Errorf("failed to read create datasource response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Create datasource failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("create datasource failed with status code: %d, response: %s", resp.StatusCode, string(body))
}
// 解析响应
var createDatasourceResp CreateDatasourceResponse
if err := json.Unmarshal(body, &createDatasourceResp); err != nil {
c.log.Errorf("Failed to parse create datasource response: %v", err)
return nil, fmt.Errorf("failed to parse create datasource response: %v", err)
}
// 检查业务状态
if !createDatasourceResp.Successful {
errorMsg := "create datasource failed"
if createDatasourceResp.Message != nil {
errorMsg = *createDatasourceResp.Message
}
c.log.Errorf("Create datasource failed: %s", errorMsg)
return nil, fmt.Errorf("create datasource failed: %s", errorMsg)
}
c.log.Infof("Successfully created datasource: %s (ID: %d)", createDatasourceResp.Data.Name, createDatasourceResp.Data.ID)
return &createDatasourceResp, nil
}
// UpdateDatasource 修改数据源
func (c *SqlWorkbenchClient) UpdateDatasource(datasourceId int64, updateDatasource UpdateDatasourceRequest, publicKey string, cookie string, organizationID int64) (*CreateDatasourceResponse, error) {
c.log.Infof("Attempting to update datasource with ID: %d", datasourceId)
if updateDatasource.Password != nil {
// 加密密码
encryptedPassword, err := c.EncryptPasswordWithRSA(*updateDatasource.Password, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt password for datasource %s: %v", *updateDatasource.Name, err)
return nil, fmt.Errorf("failed to encrypt password for datasource %s: %v", *updateDatasource.Name, err)
}
updateDatasource.Password = &encryptedPassword
}
// 构建修改数据源URL
updateDatasourceURL := fmt.Sprintf("%s/api/v2/datasource/datasources/%d?currentOrganizationId=%d", c.baseURL, datasourceId, organizationID)
// 准备JSON请求体
jsonData, err := json.Marshal(updateDatasource)
if err != nil {
c.log.Errorf("Failed to marshal update datasource data: %v", err)
return nil, fmt.Errorf("failed to marshal update datasource data: %v", err)
}
// 创建PUT请求
req, err := http.NewRequest("PUT", updateDatasourceURL, bytes.NewBuffer(jsonData))
if err != nil {
c.log.Errorf("Failed to create update datasource request: %v", err)
return nil, fmt.Errorf("failed to create update datasource request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send update datasource request: %v", err)
return nil, fmt.Errorf("failed to send update datasource request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read update datasource response: %v", err)
return nil, fmt.Errorf("failed to read update datasource response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Update datasource failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("update datasource failed with status code: %d", resp.StatusCode)
}
// 解析响应
var updateDatasourceResp CreateDatasourceResponse
if err := json.Unmarshal(body, &updateDatasourceResp); err != nil {
c.log.Errorf("Failed to parse update datasource response: %v", err)
return nil, fmt.Errorf("failed to parse update datasource response: %v", err)
}
// 检查业务状态
if !updateDatasourceResp.Successful {
errorMsg := "update datasource failed"
if updateDatasourceResp.Message != nil {
errorMsg = *updateDatasourceResp.Message
}
c.log.Errorf("Update datasource failed: %s", errorMsg)
return nil, fmt.Errorf("update datasource failed: %s", errorMsg)
}
c.log.Infof("Successfully updated datasource: %s (ID: %d)", updateDatasourceResp.Data.Name, updateDatasourceResp.Data.ID)
return &updateDatasourceResp, nil
}
// DeleteDatasource 删除数据源
func (c *SqlWorkbenchClient) DeleteDatasource(id int64, cookie string, organizationID int64) (*DeleteDatasourceResponse, error) {
c.log.Infof("Attempting to delete datasource with ID: %d", id)
// 构建删除数据源URL
deleteDatasourceURL := fmt.Sprintf("%s/api/v2/datasource/datasources/%d?currentOrganizationId=%d", c.baseURL, id, organizationID)
// 创建DELETE请求
req, err := http.NewRequest("DELETE", deleteDatasourceURL, nil)
if err != nil {
c.log.Errorf("Failed to create delete datasource request: %v", err)
return nil, fmt.Errorf("failed to create delete datasource request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send delete datasource request: %v", err)
return nil, fmt.Errorf("failed to send delete datasource request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read delete datasource response: %v", err)
return nil, fmt.Errorf("failed to read delete datasource response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Delete datasource failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("delete datasource failed with status code: %d", resp.StatusCode)
}
// 解析响应
var deleteDatasourceResp DeleteDatasourceResponse
if err := json.Unmarshal(body, &deleteDatasourceResp); err != nil {
c.log.Errorf("Failed to parse delete datasource response: %v", err)
return nil, fmt.Errorf("failed to parse delete datasource response: %v", err)
}
// 检查业务状态
if !deleteDatasourceResp.Successful {
errorMsg := "delete datasource failed"
if deleteDatasourceResp.Message != nil {
errorMsg = *deleteDatasourceResp.Message
}
c.log.Errorf("Delete datasource failed: %s", errorMsg)
return nil, fmt.Errorf("delete datasource failed: %s", errorMsg)
}
c.log.Infof("Successfully deleted datasource: %s (ID: %d)", deleteDatasourceResp.Data.Name, deleteDatasourceResp.Data.ID)
return &deleteDatasourceResp, nil
}
// CreateUsers 创建用户
func (c *SqlWorkbenchClient) CreateUsers(users []CreateUserRequest, publicKey string, cookie string) (*CreateUsersResponse, error) {
c.log.Infof("Attempting to create %d users", len(users))
// 加密所有用户的密码
for i := range users {
encryptedPassword, err := c.EncryptPasswordWithRSA(users[i].Password, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt password for user %s: %v", users[i].AccountName, err)
return nil, fmt.Errorf("failed to encrypt password for user %s: %v", users[i].AccountName, err)
}
users[i].Password = encryptedPassword
}
// 构建创建用户URL
createUsersURL := fmt.Sprintf("%s/api/v2/iam/users/batchCreate?currentOrganizationId=1", c.baseURL)
// 准备JSON请求体
jsonData, err := json.Marshal(users)
if err != nil {
c.log.Errorf("Failed to marshal users data: %v", err)
return nil, fmt.Errorf("failed to marshal users data: %v", err)
}
// 创建请求
req, err := http.NewRequest("POST", createUsersURL, bytes.NewBuffer(jsonData))
if err != nil {
c.log.Errorf("Failed to create create users request: %v", err)
return nil, fmt.Errorf("failed to create create users request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send create users request: %v", err)
return nil, fmt.Errorf("failed to send create users request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read create users response: %v", err)
return nil, fmt.Errorf("failed to read create users response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Create users failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("create users failed with status code: %d", resp.StatusCode)
}
// 解析响应
var createUsersResp CreateUsersResponse
if err := json.Unmarshal(body, &createUsersResp); err != nil {
c.log.Errorf("Failed to parse create users response: %v", err)
return nil, fmt.Errorf("failed to parse create users response: %v", err)
}
// 检查业务状态
if !createUsersResp.Successful {
errorMsg := "create users failed"
if createUsersResp.Message != nil {
errorMsg = *createUsersResp.Message
}
c.log.Errorf("Create users failed: %s", errorMsg)
return nil, fmt.Errorf("create users failed: %s", errorMsg)
}
c.log.Infof("Successfully created %d users", len(createUsersResp.Data.Contents))
return &createUsersResp, nil
}
// ActivateUser 激活用户
func (c *SqlWorkbenchClient) ActivateUser(username, currentPassword, newPassword, publicKey, cookie string) (*ActivateUserResponse, error) {
c.log.Infof("Attempting to activate user: %s", username)
// 加密当前密码和新密码
encryptedCurrentPassword, err := c.EncryptPasswordWithRSA(currentPassword, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt current password for user %s: %v", username, err)
return nil, fmt.Errorf("failed to encrypt current password for user %s: %v", username, err)
}
encryptedNewPassword, err := c.EncryptPasswordWithRSA(newPassword, publicKey)
if err != nil {
c.log.Errorf("Failed to encrypt new password for user %s: %v", username, err)
return nil, fmt.Errorf("failed to encrypt new password for user %s: %v", username, err)
}
// 构建激活用户URL
activateUserURL := fmt.Sprintf("%s/api/v2/iam/users/%s/activate?currentOrganizationId=", c.baseURL, username)
// 准备激活用户请求
activateUserReq := ActivateUserRequest{
Username: username,
CurrentPassword: encryptedCurrentPassword,
NewPassword: encryptedNewPassword,
}
// 准备JSON请求体
jsonData, err := json.Marshal(activateUserReq)
if err != nil {
c.log.Errorf("Failed to marshal activate user data: %v", err)
return nil, fmt.Errorf("failed to marshal activate user data: %v", err)
}
// 创建请求
req, err := http.NewRequest("POST", activateUserURL, bytes.NewBuffer(jsonData))
if err != nil {
c.log.Errorf("Failed to create activate user request: %v", err)
return nil, fmt.Errorf("failed to create activate user request: %v", err)
}
// 设置请求头
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Cookie", cookie)
req.Header.Set("X-Xsrf-Token", c.ExtractCookieValue(cookie, "XSRF-TOKEN"))
// 发送请求
resp, err := c.httpClient.Do(req)
if err != nil {
c.log.Errorf("Failed to send activate user request: %v", err)
return nil, fmt.Errorf("failed to send activate user request: %v", err)
}
defer resp.Body.Close()
// 读取响应
body, err := io.ReadAll(resp.Body)
if err != nil {
c.log.Errorf("Failed to read activate user response: %v", err)
return nil, fmt.Errorf("failed to read activate user response: %v", err)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
c.log.Errorf("Activate user failed with status code: %d, response: %s", resp.StatusCode, string(body))
return nil, fmt.Errorf("activate user failed with status code: %d", resp.StatusCode)
}
// 解析响应
var activateUserResp ActivateUserResponse
if err := json.Unmarshal(body, &activateUserResp); err != nil {
c.log.Errorf("Failed to parse activate user response: %v", err)
return nil, fmt.Errorf("failed to parse activate user response: %v", err)
}
// 检查业务状态
if !activateUserResp.Successful {
errorMsg := "activate user failed"
if activateUserResp.Message != nil {
errorMsg = *activateUserResp.Message
}
c.log.Errorf("Activate user failed: %s", errorMsg)
return nil, fmt.Errorf("activate user failed: %s", errorMsg)
}
c.log.Infof("Successfully activated user: %s (ID: %d)", activateUserResp.Data.Name, activateUserResp.Data.ID)
return &activateUserResp, nil
}
// EncryptPasswordWithRSA 使用RSA公钥加密密码(兼容JSEncrypt)
func (c *SqlWorkbenchClient) EncryptPasswordWithRSA(password, publicKey string) (string, error) {
// 解码Base64格式的公钥
keyBytes, err := base64.StdEncoding.DecodeString(publicKey)
if err != nil {
return "", fmt.Errorf("failed to decode base64 public key: %v", err)
}
// 解析公钥
pub, err := x509.ParsePKIXPublicKey(keyBytes)
if err != nil {
return "", fmt.Errorf("failed to parse public key: %v", err)
}
rsaPub, ok := pub.(*rsa.PublicKey)
if !ok {
return "", fmt.Errorf("not an RSA public key")
}
// 使用PKCS#1 v1.5填充加密(兼容JSEncrypt)
encrypted, err := rsa.EncryptPKCS1v15(rand.Reader, rsaPub, []byte(password))
if err != nil {
return "", fmt.Errorf("failed to encrypt password: %v", err)
}
// 返回Base64编码的加密结果
return base64.StdEncoding.EncodeToString(encrypted), nil
}
// ExtractCookieValue 从Cookie字符串中提取指定名称的值
func (c *SqlWorkbenchClient) ExtractCookieValue(cookieStr, cookieName string) string {
if cookieStr == "" {
return ""
}
// 使用正则表达式提取Cookie值
pattern := fmt.Sprintf(`%s=([^;]+)`, regexp.QuoteMeta(cookieName))
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(cookieStr)
if len(matches) > 1 {
return matches[1]
}
return ""
}
// MergeCookies 合并XSRF-TOKEN和JSESSIONID
func (c *SqlWorkbenchClient) MergeCookies(xsrfTokenStr, jsessionIdStr string) string {
xsrfToken := c.ExtractCookieValue(xsrfTokenStr, "XSRF-TOKEN")
jsessionId := c.ExtractCookieValue(jsessionIdStr, "JSESSIONID")
if xsrfToken == "" && jsessionId == "" {
return ""
}
var cookies []string
if jsessionId != "" {
cookies = append(cookies, fmt.Sprintf("JSESSIONID=%s", jsessionId))
}
if xsrfToken != "" {
cookies = append(cookies, fmt.Sprintf("XSRF-TOKEN=%s", xsrfToken))
}
return strings.Join(cookies, "; ")
}
// LoginRequest 登录请求结构
type LoginRequest struct {
Username string `form:"username"`
Password string `form:"password"`
}
// LoginResponse 登录响应结构
type LoginResponse struct {
Data string `json:"data"`
DurationMillis *int64 `json:"durationMillis"`
HTTPStatus string `json:"httpStatus"`
RequestID *string `json:"requestId"`
Server *string `json:"server"`
Successful bool `json:"successful"`
Timestamp *int64 `json:"timestamp"`
TraceID *string `json:"traceId"`
// 错误相关字段
Code *string `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
Cookie string `json:"cookie"`
Error interface{} `json:"error"`
}
// CreateUserRequest 创建用户请求结构
type CreateUserRequest struct {
AccountName string `json:"accountName"`
Name string `json:"name"`
Password string `json:"password"`
Enabled bool `json:"enabled"`
RoleIDs []int64 `json:"roleIds"`
}
// Address 地址结构
type Address struct {
Country *string `json:"country"`
Formatted *string `json:"formatted"`
Locality *string `json:"locality"`
PostalCode *string `json:"postalCode"`
Region *string `json:"region"`
StreetAddress *string `json:"streetAddress"`
}
// User 用户结构
type User struct {
AccessTokenHash *string `json:"accessTokenHash"`
AccountName string `json:"accountName"`
AccountNonExpired bool `json:"accountNonExpired"`
AccountNonLocked bool `json:"accountNonLocked"`
Active bool `json:"active"`
Address Address `json:"address"`
Attributes interface{} `json:"attributes"`
Audience *string `json:"audience"`
AuthenticatedAt *int64 `json:"authenticatedAt"`
AuthenticationContextClass *string `json:"authenticationContextClass"`
AuthenticationMethods interface{} `json:"authenticationMethods"`
Authorities interface{} `json:"authorities"`
AuthorizationCodeHash *string `json:"authorizationCodeHash"`
AuthorizedActions interface{} `json:"authorizedActions"`
AuthorizedParty *string `json:"authorizedParty"`
Birthdate *string `json:"birthdate"`
BuiltIn bool `json:"builtIn"`
Claims map[string]interface{} `json:"claims"`
CreateTime int64 `json:"createTime"`
CreatorID int64 `json:"creatorId"`
CreatorName *string `json:"creatorName"`
CredentialsNonExpired bool `json:"credentialsNonExpired"`
Description *string `json:"description"`
Email *string `json:"email"`
EmailVerified *bool `json:"emailVerified"`
Enabled bool `json:"enabled"`
ExpiresAt *int64 `json:"expiresAt"`
ExtraProperties interface{} `json:"extraProperties"`
FamilyName *string `json:"familyName"`
FullName *string `json:"fullName"`
Gender *string `json:"gender"`
GivenName *string `json:"givenName"`
ID int64 `json:"id"`
IDToken *string `json:"idToken"`
IssuedAt *int64 `json:"issuedAt"`
Issuer *string `json:"issuer"`
LastLoginTime *int64 `json:"lastLoginTime"`
Locale *string `json:"locale"`
LoginTime *int64 `json:"loginTime"`
MiddleName *string `json:"middleName"`
Name string `json:"name"`
NickName *string `json:"nickName"`
Nonce *string `json:"nonce"`
OrganizationID int64 `json:"organizationId"`
OrganizationIDs interface{} `json:"organizationIds"`
OrganizationType *string `json:"organizationType"`
PhoneNumber *string `json:"phoneNumber"`
PhoneNumberVerified *bool `json:"phoneNumberVerified"`
Picture *string `json:"picture"`
PreferredUsername *string `json:"preferredUsername"`
Profile *string `json:"profile"`
ResourceManagementPermissions interface{} `json:"resourceManagementPermissions"`
RoleIDs interface{} `json:"roleIds"`
Roles interface{} `json:"roles"`
Subject *string `json:"subject"`
SystemOperationPermissions interface{} `json:"systemOperationPermissions"`
Type string `json:"type"`
UpdateTime int64 `json:"updateTime"`
UpdatedAt *int64 `json:"updatedAt"`
UserInfo interface{} `json:"userInfo"`
Username string `json:"username"`
Website *string `json:"website"`
ZoneInfo *string `json:"zoneInfo"`
}
// CreateUsersResponse 创建用户响应结构
type CreateUsersResponse struct {
Data struct {
Contents []User `json:"contents"`
} `json:"data"`
DurationMillis int64 `json:"durationMillis"`
HTTPStatus string `json:"httpStatus"`
RequestID string `json:"requestId"`
Server string `json:"server"`
Successful bool `json:"successful"`
Timestamp float64 `json:"timestamp"`
TraceID string `json:"traceId"`
// 错误相关字段
Code *string `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
Error interface{} `json:"error,omitempty"`
}
// Organization 组织结构
type Organization struct {
Builtin bool `json:"builtin"`
CreateTime int64 `json:"createTime"`
Description string `json:"description"`
DisplayName string `json:"displayName"`
ID int64 `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
UniqueIdentifier string `json:"uniqueIdentifier"`
UpdateTime int64 `json:"updateTime"`
}
// GetOrganizationsResponse 获取组织响应结构
type GetOrganizationsResponse struct {
Data struct {
Contents []Organization `json:"contents"`
} `json:"data"`
DurationMillis int64 `json:"durationMillis"`
HTTPStatus string `json:"httpStatus"`
RequestID *string `json:"requestId"`
Server string `json:"server"`
Successful bool `json:"successful"`
Timestamp float64 `json:"timestamp"`
TraceID string `json:"traceId"`
XsrfToken string `json:"xsrfToken"`
// 错误相关字段
Code *string `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
Error interface{} `json:"error,omitempty"`
}
// SSLConfig SSL配置结构
type SSLConfig struct {
Enabled bool `json:"enabled"`
CACertObjectID *string `json:"CACertObjectId,omitempty"`
CacertObjectID *string `json:"cacertObjectId,omitempty"`
ClientCertObjectID *string `json:"clientCertObjectId,omitempty"`
ClientKeyObjectID *string `json:"clientKeyObjectId,omitempty"`
}
// CreateDatasourceRequest 创建数据源请求结构
type CreateDatasourceRequest struct {
CreatorID int64 `json:"creatorId"`
Type string `json:"type"`
Name string `json:"name"`
Username string `json:"username"`
Password string `json:"password"`
SysTenantUsername *string `json:"sysTenantUsername"`
ServiceName *string `json:"serviceName"`
SSLConfig SSLConfig `json:"sslConfig"`
SysTenantPassword *string `json:"sysTenantPassword"`
Properties interface{} `json:"properties"`
EnvironmentID int64 `json:"environmentId"`
JdbcURLParameters map[string]interface{} `json:"jdbcUrlParameters"`
Host string `json:"host"`
Port string `json:"port"`
DefaultSchema *string `json:"defaultSchema,omitempty"`
}
// UpdateDatasourceRequest 更新数据源请求结构
type UpdateDatasourceRequest struct {
Id int64 `json:"id"`
CreatorID *int64 `json:"creatorId"`
Type string `json:"type"`
Name *string `json:"name"`
Username string `json:"username"`
Password *string `json:"password"`
SysTenantUsername *string `json:"sysTenantUsername"`
ServiceName *string `json:"serviceName"`
SSLConfig SSLConfig `json:"sslConfig"`
SysTenantPassword *string `json:"sysTenantPassword"`
Properties *interface{} `json:"properties"`
EnvironmentID int64 `json:"environmentId"`
JdbcURLParameters *map[string]interface{} `json:"jdbcUrlParameters"`
Host string `json:"host"`
Port string `json:"port"`