Skip to content

Added fileExistsBehavior --file-exists-behavior support #19

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

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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ associated role has, at minimum, a policy that permits `codedeploy:*` and

Here is a rather blank example:

step([$class: 'AWSCodeDeployPublisher', applicationName: '', awsAccessKey: '', awsSecretKey: '', credentials: 'awsAccessKey', deploymentGroupAppspec: false, deploymentGroupName: '', deploymentMethod: 'deploy', excludes: '', iamRoleArn: '', includes: '**', proxyHost: '', proxyPort: 0, region: 'ap-northeast-1', s3bucket: '', s3prefix: '', subdirectory: '', versionFileName: '', waitForCompletion: false])
step([$class: 'AWSCodeDeployPublisher', applicationName: '', awsAccessKey: '', awsSecretKey: '', credentials: 'awsAccessKey', deploymentGroupAppspec: false, deploymentGroupName: '', deploymentMethod: 'deploy', excludes: '', fileExistsBehavior: 'OVERWRITE', iamRoleArn: '', includes: '**', proxyHost: '', proxyPort: 0, region: 'ap-northeast-1', s3bucket: '', s3prefix: '', subdirectory: '', versionFileName: '', waitForCompletion: false])

License
-------

This plugin is licensed under Apache 2.0. See the LICENSE file for more information.
______________________
These tools are provided as-is and without warranty or support. They do not constitute part of the Software AG product suite. Users are free to use, fork and modify them, subject to the license agreement. While Software AG welcomes contributions, we cannot guarantee to include every contribution in the master project.

Contact us at [TECHcommunity](mailto:technologycommunity@softwareag.com?subject=Github/SoftwareAG) if you have any questions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jenkins.version>1.612</jenkins.version>
<java.level>7</java.level>
<java.level>8</java.level>
</properties>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.119</version>
<version>1.11.457</version>
</dependency>
</dependencies>

