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

Parameter validation for aws-wps requests #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 1 addition & 3 deletions dev.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
dockerImage=615645230945.dkr.ecr.ap-southeast-2.amazonaws.com/javaduck:latest
geoserver=http://geoserver-123.aodn.org.au/geoserver/imos/ows
s3LambdaCodeFilename=request-handler.zip
s3LambdaCodeURL=https://s3-ap-southeast-2.amazonaws.com/wps-lambda/request-handler-0.01-lambda-package.zip
templatesURL=https://raw.githubusercontent.com/aodn/aws-wps/master/wps-common/src/main/resources/config/templates.xml
administratorEmail=developers@emii.org.au
geonetworkCatalogueURL=http://catalogue-sandbox.aodn.org.au/geonetwork
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import au.org.aodn.aws.wps.AwsApiResponse.ResponseBuilder;
import au.org.aodn.aws.wps.RequestParser;
import au.org.aodn.aws.wps.RequestParserFactory;
import au.org.aodn.aws.wps.exception.ValidationException;
import au.org.aodn.aws.util.JobFileUtil;
import au.org.aodn.aws.wps.operation.Operation;
import com.amazonaws.services.lambda.runtime.Context;
Expand Down Expand Up @@ -79,6 +80,11 @@ public AwsApiResponse handleRequest(AwsApiRequest request, Context context) {
String exceptionReportString = JobFileUtil.getExceptionReportString(oe.getExceptionText(),
oe.getExceptionCode(), oe.getLocator());
responseBuilder.body(exceptionReportString);
} catch (ValidationException ve) {
LOGGER.error("Error in request parameters " + ve.getMessage(), ve);
responseBuilder.statusCode(500);
String exceptionReportString = JobFileUtil.getExceptionReportString(ve.getMessage(), "ValidationError");
responseBuilder.body(exceptionReportString);
} catch (Exception e) {
LOGGER.error("Exception : " + e.getMessage(), e);
responseBuilder.statusCode(500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import au.org.aodn.aws.util.EmailService;
import au.org.aodn.aws.util.JobFileUtil;
import au.org.aodn.aws.wps.request.ExecuteRequestHelper;
import au.org.aodn.aws.wps.exception.ValidationException;
import au.org.aodn.aws.wps.status.*;
import com.amazonaws.services.batch.AWSBatch;
import com.amazonaws.services.batch.AWSBatchClientBuilder;
Expand Down Expand Up @@ -30,7 +31,7 @@ public ExecuteOperation(Execute executeRequest) {


@Override
public String execute() {
public String execute() throws ValidationException {

// Config items:
// queue names
Expand All @@ -54,6 +55,7 @@ public String execute() {
LOGGER.info("awsRegion: " + awsRegion);

ExecuteRequestHelper helper = new ExecuteRequestHelper(executeRequest);
helper.validateInputs();
String email = helper.getEmail();

String processIdentifier = executeRequest.getIdentifier().getValue(); // code spaces not supported for the moment
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package au.org.aodn.aws.wps.operation;

import au.org.aodn.aws.exception.OGCException;
import au.org.aodn.aws.wps.exception.ValidationException;

public interface Operation {
String execute() throws OGCException;
String execute() throws OGCException, ValidationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package au.org.aodn.aws.wps.exception;

public class ValidationException extends Exception {

public ValidationException()
{
super();
}

public ValidationException(String message)
{
super(message);
}

public ValidationException(String message, Throwable ex)
{
super(message, ex);
}

public ValidationException(Throwable ex)
{
super(ex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import net.opengis.wps.v_1_0_0.InputType;

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

import au.org.aodn.aws.wps.exception.ValidationException;

public class ExecuteRequestHelper {
private final Execute request;
Expand Down Expand Up @@ -34,6 +38,36 @@ public boolean hasRequestedOutput(String identifier) {
return false;
}

public void validateInputs() throws ValidationException {
String layerName = getLiteralInputValue("layer");
String subset = getLiteralInputValue("subset");
String email = getEmail();

if (layerName == null) {
throw new ValidationException("Request must have a layer name");
}

if (subset == null) {
throw new ValidationException("Request must have a subset");
} else {
int latLonCount = 0;
Pattern latLonPattern = Pattern.compile("([+-]?\\d+\\.?\\d+)\\s*,\\s*([+-]?\\d+\\.?\\d+)");
Copy link
Contributor

@jonescc jonescc Jan 10, 2018

Choose a reason for hiding this comment

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

Should probably align better with aggregator validation https://github.com/aodn/aws-wps/blob/master/aggregation-worker/src/main/java/au/org/emii/geoserver/client/SubsetParameters.java#L40 - we haven't used regexes for some time - they were problematic

Copy link
Contributor

Choose a reason for hiding this comment

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

Even better if they used the same code so they couldn't get out of synch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a really good point. I'll amend that

Matcher matcher = latLonPattern.matcher(subset);

while (matcher.find()) {
latLonCount++;
}

if (latLonCount != 2) {
throw new ValidationException(String.format("Invalid latitude/longitude format for subset: %s", subset));
}
}

if (email == null) {
throw new ValidationException("Request must have a callback email");
}
}

public String getRequestedMimeType(String identifier) {
for (DocumentOutputDefinitionType output: getResponseFormOutputs()) {
if (output.getIdentifier().getValue().equals(identifier)) {
Expand Down