Skip to content

Commit

Permalink
Added errorOnInvalidMacros query parameter to throw error in case of …
Browse files Browse the repository at this point in the history
…invalid oauth macro
  • Loading branch information
vikasrathee-cs committed Dec 17, 2024
1 parent d620d1c commit 5623990
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void putOAuthCredential(HttpServiceRequest request, HttpServiceResponder

if (response.getResponseCode() != 200) {
throw new OAuthServiceException(
HttpURLConnection.HTTP_INTERNAL_ERROR,
response.getResponseCode(),
"Request for refresh token did not return 200. Response code: "
+ response.getResponseCode()
+ " , response message: "
Expand Down Expand Up @@ -243,7 +243,7 @@ public void getOAuthCredential(HttpServiceRequest request, HttpServiceResponder

if (response.getResponseCode() != 200) {
throw new OAuthServiceException(
HttpURLConnection.HTTP_INTERNAL_ERROR,
response.getResponseCode(),
"Request for refresh token did not return 200. Response code: "
+ response.getResponseCode()
+ " , response message: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ public class RemoteValidationRequest {
private final String namespace;
//The original request string
private final String request;
private final boolean errorOnInvalidMacros;

public RemoteValidationRequest(String namespace, String request) {
public RemoteValidationRequest(String namespace, String request, boolean errorOnInvalidMacros) {
this.namespace = namespace;
this.request = request;
this.errorOnInvalidMacros = errorOnInvalidMacros;
}

public String getNamespace() {
Expand All @@ -37,4 +39,8 @@ public String getNamespace() {
public String getRequest() {
return request;
}

public boolean isErrorOnInvalidMacros() {
return errorOnInvalidMacros;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void run(RunnableTaskContext context) throws Exception {
RemoteValidationRequest remoteValidationRequest = GSON.fromJson(context.getParam(), RemoteValidationRequest.class);
String namespace = remoteValidationRequest.getNamespace();
String originalRequest = remoteValidationRequest.getRequest();
boolean errorOnInvalidMacros = remoteValidationRequest.isErrorOnInvalidMacros();
StageValidationRequest validationRequest;
try {
validationRequest = GSON.fromJson(originalRequest,
Expand Down Expand Up @@ -96,11 +97,14 @@ OAuthAccessTokenMacroEvaluator.FUNCTION_NAME, new OAuthAccessTokenMacroEvaluator
);
MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(new BasicArguments(arguments), evaluators,
DefaultMacroEvaluator.MAP_FUNCTIONS);
MacroParserOptions macroParserOptions = MacroParserOptions.builder()
.skipInvalidMacros()

MacroParserOptions.Builder macroParserOptionsBuilder = MacroParserOptions.builder()
.setEscaping(false)
.setFunctionWhitelist(evaluators.keySet())
.build();
.setFunctionWhitelist(evaluators.keySet());
if(!errorOnInvalidMacros){
macroParserOptionsBuilder.skipInvalidMacros();
}
MacroParserOptions macroParserOptions = macroParserOptionsBuilder.build();
Function<Map<String, String>, Map<String, String>> macroFn =
macroProperties -> systemAppContext
.evaluateMacros(namespace, macroProperties, macroEvaluator, macroParserOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@
import java.util.Collections;
import java.util.Map;
import java.util.function.Function;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

/**
* Handles validation logic for pipelines.
Expand All @@ -88,25 +90,27 @@ public void healthCheck(HttpServiceRequest request, HttpServiceResponder respond
@POST
@Path("v1/contexts/{context}/validations/stage")
public void validateStage(HttpServiceRequest request, HttpServiceResponder responder,
@PathParam("context") String namespace) throws IOException, AccessException {
@PathParam("context") String namespace,
@QueryParam("errorOnInvalidMacros") @DefaultValue("false") boolean errorOnInvalidMacros)
throws IOException, AccessException {
if (!getContext().getAdmin().namespaceExists(namespace)) {
responder.sendError(HttpURLConnection.HTTP_NOT_FOUND, String.format("Namespace '%s' does not exist", namespace));
return;
}

//Validate remotely if remote execution is enabled
if (getContext().isRemoteTaskEnabled()) {
validateRemotely(request, responder, namespace);
validateRemotely(request, responder, namespace, errorOnInvalidMacros);
return;
}
validateLocally(request, responder, namespace);
validateLocally(request, responder, namespace, errorOnInvalidMacros);
}

private void validateRemotely(HttpServiceRequest request, HttpServiceResponder responder,
String namespace) throws IOException {
String namespace, boolean errorOnInvalidMacros) throws IOException {
String validationRequestString = StandardCharsets.UTF_8.decode(request.getContent()).toString();
RemoteValidationRequest remoteValidationRequest =
new RemoteValidationRequest(namespace, validationRequestString);
new RemoteValidationRequest(namespace, validationRequestString, errorOnInvalidMacros);
RunnableTaskRequest runnableTaskRequest =
RunnableTaskRequest.getBuilder(RemoteValidationTask.class.getName())
.withParam(GSON.toJson(remoteValidationRequest)).withNamespace(namespace).build();
Expand All @@ -132,7 +136,7 @@ private int getExceptionCode(String exceptionClass, String exceptionMessage, Str
}

private void validateLocally(HttpServiceRequest request, HttpServiceResponder responder,
String namespace) throws IOException {
String namespace, boolean errorOnInvalidMacros) throws IOException {
StageValidationRequest validationRequest;
try {
validationRequest = GSON.fromJson(StandardCharsets.UTF_8.decode(request.getContent()).toString(),
Expand Down Expand Up @@ -169,11 +173,13 @@ OAuthAccessTokenMacroEvaluator.FUNCTION_NAME, new OAuthAccessTokenMacroEvaluator
);
MacroEvaluator macroEvaluator = new DefaultMacroEvaluator(new BasicArguments(arguments), evaluators,
DefaultMacroEvaluator.MAP_FUNCTIONS);
MacroParserOptions macroParserOptions = MacroParserOptions.builder()
.skipInvalidMacros()
MacroParserOptions.Builder macroParserOptionsBuilder = MacroParserOptions.builder()
.setEscaping(false)
.setFunctionWhitelist(evaluators.keySet())
.build();
.setFunctionWhitelist(evaluators.keySet());
if(!errorOnInvalidMacros){
macroParserOptionsBuilder.skipInvalidMacros();
}
MacroParserOptions macroParserOptions = macroParserOptionsBuilder.build();
Function<Map<String, String>, Map<String, String>> macroFn =
macroProperties -> getContext().evaluateMacros(namespace, macroProperties, macroEvaluator, macroParserOptions);
String validationResponse = GSON.toJson(ValidationUtils.validate(
Expand Down

0 comments on commit 5623990

Please sign in to comment.