Expand All @@ -70,6 +70,11 @@
<plugin>
<artifactId>maven-release-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.kohsuke</groupId>
<artifactId>access-modifier-checker</artifactId>
<version>1.16</version>
</plugin>
</plugins>
</build>
</project>
117 changes: 73 additions & 44 deletions src/main/java/com/amazonaws/codedeploy/AWSClients.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,68 @@
/*
* Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*
* http://aws.amazon.com/apache2.0
*
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.codedeploy;

import static org.apache.commons.lang.StringUtils.isEmpty;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.codedeploy.AmazonCodeDeployClient;
import com.amazonaws.services.codedeploy.AmazonCodeDeploy;
import com.amazonaws.services.codedeploy.AmazonCodeDeployClientBuilder;
import com.amazonaws.services.codedeploy.model.GetApplicationRequest;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder;
import com.amazonaws.services.identitymanagement.model.GetUserResult;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
import com.amazonaws.services.securitytoken.model.AssumeRoleResult;
import com.amazonaws.services.securitytoken.model.Credentials;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.UUID;

import static org.apache.commons.lang.StringUtils.isEmpty;

/**
* @author gibbon
*/
public class AWSClients {

/**
* Index in the colon-separated ARN that contains the account id
* Sample ARN: arn:aws:iam::123456789012:user/David
**/
* Index in the colon-separated ARN that contains the account id Sample ARN:
* arn:aws:iam::123456789012:user/David
*/
private static final int ARN_ACCOUNT_ID_INDEX = 4;

/**
* AWS-CodeDeploy-Jenkins-Plugin/<Version>
* This will be used as the SDK user agent suffix
**/
*/
private static final String USER_AGENT_SUFFIX = "AWS-CodeDeploy-Jenkins-Plugin/1.20";

public final AmazonCodeDeployClient codedeploy;
public final AmazonS3Client s3;
public final AmazonCodeDeploy codedeploy;
public final AmazonS3 s3;

private final String region;
private final String proxyHost;
Expand All @@ -70,34 +75,54 @@ public AWSClients(String region, AWSCredentials credentials, String proxyHost, i

//setup proxy connection:
ClientConfiguration clientCfg = new ClientConfiguration();
if (proxyHost != null && proxyPort > 0 ) {
if (proxyHost != null && proxyPort > 0) {
clientCfg.setProxyHost(proxyHost);
clientCfg.setProxyPort(proxyPort);
}

clientCfg.setUserAgentSuffix(USER_AGENT_SUFFIX);

this.s3 = credentials != null ? new AmazonS3Client(credentials, clientCfg) : new AmazonS3Client(clientCfg);
this.codedeploy = credentials != null ? new AmazonCodeDeployClient(credentials, clientCfg) : new AmazonCodeDeployClient(clientCfg);
codedeploy.setRegion(Region.getRegion(Regions.fromName(this.region)));
s3.setRegion(Region.getRegion(Regions.fromName(this.region)));

if (credentials != null) {
BasicAWSCredentials creds = new BasicAWSCredentials(credentials.getAWSAccessKeyId(), credentials.getAWSSecretKey());
final AWSStaticCredentialsProvider awsStaticCredentialsProvider = new AWSStaticCredentialsProvider(creds);
this.s3 = AmazonS3ClientBuilder.standard().
withCredentials(awsStaticCredentialsProvider).
withClientConfiguration(clientCfg).
withRegion(Regions.fromName(this.region)).build();

this.codedeploy = AmazonCodeDeployClientBuilder.standard().
withCredentials(awsStaticCredentialsProvider).
withClientConfiguration(clientCfg).
withRegion(Regions.fromName(this.region)).build();

} else {
this.s3 = AmazonS3ClientBuilder.standard().
withClientConfiguration(clientCfg).
withRegion(Regions.fromName(this.region)).build();

this.codedeploy = AmazonCodeDeployClientBuilder.standard().
withClientConfiguration(clientCfg).
withRegion(Regions.fromName(this.region)).build();
}
}

public static AWSClients fromDefaultCredentialChain(String region, String proxyHost, int proxyPort) {
return new AWSClients(region, null, proxyHost, proxyPort);
}

public static AWSClients fromIAMRole(String region, String iamRole, String externalId, String proxyHost, int proxyPort) {
return new AWSClients(region, getCredentials(iamRole, externalId), proxyHost, proxyPort);
}

public static AWSClients fromBasicCredentials(String region, String awsAccessKey, String awsSecretKey, String proxyHost, int proxyPort) {
return new AWSClients(region, new BasicAWSCredentials(awsAccessKey, awsSecretKey), proxyHost, proxyPort);
}

/**
* Via the default provider chain (i.e., global keys for this Jenkins instance), return the account ID for the
* currently authenticated user.
* Via the default provider chain (i.e., global keys for this Jenkins
* instance), return the account ID for the currently authenticated user.
*
* @param proxyHost hostname of the proxy to use (if any)
* @param proxyPort port of the proxy to use (if any)
* @return 12-digit account id
Expand All @@ -107,11 +132,13 @@ public static String getAccountId(String proxyHost, int proxyPort) {
String arn = "";
try {
ClientConfiguration clientCfg = new ClientConfiguration();
if (proxyHost != null && proxyPort > 0 ) {
if (proxyHost != null && proxyPort > 0) {
clientCfg.setProxyHost(proxyHost);
clientCfg.setProxyPort(proxyPort);
}
AmazonIdentityManagementClient iam = new AmazonIdentityManagementClient(clientCfg);
AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.standard().
withClientConfiguration(clientCfg).build();

GetUserResult user = iam.getUser();
arn = user.getUser().getArn();
} catch (AmazonServiceException e) {
Expand Down Expand Up @@ -150,22 +177,24 @@ private File createTestFile() throws IOException {
}

private static AWSCredentials getCredentials(String iamRole, String externalId) {
if (isEmpty(iamRole)) return null;
if (isEmpty(iamRole)) {
return null;
}

AWSSecurityTokenServiceClient sts = new AWSSecurityTokenServiceClient();
AWSSecurityTokenService sts = AWSSecurityTokenServiceClientBuilder.defaultClient();

int credsDuration = (int) (AWSCodeDeployPublisher.DEFAULT_TIMEOUT_SECONDS
* AWSCodeDeployPublisher.DEFAULT_POLLING_FREQUENCY_SECONDS);
* AWSCodeDeployPublisher.DEFAULT_POLLING_FREQUENCY_SECONDS);

if (credsDuration > 3600) {
credsDuration = 3600;
}

AssumeRoleResult assumeRoleResult = sts.assumeRole(new AssumeRoleRequest()
.withRoleArn(iamRole)
.withExternalId(externalId)
.withDurationSeconds(credsDuration)
.withRoleSessionName(AWSCodeDeployPublisher.ROLE_SESSION_NAME)
.withRoleArn(iamRole)
.withExternalId(externalId)
.withDurationSeconds(credsDuration)
.withRoleSessionName(AWSCodeDeployPublisher.ROLE_SESSION_NAME)
);

Credentials stsCredentials = assumeRoleResult.getCredentials();
Expand Down
Loading