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.
Add REST API endpoints for secrets (#662)
* WIP: Start implementing Secrets APIs Signed-off-by: Eamonn Mansour <[email protected]> * feat: Add GET /secrets and PUT /secrets/{secret-name} endpoints Signed-off-by: Eamonn Mansour <[email protected]> * feat: Update secrets endpoints in openapi spec Signed-off-by: Eamonn Mansour <[email protected]> * fix: Add secretName path parameter to /secrets/{secretName} endpoints Signed-off-by: Eamonn Mansour <[email protected]> * feat: PUT /secrets/{secretName} now supports changing secret types and updating existing secrets Signed-off-by: Eamonn Mansour <[email protected]> * fix: Throw error if unexpected fields are given to update secrets Signed-off-by: Eamonn Mansour <[email protected]> * review: Replaced secret ID with secret name Signed-off-by: Eamonn Mansour <[email protected]> * feat: Add description, lastUpdatedTime, and lastUpdatedBy to secrets Signed-off-by: Eamonn Mansour <[email protected]> * refactor: Separate validation from resource processors Signed-off-by: Eamonn Mansour <[email protected]> * fix: Retain secret description when updating a secret Signed-off-by: Eamonn Mansour <[email protected]> --------- Signed-off-by: Eamonn Mansour <[email protected]>
- Loading branch information
Showing
55 changed files
with
4,080 additions
and
368 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
46 changes: 46 additions & 0 deletions
46
...api.common/src/main/java/dev/galasa/framework/api/common/resources/AbstractValidator.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,46 @@ | ||
/* | ||
* Copyright contributors to the Galasa project | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package dev.galasa.framework.api.common.resources; | ||
|
||
/** | ||
* A base validator class that contains commonly-used validation methods | ||
*/ | ||
public abstract class AbstractValidator { | ||
|
||
/** | ||
* Checks whether a given string is in valid Latin-1 format (e.g. characters in the range 0 - 255) | ||
* | ||
* @param str the string to validate | ||
* @return true if the string is in valid Latin-1 format, or false otherwise | ||
*/ | ||
public boolean isLatin1(String str) { | ||
boolean isValidLatin1 = true; | ||
for (char i = 0; i < str.length(); i++) { | ||
if (str.charAt(i) > 255) { | ||
isValidLatin1 = false; | ||
break; | ||
} | ||
} | ||
return isValidLatin1; | ||
} | ||
|
||
/** | ||
* Checks whether a given string contains only alphanumeric characters, '-', and '_' | ||
* | ||
* @param str the string to validate | ||
* @return true if the string contains only alphanumeric characters, '-', and '_', or false otherwise | ||
*/ | ||
public boolean isAlphanumWithDashes(String str) { | ||
boolean isValid = true; | ||
for (char c : str.toCharArray()) { | ||
if (!Character.isLetterOrDigit(c) && c != '-' && c != '_') { | ||
isValid = false; | ||
break; | ||
} | ||
} | ||
return isValid; | ||
} | ||
} |
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
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
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.