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

Pass region and AWS credentials with AWS API requests #92

Merged
merged 3 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -5,23 +5,25 @@
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.graylog.integrations.aws.service.KinesisService;
import org.graylog.integrations.aws.service.CloudWatchService;
import org.graylog.integrations.aws.resources.requests.AWSRequestImpl;
import org.graylog.integrations.aws.resources.requests.KinesisHealthCheckRequest;
import org.graylog.integrations.aws.resources.responses.AvailableAWSServiceSummmary;
import org.graylog.integrations.aws.resources.responses.KinesisHealthCheckResponse;
import org.graylog.integrations.aws.resources.responses.RegionResponse;
import org.graylog.integrations.aws.resources.responses.AvailableServiceResponse;
import org.graylog.integrations.aws.resources.responses.HealthCheckResponse;
import org.graylog.integrations.aws.resources.responses.LogGroupsResponse;
import org.graylog.integrations.aws.resources.responses.RegionsResponse;
import org.graylog.integrations.aws.resources.responses.StreamsResponse;
import org.graylog.integrations.aws.service.AWSService;
import org.graylog.integrations.aws.service.CloudWatchService;
import org.graylog.integrations.aws.service.KinesisService;
import org.graylog2.plugin.rest.PluginRestResource;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -55,36 +57,62 @@ public AWSResource(AWSService awsService, KinesisService kinesisService, CloudWa
@Timed
@Path("/regions")
@ApiOperation(value = "Get all available AWS regions")
public List<RegionResponse> getAwsRegions() {
public List<RegionsResponse> getAwsRegions() {
return awsService.getAvailableRegions();
}

@GET
@Timed
@Path("/availableServices")
danotorrey marked this conversation as resolved.
Show resolved Hide resolved
@ApiOperation(value = "Get all available AWS services")
public AvailableAWSServiceSummmary getAvailableServices() {
public AvailableServiceResponse getAvailableServices() {
return awsService.getAvailableServices();
}

// TODO: Rework to accept a form post body with credentials
@GET
/**
* Get all available AWS CloudWatch log groups names for the specified region.
*
* Example request:
* curl 'http://user:pass@localhost:9000/api/plugins/org.graylog.integrations/aws/cloudWatch/logGroups' \
* -X POST \
* -H 'X-Requested-By: XMLHttpRequest' \
* -H 'Content-Type: application/json' \
* -H 'Accept: application/json' \
* --data-binary '{
* "region": "us-east-1",
* "aws_access_key_id": "some-key",
* "aws_secret_access_key": "some-secret"
* }'
*/
@POST
@Timed
@Path("/cloudWatch/logGroups/{regionName}")
@Path("/cloudWatch/logGroups")
danotorrey marked this conversation as resolved.
Show resolved Hide resolved
@ApiOperation(value = "Get all available AWS CloudWatch log groups names for the specified region")
public List<String> getLogGroupNames(@ApiParam(name = "regionName", required = true)
@PathParam("regionName") String regionName) {
return cloudWatchService.getLogGroupNames(regionName);
public LogGroupsResponse getLogGroupNames(@ApiParam(name = "JSON body", required = true) @Valid @NotNull AWSRequestImpl awsRequest) {
return cloudWatchService.getLogGroupNames(awsRequest.region(), awsRequest.awsAccessKeyId(), awsRequest.awsSecretAccessKey());
}

// TODO: Rework to accept a form post body with credentials
@GET
/**
* Get all available Kinesis streams for the specified region.
*
* Example request:
* curl 'http://user:pass@localhost:9000/api/plugins/org.graylog.integrations/aws/kinesis/streams' \
* -X POST \
* -H 'X-Requested-By: XMLHttpRequest' \
* -H 'Content-Type: application/json' \
* -H 'Accept: application/json' \
* --data-binary '{
* "region": "us-east-1",
* "aws_access_key_id": "some-key",
* "aws_secret_access_key": "some-secret"
* }'
*/
@POST
@Timed
@Path("/kinesis/streams/{regionName}")
@ApiOperation(value = "Get all available AWS Kinesis streams for the specified region")
public List<String> getKinesisStreams(@ApiParam(name = "regionName", required = true)
@PathParam("regionName") String regionName) throws ExecutionException {
return kinesisService.getKinesisStreams(regionName, null, null);
@Path("/kinesis/streams")
@ApiOperation(value = "Get all available Kinesis streams for the specified region")
public StreamsResponse getKinesisStreams(@ApiParam(name = "JSON body", required = true) @Valid @NotNull AWSRequestImpl awsRequest) throws ExecutionException {
return kinesisService.getKinesisStreamNames(awsRequest.region(), awsRequest.awsAccessKeyId(), awsRequest.awsSecretAccessKey());
}

/**
Expand All @@ -93,29 +121,28 @@ public List<String> getKinesisStreams(@ApiParam(name = "regionName", required =
* Sample CURL command for executing this method. Use this to model the UI request.
* Note the --data-binary param that includes the put body JSON with region and AWS credentials.
*
* curl 'http://someuser:somepass@localhost:9000/api/plugins/org.graylog.integrations/aws/kinesis/healthCheck' \
* -X PUT \
* curl 'http://user:pass@localhost:9000/api/plugins/org.graylog.integrations/aws/kinesis/healthCheck' \
* -X POST \
* -H 'X-Requested-By: XMLHttpRequest' \
* -H 'Content-Type: application/json' \
* -H 'Accept: application/json' \
* --data-binary '{
* "region": "us-east-1",
* "aws_access_key_id": "some-key",
* "aws_secret_access_key": "some-secret",
* "stream_name": "a-stream",
* "log_group_name": "a-log-group"
* "region": "us-east-1",
* "aws_access_key_id": "some-key",
* "aws_secret_access_key": "some-secret",
* "stream_name": "a-stream",
* "log_group_name": "a-log-group"
* }'
*
*/
@PUT
@POST
@Timed
@Path("/kinesis/healthCheck")
@ApiOperation(
value = "Attempt to retrieve logs from the indicated AWS log group with the specified credentials.",
response = KinesisHealthCheckResponse.class
response = HealthCheckResponse.class
)
public Response kinesisHealthCheck(@ApiParam(name = "JSON body", required = true) @Valid @NotNull KinesisHealthCheckRequest heathCheckRequest) throws ExecutionException, IOException {
KinesisHealthCheckResponse response = kinesisService.healthCheck(heathCheckRequest);
HealthCheckResponse response = kinesisService.healthCheck(heathCheckRequest);
return Response.accepted().entity(response).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.graylog.integrations.aws.resources.requests;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.autovalue.WithBeanGetter;

/**
* A common implementation on AWSRequest, which can be used for any AWS request that just needs region and credentials.
*/
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class AWSRequestImpl implements AWSRequest {

@JsonProperty(REGION)
public abstract String region();

@JsonProperty(AWS_ACCESS_KEY_ID)
public abstract String awsAccessKeyId();

@JsonProperty(AWS_SECRET_ACCESS_KEY)
public abstract String awsSecretAccessKey();

@JsonCreator
public static AWSRequestImpl create(@JsonProperty(REGION) String region,
@JsonProperty(AWS_ACCESS_KEY_ID) String awsAccessKeyId,
@JsonProperty(AWS_SECRET_ACCESS_KEY) String awsSecretAccessKey) {
return new AutoValue_AWSRequestImpl(region, awsAccessKeyId, awsSecretAccessKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class AvailableAWSService {
public abstract class AvailableService {

private static final String NAME = "name";
private static final String DESCRIPTION = "description";
Expand All @@ -31,11 +31,11 @@ public abstract class AvailableAWSService {
@JsonProperty(LEARN_MORE_LINK)
public abstract String LearnMoreLink();

public static AvailableAWSService create(@JsonProperty(NAME) String name,
@JsonProperty(DESCRIPTION) String description,
@JsonProperty(POLICY) String policy,
@JsonProperty(HELPER_TEXT) String helperText,
@JsonProperty(LEARN_MORE_LINK) String LearnMoreLink) {
return new AutoValue_AvailableAWSService(name, description, policy, helperText, LearnMoreLink);
public static AvailableService create(@JsonProperty(NAME) String name,
@JsonProperty(DESCRIPTION) String description,
@JsonProperty(POLICY) String policy,
@JsonProperty(HELPER_TEXT) String helperText,
@JsonProperty(LEARN_MORE_LINK) String LearnMoreLink) {
return new AutoValue_AvailableService(name, description, policy, helperText, LearnMoreLink);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class AvailableAWSServiceSummmary {
public abstract class AvailableServiceResponse {

private static final String SERVICES = "services";
private static final String TOTAL = "total";

@JsonProperty(SERVICES)
public abstract List<AvailableAWSService> services();
public abstract List<AvailableService> services();

@JsonProperty(TOTAL)
public abstract long total();

public static AvailableAWSServiceSummmary create(@JsonProperty(SERVICES) List<AvailableAWSService> services,
@JsonProperty(TOTAL) long total) {
return new AutoValue_AvailableAWSServiceSummmary(services, total);
public static AvailableServiceResponse create(@JsonProperty(SERVICES) List<AvailableService> services,
@JsonProperty(TOTAL) long total) {
return new AutoValue_AvailableServiceResponse(services, total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class KinesisHealthCheckResponse {
public abstract class HealthCheckResponse {

private static final String SUCCESS = "success";
private static final String LOG_TYPE = "log_type";
Expand All @@ -36,10 +36,10 @@ public abstract class KinesisHealthCheckResponse {
@JsonProperty(MESSAGE_SUMMARY)
public abstract String messageSummary();

public static KinesisHealthCheckResponse create(@JsonProperty(SUCCESS) boolean success,
@JsonProperty(LOG_TYPE) String logType,
@JsonProperty(EXPLANATION) String explanation,
@JsonProperty(MESSAGE_SUMMARY) String messageSummary) {
return new AutoValue_KinesisHealthCheckResponse(success, logType, explanation, messageSummary);
public static HealthCheckResponse create(@JsonProperty(SUCCESS) boolean success,
@JsonProperty(LOG_TYPE) String logType,
@JsonProperty(EXPLANATION) String explanation,
@JsonProperty(MESSAGE_SUMMARY) String messageSummary) {
return new AutoValue_HealthCheckResponse(success, logType, explanation, messageSummary);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.graylog.integrations.aws.resources.responses;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.autovalue.WithBeanGetter;

import java.util.List;

@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class LogGroupsResponse {

private static final String LOG_GROUPS = "log_groups";
private static final String TOTAL = "total";

@JsonProperty(LOG_GROUPS)
public abstract List<String> logGroups();

@JsonProperty(TOTAL)
public abstract long total();

public static LogGroupsResponse create(@JsonProperty(LOG_GROUPS) List<String> logGroups,
@JsonProperty(TOTAL) long total) {
return new AutoValue_LogGroupsResponse(logGroups, total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class RegionResponse {
public abstract class RegionsResponse {

private static final String REGION_ID = "region_id";
private static final String REGION_DESCRIPTION = "region_description";
Expand All @@ -27,9 +27,9 @@ public abstract class RegionResponse {
@JsonProperty(DISPLAY_VALUE)
public abstract String displayValue();

public static RegionResponse create(@JsonProperty(REGION_ID) String regionId,
@JsonProperty(REGION_DESCRIPTION) String regionDescription,
@JsonProperty(DISPLAY_VALUE) String displayValue) {
return new AutoValue_RegionResponse(regionId, regionDescription, displayValue);
public static RegionsResponse create(@JsonProperty(REGION_ID) String regionId,
@JsonProperty(REGION_DESCRIPTION) String regionDescription,
@JsonProperty(DISPLAY_VALUE) String displayValue) {
return new AutoValue_RegionsResponse(regionId, regionDescription, displayValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.graylog.integrations.aws.resources.responses;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.graylog.autovalue.WithBeanGetter;

import java.util.List;

@JsonAutoDetect
@AutoValue
@WithBeanGetter
public abstract class StreamsResponse {

private static final String STREAMS = "streams";
private static final String TOTAL = "total";

@JsonProperty(STREAMS)
public abstract List<String> streams();

@JsonProperty(TOTAL)
public abstract long total();

public static StreamsResponse create(@JsonProperty(STREAMS) List<String> streams,
@JsonProperty(TOTAL) long total) {
return new AutoValue_StreamsResponse(streams, total);
}
}
Loading