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

Allow specifying AWS role at runtime #81

Merged
merged 1 commit into from
Aug 9, 2021
Merged

Allow specifying AWS role at runtime #81

merged 1 commit into from
Aug 9, 2021

Conversation

rittneje
Copy link
Contributor

@rittneje rittneje commented Oct 21, 2020

Fixes #80.

Testing

We have been using this feature on our CloudBees instance for several months now. We have not experienced any issues with it. We have been fetching credentials in two ways:

  1. credentialsId, roleArn and roleSessionName provided
  2. credentialsId, accessKeyVariable, and secretKeyVariable provided

Both have been working flawlessly.

In addition, when implementing this change, I did integration testing on our CloudBees instance to cover the following cases:

  1. credentialsId, roleArn, roleSessionName, and roleSessionDurationSeconds
  2. credentialsId and roleArn
  3. credentialsId and roleArn, with roleSessionName as the empty string
  4. credentialsId, with roleArn as the empty string
  5. just credentialsId

All of these cases passed, as verified by calling sh 'aws sts get-caller-identity' within the withCredentials block.

@rittneje
Copy link
Contributor Author

ping @jglick @escoem

@jglick
Copy link
Member

jglick commented Oct 29, 2020

I am not a maintainer.

@rittneje
Copy link
Contributor Author

@jglick Any idea who the maintainers are?

@jglick
Copy link
Member

jglick commented Oct 30, 2020

@escoem perhaps.

@rittneje
Copy link
Contributor Author

ping @escoem

3 similar comments
@rittneje
Copy link
Contributor Author

ping @escoem

@rittneje
Copy link
Contributor Author

ping @escoem

@rittneje
Copy link
Contributor Author

ping @escoem

@rittneje
Copy link
Contributor Author

@escoem Is this project abandoned? This change is pretty important for us.

@rittneje
Copy link
Contributor Author

ping @andresrc

Copy link
Member

@alecharp alecharp left a comment

Choose a reason for hiding this comment

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

The big missing part of this PR is a test.
I don't know how much of code coverage we have here but changing code related with credentials without tests is a big risk.

The RoleSessionDurationSeconds limits problem I commented would have been detected with a test.

src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
src/main/java/com/cloudbees/AWSCredentialsUtils.java Outdated Show resolved Hide resolved
Comment on lines +144 to +149
.withStsClient(stsClient);

if (this.roleSessionDurationSeconds > 0) {
assumeRoleProviderBuilder = assumeRoleProviderBuilder
.withRoleSessionDurationSeconds(this.roleSessionDurationSeconds);
}
Copy link
Member

Choose a reason for hiding this comment

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

We might have a problem here.
The setter requires a value between 900 and 3600, or it throws an IllegalArgumentException. But we are not checking this.
However, the STSAssumeRoleSessionCredentialsProvider constructor will use DEFAULT_DURATION_SECONDS (900) if the value is equal to 0.

I would suggest to either have a doCheckX method in the descriptor to validate that the duration is truly between 900 and 3600. You can even have the default value to 900. This would not be used in pipeline setup.
Or you need to change the condition here.

(option 1)

Suggested change
.withStsClient(stsClient);
if (this.roleSessionDurationSeconds > 0) {
assumeRoleProviderBuilder = assumeRoleProviderBuilder
.withRoleSessionDurationSeconds(this.roleSessionDurationSeconds);
}
.withStsClient(stsClient)
.withRoleSessionDurationSeconds(this.roleSessionDurationSeconds);

(option 2)

