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

Support GCP Web Identity session credentials #645

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-iamcredentials</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>


Expand Down
57 changes: 53 additions & 4 deletions src/main/java/io/prometheus/cloudwatch/CloudWatchCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static io.prometheus.cloudwatch.CachingDimensionSource.DimensionCacheConfig;

import com.google.cloud.iam.credentials.v1.GenerateIdTokenRequest;
import com.google.cloud.iam.credentials.v1.GenerateIdTokenResponse;
import com.google.cloud.iam.credentials.v1.IamCredentialsClient;
import io.prometheus.client.Collector;
import io.prometheus.client.Collector.Describable;
import io.prometheus.client.Counter;
Expand All @@ -24,6 +27,7 @@
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
Expand All @@ -39,11 +43,17 @@
import software.amazon.awssdk.services.resourcegroupstaggingapi.model.TagFilter;
import software.amazon.awssdk.services.sts.StsClient;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider;
import software.amazon.awssdk.services.sts.auth.StsAssumeRoleWithWebIdentityCredentialsProvider;
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
import software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityRequest;

public class CloudWatchCollector extends Collector implements Describable {
private static final Logger LOGGER = Logger.getLogger(CloudWatchCollector.class.getName());

private static final String SERVICE_ACCOUNT_NAME_FORMAT = "projects/-/serviceAccounts/%s";

private static final int WEB_IDENTITY_CREDENTIAL_DURATION_SECONDS = 3600;

static class ActiveConfig {
ArrayList<MetricRule> rules;
CloudWatchClient cloudWatchClient;
Expand Down Expand Up @@ -347,14 +357,53 @@ private void loadConfig(
}
}

private static String getIdToken(String serviceAccountEmail) throws IOException {
try (IamCredentialsClient credentialsClient = IamCredentialsClient.create()) {
GenerateIdTokenResponse idTokenResponse =
credentialsClient.generateIdToken(
GenerateIdTokenRequest.newBuilder()
.setName(String.format(SERVICE_ACCOUNT_NAME_FORMAT, serviceAccountEmail))
.setAudience(serviceAccountEmail)
.setIncludeEmail(true)
.build());
return idTokenResponse.getToken();
}
}

private AwsCredentialsProvider getRoleCredentialProvider(Map<String, Object> config) {
String roleArn = (String) config.get("role_arn");
if (config.containsKey("assume_role_web_identity")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this only works for GCP, I think this config key should reflect it?

// indicates we need to use gcp-based web identity to assume the role
return StsAssumeRoleWithWebIdentityCredentialsProvider.builder()
// intentionally using anonymous credentials since this is meant to be run only in gcp
// environments, and the AssumeRoleWithWebIdentityRequest can run without any credentials
.stsClient(
StsClient.builder()
.credentialsProvider(AnonymousCredentialsProvider.create())
.region(Region.US_WEST_1)
.build())
.refreshRequest(
() -> {
String idToken;
try {
idToken = getIdToken((String) config.get("assume_role_web_identity"));
} catch (IOException e) {
throw new RuntimeException("Failed to get id token for role arn: " + roleArn, e);
}
String[] roleSplit = roleArn.split("/");
return AssumeRoleWithWebIdentityRequest.builder()
.roleArn(roleArn)
.webIdentityToken(idToken)
.roleSessionName(roleSplit[roleSplit.length - 1])
.durationSeconds(WEB_IDENTITY_CREDENTIAL_DURATION_SECONDS)
.build();
})
.build();
}
StsClient stsClient =
StsClient.builder().region(Region.of((String) config.get("region"))).build();
AssumeRoleRequest assumeRoleRequest =
AssumeRoleRequest.builder()
.roleArn((String) config.get("role_arn"))
.roleSessionName("cloudwatch_exporter")
.build();
AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName("cloudwatch_exporter").build();
return StsAssumeRoleCredentialsProvider.builder()
.stsClient(stsClient)
.refreshRequest(assumeRoleRequest)
Expand Down