Skip to content

Commit 1fdbb57

Browse files
author
dfounderliu
committed
fix login
1 parent 3840290 commit 1fdbb57

File tree

2 files changed

+78
-117
lines changed

2 files changed

+78
-117
lines changed

deploy/lib/deployFunction.js

Lines changed: 46 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -38,134 +38,76 @@ class DeployFunction extends AbstractHandler {
3838
async addRole() {
3939
try {
4040
const roleName = 'SCF_QcsRole'
41-
const policyNameList = [
42-
'QcloudCOSFullAccess',
43-
'QcloudCOSBucketConfigWrite',
44-
'QcloudCOSBucketConfigRead',
45-
'QcloudCOSDataReadOnly',
46-
'QcloudAPIGWFullAccess'
47-
]
41+
const policyName = 'QcloudAccessForScfRole'
4842
const listPoliciesModels = new camModels.ListPoliciesRequest()
4943
const listPoliciesHandler = util.promisify(this.camClient.ListPolicies.bind(this.camClient))
50-
const policyIdList = new Array()
44+
let havePolicy = false
45+
let policyId
5146
let pagePolicyCount = 200
52-
let body = { Rp: 200, Page: 0 }
53-
while (policyIdList.length < 5 || pagePolicyCount == 200) {
47+
const body = { Rp: 200, Page: 0 }
48+
while (!havePolicy && pagePolicyCount == 200) {
5449
body.Page = body.Page + 1
5550
listPoliciesModels.from_json_string(JSON.stringify(body))
5651
try {
5752
const pagePolicList = await listPoliciesHandler(listPoliciesModels)
5853
for (let i = 0; i < pagePolicList.List.length; i++) {
59-
if (policyNameList.indexOf(pagePolicList.List[i].PolicyName) > -1) {
60-
policyIdList.push(pagePolicList.List[i].PolicyId)
54+
if (policyName == pagePolicList.List[i].PolicyName) {
55+
havePolicy = true
56+
policyId = pagePolicList.List[i].PolicyId
57+
break
6158
}
6259
}
6360
pagePolicyCount = pagePolicList.List.length
64-
} catch (e) {}
61+
} catch (e) {
62+
pagePolicyCount = 0
63+
}
6564
await utils.sleep(400)
6665
}
6766

68-
let roleState = 1
69-
70-
// Get role
67+
// Create role and attach policy
7168
try {
72-
const getRoleModels = new camModels.GetRoleRequest()
73-
getRoleModels.from_json_string(JSON.stringify({ RoleName: roleName }))
74-
const getRoleHandler = util.promisify(this.camClient.GetRole.bind(this.camClient))
75-
await getRoleHandler(getRoleModels)
69+
const createRoleModels = new camModels.CreateRoleRequest()
70+
createRoleModels.from_json_string(
71+
JSON.stringify({
72+
RoleName: roleName,
73+
PolicyDocument: JSON.stringify({
74+
version: '2.0',
75+
statement: [
76+
{
77+
effect: 'allow',
78+
principal: {
79+
service: 'scf.qcloud.com'
80+
},
81+
action: 'sts:AssumeRole'
82+
}
83+
]
84+
})
85+
})
86+
)
87+
const createRoleHandler = util.promisify(this.camClient.CreateRole.bind(this.camClient))
88+
await createRoleHandler(createRoleModels)
7689
} catch (e) {
77-
if (e.message.includes('role not exist')) {
78-
roleState = -1
90+
if (e && e.message.match('role name in use')) {
7991
} else {
80-
roleState = 0
92+
this.serverless.cli.log('Create role error : ' + e)
8193
}
8294
}
83-
84-
const haveIdList = new Array()
85-
const addIdList = new Array()
86-
87-
// Get role policy list
8895
try {
89-
pagePolicyCount = 200
90-
body = { Rp: 200, Page: 0, RoleName: roleName }
91-
const listRolePoliciesModels = new camModels.ListAttachedRolePoliciesRequest()
92-
const listRolePoliciesHandler = util.promisify(
93-
this.camClient.ListAttachedRolePolicies.bind(this.camClient)
96+
const attachRolePolicyModels = new camModels.AttachRolePolicyRequest()
97+
const attachRolePolicyHandler = util.promisify(
98+
this.camClient.AttachRolePolicy.bind(this.camClient)
9499
)
95-
while (pagePolicyCount == 200) {
96-
body.Page = body.Page + 1
97-
listRolePoliciesModels.from_json_string(JSON.stringify(body))
98-
try {
99-
const pagePolicList = await listRolePoliciesHandler(listRolePoliciesModels)
100-
for (let i = 0; i < pagePolicList.List.length; i++) {
101-
haveIdList.push(pagePolicList.List[i].PolicyId)
102-
}
103-
pagePolicyCount = pagePolicList.List.length
104-
} catch (e) {
105-
pagePolicyCount = 0
106-
}
107-
await utils.sleep(400)
108-
}
109-
} catch (e) {}
110-
111-
// Get policy id which need to add in SCF_QcsRole
112-
for (let i = 0; i < policyIdList.length; i++) {
113-
if (haveIdList.indexOf(policyIdList[i]) <= -1) {
114-
addIdList.push(policyIdList[i])
115-
}
116-
}
117-
118-
// Create role and attach policy
119-
if (roleState <= 0) {
120-
try {
121-
const createRoleModels = new camModels.CreateRoleRequest()
122-
createRoleModels.from_json_string(
123-
JSON.stringify({
124-
RoleName: roleName,
125-
PolicyDocument: JSON.stringify({
126-
version: '2.0',
127-
statement: [
128-
{
129-
effect: 'allow',
130-
principal: {
131-
service: 'scf.qcloud.com'
132-
},
133-
action: 'sts:AssumeRole'
134-
}
135-
]
136-
})
137-
})
138-
)
139-
const createRoleHandler = util.promisify(this.camClient.CreateRole.bind(this.camClient))
140-
await createRoleHandler(createRoleModels)
141-
} catch (e) {
142-
this.serverless.cli.log('Create role error: ' + e)
100+
const attachRolePolicyBody = {
101+
AttachRoleName: roleName
143102
}
144-
}
145-
if (addIdList.length > 0) {
146103
try {
147-
const attachRolePolicyModels = new camModels.AttachRolePolicyRequest()
148-
const attachRolePolicyHandler = util.promisify(
149-
this.camClient.AttachRolePolicy.bind(this.camClient)
150-
)
151-
const attachRolePolicyBody = {
152-
AttachRoleName: roleName
153-
}
154-
for (let i = 0; i < addIdList.length; i++) {
155-
try {
156-
attachRolePolicyBody.PolicyId = addIdList[i]
157-
attachRolePolicyModels.from_json_string(JSON.stringify(attachRolePolicyBody))
158-
await attachRolePolicyHandler(attachRolePolicyModels)
159-
} catch (e) {
160-
this.context.debug(`Attach policy id '${attachRolePolicyBody.PolicyId}' error: ${e}`)
161-
}
162-
await utils.sleep(400)
163-
}
104+
attachRolePolicyBody.PolicyId = policyId
105+
attachRolePolicyModels.from_json_string(JSON.stringify(attachRolePolicyBody))
106+
await attachRolePolicyHandler(attachRolePolicyModels)
164107
} catch (e) {}
165-
}
166-
} catch (e) {
167-
this.serverless.cli.log('Check policy list error: ' + e)
168-
}
108+
await utils.sleep(400)
109+
} catch (e) {}
110+
} catch (e) {}
169111
}
170112

171113
async updateFunctionCode(ns, funcObject) {

provider/tencentProvider.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,21 @@ class TencentProvider {
126126
const tencent_credentials = await login.login()
127127
if (tencent_credentials) {
128128
tencent_credentials.timestamp = Date.now() / 1000
129-
const tencent_credentials_json = JSON.stringify(tencent_credentials)
130129
try {
131130
const tencent = {
132-
tencent_secret_id: tencent_credentials.tencent_secret_id,
133-
tencent_secret_key: tencent_credentials.tencent_secret_key,
134-
tencent_appid: tencent_credentials.tencent_appid,
135-
token: tencent_credentials.tencent_token,
131+
tencent_secret_id: tencent_credentials.secret_id,
132+
tencent_secret_key: tencent_credentials.secret_key,
133+
tencent_appid: tencent_credentials.appid,
134+
token: tencent_credentials.token,
135+
expired: tencent_credentials.expired,
136+
signature: tencent_credentials.signature,
137+
uuid: tencent_credentials.uuid,
136138
timestamp: tencent_credentials.timestamp
137139
}
138-
await fs.writeFileSync('./.serverless/.env', tencent_credentials_json)
140+
await fs.writeFileSync('./.env_temp', JSON.stringify(tencent))
141+
this.context.debug(
142+
'The temporary key is saved successfully, and the validity period is two hours.'
143+
)
139144
return tencent
140145
} catch (e) {
141146
throw 'Error getting temporary key: ' + e
@@ -146,16 +151,30 @@ class TencentProvider {
146151
async getTempKey() {
147152
const that = this
148153
try {
149-
const data = await fs.readFileSync('./.serverless/.env', 'utf8')
154+
const data = await fs.readFileSync('./.env_temp', 'utf8')
150155
try {
151156
const tencent = {}
152157
const tencent_credentials_read = JSON.parse(data)
153-
if (Date.now() / 1000 - tencent_credentials_read.timestamp <= 7000) {
154-
tencent.tencent_secret_id = tencent_credentials_read.tencent_secret_id
155-
tencent.tencent_secret_key = tencent_credentials_read.tencent_secret_key
156-
tencent.tencent_appid = tencent_credentials_read.tencent_appid
157-
tencent.token = tencent_credentials_read.tencent_token
158-
tencent.timestamp = tencent_credentials_read.timestamp
158+
if (Date.now() / 1000 - tencent_credentials_read.timestamp <= 6000) {
159+
return tencent_credentials_read
160+
}
161+
const login = new TencentLogin()
162+
const tencent_credentials_flush = await login.flush(
163+
tencent_credentials_read.uuid,
164+
tencent_credentials_read.expired,
165+
tencent_credentials_read.signature,
166+
tencent_credentials_read.tencent_appid
167+
)
168+
if (tencent_credentials_flush) {
169+
tencent.tencent_secret_id = tencent_credentials_flush.secret_id
170+
tencent.tencent_secret_key = tencent_credentials_flush.secret_key
171+
tencent.tencent_appid = tencent_credentials_flush.appid
172+
tencent.token = tencent_credentials_flush.token
173+
tencent.expired = tencent_credentials_flush.expired
174+
tencent.signature = tencent_credentials_flush.signature
175+
tencent.uuid = tencent_credentials_read.uuid
176+
tencent.timestamp = Date.now() / 1000
177+
await fs.writeFileSync('./.env_temp', JSON.stringify(tencent))
159178
return tencent
160179
}
161180
return await that.doLogin()

0 commit comments

Comments
 (0)