Skip to content

Commit 5e0fb6b

Browse files
committed
support login status
1 parent 366a915 commit 5e0fb6b

File tree

11 files changed

+248
-60
lines changed

11 files changed

+248
-60
lines changed

package-lock.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
}
113113
],
114114
"commands": [
115+
{
116+
"command": "tcTerraform.login.status",
117+
"title": "login.status",
118+
"category": "tiat internel category"
119+
},
115120
{
116121
"command": "tcTerraform.login",
117122
"title": "Login: %TcTerraform.view.login.welcome%",
@@ -288,7 +293,9 @@
288293
"shared": "^0.2.0",
289294
"strip-ansi": "=6.0.0",
290295
"tencentcloud-sdk-nodejs": "^4.0.580",
296+
"tencentcloud-sdk-nodejs-cam": "^4.0.746",
291297
"tencentcloud-sdk-nodejs-cvm": "^4.0.580",
298+
"tencentcloud-sdk-nodejs-sts": "^4.0.746",
292299
"tencentcloud-sdk-nodejs-tke": "^4.0.576",
293300
"vscode-extension-telemetry-wrapper": "^0.13.3",
294301
"vscode-nls-i18n": "^0.2.4"

package.nls.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
"TcTerraform.msg.aksk.notfound": "Cannot find TENCENTCLOUD_SECRET_ID and TENCENTCLOUD_SECRET_KEY, please sign in first!",
2121
"TcTerraform.logout": "Logout Tencent Cloud ({0})",
2222
"TcTerraform.login": "Login Tencent Cloud...",
23-
"TcTerraform.login.success": "Logged into Tencent Cloud successfully."
23+
"TcTerraform.login.success": "Logged into Tencent Cloud successfully.",
24+
"TcTerraform.login.failed": "Logged failed, please try angin."
2425
}

src/commons/tencent/commands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import user from "./user";
44
export function registerLoginCmds() {
55
commands.registerCommand(command.TENCENT_LOGIN, user.login);
66

7-
// commands.registerCommand(command.TENCENT_LOGINOUT, user.loginOut);
7+
commands.registerCommand(command.TENCENT_LOGINOUT, user.loginOut);
8+
9+
// commands.registerCommand(command.TENCENT_LOGIN_STATUS, user.loginStatus);
810
}
911

1012
export namespace command {
1113
// login command
1214
export const TENCENT_LOGIN = "tcTerraform.login";
1315
// logout command
1416
export const TENCENT_LOGINOUT = "tcTerraform.logout";
17+
18+
export const TENCENT_LOGIN_STATUS = "tcTerraform.login.status";
1519
}

src/commons/tencent/treeDataProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export namespace tree {
1616
const treeDataProvider =
1717
container.getAll<TreeDataProvider>(TencentTreeProvider);
1818

19-
treeDataProvider.map((item) => item.refresh());
19+
treeDataProvider.forEach((item) => item.refresh());
2020
window.showInformationMessage(localize("TcTerraform.refresh.success"));
2121
}
2222

src/commons/tencent/user/index.ts

Lines changed: 123 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
import { localize } from "vscode-nls-i18n";
2-
import { ExtensionContext, workspace, ConfigurationTarget, window } from "vscode";
2+
import { ExtensionContext, workspace, ConfigurationTarget, window, ProgressLocation } from "vscode";
33

44
import { container } from "../../container";
55
import { Context } from "../../context";
66
import { tree } from "../treeDataProvider";
77
import { getCredentailByInput } from "./auth";
8+
import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client";
9+
import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface";
810
import { LoginProvider } from "../../../views/login/loginExplorer";
911
import { terraformShellManager } from "../../../client/terminal/terraformShellManager";
12+
import { getStsClient } from "@/connectivity/client";
13+
import { StreamingStatistics } from "tencentcloud-sdk-nodejs/tencentcloud/services/dlc/v20210125/dlc_models";
1014

1115
export namespace user {
1216
interface UserInfo {
1317
secretId: string;
1418
secretKey: string;
1519
token?: string;
1620
uin: string;
21+
subuin?: string;
22+
type?: string;
23+
appid?: string;
24+
region?: string;
1725
}
1826

1927
export const AKSK_TITLE = "TcTerraform.pickup.aksk";
@@ -28,6 +36,7 @@ export namespace user {
2836
const oauth = localize(OAUTH_TITLE);
2937
const pick = await window.showQuickPick([aksk, oauth]);
3038

39+
// only support aksk way right now
3140
if (aksk === pick) {
3241
const credential = await getCredentailByInput();
3342
const accessKey = credential.secretId;
@@ -51,20 +60,37 @@ export namespace user {
5160
process.env.TENCENTCLOUD_SECRET_ID = accessKey;
5261
process.env.TENCENTCLOUD_SECRET_KEY = secretKey;
5362

54-
tree.refreshTreeData();
55-
window.showInformationMessage(localize("TcTerraform.login.success"));
63+
// query user info
64+
const resp = await (await getStsClient()).GetCallerIdentity({}).
65+
then(
66+
(result) => {
67+
console.debug('[DEBUG]--------------------------------result:', result);
68+
if (!result) {
69+
throw new Error('[Warn] GetCallerIdentity result.TotalCount is 0.');
70+
}
71+
return result;
72+
},
73+
(err) => {
74+
console.error('[TencentCloudSDKError] GetCallerIdentity got a error from SDK.', err.message);
75+
window.showErrorMessage('Login Failed. Reason:' + err.message);
76+
return err;
77+
}
78+
);
79+
80+
// set user info
81+
let userinfo: UserInfo = {
82+
secretId: accessKey,
83+
secretKey: secretKey,
84+
uin: resp.PrincipalId ?? resp.UserId ?? "-",
85+
type: resp.Type ?? "unknow"
86+
};
87+
setInfo(userinfo);
88+
89+
// tree.refreshTreeData();
5690
}
57-
}
58-
59-
export async function getInfo(): Promise<UserInfo | undefined> {
60-
const { secrets } = container.get<ExtensionContext>(Context);
61-
const userinfo = await secrets.get(USER_INFO);
62-
63-
if (userinfo) {
64-
return JSON.parse(userinfo) as UserInfo;
91+
if (oauth === pick) {
92+
// to do
6593
}
66-
67-
return undefined;
6894
}
6995

7096
export async function loginOut() {
@@ -86,6 +112,90 @@ export namespace user {
86112

87113
tree.refreshTreeData();
88114
}
115+
116+
async function loginBySecret(credential: Credential) {
117+
const client = new AbstractClient(
118+
"open.test.tencentcloudapi.com",
119+
"2018-12-25",
120+
{
121+
credential,
122+
profile: {
123+
httpProfile: {
124+
proxy: "http://9.135.97.58:8899",
125+
},
126+
},
127+
}
128+
);
129+
try {
130+
const res = await client.request("GetUserAuthInfo", {});
131+
132+
const { Error: error, ...rest } = res;
133+
134+
if (error) {
135+
const err = new Error(error.Message);
136+
err.stack = JSON.stringify(error);
137+
138+
return Promise.reject(err);
139+
}
140+
141+
return rest;
142+
} catch (e) {
143+
throw e;
144+
}
145+
}
146+
147+
async function loginByCredentail() {
148+
let userInfo: UserInfo | undefined;
149+
const credential = await getCredentailByInput();
150+
151+
if (credential) {
152+
try {
153+
await window.withProgress(
154+
{
155+
title: localize("login.title"),
156+
location: ProgressLocation.Notification,
157+
},
158+
async () => {
159+
const res = await loginBySecret(credential);
160+
if (res) {
161+
userInfo = {
162+
uin: res.Uin,
163+
secretId: credential.secretId,
164+
secretKey: credential.secretKey,
165+
token: res.token,
166+
};
167+
setInfo(userInfo);
168+
}
169+
}
170+
);
171+
} catch (error) {
172+
console.error("loginByCredentail", error);
173+
174+
const message = error instanceof Error ? `: ${error.message}` : "";
175+
window.showErrorMessage(localize("login.fail", message));
176+
}
177+
}
178+
179+
return userInfo;
180+
}
181+
182+
async function setInfo(info: UserInfo) {
183+
const { secrets } = container.get<ExtensionContext>(Context);
184+
185+
await secrets.store(USER_INFO, JSON.stringify(info));
186+
tree.refreshTreeData();
187+
}
188+
189+
export async function getInfo(): Promise<UserInfo | undefined> {
190+
const { secrets } = container.get<ExtensionContext>(Context);
191+
const userinfo = await secrets.get(USER_INFO);
192+
193+
if (userinfo) {
194+
return JSON.parse(userinfo) as UserInfo;
195+
}
196+
197+
return undefined;
198+
}
89199
}
90200

91201
export default user;

src/connectivity/client.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
12
"use strict";
23

34
import * as vscode from "vscode";
45
import { Client as CvmClient } from "tencentcloud-sdk-nodejs-cvm/tencentcloud/services/cvm/v20170312/cvm_client";
56
import { Client as TkeClient } from "tencentcloud-sdk-nodejs-tke/tencentcloud/services/tke/v20180525/tke_client";
7+
import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client";
8+
import * as tencentcloud from "tencentcloud-sdk-nodejs";
69
import { localize } from "vscode-nls-i18n";
710
import * as settingUtils from "../utils/settingUtils";
811

9-
const tkeClient = TkeClient;
10-
const cvmClient = CvmClient;
1112

1213
export async function getTkeClient(): Promise<TkeClient> {
1314
const secretId = process.env.TENCENTCLOUD_SECRET_ID;
@@ -18,13 +19,12 @@ export async function getTkeClient(): Promise<TkeClient> {
1819
return null;
1920
}
2021

21-
return new tkeClient({
22+
return new TkeClient({
2223
credential: {
2324
secretId: process.env.TENCENTCLOUD_SECRET_ID,
2425
secretKey: process.env.TENCENTCLOUD_SECRET_KEY,
2526
},
26-
region: (process.env.TENCENTCLOUD_REGION === undefined) ?
27-
"ap-guangzhou" : process.env.TENCENTCLOUD_REGION,
27+
region: process.env.TENCENTCLOUD_REGION ?? "ap-guangzhou",
2828
profile: {
2929
signMethod: "TC3-HMAC-SHA256", // 签名方法
3030
httpProfile: {
@@ -57,8 +57,7 @@ export async function getCvmClient(region?: string): Promise<CvmClient> {
5757
secretKey: secretKey,
5858
},
5959
// 产品地域
60-
region: (process.env.TENCENTCLOUD_REGION === undefined) ?
61-
"ap-guangzhou" : process.env.TENCENTCLOUD_REGION,
60+
region: process.env.TENCENTCLOUD_REGION ?? "ap-guangzhou",
6261
// 可选配置实例
6362
profile: {
6463
// signMethod: "TC3-HMAC-SHA256", // 签名方法
@@ -68,10 +67,10 @@ export async function getCvmClient(region?: string): Promise<CvmClient> {
6867
endpoint: "cvm.tencentcloudapi.com",
6968
},
7069
},
71-
})
70+
});
7271
}
7372

74-
export async function getStsClient(region?: string): Promise<CvmClient> {
73+
export async function getCommonClient(region?: string): Promise<AbstractClient> {
7574
const [secretId, secretKey] = settingUtils.getAKSK();
7675

7776
if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null) {
@@ -80,22 +79,39 @@ export async function getStsClient(region?: string): Promise<CvmClient> {
8079
return null;
8180
}
8281

83-
return new CvmClient({
82+
const client = new AbstractClient(
83+
"open.test.tencentcloudapi.com",
84+
"2018-12-25",
85+
{
86+
credential: {
87+
secretId: secretId,
88+
secretKey: secretKey,
89+
},
90+
profile: {
91+
httpProfile: {
92+
proxy: "http://9.135.97.58:8899",
93+
},
94+
},
95+
}
96+
);
97+
98+
return client;
99+
}
100+
101+
export async function getStsClient(region?: string): Promise<any> {
102+
const [secretId, secretKey] = settingUtils.getAKSK();
103+
104+
if (secretId === undefined || secretKey === undefined || secretId === null || secretKey === null) {
105+
let msg = localize("TcTerraform.msg.aksk.notfound");
106+
vscode.window.showErrorMessage(msg);
107+
return null;
108+
}
109+
const StsClient = tencentcloud.sts.v20180813.Client;
110+
return new StsClient({
84111
credential: {
85112
secretId: secretId,
86113
secretKey: secretKey,
87114
},
88-
// 产品地域
89-
region: (process.env.TENCENTCLOUD_REGION === undefined) ?
90-
"ap-guangzhou" : process.env.TENCENTCLOUD_REGION,
91-
// 可选配置实例
92-
profile: {
93-
// signMethod: "TC3-HMAC-SHA256", // 签名方法
94-
httpProfile: {
95-
reqMethod: "POST", // 请求方法
96-
// reqTimeout: 60, // 请求超时时间,默认60s
97-
endpoint: "cvm.tencentcloudapi.com",
98-
},
99-
},
100-
})
115+
region: process.env.TENCENTCLOUD_REGION ?? "ap-guangzhou",
116+
});
101117
}

0 commit comments

Comments
 (0)