Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Add static cred provider for manually setting auth data for Cerberus … (
Browse files Browse the repository at this point in the history
#5)

Add static credential provider for manually setting auth data for Cerberus iam auth
  • Loading branch information
fieldju authored Jan 19, 2017
1 parent 8bf8011 commit 044c6ec
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
# limitations under the License.
#

version=1.2.0
version=1.3.0
groupId=com.nike
artifactId=cerberus-client
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.nike.cerberus.client.auth.aws;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.nike.vault.client.StaticVaultUrlResolver;
import com.nike.vault.client.UrlResolver;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Provider for allowing users to explicitly set the account id, rolename and region that they want to authenticate as.
*/
public class StaticIamRoleVaultCredentialsProvider extends BaseAwsCredentialsProvider {

protected String accountId;
protected String roleName;
protected Region region;

public StaticIamRoleVaultCredentialsProvider(UrlResolver urlResolver, String accountId, String roleName, String region) {
this(urlResolver);
this.accountId = accountId;
this.roleName = roleName;
this.region = Region.getRegion(Regions.fromName(region));
}

public StaticIamRoleVaultCredentialsProvider(String vaultUrl, String accountId, String roleName, String region) {
this(new StaticVaultUrlResolver(vaultUrl));
this.accountId = accountId;
this.roleName = roleName;
this.region = Region.getRegion(Regions.fromName(region));
}

public StaticIamRoleVaultCredentialsProvider(UrlResolver urlResolver, String accountId, String roleName, Region region) {
this(urlResolver);
this.accountId = accountId;
this.roleName = roleName;
this.region = region;
}

public StaticIamRoleVaultCredentialsProvider(String vaultUrl, String accountId, String roleName, Region region) {
this(new StaticVaultUrlResolver(vaultUrl));
this.accountId = accountId;
this.roleName = roleName;
this.region = region;
}

public StaticIamRoleVaultCredentialsProvider(UrlResolver urlResolver, String iamRoleArn, String region) {
this(urlResolver);
this.accountId = getAccountIdFromArn(iamRoleArn);
this.roleName = getRoleNameFromArn(iamRoleArn);
this.region = Region.getRegion(Regions.fromName(region));
}


public StaticIamRoleVaultCredentialsProvider(String vaultUrl, String iamRoleArn, String region) {
this(new StaticVaultUrlResolver(vaultUrl));
this.accountId = getAccountIdFromArn(iamRoleArn);
this.roleName = getRoleNameFromArn(iamRoleArn);
this.region = Region.getRegion(Regions.fromName(region));
}


public StaticIamRoleVaultCredentialsProvider(UrlResolver urlResolver, String iamRoleArn, Region region) {
this(urlResolver);
this.accountId = getAccountIdFromArn(iamRoleArn);
this.roleName = getRoleNameFromArn(iamRoleArn);
this.region = region;
}

public StaticIamRoleVaultCredentialsProvider(String vaultUrl, String iamRoleArn, Region region) {
this(new StaticVaultUrlResolver(vaultUrl));
this.accountId = getAccountIdFromArn(iamRoleArn);
this.roleName = getRoleNameFromArn(iamRoleArn);
this.region = region;
}

private StaticIamRoleVaultCredentialsProvider(UrlResolver urlResolver) {
super(urlResolver);
}

private String getAccountIdFromArn(String arn) {
Matcher m = Pattern.compile("arn:aws:iam::(.*?):role.*").matcher(arn);
boolean found = m.find();
if (found) {
return m.group(1);
}

throw new IllegalArgumentException("Invalid IAM role ARN supplied, expected arn:aws:iam::%s:role/%s");
}

private String getRoleNameFromArn(String arn) {
Matcher m = Pattern.compile("arn:aws:iam::.*?:role/(.*)").matcher(arn);
boolean found = m.find();
if (found) {
return m.group(1);
}

throw new IllegalArgumentException("Invalid IAM role ARN supplied, expected arn:aws:iam::%s:role/%s");
}

@Override
protected void authenticate() {
getAndSetToken(accountId, roleName, region);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package com.nike.cerberus.client.auth.aws;

import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.nike.vault.client.StaticVaultUrlResolver;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class StaticIamRoleVaultCredentialsProviderTest {

private static final String ACCOUNT_ID = "1234";
private static final String ROLE_NAME = "foo/base/bar";
private static final String ROLE_ARN = String.format("arn:aws:iam::%s:role/%s", ACCOUNT_ID, ROLE_NAME);
private static final String REGION_STRING = "us-west-2";
private static final Region REGION = Region.getRegion(Regions.US_WEST_2);

@Test
public void test_constructor_1() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
new StaticVaultUrlResolver("foo"),
ACCOUNT_ID,
ROLE_NAME,
REGION_STRING
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_2() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
ACCOUNT_ID,
ROLE_NAME,
REGION_STRING
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_3() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
new StaticVaultUrlResolver("foo"),
ACCOUNT_ID,
ROLE_NAME,
REGION
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_4() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
ACCOUNT_ID,
ROLE_NAME,
REGION
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_5() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
new StaticVaultUrlResolver("foo"),
ROLE_ARN,
REGION_STRING
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_6() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
ROLE_ARN,
REGION_STRING
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_7() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
new StaticVaultUrlResolver("foo"),
ROLE_ARN,
REGION
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test
public void test_constructor_8() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
ROLE_ARN,
REGION
);

assertEquals(ACCOUNT_ID, provider.accountId);
assertEquals(ROLE_NAME, provider.roleName);
assertEquals(REGION, provider.region);
}

@Test(expected = IllegalArgumentException.class)
public void test_constructor_bad_arn1() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
"foo",
REGION
);
}

@Test(expected = IllegalArgumentException.class)
public void test_constructor_bad_arn2() {
StaticIamRoleVaultCredentialsProvider provider = new StaticIamRoleVaultCredentialsProvider(
"foo",
"arn:aws:iam::123:rolefoo",
REGION
);
}

}

0 comments on commit 044c6ec

Please sign in to comment.