Skip to content

Commit

Permalink
JENKINS-29845
Browse files Browse the repository at this point in the history
Added DefaultCredentialProviderChain credential method when enabling instance credentials checkbox
  • Loading branch information
cast committed Aug 6, 2015
1 parent 3840ab4 commit 8653a23
Show file tree
Hide file tree
Showing 20 changed files with 261 additions and 92 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Expand Up @@ -87,6 +87,15 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
Expand Down
Expand Up @@ -42,6 +42,7 @@
* Describable containing Lambda post build action config, checking feasability of migrating it to upload package.
*/
public class LambdaVariables extends AbstractDescribableImpl<LambdaVariables> {
private boolean useInstanceCredentials;
private String awsAccessKeyId;
private Secret awsSecretKey;
private String awsRegion;
Expand All @@ -57,7 +58,8 @@ public class LambdaVariables extends AbstractDescribableImpl<LambdaVariables> {
private String updateMode;

@DataBoundConstructor
public LambdaVariables(String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, boolean successOnly, String updateMode) {
public LambdaVariables(boolean useInstanceCredentials, String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, boolean successOnly, String updateMode) {
this.useInstanceCredentials = useInstanceCredentials;
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
Expand All @@ -73,6 +75,10 @@ public LambdaVariables(String awsAccessKeyId, Secret awsSecretKey, String awsReg
this.updateMode = updateMode;
}

public boolean getUseInstanceCredentials() {
return useInstanceCredentials;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}
Expand Down Expand Up @@ -125,6 +131,10 @@ public boolean getSuccessOnly(){
return successOnly;
}

public void setUseInstanceCredentials(boolean useInstanceCredentials) {
this.useInstanceCredentials = useInstanceCredentials;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}
Expand Down Expand Up @@ -190,15 +200,19 @@ public void expandVariables(EnvVars env) {
}

public LambdaVariables getClone(){
return new LambdaVariables(awsAccessKeyId, awsSecretKey, awsRegion, artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, successOnly, updateMode);
return new LambdaVariables(useInstanceCredentials, awsAccessKeyId, awsSecretKey, awsRegion, artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, successOnly, updateMode);
}

public DeployConfig getUploadConfig(){
return new DeployConfig(artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, updateMode);
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
if(useInstanceCredentials){
return new LambdaClientConfig(awsRegion);
} else {
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}
}

private String expand(String value, EnvVars env) {
Expand Down
Expand Up @@ -39,6 +39,7 @@
import java.util.List;

public class LambdaInvokeBuildStepVariables extends AbstractDescribableImpl<LambdaInvokeBuildStepVariables> {
private boolean useInstanceCredentials;
private String awsAccessKeyId;
private Secret awsSecretKey;
private String awsRegion;
Expand All @@ -51,7 +52,8 @@ public LambdaInvokeBuildStepVariables() {
}

@DataBoundConstructor
public LambdaInvokeBuildStepVariables(String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String functionName, String payload, boolean synchronous, List<JsonParameterVariables> jsonParameters) {
public LambdaInvokeBuildStepVariables(boolean useInstanceCredentials, String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String functionName, String payload, boolean synchronous, List<JsonParameterVariables> jsonParameters) {
this.useInstanceCredentials = useInstanceCredentials;
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
Expand All @@ -61,6 +63,10 @@ public LambdaInvokeBuildStepVariables(String awsAccessKeyId, Secret awsSecretKey
this.jsonParameters = jsonParameters;
}

public boolean getUseInstanceCredentials() {
return useInstanceCredentials;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}
Expand Down Expand Up @@ -93,6 +99,10 @@ public List<JsonParameterVariables> getJsonParameters() {
}
}

public void setUseInstanceCredentials(boolean useInstanceCredentials) {
this.useInstanceCredentials = useInstanceCredentials;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}
Expand Down Expand Up @@ -135,7 +145,7 @@ public void expandVariables(EnvVars env) {
}

public LambdaInvokeBuildStepVariables getClone(){
return new LambdaInvokeBuildStepVariables(awsAccessKeyId, awsSecretKey, awsRegion, functionName, payload, synchronous, jsonParameters);
return new LambdaInvokeBuildStepVariables(useInstanceCredentials, awsAccessKeyId, awsSecretKey, awsRegion, functionName, payload, synchronous, jsonParameters);
}

private String expand(String value, EnvVars env) {
Expand All @@ -151,7 +161,11 @@ public InvokeConfig getInvokeConfig(){
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
if(useInstanceCredentials){
return new LambdaClientConfig(awsRegion);
} else {
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}
}

@Extension // This indicates to Jenkins that this is an implementation of an extension point.
Expand All @@ -173,28 +187,32 @@ public boolean equals(Object o) {

LambdaInvokeBuildStepVariables variables = (LambdaInvokeBuildStepVariables) o;

if (synchronous != variables.synchronous) return false;
if (awsAccessKeyId != null ? !awsAccessKeyId.equals(variables.awsAccessKeyId) : variables.awsAccessKeyId != null)
if (getUseInstanceCredentials() != variables.getUseInstanceCredentials()) return false;
if (getSynchronous() != variables.getSynchronous()) return false;
if (getAwsAccessKeyId() != null ? !getAwsAccessKeyId().equals(variables.getAwsAccessKeyId()) : variables.getAwsAccessKeyId() != null)
return false;
if (getAwsSecretKey() != null ? !getAwsSecretKey().equals(variables.getAwsSecretKey()) : variables.getAwsSecretKey() != null)
return false;
if (getAwsRegion() != null ? !getAwsRegion().equals(variables.getAwsRegion()) : variables.getAwsRegion() != null)
return false;
if (awsSecretKey != null ? !awsSecretKey.equals(variables.awsSecretKey) : variables.awsSecretKey != null)
if (getFunctionName() != null ? !getFunctionName().equals(variables.getFunctionName()) : variables.getFunctionName() != null)
return false;
if (awsRegion != null ? !awsRegion.equals(variables.awsRegion) : variables.awsRegion != null) return false;
if (functionName != null ? !functionName.equals(variables.functionName) : variables.functionName != null)
if (getPayload() != null ? !getPayload().equals(variables.getPayload()) : variables.getPayload() != null)
return false;
if (payload != null ? !payload.equals(variables.payload) : variables.payload != null) return false;
return !(jsonParameters != null ? !jsonParameters.equals(variables.jsonParameters) : variables.jsonParameters != null);
return !(getJsonParameters() != null ? !getJsonParameters().equals(variables.getJsonParameters()) : variables.getJsonParameters() != null);

}

@Override
public int hashCode() {
int result = awsAccessKeyId != null ? awsAccessKeyId.hashCode() : 0;
result = 31 * result + (awsSecretKey != null ? awsSecretKey.hashCode() : 0);
result = 31 * result + (awsRegion != null ? awsRegion.hashCode() : 0);
result = 31 * result + (functionName != null ? functionName.hashCode() : 0);
result = 31 * result + (payload != null ? payload.hashCode() : 0);
result = 31 * result + (synchronous ? 1 : 0);
result = 31 * result + (jsonParameters != null ? jsonParameters.hashCode() : 0);
int result = (getUseInstanceCredentials() ? 1 : 0);
result = 31 * result + (getAwsAccessKeyId() != null ? getAwsAccessKeyId().hashCode() : 0);
result = 31 * result + (getAwsSecretKey() != null ? getAwsSecretKey().hashCode() : 0);
result = 31 * result + (getAwsRegion() != null ? getAwsRegion().hashCode() : 0);
result = 31 * result + (getFunctionName() != null ? getFunctionName().hashCode() : 0);
result = 31 * result + (getPayload() != null ? getPayload().hashCode() : 0);
result = 31 * result + (getSynchronous() ? 1 : 0);
result = 31 * result + (getJsonParameters() != null ? getJsonParameters().hashCode() : 0);
return result;
}
}
Expand Up @@ -39,6 +39,7 @@
import java.util.List;

public class LambdaInvokeVariables extends AbstractDescribableImpl<LambdaInvokeVariables> {
private boolean useInstanceCredentials;
private String awsAccessKeyId;
private Secret awsSecretKey;
private String awsRegion;
Expand All @@ -49,7 +50,8 @@ public class LambdaInvokeVariables extends AbstractDescribableImpl<LambdaInvokeV
private List<JsonParameterVariables> jsonParameters = new ArrayList<JsonParameterVariables>();

@DataBoundConstructor
public LambdaInvokeVariables(String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String functionName, String payload, boolean synchronous, boolean successOnly, List<JsonParameterVariables> jsonParameters) {
public LambdaInvokeVariables(boolean useInstanceCredentials, String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String functionName, String payload, boolean synchronous, boolean successOnly, List<JsonParameterVariables> jsonParameters) {
this.useInstanceCredentials = useInstanceCredentials;
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
Expand All @@ -60,6 +62,10 @@ public LambdaInvokeVariables(String awsAccessKeyId, Secret awsSecretKey, String
this.jsonParameters = jsonParameters;
}

public boolean getUseInstanceCredentials() {
return useInstanceCredentials;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}
Expand Down Expand Up @@ -96,6 +102,10 @@ public List<JsonParameterVariables> getJsonParameters() {
}
}

public void setUseInstanceCredentials(boolean useInstanceCredentials) {
this.useInstanceCredentials = useInstanceCredentials;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}
Expand Down Expand Up @@ -140,7 +150,7 @@ public void expandVariables(EnvVars env) {
}

public LambdaInvokeVariables getClone(){
return new LambdaInvokeVariables(awsAccessKeyId, awsSecretKey, awsRegion, functionName, payload, synchronous, successOnly, jsonParameters);
return new LambdaInvokeVariables(useInstanceCredentials,awsAccessKeyId, awsSecretKey, awsRegion, functionName, payload, synchronous, successOnly, jsonParameters);
}

private String expand(String value, EnvVars env) {
Expand All @@ -156,7 +166,11 @@ public InvokeConfig getInvokeConfig(){
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
if(useInstanceCredentials){
return new LambdaClientConfig(awsRegion);
} else {
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}
}

@Extension // This indicates to Jenkins that this is an implementation of an extension point.
Expand All @@ -178,28 +192,33 @@ public boolean equals(Object o) {

LambdaInvokeVariables that = (LambdaInvokeVariables) o;

if (synchronous != that.synchronous) return false;
if (successOnly != that.successOnly) return false;
if (awsAccessKeyId != null ? !awsAccessKeyId.equals(that.awsAccessKeyId) : that.awsAccessKeyId != null)
if (getUseInstanceCredentials() != that.getUseInstanceCredentials()) return false;
if (getSynchronous() != that.getSynchronous()) return false;
if (getSuccessOnly() != that.getSuccessOnly()) return false;
if (getAwsAccessKeyId() != null ? !getAwsAccessKeyId().equals(that.getAwsAccessKeyId()) : that.getAwsAccessKeyId() != null)
return false;
if (getAwsSecretKey() != null ? !getAwsSecretKey().equals(that.getAwsSecretKey()) : that.getAwsSecretKey() != null)
return false;
if (getAwsRegion() != null ? !getAwsRegion().equals(that.getAwsRegion()) : that.getAwsRegion() != null)
return false;
if (getFunctionName() != null ? !getFunctionName().equals(that.getFunctionName()) : that.getFunctionName() != null)
return false;
if (awsSecretKey != null ? !awsSecretKey.equals(that.awsSecretKey) : that.awsSecretKey != null) return false;
if (awsRegion != null ? !awsRegion.equals(that.awsRegion) : that.awsRegion != null) return false;
if (functionName != null ? !functionName.equals(that.functionName) : that.functionName != null) return false;
if (payload != null ? !payload.equals(that.payload) : that.payload != null) return false;
return !(jsonParameters != null ? !jsonParameters.equals(that.jsonParameters) : that.jsonParameters != null);
if (getPayload() != null ? !getPayload().equals(that.getPayload()) : that.getPayload() != null) return false;
return !(getJsonParameters() != null ? !getJsonParameters().equals(that.getJsonParameters()) : that.getJsonParameters() != null);

}

@Override
public int hashCode() {
int result = awsAccessKeyId != null ? awsAccessKeyId.hashCode() : 0;
result = 31 * result + (awsSecretKey != null ? awsSecretKey.hashCode() : 0);
result = 31 * result + (awsRegion != null ? awsRegion.hashCode() : 0);
result = 31 * result + (functionName != null ? functionName.hashCode() : 0);
result = 31 * result + (payload != null ? payload.hashCode() : 0);
result = 31 * result + (synchronous ? 1 : 0);
result = 31 * result + (successOnly ? 1 : 0);
result = 31 * result + (jsonParameters != null ? jsonParameters.hashCode() : 0);
int result = (getUseInstanceCredentials() ? 1 : 0);
result = 31 * result + (getAwsAccessKeyId() != null ? getAwsAccessKeyId().hashCode() : 0);
result = 31 * result + (getAwsSecretKey() != null ? getAwsSecretKey().hashCode() : 0);
result = 31 * result + (getAwsRegion() != null ? getAwsRegion().hashCode() : 0);
result = 31 * result + (getFunctionName() != null ? getFunctionName().hashCode() : 0);
result = 31 * result + (getPayload() != null ? getPayload().hashCode() : 0);
result = 31 * result + (getSynchronous() ? 1 : 0);
result = 31 * result + (getSuccessOnly() ? 1 : 0);
result = 31 * result + (getJsonParameters() != null ? getJsonParameters().hashCode() : 0);
return result;
}
}
Expand Up @@ -41,6 +41,7 @@
* Describable containing Lambda post build action config, checking feasability of migrating it to upload package.
*/
public class LambdaUploadBuildStepVariables extends AbstractDescribableImpl<LambdaUploadBuildStepVariables> {
private boolean useInstanceCredentials;
private String awsAccessKeyId;
private Secret awsSecretKey;
private String awsRegion;
Expand All @@ -55,7 +56,8 @@ public class LambdaUploadBuildStepVariables extends AbstractDescribableImpl<Lamb
private String updateMode;

@DataBoundConstructor
public LambdaUploadBuildStepVariables(String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, String updateMode) {
public LambdaUploadBuildStepVariables(boolean useInstanceCredentials, String awsAccessKeyId, Secret awsSecretKey, String awsRegion, String artifactLocation, String description, String functionName, String handler, Integer memorySize, String role, String runtime, Integer timeout, String updateMode) {
this.useInstanceCredentials = useInstanceCredentials;
this.awsAccessKeyId = awsAccessKeyId;
this.awsSecretKey = awsSecretKey;
this.awsRegion = awsRegion;
Expand All @@ -70,6 +72,10 @@ public LambdaUploadBuildStepVariables(String awsAccessKeyId, Secret awsSecretKey
this.updateMode = updateMode;
}

public boolean getUseInstanceCredentials() {
return useInstanceCredentials;
}

public String getAwsAccessKeyId() {
return awsAccessKeyId;
}
Expand Down Expand Up @@ -118,6 +124,10 @@ public String getUpdateMode() {
return updateMode;
}

public void setUseInstanceCredentials(boolean useInstanceCredentials) {
this.useInstanceCredentials = useInstanceCredentials;
}

public void setAwsAccessKeyId(String awsAccessKeyId) {
this.awsAccessKeyId = awsAccessKeyId;
}
Expand Down Expand Up @@ -179,15 +189,19 @@ public void expandVariables(EnvVars env) {
}

public LambdaUploadBuildStepVariables getClone(){
return new LambdaUploadBuildStepVariables(awsAccessKeyId, awsSecretKey, awsRegion, artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, updateMode);
return new LambdaUploadBuildStepVariables(useInstanceCredentials, awsAccessKeyId, awsSecretKey, awsRegion, artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, updateMode);
}

public DeployConfig getUploadConfig(){
return new DeployConfig(artifactLocation, description, functionName, handler, memorySize, role, runtime, timeout, updateMode);
}

public LambdaClientConfig getLambdaClientConfig(){
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
if(useInstanceCredentials){
return new LambdaClientConfig(awsRegion);
} else {
return new LambdaClientConfig(awsAccessKeyId, Secret.toString(awsSecretKey), awsRegion);
}
}

private String expand(String value, EnvVars env) {
Expand Down
@@ -1,13 +1,19 @@
package com.xti.jenkins.plugin.awslambda.util;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.lambda.AWSLambdaClient;

public class LambdaClientConfig {
private AWSLambdaClient client;

public LambdaClientConfig(String region){
client = new AWSLambdaClient(new DefaultAWSCredentialsProviderChain());
client.setRegion(Region.getRegion(Regions.fromName(region)));
}

public LambdaClientConfig(String accessKeyId, String secretKey, String region) {
client = new AWSLambdaClient(new BasicAWSCredentials(accessKeyId, secretKey));
client.setRegion(Region.getRegion(Regions.fromName(region)));
Expand Down

0 comments on commit 8653a23

Please sign in to comment.