@cloudbase/manager-node 5.5.2 → 5.5.3-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,14 @@ class DatabaseService {
46
46
  Tag: Databases[0].InstanceId
47
47
  };
48
48
  }
49
+ getMongoConnector() {
50
+ const currEnv = this.environment;
51
+ const { Databases } = currEnv.lazyEnvironmentConfig;
52
+ return {
53
+ DatabaseName: Databases[0].InstanceId,
54
+ InstanceId: 'flexdb'
55
+ };
56
+ }
49
57
  async checkCollectionExists(collectionName) {
50
58
  try {
51
59
  const result = await this.describeCollection(collectionName);
@@ -63,10 +71,10 @@ class DatabaseService {
63
71
  }
64
72
  }
65
73
  async createCollection(collectionName) {
66
- let { Tag } = this.getDatabaseConfig();
74
+ const MongoConnector = this.getMongoConnector();
67
75
  const res = await this.dbOpService.request('CreateTable', {
68
76
  EnvId: this.envId,
69
- Tag,
77
+ MongoConnector,
70
78
  TableName: collectionName
71
79
  });
72
80
  return res;
@@ -75,10 +83,10 @@ class DatabaseService {
75
83
  // 先检查当前集合是否存在
76
84
  const existRes = await this.checkCollectionExists(collectionName);
77
85
  if (existRes.Exists) {
78
- let { Tag } = this.getDatabaseConfig();
86
+ const MongoConnector = this.getMongoConnector();
79
87
  const res = await this.dbOpService.request('DeleteTable', {
80
88
  EnvId: this.envId,
81
- Tag,
89
+ MongoConnector,
82
90
  TableName: collectionName
83
91
  });
84
92
  return res;
@@ -88,15 +96,15 @@ class DatabaseService {
88
96
  }
89
97
  }
90
98
  async updateCollection(collectionName, indexiesInfo) {
91
- let { Tag } = this.getDatabaseConfig();
92
- const res = await this.dbOpService.request('UpdateTable', Object.assign({ EnvId: this.envId, Tag, TableName: collectionName }, indexiesInfo));
99
+ const MongoConnector = this.getMongoConnector();
100
+ const res = await this.dbOpService.request('UpdateTable', Object.assign({ EnvId: this.envId, MongoConnector, TableName: collectionName }, indexiesInfo));
93
101
  return res;
94
102
  }
95
103
  async describeCollection(collectionName) {
96
- let { Tag } = this.getDatabaseConfig();
104
+ const MongoConnector = this.getMongoConnector();
97
105
  return this.dbOpService.request('DescribeTable', {
98
106
  EnvId: this.envId,
99
- Tag,
107
+ MongoConnector,
100
108
  TableName: collectionName
101
109
  });
102
110
  }
@@ -105,14 +113,14 @@ class DatabaseService {
105
113
  MgoLimit: this.DEFAULT_MGO_LIMIT,
106
114
  MgoOffset: this.DEFAULT_MGO_OFFSET
107
115
  }) {
108
- let { Tag } = this.getDatabaseConfig();
116
+ const MongoConnector = this.getMongoConnector();
109
117
  if (options.MgoLimit === undefined) {
110
118
  options.MgoLimit = this.DEFAULT_MGO_LIMIT;
111
119
  }
112
120
  if (options.MgoOffset === undefined) {
113
121
  options.MgoOffset = this.DEFAULT_MGO_OFFSET;
114
122
  }
115
- const res = await this.dbOpService.request('ListTables', Object.assign({ EnvId: this.envId, Tag }, options));
123
+ const res = await this.dbOpService.request('ListTables', Object.assign({ EnvId: this.envId, MongoConnector }, options));
116
124
  if (res.Tables === null) {
117
125
  // 无集合
118
126
  res.Collections = [];
@@ -277,12 +285,16 @@ class DatabaseService {
277
285
  }
278
286
  /**
279
287
  * 执行文档型数据库命令
280
- * @param options 命令参数(命令列表、可选的 Tag/EnvId/MongoConnector)
288
+ * @param options 命令参数(命令列表、可选的 EnvId/MongoConnector)
281
289
  * @returns 执行结果,Data 为 JSON 字符串数组
282
290
  */
283
291
  async runCommands(options) {
284
- let { Tag } = this.getDatabaseConfig();
285
- return this.dbOpService.request('RunCommands', Object.assign({ MgoCommands: options.MgoCommands, Tag: options.Tag || Tag, EnvId: options.EnvId || this.envId }, (options.MongoConnector ? { MongoConnector: options.MongoConnector } : {})));
292
+ const MongoConnector = options.MongoConnector || this.getMongoConnector();
293
+ return this.dbOpService.request('RunCommands', {
294
+ MgoCommands: options.MgoCommands,
295
+ MongoConnector,
296
+ EnvId: options.EnvId || this.envId,
297
+ });
286
298
  }
287
299
  /**
288
300
  * 在 PostgreSQL 数据库上执行 SQL 语句
package/lib/env/index.js CHANGED
@@ -12,6 +12,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.EnvService = void 0;
13
13
  const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
14
14
  const util_1 = __importDefault(require("util"));
15
+ const http_request_1 = require("../utils/http-request");
16
+ const signature_nodejs_1 = require("@cloudbase/signature-nodejs");
15
17
  const error_1 = require("../error");
16
18
  const utils_1 = require("../utils");
17
19
  const constant_1 = require("../constant");
@@ -26,6 +28,7 @@ class EnvService {
26
28
  this.tcbService = new utils_1.CloudService(environment.cloudBaseContext, 'tcb', '2018-06-08');
27
29
  this.commonService = environment.getCommonService('lowcode', '2021-01-08');
28
30
  this.billingService = new billing_1.BillingService(environment.cloudBaseContext);
31
+ this.tokenCache = new Map();
29
32
  }
30
33
  /**
31
34
  * 列出所有环境
@@ -410,6 +413,74 @@ class EnvService {
410
413
  async describeApiKeyList(options) {
411
414
  return this.tcbService.request('DescribeApiKeyList', Object.assign({ EnvId: this.envId }, options));
412
415
  }
416
+ /**
417
+ * 根据密钥信息获取管理员访问令牌
418
+ * 使用 secretId、secretKey 和 sessionToken 签发访问令牌
419
+ * 默认从初始化时的 cloudBaseContext 中读取密钥,也可显式传入 credentials 覆盖
420
+ * @param {Object} [credentials] 密钥信息,不传则使用初始化时的密钥
421
+ * @param {string} credentials.secretId 密钥ID
422
+ * @param {string} credentials.secretKey 密钥Key
423
+ * @param {string} [credentials.token] sessionToken
424
+ * @returns {Promise<string>} 访问令牌
425
+ */
426
+ async getAdminAccessToken(credentials) {
427
+ const { secretId, secretKey, token } = credentials || this.environment.cloudBaseContext;
428
+ if (!secretId || !secretKey) {
429
+ throw new error_1.CloudBaseError('missing secretId or secretKey of tencent cloud');
430
+ }
431
+ const envId = this.envId;
432
+ const cacheKey = `access_token_secret_${envId}_${secretId}`;
433
+ const cached = this.tokenCache.get(cacheKey);
434
+ if (cached && cached.expiry > Date.now()) {
435
+ return cached.token;
436
+ }
437
+ const host = `${envId}${this.environment.cloudBaseContext.isInternalEndpoint()
438
+ ? '.api.intl.tcloudbasegateway.com'
439
+ : '.api.tcloudbasegateway.com'}`;
440
+ const url = `https://${host}/auth/v1/token/clientCredential`;
441
+ const method = 'POST';
442
+ const headers = {
443
+ 'Content-Type': 'application/json',
444
+ Host: host,
445
+ };
446
+ const data = { grant_type: 'client_credentials' };
447
+ const timestamp = Math.floor(new Date().getTime() / 1000) - 1;
448
+ const { authorization, timestamp: signTimestamp } = (0, signature_nodejs_1.sign)({
449
+ secretId,
450
+ secretKey,
451
+ method,
452
+ url,
453
+ headers,
454
+ params: data,
455
+ timestamp,
456
+ withSignedParams: false,
457
+ isCloudApi: true,
458
+ });
459
+ headers['Authorization'] = `${authorization}, Timestamp=${signTimestamp}${token ? `, Token=${token}` : ''}`;
460
+ headers['X-Signature-Expires'] = 600;
461
+ headers['X-Timestamp'] = signTimestamp;
462
+ try {
463
+ const response = await (0, http_request_1.fetch)(url, {
464
+ method,
465
+ headers,
466
+ body: JSON.stringify(data),
467
+ });
468
+ const res = await response.json();
469
+ const access_token = res === null || res === void 0 ? void 0 : res.access_token;
470
+ const expires_in = (res === null || res === void 0 ? void 0 : res.expires_in) || 0;
471
+ if (access_token && expires_in) {
472
+ this.tokenCache.set(cacheKey, {
473
+ token: access_token,
474
+ expiry: Date.now() + (expires_in * 1000) / 2,
475
+ });
476
+ }
477
+ return access_token;
478
+ }
479
+ catch (e) {
480
+ console.log(e);
481
+ throw e;
482
+ }
483
+ }
413
484
  // 创建自定义登录私钥
414
485
  async createCustomLoginKeys() {
415
486
  return this.cloudService.request('CreateCustomLoginKeys', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/manager-node",
3
- "version": "5.5.2",
3
+ "version": "5.5.3-beta.0",
4
4
  "description": "The node manage service api for cloudbase.",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
@@ -41,6 +41,7 @@
41
41
  "typescript": "^5.8.3"
42
42
  },
43
43
  "dependencies": {
44
+ "@cloudbase/signature-nodejs": "^1.0.0",
44
45
  "@cloudbase/database": "^0.6.2",
45
46
  "archiver": "^3.1.1",
46
47
  "cos-nodejs-sdk-v5": "^2.14.0",
@@ -155,6 +155,7 @@ export declare class DatabaseService {
155
155
  constructor(environment: Environment);
156
156
  getCurrEnvironment(): Environment;
157
157
  getDatabaseConfig(): IDatabaseConfig;
158
+ getMongoConnector(): IMongoConnector;
158
159
  checkCollectionExists(collectionName: string): Promise<IExistsRes>;
159
160
  createCollection(collectionName: string): Promise<any>;
160
161
  deleteCollection(collectionName: string): Promise<any>;
@@ -197,7 +198,7 @@ export declare class DatabaseService {
197
198
  restoreTables(time: string, modifyTableNamesInfo: IModifyTableNamesInfo[], instanceId?: string): Promise<IRestoreTCBTablesResult>;
198
199
  /**
199
200
  * 执行文档型数据库命令
200
- * @param options 命令参数(命令列表、可选的 Tag/EnvId/MongoConnector)
201
+ * @param options 命令参数(命令列表、可选的 EnvId/MongoConnector)
201
202
  * @returns 执行结果,Data 为 JSON 字符串数组
202
203
  */
203
204
  runCommands(options: IRunCommandsOptions): Promise<IRunCommandsResult>;
@@ -559,6 +559,7 @@ export declare class EnvService {
559
559
  private envType?;
560
560
  private commonService;
561
561
  private billingService;
562
+ private tokenCache;
562
563
  constructor(environment: Environment);
563
564
  /**
564
565
  * 列出所有环境
@@ -779,6 +780,21 @@ export declare class EnvService {
779
780
  * @returns {Promise<IDescribeApiKeyListRes>}
780
781
  */
781
782
  describeApiKeyList(options?: IDescribeApiKeyListParams): Promise<IDescribeApiKeyListRes>;
783
+ /**
784
+ * 根据密钥信息获取管理员访问令牌
785
+ * 使用 secretId、secretKey 和 sessionToken 签发访问令牌
786
+ * 默认从初始化时的 cloudBaseContext 中读取密钥,也可显式传入 credentials 覆盖
787
+ * @param {Object} [credentials] 密钥信息,不传则使用初始化时的密钥
788
+ * @param {string} credentials.secretId 密钥ID
789
+ * @param {string} credentials.secretKey 密钥Key
790
+ * @param {string} [credentials.token] sessionToken
791
+ * @returns {Promise<string>} 访问令牌
792
+ */
793
+ getAdminAccessToken(credentials?: {
794
+ secretId: string;
795
+ secretKey: string;
796
+ token?: string;
797
+ }): Promise<string>;
782
798
  createCustomLoginKeys(): Promise<{
783
799
  PrivateKey: string;
784
800
  KeyID: string;