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

Add functionalities to solve logging issues found in pentest #302

Merged
merged 1 commit into from
Jun 4, 2024
Merged
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
7 changes: 6 additions & 1 deletion src/main/java/ee/buerokratt/ruuter/domain/DslInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
Expand Down Expand Up @@ -94,7 +95,11 @@ private void executeStep(String stepName, List<String> stepNames) {

if (getProperties().getStopInCaseOfException() != null && getProperties().getStopInCaseOfException()) {
Thread.currentThread().interrupt();
throw new StepExecutionException(name, e);
if (properties.getLogging().getPrintStackTrace() != null && properties.getLogging().getPrintStackTrace())
throw new StepExecutionException(name, e);
else {
log.error("%s: %s".formatted(name, e.getMessage()));
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/ee/buerokratt/ruuter/domain/Logging.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class Logging {
private Boolean displayResponseContent;

private Boolean meaningfulErrors;

private Boolean printStackTrace;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ee.buerokratt.ruuter.domain.steps;

import com.fasterxml.jackson.annotation.JsonAlias;
import ee.buerokratt.ruuter.domain.DslInstance;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -37,22 +38,23 @@ public String getType() {
return "declare";
}


public List<String> getAllowedBody() {
if (allowedBody == null) {
if (allowedBody == null && allowlist != null && allowlist.body != null) {
allowedBody = allowlist.body.stream().map(field -> field.getField()).toList();
}
return allowedBody;
}

public List<String> getAllowedHeader() {
if (allowedHeader == null) {
if (allowedHeader == null && allowlist != null && allowlist.header != null) {
allowedHeader = allowlist.header.stream().map(field -> field.getField()).toList();
}
return allowedHeader;
}

public List<String> getAllowedParams() {
if (allowedParams == null) {
if (allowedParams == null && allowlist != null && allowlist.params != null) {
allowedParams = allowlist.params.stream().map(field -> field.getField()).toList();
}
return allowedParams;
Expand All @@ -61,8 +63,8 @@ public List<String> getAllowedParams() {
@Getter
public class AllowList {
List<DslField> body;
@JsonAlias("headers")
List<DslField> header;
List<DslField> params;
}

}
21 changes: 21 additions & 0 deletions src/main/java/ee/buerokratt/ruuter/service/DslService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import ee.buerokratt.ruuter.domain.steps.DslStep;
import ee.buerokratt.ruuter.helper.*;
import ee.buerokratt.ruuter.helper.exception.LoadDslsException;
import ee.buerokratt.ruuter.service.exception.StepExecutionException;
import ee.buerokratt.ruuter.util.FileUtils;
import ee.buerokratt.ruuter.util.LoggingUtils;
import io.swagger.v3.oas.models.OpenAPI;
Expand Down Expand Up @@ -140,8 +141,13 @@ public DslInstance execute(String dslName, String requestType, Map<String, Objec

if (dsl.getDeclaration() != null) {
requestBody = filterFields(requestBody, dsl.getDeclaration().getAllowedBody());
if ("POST".equals(requestType.toUpperCase()))
checkFields(requestBody, dsl.getDeclaration().getAllowedBody());

requestHeaders = filterFields(requestHeaders, dsl.getDeclaration().getAllowedHeader());
requestQuery = filterFields(requestQuery, dsl.getDeclaration().getAllowedParams());
if ("GET".equals(requestType.toUpperCase()))
checkFields(requestQuery, dsl.getDeclaration().getAllowedBody());
}
log.debug("body after: "+ LoggingUtils.mapDeepToString(requestBody));
} else {
Expand Down Expand Up @@ -234,6 +240,21 @@ <V> Map<String, V> filterFields(Map<String, V> requestFields, List<String> allow
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
}

<V> void checkFields(Map<String, V> requestFields, List<String> requestedFields) {
requestedFields.forEach((field) -> {
if (!requestFields.containsKey(field)) {
String message = "Field missing: %s".formatted(field);
if (properties.getLogging().getPrintStackTrace() != null && properties.getLogging().getPrintStackTrace())
throw new StepExecutionException("declare", new Exception(message));
else {
log.error(message);
Thread.currentThread().interrupt();
}
}
}
);
}

public OpenAPI getOpenAPISpec() {
if (openApiBuilder == null)
throw new RuntimeException("OpenAPI spec not generated");
Expand Down