Search in sources :

Example 1 with AWSSimpleSystemsManagementClient

use of com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient in project Gatekeeper by FINRAOS.

the class SsmService method executeSsm.

private Map<String, String> executeSsm(AWSEnvironment environment, List<String> instanceIds, String platform, Map<String, ArrayList<String>> parameters, GatekeeperSsmProperties.SsmDocument documentProperties) {
    AWSSimpleSystemsManagementClient ssmClient = awsSessionService.getSsmSession(environment);
    SendCommandRequest scr = new SendCommandRequest().withInstanceIds(instanceIds).withDocumentName(documentProperties.getDocumentName());
    for (String key : parameters.keySet()) {
        scr.addParametersEntry(key, parameters.get(key));
    }
    return waitForSsmCommand(ssmClient, ssmClient.sendCommand(scr).getCommand().getCommandId(), instanceIds.size(), documentProperties.getTimeout(), documentProperties.getWaitInterval());
}
Also used : AWSSimpleSystemsManagementClient(com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient)

Example 2 with AWSSimpleSystemsManagementClient

use of com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient in project Gatekeeper by FINRAOS.

the class SsmService method checkInstancesWithSsm.

public Map<String, String> checkInstancesWithSsm(AWSEnvironment environment, List<String> instanceIds) {
    Map<String, String> instanceStatuses = new HashMap<>();
    AWSSimpleSystemsManagementClient ssmClient = awsSessionService.getSsmSession(environment);
    for (int i = 0; i < instanceIds.size(); i += 50) {
        // since we're partitioning 50 at a time we need to make sure that we don't go over the index of the actual size of the set itself
        // if we do then we will use the upper value of the set.
        Integer upperBound = i + 50 < instanceIds.size() ? i + 50 : instanceIds.size();
        List<String> partitionedList = instanceIds.subList(i, upperBound);
        DescribeInstanceInformationRequest describeInstanceInformationRequest = new DescribeInstanceInformationRequest();
        InstanceInformationFilter filter = new InstanceInformationFilter();
        filter.setKey("InstanceIds");
        filter.setValueSet(partitionedList);
        List<InstanceInformationFilter> informationFilters = new ArrayList<>();
        informationFilters.add(filter);
        describeInstanceInformationRequest.setInstanceInformationFilterList(informationFilters);
        describeInstanceInformationRequest.setMaxResults(50);
        // make the initial call, SSM Chunks it up so we need to call it with tokens til the string returns empty.
        DescribeInstanceInformationResult describeInstanceInformationResult = ssmClient.describeInstanceInformation(describeInstanceInformationRequest);
        describeInstanceInformationResult.getInstanceInformationList().forEach(instance -> instanceStatuses.put(instance.getInstanceId(), instance.getPingStatus()));
        while (describeInstanceInformationResult.getNextToken() != null) {
            // get the next chunk of results
            describeInstanceInformationResult = ssmClient.describeInstanceInformation(describeInstanceInformationRequest.withNextToken(describeInstanceInformationResult.getNextToken()));
            describeInstanceInformationResult.getInstanceInformationList().forEach(instance -> instanceStatuses.put(instance.getInstanceId(), instance.getPingStatus()));
        }
    }
    return instanceStatuses;
}
Also used : AWSSimpleSystemsManagementClient(com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient)

Example 3 with AWSSimpleSystemsManagementClient

use of com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient in project Gatekeeper by FINRAOS.

the class AwsSessionService method getSsmSession.

public AWSSimpleSystemsManagementClient getSsmSession(AWSEnvironment environment) {
    BasicSessionCredentials creds = credentialCache.getUnchecked(environment);
    AWSSimpleSystemsManagementClient ssm = awsSessionFactory.createSsmSession(creds);
    ssm.setRegion(Region.getRegion(Regions.fromName(environment.getRegion())));
    return ssm;
}
Also used : BasicSessionCredentials(com.amazonaws.auth.BasicSessionCredentials) AWSSimpleSystemsManagementClient(com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient)

Example 4 with AWSSimpleSystemsManagementClient

use of com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient in project Gatekeeper by FINRAOS.

the class AwsSessionServiceTests method testGetSsmSession.

@Test
public void testGetSsmSession() {
    AWSSimpleSystemsManagementClient client = awsSessionService.getSsmSession(awsEnvironment);
    Assert.assertNotNull("Verify Ssm Session is fetched", client);
    Mockito.verify(awsSimpleSystemsManagementClient, times(1)).setRegion(com.amazonaws.regions.Region.getRegion(Regions.fromName(awsEnvironment.getRegion())));
}
Also used : AWSSimpleSystemsManagementClient(com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient) Test(org.junit.Test)

Aggregations

AWSSimpleSystemsManagementClient (com.amazonaws.services.simplesystemsmanagement.AWSSimpleSystemsManagementClient)4 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 Test (org.junit.Test)1