Skip to content
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

Refactored the credentials implementation #730

Merged
merged 11 commits into from Jan 6, 2020
Merged

Conversation

hiranya911
Copy link
Contributor

@hiranya911 hiranya911 commented Dec 16, 2019

Refactoring and simplifying the existing credential implementation for better design and async project ID discovery.

  • CertCredential class was renamed to ServiceAccountCredential.
  • Certificate type is renamed to ServiceAccount, and is no longer exported. Instead the projectId, clientEmail and privateKey properties are directly exposed from the ServiceAccountCredential class.
  • MetadataServiceCredential class was renamed to ComputeEngineCredential. This class now also has getProjectId(): Promise<string> method to discover the project ID from the Compute Engine environment.
  • ApplicationDefaultCredential class has been removed. Instead now there's a getApplicationDefault() function that returns a Credential implementation suitable for the environment.

);
}
if (!validator.isNonEmptyString(certificate.clientEmail) || !validator.isNonEmptyString(certificate.privateKey)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need this validation with the new changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These validations happen during the construction of ServiceAccountCredential object. So we can assume, they are all present.

Copy link
Member

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM!

}

constructor(json: object) {
if (typeof json !== 'object' || json === null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the validators utils here and below for type validation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

copyAttr(this, json, 'clientId', 'client_id');
copyAttr(this, json, 'clientSecret', 'client_secret');
copyAttr(this, json, 'refreshToken', 'refresh_token');
copyAttr(this, json, 'type', 'type');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering why the type property is needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's kind of redundant. It's always going to have the constant value "authorized_user" in this case. I think this type is meant to represent the refresh token JSON file, which usually contains a "type" field. I'd keep it as is, since removal of the property is not really related to this refactor. We can remove it in a future PR if needed.

return new ComputeEngineCredential(httpAgent);
}

function copyAttr(to: {[key: string]: any}, from: {[key: string]: any}, key: string, alt: string) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like it should be a utility function. Also add some javadoc for some of these function that may not be trivial to figure out. For example, it was not clear to me what alt stands for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some jsdoc comments. This was also an pre-existing function.

expect(() => getApplicationDefault()).to.throw(Error);
});

it('should throw if a the credentials file content is not an object', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@bojeil-google bojeil-google removed their assignment Jan 6, 2020
@hiranya911 hiranya911 merged commit e953b34 into master Jan 6, 2020
@hiranya911 hiranya911 deleted the hkj-cred-refactor branch January 6, 2020 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants