-
Notifications
You must be signed in to change notification settings - Fork 25
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: changes to node-sdk-core to work with service factory feature #72
Conversation
Codecov Report
@@ Coverage Diff @@
## master #72 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 8 8
Branches 1 1
=====================================
Hits 8 8 Continue to review full report at Codecov.
|
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 good... just left a few comments about some nitpicky things :)
I'll approve assuming those are addressed.
I suggested alternate comments for testcases and a new name for a function or two. My specific naming suggestions aren't that important, but the general point is to maybe just use a more descriptive name, etc.
auth/utils/read-external-sources.ts
Outdated
@@ -97,8 +96,31 @@ function filterPropertiesByServiceName(envObj: any, serviceName: string): any { | |||
* Pulls credentials from VCAP_SERVICES env property that IBM Cloud sets | |||
* | |||
*/ | |||
function getCredentials(name) { |
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.
After copying this code from its original package into this file, I think it would be good to give this a more descriptive name.
In fact, I'll suggest that you rename getCredentialsFromCloud
to be getPropertiesFromVCAP
and then rename getCredentials
to be something like getVCAPCredentialsForService
, or just move the getCredentials
logic into the newly-renamed getPropertiesFromVCAP
.
test/unit/getCredentials.test.js
Outdated
expect(credentials.password).toEqual("not-a-password") | ||
}); | ||
|
||
it('should return first service in the list when multiple services found', () => { |
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.
it('should return first service in the list when multiple services found', () => { | |
it('should return first service in the list when matching on primary service key', () => { |
test/unit/getCredentials.test.js
Outdated
expect(Object.keys(credentials).length).toEqual(0); | ||
}); | ||
|
||
it('should return empty credential object when looking for credentials by service name', () => { |
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.
it('should return empty credential object when looking for credentials by service name', () => { | |
it('should return credential object matching service name field not first list element', () => { |
test/unit/getCredentials.test.js
Outdated
expect(credentials.password).toEqual("not-a-password") | ||
}); | ||
|
||
it('should return empty credential object when looking for credentials by service name', () => { |
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.
it('should return empty credential object when looking for credentials by service name', () => { | |
it('should return credential object when matching service name field on first list element', () => { |
test/unit/getCredentials.test.js
Outdated
expect(credentials.password).toEqual("not-a-password") | ||
}); | ||
|
||
it('should return empty credential object when service key is no-creds-service-two', () => { |
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.
it('should return empty credential object when service key is no-creds-service-two', () => { | |
it('should return empty credential object when matching on service name with no credentials field', () => { |
3fab5cd
to
cdf8729
Compare
cdf8729
to
c068ba7
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.
This looks good! I made a few suggestions, which don't necessarily need to be adhered to. I'm mostly requesting changes to get clarity on if this is a breaking change or not before we merge
test/resources/vcap.json
Outdated
"plan": "elite" | ||
} | ||
] | ||
} |
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.
nit: will you add a newline here?
} | |
} | |
const credentials = vcapServices.getCredentials(serviceName); | ||
function getVCAPCredentialsForService(name) { | ||
if (process.env.VCAP_SERVICES) { | ||
const services = JSON.parse(process.env.VCAP_SERVICES); |
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 think it would be safer to wrap the JSON parse in a try/catch
since it throws an error if anything about the JSON is malformatted. Not that it should be, since that env variable is generated by the cloud. So this is an optional change, up to you.
const services = JSON.parse(process.env.VCAP_SERVICES); | |
let services; | |
try { | |
services = JSON.parse(process.env.VCAP_SERVICES); | |
} catch () { | |
return {}; | |
} |
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.
Wrapping the JSON parse in a try/catch
might eat up/silence the error, and the user wouldn't know their VCAP_SERVICES
env is not parsed. So I think it's fine to let it fail if for some reason JSON is malformatted.
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.
Okay, that's a fair point. I just remembered that the core has it's own logger now. So another thing to consider would be printing an error log for a malformatted VCAP variable so the user knows about it. My only concern here is the code crashing even though the user typically cannot control the contents of VCAP_SERVICES
.
if (services[serviceName].length > 0) { | ||
return services[serviceName][0].credentials || {}; | ||
} else { | ||
return {} |
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.
Speaking of the logger, I think it's a good idea to add debug or verbose logging statements before an empty object is returned saying something like "no data read from vcap services"
55a1100
to
6caf329
Compare
} | ||
} | ||
} | ||
return {}; |
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.
One more place to add a debug
auth/utils/read-external-sources.ts
Outdated
* | ||
* If no match against the service entry's "name" field is found, then find the | ||
* service list with a key matching the serviceKey value. If found, return its | ||
* credentials. |
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.
Should mention that you are returning the first credentials object in the service list.
6caf329
to
d4e318d
Compare
d4e318d
to
17503f9
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 good! 👍
Make sure you format the commit message properly to ensure a good message in the CHANGELOG
# [2.0.0](v1.3.0...v2.0.0) (2019-11-19) ### Features * changes to node-sdk-core to work with service factory feature ([#72](#72)) ([cde4cd6](cde4cd6)) ### BREAKING CHANGES * The `BaseService` will no longer look for configurations externally by default. A new factory method is provided to create an instance from external configuration. * feat: changes to node-sdk-core to work with service factory feature * `BaseService` constructor will no longer call `configureService`. * updated test to reflect base service constructor does not call configureService * added test for getting credentials from vcap * removed `name` and `serviceVersion` because they are not referenced anymore * added comment for vcap parsing function. removed vcap_services dependency * added debug messages for when returning empty credential
🎉 This PR is included in version 2.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Checklist
npm test
passes (tip:npm run lint-fix
can correct most style issues)