This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Separate validation from resource processors
Signed-off-by: Eamonn Mansour <[email protected]>
- Loading branch information
Showing
17 changed files
with
338 additions
and
208 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...mmon/src/main/java/dev/galasa/framework/api/common/resources/GalasaResourceValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package dev.galasa.framework.api.common.resources; | ||
|
||
import static dev.galasa.framework.api.common.ServletErrorMessage.*; | ||
import static dev.galasa.framework.api.common.resources.ResourceAction.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import com.google.gson.JsonObject; | ||
|
||
import dev.galasa.framework.api.common.IBeanValidator; | ||
import dev.galasa.framework.api.common.InternalServletException; | ||
import dev.galasa.framework.api.common.ServletError; | ||
|
||
/** | ||
* An abstract class containing the base methods used to validate Galasa resources. | ||
*/ | ||
public abstract class GalasaResourceValidator<T> extends AbstractValidator implements IBeanValidator<T> { | ||
|
||
public static final String DEFAULT_API_VERSION = "galasa-dev/v1alpha1"; | ||
|
||
protected List<String> validationErrors = new ArrayList<>(); | ||
protected ResourceAction action; | ||
|
||
public GalasaResourceValidator() {} | ||
|
||
public GalasaResourceValidator(ResourceAction action) { | ||
this.action = action; | ||
} | ||
|
||
public List<String> getValidationErrors() { | ||
return validationErrors; | ||
} | ||
|
||
private List<String> getRequiredResourceFields() { | ||
List<String> requiredFields = new ArrayList<>(); | ||
requiredFields.add("apiVersion"); | ||
requiredFields.add("metadata"); | ||
if (action != DELETE) { | ||
requiredFields.add("data"); | ||
} | ||
return requiredFields; | ||
} | ||
|
||
protected List<String> getMissingResourceFields(JsonObject resourceJson, List<String> requiredFields) { | ||
List<String> missingFields = new ArrayList<>(); | ||
for (String field : requiredFields) { | ||
if (!resourceJson.has(field)) { | ||
missingFields.add(field); | ||
} | ||
} | ||
return missingFields; | ||
} | ||
|
||
protected void checkResourceHasRequiredFields( | ||
JsonObject resourceJson, | ||
String expectedApiVersion | ||
) throws InternalServletException { | ||
List<String> requiredFields = getRequiredResourceFields(); | ||
List<String> missingFields = getMissingResourceFields(resourceJson, requiredFields); | ||
if (!missingFields.isEmpty()) { | ||
ServletError error = new ServletError(GAL5069_MISSING_REQUIRED_FIELDS, String.join(", ", missingFields)); | ||
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST); | ||
} | ||
|
||
String apiVersion = resourceJson.get("apiVersion").getAsString(); | ||
if (!apiVersion.equals(expectedApiVersion)) { | ||
ServletError error = new ServletError(GAL5027_UNSUPPORTED_API_VERSION, expectedApiVersion); | ||
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...k.api.common/src/main/java/dev/galasa/framework/api/common/resources/SecretValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package dev.galasa.framework.api.common.resources; | ||
|
||
import static dev.galasa.framework.api.common.ServletErrorMessage.*; | ||
|
||
import java.util.List; | ||
|
||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import dev.galasa.framework.api.common.InternalServletException; | ||
import dev.galasa.framework.api.common.ServletError; | ||
|
||
public abstract class SecretValidator<T> extends GalasaResourceValidator<T> { | ||
|
||
public static final List<String> SUPPORTED_ENCODING_SCHEMES = List.of("base64"); | ||
|
||
public SecretValidator() {} | ||
|
||
public SecretValidator(ResourceAction action) { | ||
super(action); | ||
} | ||
|
||
protected void validateSecretName(String secretName) throws InternalServletException { | ||
if (secretName == null || secretName.isBlank() || secretName.contains(".") || !isLatin1(secretName)) { | ||
ServletError error = new ServletError(GAL5092_INVALID_SECRET_NAME_PROVIDED); | ||
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST); | ||
} | ||
} | ||
|
||
protected void validateDescription(String description) throws InternalServletException { | ||
if (description != null && (description.isBlank() || !isLatin1(description))) { | ||
ServletError error = new ServletError(GAL5102_INVALID_SECRET_DESCRIPTION_PROVIDED); | ||
throw new InternalServletException(error, HttpServletResponse.SC_BAD_REQUEST); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.