-
Notifications
You must be signed in to change notification settings - Fork 21
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
macdude357
wants to merge
1
commit into
intuit:master
from
macdude357:feature/AddAccountNumberVerification
Closed
Initial Commit #13
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
cloudraider-core/src/main/java/com/intuit/cloudraider/commons/DelegatorBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
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(); | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
cloudraider-core/src/test/java/com/intuit/cloudraider/commons/BaseDelegatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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