mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
Thanks again, @danjee! This is a great addition to Floci. Your continued support really makes a difference. Hope to see you around soon! Closes #76
This commit is contained in:
1 parent
838016660c
commit
c57d9ebcf8
2 files changed
+245
No files matched your search
+118
@@ -11,6 +11,7 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -42,6 +43,7 @@ public class SecretsManagerJsonHandler {
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
case "ListSecretVersionIds" -> handleListSecretVersionIds(request, region);
|
||||
case "GetResourcePolicy" -> handleGetResourcePolicy(request, region);
|
||||
case "GetRandomPassword" -> handleGetRandomPassword(request, region);
|
||||
case "DeleteResourcePolicy" -> Response.ok(objectMapper.createObjectNode()).build();
|
||||
case "PutResourcePolicy" -> Response.ok(objectMapper.createObjectNode()).build();
|
||||
default -> Response.status(400)
|
||||
@@ -301,6 +303,122 @@ public class SecretsManagerJsonHandler {
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a random password.
|
||||
* <p>
|
||||
* By default uses uppercase and lowercase letters, numbers, and the following special characters:
|
||||
* {@code !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~}
|
||||
*
|
||||
* @param request JSON request body with the following optional fields:
|
||||
* <ul>
|
||||
* <li>{@code PasswordLength} (Long) – Length of the password. Default: 32. Min: 1, Max: 4096.</li>
|
||||
* <li>{@code ExcludeCharacters} (String) – Characters to exclude from the password. Max length: 4096.</li>
|
||||
* <li>{@code ExcludeLowercase} (Boolean) – Exclude lowercase letters.</li>
|
||||
* <li>{@code ExcludeUppercase} (Boolean) – Exclude uppercase letters.</li>
|
||||
* <li>{@code ExcludeNumbers} (Boolean) – Exclude numbers.</li>
|
||||
* <li>{@code ExcludePunctuation} (Boolean) – Exclude punctuation characters.</li>
|
||||
* <li>{@code IncludeSpace} (Boolean) – Include the space character.</li>
|
||||
* <li>{@code RequireEachIncludedType} (Boolean) – Require at least one character from each included type. Default: true.</li>
|
||||
* </ul>
|
||||
* @param region AWS region (unused for this operation)
|
||||
* @return response containing {@code RandomPassword} string
|
||||
* @see <a href="https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetRandomPassword.html">AWS Secrets Manager – GetRandomPassword</a>
|
||||
*/
|
||||
private Response handleGetRandomPassword(JsonNode request, String region) {
|
||||
|
||||
var excludeCharacters = request.get("ExcludeCharacters");
|
||||
var excludeLowerCase = request.get("ExcludeLowercase");
|
||||
var excludeNumbers = request.get("ExcludeNumbers");
|
||||
var excludePunctuation = request.get("ExcludePunctuation");
|
||||
var excludeUpperCase = request.get("ExcludeUppercase");
|
||||
var includeSpace = request.get("IncludeSpace");
|
||||
var passwordLength = request.get("PasswordLength");
|
||||
var requireEachIncludedType = request.get("RequireEachIncludedType");
|
||||
|
||||
int length = (passwordLength != null && !passwordLength.isNull()) ? passwordLength.asInt(32) : 32;
|
||||
if (length < 1 || length > 4096) {
|
||||
return Response.status(400)
|
||||
.entity(new AwsErrorResponse("InvalidParameterException", "PasswordLength must be between 1 and 4096."))
|
||||
.build();
|
||||
}
|
||||
|
||||
StringBuilder charset = new StringBuilder();
|
||||
if (excludeLowerCase == null || excludeLowerCase.isNull() || !excludeLowerCase.asBoolean()) {
|
||||
charset.append("abcdefghijklmnopqrstuvwxyz");
|
||||
}
|
||||
if (excludeUpperCase == null || excludeUpperCase.isNull() || !excludeUpperCase.asBoolean()) {
|
||||
charset.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
}
|
||||
if (excludeNumbers == null || excludeNumbers.isNull() || !excludeNumbers.asBoolean()) {
|
||||
charset.append("0123456789");
|
||||
}
|
||||
if (excludePunctuation == null || excludePunctuation.isNull() || !excludePunctuation.asBoolean()) {
|
||||
charset.append("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
|
||||
}
|
||||
if (includeSpace != null && !includeSpace.isNull() && includeSpace.asBoolean()) {
|
||||
charset.append(" ");
|
||||
}
|
||||
if (excludeCharacters != null && !excludeCharacters.isNull()) {
|
||||
String excluded = excludeCharacters.asText();
|
||||
for (int i = charset.length() - 1; i >= 0; i--) {
|
||||
if (excluded.indexOf(charset.charAt(i)) >= 0) {
|
||||
charset.deleteCharAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SecureRandom random = new SecureRandom();
|
||||
StringBuilder password = new StringBuilder(length);
|
||||
|
||||
boolean requireEach = requireEachIncludedType == null || requireEachIncludedType.isNull() || requireEachIncludedType.asBoolean();
|
||||
if (requireEach) {
|
||||
List<String> includedTypes = new ArrayList<>();
|
||||
if (excludeLowerCase == null || excludeLowerCase.isNull() || !excludeLowerCase.asBoolean()) {
|
||||
includedTypes.add("abcdefghijklmnopqrstuvwxyz");
|
||||
}
|
||||
if (excludeUpperCase == null || excludeUpperCase.isNull() || !excludeUpperCase.asBoolean()) {
|
||||
includedTypes.add("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
|
||||
}
|
||||
if (excludeNumbers == null || excludeNumbers.isNull() || !excludeNumbers.asBoolean()) {
|
||||
includedTypes.add("0123456789");
|
||||
}
|
||||
if (excludePunctuation == null || excludePunctuation.isNull() || !excludePunctuation.asBoolean()) {
|
||||
includedTypes.add("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
|
||||
}
|
||||
if (includeSpace != null && !includeSpace.isNull() && includeSpace.asBoolean()) {
|
||||
includedTypes.add(" ");
|
||||
}
|
||||
if (excludeCharacters != null && !excludeCharacters.isNull()) {
|
||||
String excluded = excludeCharacters.asText();
|
||||
includedTypes = includedTypes.stream()
|
||||
.map(t -> t.chars().filter(c -> excluded.indexOf(c) < 0)
|
||||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString())
|
||||
.filter(t -> !t.isEmpty())
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
}
|
||||
// Seed one char from each required type
|
||||
for (String type : includedTypes) {
|
||||
password.append(type.charAt(random.nextInt(type.length())));
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = password.length(); i < length; i++) {
|
||||
password.append(charset.charAt(random.nextInt(charset.length())));
|
||||
}
|
||||
|
||||
// Shuffle so required chars aren't always at the start
|
||||
for (int i = length - 1; i > 0; i--) {
|
||||
int j = random.nextInt(i + 1);
|
||||
char tmp = password.charAt(i);
|
||||
password.setCharAt(i, password.charAt(j));
|
||||
password.setCharAt(j, tmp);
|
||||
}
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("RandomPassword", password.toString());
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private List<Secret.Tag> parseTags(JsonNode request) {
|
||||
List<Secret.Tag> tags = new ArrayList<>();
|
||||
JsonNode tagsNode = request.path("Tags");
|
||||
|
||||
Reference in new issue
Block a user