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

Initial Commit #13

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,61 +28,45 @@
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.AmazonAutoScalingClientBuilder;
import com.intuit.cloudraider.model.Credentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
* Creating access to Amazon AutoScaling functionality through AmazonAutoScaling
* <p>
*/
@Component
public class ASGDelegator {
public class ASGDelegator extends DelegatorBase<AmazonAutoScaling> {

/**
* The Logger.
*/
Logger logger = LoggerFactory.getLogger(this.getClass());

private AmazonAutoScaling asgClient;
private AWSCredentials awsCredentials;
private String region;

@Autowired
private Credentials credentials;


/**
* Instantiates a new Asg delegator.
*/
public ASGDelegator() {

}

@PostConstruct
private void init()
{
awsCredentials = credentials.getAwsCredentials();
region = credentials.getRegion();
asgClient = AmazonAutoScalingClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(region)
.build();
public ASGDelegator(){

}


/**
* Gets asg client.
*
* @return the asg client
*/
public AmazonAutoScaling getAsgClient() {
return asgClient;
return getClient();
}

@Override
protected AmazonAutoScaling buildClient(AWSCredentials creds, String region) {
return AmazonAutoScalingClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(creds))
.withRegion(region)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ public ApplicationLoadBalancerDelegator() {
private void init()
{
awsCredentials = credentials.getAwsCredentials();

region = credentials.getRegion();

amazonApplicationLoadBalancing = AmazonElasticLoadBalancingClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.withRegion(region)
.build();

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.intuit.cloudraider.commons;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityResult;
import com.intuit.cloudraider.model.Credentials;

@Component
public abstract class DelegatorBase<T> {

private T client;

Logger logger = LoggerFactory.getLogger(this.getClass());

private static final String TARGET_ACCOUNT = System.getenv("TargetAccount");

@Autowired
private Credentials credentials;

protected abstract T buildClient(AWSCredentials creds, String region);

@PostConstruct
private void init() {
validateAccount();
client = buildClient(credentials.getAwsCredentials(), credentials.getRegion());
}

protected T getClient() {
return client;
}

private void validateAccount() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function looks good to me.
But I would put this function in a different place...

Copy link
Collaborator

Choose a reason for hiding this comment

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

In addition to this I would check if the config file has an account number.
So the check priority will be like as follows:

  1. account number from env variable (doucment the variable name in readme)
  2. account number in config file
  3. no account number found in above scenarios, so dont do account number verification (log the same)

Copy link
Author

Choose a reason for hiding this comment

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

"But I would put this function in a different place..."
Any suggestions?

Copy link
Collaborator

Choose a reason for hiding this comment

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

As for the location of the validateAccount function I would prefer its moved to https://github.com/intuit/CloudRaider/blob/master/cloudraider-core/src/main/java/com/intuit/cloudraider/model/BasicCredentials.java
This is the place I was referring to in this comment - #8 (comment)

Thats where all credentials are fetched from and it will become a single place to fetch and verify the account and credentials. This will remove the overhead of changing all the Delegator functions in com.intuit.cloudraider.commons

I would rather spend the refactoring time to refactor BasicCredentials.java


if (TARGET_ACCOUNT != null) {
AWSSecurityTokenService sts = getSecurityTokenService();
GetCallerIdentityRequest getCallerIdentityRequest = new GetCallerIdentityRequest();
GetCallerIdentityResult result = sts.getCallerIdentity(getCallerIdentityRequest);
if (!result.getAccount().equals(TARGET_ACCOUNT.replace("-", "").trim())) {
throw new RuntimeException(String.format("account: %s does not match target account: %s", result.getAccount(), TARGET_ACCOUNT));
}
}
}

protected AWSSecurityTokenService getSecurityTokenService() {
return AWSSecurityTokenServiceClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials.getAwsCredentials())).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.intuit.cloudraider.commons;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityRequest;
import com.amazonaws.services.securitytoken.model.GetCallerIdentityResult;
import com.intuit.cloudraider.model.BasicCredentials;
import com.intuit.cloudraider.model.Credentials;

import static org.mockito.Mockito.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BaseDelegatorTest {

public AWSSecurityTokenService sts = mock(AWSSecurityTokenService.class);

DelegatorBase<String> delegator;

@Test
public void testCorrectAccount() {
when(sts.getCallerIdentity((GetCallerIdentityRequest) notNull())).thenAnswer(new Answer<GetCallerIdentityResult>() {

@Override
public GetCallerIdentityResult answer(InvocationOnMock invocation) throws Throwable {
GetCallerIdentityResult result = new GetCallerIdentityResult();
result.setAccount("1234567890");
return result;
}

});

TestDelegator d = new TestDelegator(sts);


}


protected static class TestDelegator extends DelegatorBase<String> {

AWSSecurityTokenService sts;
public TestDelegator(AWSSecurityTokenService sts) {
this.sts = sts;
}

@Override
protected String buildClient(AWSCredentials creds, String region) {
// TODO Auto-generated method stub
return null;
}

@Override
protected AWSSecurityTokenService getSecurityTokenService() { return sts;}


}

/**
* The type Asg delegator test context configuration.
*/
@Configuration
protected static class ASGDelegatorTestContextConfiguration {

/**
* Asg delegator asg delegator.
*
* @return the asg delegator
*/
@Bean
public ASGDelegator asgDelegator() {
return new ASGDelegator();
}

/**
* Credentials credentials.
*
* @return the credentials
*/
@Bean
public Credentials credentials() {
return new BasicCredentials();
}

}

}