-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add account-related methods #73
Conversation
Codecov Report
@@ Coverage Diff @@
## master #73 +/- ##
==========================================
+ Coverage 89.17% 90.14% +0.96%
==========================================
Files 8 9 +1
Lines 194 213 +19
==========================================
+ Hits 173 192 +19
Misses 21 21
Continue to review full report at Codecov.
|
const jwtDecode = require('jwt-decode'); | ||
const _ = require('lodash'); | ||
|
||
const logout = () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding method from platform-sdk
: https://github.com/serverless/platform-sdk/blob/master/src/logout/index.js#L10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made a decision to drop manipulation of enterprise
section as it seems to be "legacy" part of the configuration - please correct me if I'm wrong here.
// TODO: ADD EXPLANATION TO NOT INTERACT WITH `enterprise` config scope | ||
}; | ||
|
||
const refreshToken = async (sdk) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding method in platform-sdk
: https://github.com/serverless/platform-sdk/blob/master/src/login/refreshToken.js#L6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've decided to skip saving expiresAt
field as it seems we're not using it anywhere and the expiration date is always derived from idToken
anyway. Please let me know if you feel like we're still supposed to save it
return; | ||
} | ||
|
||
const tokens = await sdk.session.refreshToken(loggedInUser.refreshToken); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding method from platform-client
: https://github.com/serverlessinc/platform/blob/master/sdk/src/index.js#L79
account.js
Outdated
|
||
if (loggedInUser.idToken) { | ||
const accessKeyTitle = title || `serverless_${Math.round(+new Date() / 1000)}`; | ||
const createdAccessKeyResponse = await sdk.accessKeys.create( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding method from platform-client
: https://github.com/serverlessinc/platform/blob/master/sdk/src/index.js#L461
account.js
Outdated
configUtils.set(data); | ||
}; | ||
|
||
const getAccessKeyForOrg = async (sdk, orgName, title) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corresponding method in platform-sdk
: https://github.com/serverless/platform-sdk/blob/master/src/accessKeys/accessKeys.js#L37
9f226d3
to
a7f12b2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great @pgrzesik 👍 I have just few minor concerns
account.js
Outdated
}; | ||
|
||
const getAccessKeyForOrg = async (sdk, orgName, title) => { | ||
if (process.env.SERVERLESS_ACCESS_KEY) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should be handled here, and not where getAccessKeyForOrg
is called.
I'd rather see this function to unconditionally retrieve access key for org, and to be invoked when such access key is needed (not injected via env var).
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good question - the potential problem here is that all clients of that method would have to account for that fact and implement a potential wrapper that will first check if there's a value injected via SERVERLESS_ACCESS_KEY
env var. I think it should be centralized in one place, but I'm no longer sure if utils
in the best place, I'll explain in another comment.
account.js
Outdated
const loggedInUser = configUtils.getLoggedInUser(); | ||
|
||
if (!loggedInUser) { | ||
throw new Error('Could not find logged in user. Please log in.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I take this signals programmer error (not something we envision with correct utils usage). In such case, it'll be better to shorten message to simply: Could not find logged in user
("Please log in." suggests it's intended to be presented in valid scenarios to users, and such error should not be thrown with regular Error
constructor)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great point - how would you go about it then? I've adapted to the approach that we currently have in platform-sdk
, where such errors are thrown with instructions for users to logout/login
(reference: https://github.com/serverless/platform-sdk/blob/master/src/accessKeys/accessKeys.js#L66) and aren't being handled in any specific way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If those errors handle valid scenarios, and are intended to host messages to be shown to users. Then they should be thrown with ServerlessError
(in such case we should move serverless-error
utility to this repository).
If those errors are to point errors in our internal flow (and by no means can be triggered by valid user flow), then they should not contain any user facing messaging, but simply state what's wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So my recommendation depends on what kind of error it is (user or programmer?). It's not perfectly clear to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you and sorry for not being clear. These errors can be triggered by valid flow as far as I'm concerned and they should be visible to users. However, at this point I think it makes more sense to move this function to enterprise-plugin
as it's going to be the only client. I'll remove it from this PR, sorry for the confusion
account.js
Outdated
|
||
if (loggedInUser.idToken) { | ||
const accessKeyTitle = title || `serverless_${Math.round(+new Date() / 1000)}`; | ||
const createdAccessKeyResponse = await sdk.accessKeys.create( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*Reponse
suggests it's a HTTP Response object, and in such case response body will be placed on response.body
.
If it's already a parsed response body. I would simply name var accessKeyData
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call 👍
account.js
Outdated
throw new Error( | ||
`Could not find an access key for organization ${orgName}. Log out and log in again to create a new access key for this organization.` | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, it'll be good clarify if it's a programmer or user error, and configure it as one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see my other comment about approach to errors
Thanks @medikoo for a review 🙇 I've started thinking again about proper placement for EDIT: I've also added reconfiguration of |
a7f12b2
to
6ed9287
Compare
if it's just for Otherwise this utils project is more a common (not really utils) project. It's to host all functionalities that are used by more than one of our CLI packages. Relying on env vars here is fine. I was just questioning util design, but I don't know all the places where it's used. |
6ed9287
to
3bddaff
Compare
Thanks for a review, as mentioned, I've decided to remove |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great 👍
Adds account-related methods as described in linked issue.
I've made a decision to depend on injected
sdk
instance, which is supposed to be an instance ofServerlessSDK
from@serverless/platform-client
library. It is aimed to be temporary solution, which will be cleaned up whencomponents
,enterprise-plugin
andserverless
will be moved into one repository.Closes: #72