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

feat: add support for new ContainerAuthenticator #151

Merged
merged 11 commits into from
Aug 10, 2021

Conversation

dpopp07
Copy link
Member

@dpopp07 dpopp07 commented Aug 5, 2021

This PR adds an authenticator and token manager for the new "container" flavor of authentication. This style retrieves a compute resource token from the file system and uses that to retrieve an IAM access token, which is used to authenticate with the service.

Note: I realized that I refactored the IAM token manager but not the authenticator. It really should be both, so as part of this PR I refactored the authenticator to match the refactoring in the token manager.

* limitations under the License.
*/

import { ContainerTokenManager } from '../token-managers';
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that most of the "meat" of this change is in the token manager, not the authenticator

@@ -24,6 +24,6 @@
*/

export * from './helpers';
export * from './read-credentials-file';
export * from './file-reading-helpers';
Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the name of this file to be more accurate. Even though this file is importable by users, it is not part of our "official" API so I don't consider it a breaking change. We wouldn't expect any users to be using this file, it only contains internal utilities.

@@ -67,7 +67,6 @@ describe('IAM Authenticator', () => {
// verify the scope param wasn't set
expect(authenticator.scope).toBeUndefined();
expect(authenticator.tokenManager.scope).toBeUndefined();
done();
});
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't make anything more than superficial changes to these tests to ensure the refactoring maintains compatibility

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

LGTM
Initially, I was a little surprised to not find much testing of the requestToken() method in terms of the various scenarios with token expiration, etc. but after poking around it looks like the jwt-token-manager.test.js script contains tests for those types of scenarios in sort of a generic sense (i.e. not specific to cp4d, iam or container authenticators). Is that right?

auth/authenticators/iam-request-based-authenticator.ts Outdated Show resolved Hide resolved

/**
* Setter for the filename of the compute resource token.
* @param {string} scope A string containing a path to the CR token file
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @param {string} scope A string containing a path to the CR token file
* @param {string} crTokenFilename A string containing a path to the CR token file

copy/paste faux pas??? :)
other instances of this below as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch 🙂 I'll fix

Copy link
Member

@pyrooka pyrooka left a comment

Choose a reason for hiding this comment

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

2 small questions, but it looks good!

throw new Error(`File does not exist: ${filepath}`);
}

if (token === '') {
Copy link
Member

Choose a reason for hiding this comment

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

In the other SDKs we don't check for empty file. I think we should be consistent across the languages, so I will happily add this to the Python core if you wish.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair - I saw @padamstx comment on your PR about having one error to capture the "can't read token" scenario. Perhaps I will update this code to only throw one generic error, but leave in the more specific log messages to help the user in the case of an issue.

What do you think about that?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me!

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, update made 👍

Comment on lines 84 to 85
logger.error(`Expected to find CR token file but the file does not exist: ${filepath}`);
throw new Error(`File does not exist: ${filepath}`);
Copy link
Member

@pyrooka pyrooka Aug 9, 2021

Choose a reason for hiding this comment

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

Does the fileExistsAtPath catch permission issues? If not, these messages could be misleading. What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, permission issues will throw an error within the actual readFileSync method and the error shown will say "permission denied". So there shouldn't be an issue with confusing error messages here

@dpopp07
Copy link
Member Author

dpopp07 commented Aug 9, 2021

@padamstx

I was a little surprised to not find much testing of the requestToken() method in terms of the various scenarios with token expiration, etc. but after poking around it looks like the jwt-token-manager.test.js script contains tests for those types of scenarios in sort of a generic sense (i.e. not specific to cp4d, iam or container authenticators). Is that right?

That's right - everything but the actual request for the token is abstracted away into the shared parent, the JWT token manager. So, that behavior is all captured in the JWT token manager tests.

@dpopp07 dpopp07 merged commit b01c011 into main Aug 10, 2021
@dpopp07 dpopp07 deleted the dp/add-container-authenticator branch August 10, 2021 17:04
ibm-devx-sdk pushed a commit that referenced this pull request Aug 10, 2021
# [2.12.0](v2.11.3...v2.12.0) (2021-08-10)

### Features

* add support for new ContainerAuthenticator ([#151](#151)) ([b01c011](b01c011))
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 2.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants