-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
734 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const { ServerlessSDK } = require('@serverless/platform-client'); | ||
const accountUtils = require('@serverless/utils/account'); | ||
const configUtils = require('@serverless/utils/config'); | ||
|
||
const createAccessKeyForOrg = async (orgName) => { | ||
const sdk = new ServerlessSDK(); | ||
|
||
await accountUtils.refreshToken(sdk); | ||
|
||
const user = configUtils.getLoggedInUser(); | ||
|
||
if (!user || !user.idToken) { | ||
return null; | ||
} | ||
|
||
sdk.config({ accessKey: user.idToken }); | ||
|
||
const accessKeyTitle = `serverless_${Math.round(+new Date() / 1000)}`; | ||
const result = await sdk.accessKeys.create(orgName, user.username, accessKeyTitle); | ||
|
||
configUtils.set({ | ||
users: { | ||
[user.userId]: { | ||
dashboard: { accessKeys: { [orgName]: result.secretAccessKey } }, | ||
}, | ||
}, | ||
}); | ||
|
||
return result.secretAccessKey; | ||
}; | ||
|
||
const getOrCreateAccessKeyForOrg = async (orgName) => { | ||
if (process.env.SERVERLESS_ACCESS_KEY) { | ||
return process.env.SERVERLESS_ACCESS_KEY; | ||
} | ||
|
||
const user = configUtils.getLoggedInUser(); | ||
|
||
if (!user) { | ||
throw new Error('Could not find logged in user. Please log in.'); | ||
} | ||
|
||
// Try to get existing access key for specifier orgName | ||
const accessKeyFromConfig = user.accessKeys && user.accessKeys[orgName]; | ||
|
||
if (accessKeyFromConfig) { | ||
return accessKeyFromConfig; | ||
} | ||
|
||
// Try to create a new access key if not found existing one | ||
const createdAccessKey = await createAccessKeyForOrg(orgName); | ||
|
||
if (!createdAccessKey) { | ||
throw new Error('Could not create a new access key. Please log out and log in and try again.'); | ||
} | ||
|
||
return createdAccessKey; | ||
}; | ||
|
||
const getPlatformClientWithAccessKey = async (orgName) => { | ||
const accessKey = await getOrCreateAccessKeyForOrg(orgName); | ||
|
||
const sdk = new ServerlessSDK({ accessKey }); | ||
|
||
return sdk; | ||
}; | ||
|
||
module.exports = { | ||
getPlatformClientWithAccessKey, | ||
getOrCreateAccessKeyForOrg, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
|
||
const chai = require('chai'); | ||
const proxyquire = require('proxyquire'); | ||
const overrideEnv = require('process-utils/override-env'); | ||
const sinon = require('sinon'); | ||
|
||
chai.use(require('chai-as-promised')); | ||
|
||
const expect = chai.expect; | ||
|
||
const platformClientStub = { | ||
ServerlessSDK: class ServerlessSDK { | ||
constructor() { | ||
this.accessKeys = { | ||
create: async () => ({ secretAccessKey: 'createdAccessKey' }), | ||
}; | ||
} | ||
|
||
config() {} | ||
}, | ||
}; | ||
|
||
describe('lib/clientUtils.test.js', () => { | ||
const orgName = 'testorg'; | ||
|
||
describe('getOrCreateAccessKeyForOrg', () => { | ||
it('returns SERVERLESS_ACCESS_KEY from env if set', async () => { | ||
const { getOrCreateAccessKeyForOrg } = proxyquire('./clientUtils', { | ||
'@serverless/utils/config': { | ||
getLoggedInUser: () => null, | ||
}, | ||
}); | ||
let result; | ||
const accessKeyFromEnv = 'accesskeyfromenv'; | ||
|
||
await overrideEnv({ variables: { SERVERLESS_ACCESS_KEY: accessKeyFromEnv } }, async () => { | ||
result = await getOrCreateAccessKeyForOrg(orgName); | ||
}); | ||
|
||
expect(result).to.equal(accessKeyFromEnv); | ||
}); | ||
|
||
it('throws an error if user does not exist', async () => { | ||
const { getOrCreateAccessKeyForOrg } = proxyquire('./clientUtils', { | ||
'@serverless/utils/config': { | ||
getLoggedInUser: () => null, | ||
}, | ||
}); | ||
|
||
await expect(getOrCreateAccessKeyForOrg(orgName)).to.be.rejectedWith( | ||
'Could not find logged in user. Please log in.' | ||
); | ||
}); | ||
|
||
it('returns an existing access key from config', async () => { | ||
const accessKeyFromConfig = 'existingaccesskey'; | ||
|
||
const { getOrCreateAccessKeyForOrg } = proxyquire('./clientUtils', { | ||
'@serverless/utils/config': { | ||
getLoggedInUser: () => ({ | ||
userId: 'someuserid', | ||
accessKeys: { | ||
[orgName]: accessKeyFromConfig, | ||
}, | ||
}), | ||
}, | ||
}); | ||
|
||
const result = await getOrCreateAccessKeyForOrg(orgName); | ||
expect(result).to.equal(accessKeyFromConfig); | ||
}); | ||
|
||
it('throws if it could not create an access key when user does not have idToken', async () => { | ||
const { getOrCreateAccessKeyForOrg } = proxyquire('./clientUtils', { | ||
'@serverless/utils/config': { | ||
getLoggedInUser: () => ({ | ||
userId: 'someuserid', | ||
}), | ||
}, | ||
}); | ||
|
||
await expect(getOrCreateAccessKeyForOrg(orgName)).to.be.rejectedWith( | ||
'Could not create a new access key. Please log out and log in and try again.' | ||
); | ||
}); | ||
|
||
it('succesfully returns and persists created access key', async () => { | ||
const configUtilsSetStub = sinon.stub(); | ||
const { getOrCreateAccessKeyForOrg } = proxyquire('./clientUtils', { | ||
'@serverless/utils/config': { | ||
getLoggedInUser: () => ({ userId: 'userid', idToken: 'sometoken', username: 'user' }), | ||
set: configUtilsSetStub, | ||
}, | ||
'@serverless/platform-client': platformClientStub, | ||
}); | ||
|
||
const result = await getOrCreateAccessKeyForOrg(orgName); | ||
expect(result).to.equal('createdAccessKey'); | ||
expect( | ||
configUtilsSetStub.calledWith({ | ||
users: { | ||
userid: { | ||
dashboard: { accessKeys: { [orgName]: result } }, | ||
}, | ||
}, | ||
}) | ||
).to.be.true; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.