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

[s3-repository] Support IAM roles for Kubernetes service accounts #81255

Merged
merged 35 commits into from
Jan 19, 2022

Conversation

arteam
Copy link
Contributor

@arteam arteam commented Dec 2, 2021

There have been many requests to support repository-s3 authentication via IAM roles in Kubernetes service accounts.

The AWS SDK is supposed to support them out of the box with the aws-java-sdk-sts library. Unfortunately, we can't use WebIdentityTokenCredentialsProvider from the SDK. It reads the token from AWS_WEB_IDENTITY_TOKEN_FILE environment variable which is usually mounted to /var/run/secrets/eks.amazonaws.com/serviceaccount/token and the S3 repository doesn't have the permissions to read it. We don't want to hard-code a file permission for the S3 repository, because the location of AWS_WEB_IDENTITY_TOKEN_FILE can change at any time in the future and we would also generally prefer to restrict the ability of plugins to access things outside of their config directory.

To overcome this limitation, this change adds a custom WebIdentityCredentials provider that reads the service account from a symlink to AWS_WEB_IDENTITY_TOKEN_FILE created in the repository's config directory. We expect the end user to create the symlink to indicate that they want to use service accounts for authentication like this:

mkdir -p "${ES_PATH_CONF}/repository-s3"
ln -s $AWS_WEB_IDENTITY_TOKEN_FILE "${ES_PATH_CONF}/repository-s3/aws-web-identity-token-file"

Service accounts are checked and exchanged for session tokens by the AWS STS. To test the authentification flow, this change adds a test fixture which mocks the assume-role-with-web-identity call to the service and returns a response with test credentials.

Fixes #52625

@arteam arteam force-pushed the amazon-web-identity-token branch 20 times, most recently from 4c753f2 to 199892a Compare December 3, 2021 14:05
@arteam arteam changed the title Support Web Identity Tokens for authentication to S3 Support AWS IAM Kubernetes service accounts Dec 3, 2021
@arteam arteam force-pushed the amazon-web-identity-token branch 4 times, most recently from 3923d93 to db09282 Compare December 6, 2021 17:04
@arteam arteam added :Distributed/Discovery-Plugins Anything related to our integration plugins with EC2, GCP and Azure :Core/Infra/Plugins Plugin API and infrastructure :Distributed/Snapshot/Restore Anything directly related to the `_snapshot/*` APIs and removed :Distributed/Discovery-Plugins Anything related to our integration plugins with EC2, GCP and Azure labels Dec 6, 2021
Copy link
Member

@tlrx tlrx left a comment

Choose a reason for hiding this comment

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

Sorry for the delay Artem - I've left more comments but it looks good overall.


public void shutdown() {
if (credentialsProvider != null) {
credentialsProvider.close();
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use IOUtils.close() here so that all objects are teared down but an exception is still retrhrown?

@arteam arteam requested a review from tlrx January 18, 2022 12:16
@arteam
Copy link
Contributor Author

arteam commented Jan 18, 2022

@elasticmachine update branch

Copy link
Member

@tlrx tlrx left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the extra iterations Artem. I've left some minor comments - feel free to address them or not.

@arteam
Copy link
Contributor Author

arteam commented Jan 19, 2022

@elasticmachine update branch

@arteam
Copy link
Contributor Author

arteam commented Jan 19, 2022

@elasticmachine update branch

@arteam arteam merged commit e47b7a6 into elastic:master Jan 19, 2022
@arteam
Copy link
Contributor Author

arteam commented Jan 19, 2022

Thank you very much, Tanguy!

arteam added a commit to arteam/elasticsearch that referenced this pull request Jan 19, 2022
…astic#81255)

There have been many requests to support repository-s3 authentication via IAM roles in Kubernetes service accounts.

The AWS SDK is supposed to support them out of the box with the aws-java-sdk-sts library. Unfortunately, we can't use WebIdentityTokenCredentialsProvider from the SDK. It reads the token from AWS_WEB_IDENTITY_TOKEN_FILE environment variable which is usually mounted to /var/run/secrets/eks.amazonaws.com/serviceaccount/token and the S3 repository doesn't have the read permission to read it. We don't want to hard-code a file permission for the repository, because the location of AWS_WEB_IDENTITY_TOKEN_FILE can change at any time in the future and we would also generally prefer to restrict the ability of plugins to access things outside of their config directory.

To overcome this limitation, this change adds a custom WebIdentityCredentials provider that reads the service account from a symlink to AWS_WEB_IDENTITY_TOKEN_FILE created in the repository's config directory. We expect the end user to create the symlink to indicate that they want to use service accounts for authentification.

Service accounts are checked and exchanged for session tokens by the AWS STS. To test the authentification flow, this change adds a test fixture which mocks the assume-role-with-web-identity call to the service and returns a response with test credentials.

Fixes elastic#52625
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.0

@arteam arteam deleted the amazon-web-identity-token branch January 19, 2022 13:06
arteam added a commit that referenced this pull request Jan 19, 2022
…ts (#81255) (#82796)

There have been many requests to support repository-s3 authentication via IAM roles in Kubernetes service accounts.

The AWS SDK is supposed to support them out of the box with the aws-java-sdk-sts library. Unfortunately, we can't use WebIdentityTokenCredentialsProvider from the SDK. It reads the token from AWS_WEB_IDENTITY_TOKEN_FILE environment variable which is usually mounted to /var/run/secrets/eks.amazonaws.com/serviceaccount/token and the S3 repository doesn't have the read permission to read it. We don't want to hard-code a file permission for the repository, because the location of AWS_WEB_IDENTITY_TOKEN_FILE can change at any time in the future and we would also generally prefer to restrict the ability of plugins to access things outside of their config directory.

To overcome this limitation, this change adds a custom WebIdentityCredentials provider that reads the service account from a symlink to AWS_WEB_IDENTITY_TOKEN_FILE created in the repository's config directory. We expect the end user to create the symlink to indicate that they want to use service accounts for authentification.

Service accounts are checked and exchanged for session tokens by the AWS STS. To test the authentification flow, this change adds a test fixture which mocks the assume-role-with-web-identity call to the service and returns a response with test credentials.

Fixes #52625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-backport Automatically create backport pull requests when merged :Distributed/Snapshot/Restore Anything directly related to the `_snapshot/*` APIs >feature Team:Distributed Meta label for distributed team v8.0.0-rc2 v8.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for AWS IAM Kubernetes service account permissions
5 participants