diff --git a/src/main/java/io/jenkins/plugins/cdevents/CDEventsGlobalConfig.java b/src/main/java/io/jenkins/plugins/cdevents/CDEventsGlobalConfig.java index 9cdfcae..8b5b241 100644 --- a/src/main/java/io/jenkins/plugins/cdevents/CDEventsGlobalConfig.java +++ b/src/main/java/io/jenkins/plugins/cdevents/CDEventsGlobalConfig.java @@ -25,6 +25,7 @@ public class CDEventsGlobalConfig extends GlobalConfiguration { private String kinesisStreamName; private String kinesisRegion; private String kinesisEndpoint; + private String iamRole; @SuppressFBWarnings(value = {"CD_CIRCULAR_DEPENDENCY", "MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR"}, justification = "Circular dependency is false positive triggered by jenkins.model.GlobalConfiguration. " + "Overridable method call in constructor is unavoidable.") public CDEventsGlobalConfig() { @@ -99,6 +100,17 @@ public void setKinesisEndpoint(String kinesisEndpoint) { save(); } + public String getIamRole() { + return this.iamRole; + } + + @DataBoundSetter + public void setIamRole(String iamRole) { + this.iamRole = iamRole; + KinesisSink.nullifyKinesisClient(); + save(); + } + public FormValidation doCheckKinesisStreamName(@QueryParameter("kinesisStreamName") String kinesisStreamName) { if (isNullOrEmpty(kinesisStreamName)) { return FormValidation.error("Kinesis stream cannot be blank"); @@ -119,4 +131,11 @@ public FormValidation doCheckKinesisEndpoint(@QueryParameter("kinesisEndpoint") } return FormValidation.ok(); } + + public FormValidation doCheckIamRole(@QueryParameter("iamRole") String iamRole) { + if (isNullOrEmpty(iamRole)) { + return FormValidation.error("IAM Role cannot be blank"); + } + return FormValidation.ok(); + } } diff --git a/src/main/java/io/jenkins/plugins/cdevents/sinks/KinesisSink.java b/src/main/java/io/jenkins/plugins/cdevents/sinks/KinesisSink.java index b295da3..025f644 100644 --- a/src/main/java/io/jenkins/plugins/cdevents/sinks/KinesisSink.java +++ b/src/main/java/io/jenkins/plugins/cdevents/sinks/KinesisSink.java @@ -5,6 +5,7 @@ package io.jenkins.plugins.cdevents.sinks; +import com.amazonaws.auth.STSAssumeRoleSessionCredentialsProvider; import com.amazonaws.client.builder.AwsClientBuilder; import com.amazonaws.services.kinesis.AmazonKinesis; import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder; @@ -26,6 +27,7 @@ public class KinesisSink extends CDEventsSink { private volatile static String streamName; private volatile static String region; private volatile static String endpoint; + private volatile static String iamRole; public KinesisSink() { if (Jenkins.get().getPlugin("aws-java-sdk") == null @@ -51,6 +53,8 @@ public static synchronized void rebuildKinesisClient() { streamName = CDEventsGlobalConfig.get().getKinesisStreamName().trim(); region = CDEventsGlobalConfig.get().getKinesisRegion(); endpoint = CDEventsGlobalConfig.get().getKinesisEndpoint(); + iamRole = CDEventsGlobalConfig.get().getIamRole(); + String roleSessionName = "cdevents-plugin"; AmazonKinesisClientBuilder kinesisBuilder = AmazonKinesisClientBuilder.standard(); if (region != null && !region.isEmpty()) { @@ -61,9 +65,14 @@ public static synchronized void rebuildKinesisClient() { endpoint, region); kinesisBuilder.withEndpointConfiguration(endpointConfiguration); } + if (iamRole != null && !iamRole.isEmpty()) { + STSAssumeRoleSessionCredentialsProvider credentialsProvider = new STSAssumeRoleSessionCredentialsProvider.Builder( + iamRole, roleSessionName).build(); + kinesisBuilder.withCredentials(credentialsProvider); + } - LOGGER.info(String.format("Instantiating new Kinesis client {stream=%s, region=%s, endpoint=%s}", - streamName, region, endpoint)); + LOGGER.info(String.format("Instantiating new Kinesis client {stream=%s, region=%s, endpoint=%s, iamRole=%s}", + streamName, region, endpoint, iamRole)); kinesis = kinesisBuilder.build(); } } diff --git a/src/main/resources/io/jenkins/plugins/cdevents/CDEventsGlobalConfig/config.jelly b/src/main/resources/io/jenkins/plugins/cdevents/CDEventsGlobalConfig/config.jelly index eea070a..cbbe1c8 100644 --- a/src/main/resources/io/jenkins/plugins/cdevents/CDEventsGlobalConfig/config.jelly +++ b/src/main/resources/io/jenkins/plugins/cdevents/CDEventsGlobalConfig/config.jelly @@ -19,6 +19,9 @@ + + +