Skip to content

Commit

Permalink
[substitutor] enable to decode a base64 string
Browse files Browse the repository at this point in the history
  • Loading branch information
rmannibucau committed May 17, 2024
1 parent fae78e5 commit b120b7b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,16 @@ protected String doSubstitute(final AtomicReference<Substitutor> self, final Con
final var src = readResource(placeholder, "bundlebee-base64-file:");
return src == null ? null : Base64.getEncoder().encodeToString(src);
}
if (placeholder.startsWith("bundlebee-base64-decode-file:")) {
final var src = readResource(placeholder, "bundlebee-base64-decode-file:");
return src == null ? null : new String(Base64.getDecoder().decode(src), StandardCharsets.UTF_8);
}
if (placeholder.startsWith("bundlebee-base64:")) {
return Base64.getEncoder().encodeToString(placeholder.substring("bundlebee-base64:".length()).getBytes(StandardCharsets.UTF_8));
}
if (placeholder.startsWith("bundlebee-base64-decode:")) {
return new String(Base64.getDecoder().decode(placeholder.substring("bundlebee-base64-decode:".length()).getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
}
if (placeholder.startsWith("bundlebee-quote-escaped-inline-file:")) {
final var resource = readResource(placeholder, "bundlebee-quote-escaped-inline-file:");
return resource == null ? null : new String(resource, StandardCharsets.UTF_8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ void base64() {
substitutor.getOrDefault("bundlebee-base64:content", "failed"));
}

@Test
void debase64() {
assertEquals(
"content",
substitutor.getOrDefault("bundlebee-base64-decode:" + Base64.getEncoder().encodeToString("content".getBytes(StandardCharsets.UTF_8)), "failed"));
}

@Test
void base64File(@TempDir final Path root) throws IOException {
final var file = root.resolve("test.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ Placeholders can use some keywords to get some particular values:
- `bundlebee-indent:<indent size>:<value>`: indents a value with the provided space size, it is generally combined with another interpolation (file ones in particular) as value, ex `{{bundlebee-indent:8:{{bundlebee-inline-file:myfile.txt}}}}`,
- `bundlebee-inline-file:<file path>`: load the file content as value,
- `bundlebee-base64-file:<file path>`: converts the file in base64 (useful to write `data:xxxx,<base64>` values for ex keeping the raw file in the filesystem, very helpful for images),
- `bundlebee-base64-decode-file:<file path>`: decode the file from a base64 content,
- `bundlebee-base64:<text>`: encodes in base64 the text,
- `bundlebee-base64-decode:<text>`: decode from base64 to text the value `text`,
- `bundlebee-digest:BASE64|HEX,<algorithm>,<text>`: computes the digest of the text encoded in base64 or hexa format (useful to read files like `ConfigMap` or `Secret` and inject their digest value in a `Deployment` annotations to force its reload for example),
- `bundlebee-quote-escaped-inline-file:<file path>`: load the file content as a quoted value,
- `bundlebee-json-inline-file:<file path>`: load the file content as a JSON string value - without quotes,
Expand Down

0 comments on commit b120b7b

Please sign in to comment.