Suggested change
.withStsClient(stsClient);
if (this.roleSessionDurationSeconds > 0) {
assumeRoleProviderBuilder = assumeRoleProviderBuilder
.withRoleSessionDurationSeconds(this.roleSessionDurationSeconds);
}
.withStsClient(stsClient);
if (this.roleSessionDurationSeconds >= 900 && this.roleSessionDurationSeconds <= 3600) {
assumeRoleProviderBuilder = assumeRoleProviderBuilder
.withRoleSessionDurationSeconds(this.roleSessionDurationSeconds);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My intent was to let the bounds just be controlled by the AWS SDK so that you don't have to keep the two in sync. (For example, a newer version of the SDK might change it so you can assume a role for up to 2 hours.) Similarly, I think it makes more sense to allow the default value to be chosen by the SDK, as it currently is.

Copy link
Member

Choose a reason for hiding this comment

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

I understand. The problem is that the "check" will be done by the library which throws an exception. I'm not sure this is the most clear way to handle the situation for users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a little confused. What exactly would we do in this code if the role session duration is out of bounds other than throw an exception ourselves?

@rittneje
Copy link
Contributor Author

rittneje commented Mar 3, 2021

The big missing part of this PR is a test.
I don't know how much of code coverage we have here but changing code related with credentials without tests is a big risk.

The RoleSessionDurationSeconds limits problem I commented would have been detected with a test.

@alecharp I would need some guidance on how to write such tests. There don't appear to be any existing tests other than ConfigurationAsCodeTest.java (which is irrelevant).

@jglick
Copy link
Member

jglick commented Mar 3, 2021

Neither this plugin, nor aws-global-configuration, nor aws-java-sdk, have any tests which could actually exercise services.

artifact-manager-s3 has some Dockerized tests which run against MinIO; and some “live” tests which require AWS credentials and currently run only on a private server maintained by CloudBees, or on your local machine if you care to run them. In neither case would IAM/STS exotica like role assumption or MFAs be covered.

pipeline-cloudwatch-logs also has some live tests (currently not run in CI anywhere) and actually does do an AssumeRole call, but for specialized purposes and of course not covering the code in this PR.

Writing tests against live AWS is certainly possible, with some effort; making them exercise specific IAM/STS features is particularly hard, since the person/process running the test may not physically be permitted to use that feature, depending on how their account is set up. You can try to run Dockerized tests against OpenStack or various mock frameworks, but identity & security systems are likely to be the least accurately simulated, and anyway this is hardly proving that the code would actually work in AWS itself (though it could help detect regressions from routine refactorings not touching the basic design).

@rittneje
Copy link
Contributor Author

rittneje commented Mar 8, 2021

ping @alecharp

4 similar comments
@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

rittneje commented Apr 9, 2021

ping @alecharp

@rittneje rittneje requested a review from alecharp April 9, 2021 15:30
…ration at runtime. Fixes #80.

Signed-off-by: Jesse Rittner <[email protected]>
@rittneje
Copy link
Contributor Author

ping @alecharp

7 similar comments
@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

rittneje commented May 4, 2021

ping @alecharp

@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

ping @alecharp

@rittneje
Copy link
Contributor Author

rittneje commented Jun 2, 2021

ping @alecharp

@rittneje
Copy link
Contributor Author

rittneje commented Jun 7, 2021

ping @alecharp

@rittneje
Copy link
Contributor Author

@alecharp This pull request has been open for almost 8 months. It has been over 3 months since you reviewed. Please merge this already. If you don't have the time to review it, then please tell me who can.

cc @escoem @andresrc

@rittneje
Copy link
Contributor Author

@alecharp @escoem @andresrc

@rittneje
Copy link
Contributor Author

@alecharp @escoem @andresrc

2 similar comments
@rittneje
Copy link
Contributor Author

rittneje commented Jul 6, 2021

@alecharp @escoem @andresrc

@rittneje
Copy link
Contributor Author

@alecharp @escoem @andresrc

Copy link
Member

@alecharp alecharp left a comment

Choose a reason for hiding this comment

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

LGTM. Seems we just have to wait a bit for @escoem to be available to merge and release this.

@escoem escoem merged commit c748e81 into jenkinsci:master Aug 9, 2021
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.

Allow specifying role at runtime
4 participants