-
Notifications
You must be signed in to change notification settings - Fork 68
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 support for templating in init-container #204
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
139f888
make it easy to develop the init container
15a023d
use templates for outputs
740ef3b
add support for custom templates
000677e
fix the tests
73084b6
added tests for the custom template
34057e3
fix the tests
bcef512
fix the build
66e1807
version bump
5b9e206
fix the build
ab8d9cd
explain arguments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/** |
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,47 @@ | ||
# Running the init container locally | ||
|
||
1. Make sure you have a working Kubernetes cluster (run `kubectl get pods` to check). | ||
2. Start by running the decryptor API - run the following in the decryptor folder (`src/decrypt-api`): | ||
``` | ||
dotnet run | ||
``` | ||
3. Run the following command to get a valid service account token: | ||
``` | ||
kubectl get secret $(kubectl get sa default -o jsonpath='{.secrets[0].name}') -o jsonpath='{.data.token}' | base64 -D > token.txt | ||
``` | ||
4. Set the following env var for the init container to work correctly: | ||
``` | ||
export set TOKEN_FILE_PATH=$(pwd)/token.txt | ||
export set KAMUS_URL=http://localhost:5000 | ||
``` | ||
5. Now you can run the init container: | ||
``` | ||
node index.js -e encrypted -d decrypted -n output.json | ||
``` | ||
|
||
The first argument (`-e`) point to the folder with the encrypted files, the second (`-d`) point to the folder that will contain the decrypted items and (`-n`) is the decrypted file name. | ||
|
||
And you should see a file name `output.json` created under `decrypted` folder with the decrypted content. | ||
Now you can run the init continer locally, debug it and add features. | ||
|
||
Having more questions? Something unclear? Reach out to us via [Slack](https://join.slack.com/t/k8s-kamus/shared_invite/enQtNTQwMjc2MzIxMTM3LTgyYTcwMTUxZjJhN2JiMTljMjNmOTBmYjEyNWNmZTRiNjVhNTUyYjMwZDQ0YWQ3Y2FmMTBlODA5MzFlYjYyNWE) or [file an issue](/~https://github.com/Soluto/kamus/issues/new) | ||
|
||
## Tests | ||
The tests are using [WireMock](http://wiremock.org/) to mock the decryptor api (you can find ore about it [here](https://www.omerlh.info/2019/02/06/wiremock-for-fun-and-mocking/)). | ||
Running the tests is simple: | ||
1. Build the init container docker image: | ||
``` | ||
yarn run build | ||
``` | ||
2. Set the environment variable so the tests will use the local image: | ||
``` | ||
export set INIT_CONTAINER_IMAGE=init-container | ||
``` | ||
3. Run the tests | ||
``` | ||
yarn run test | ||
``` | ||
|
||
Repeat steps 1 & 3 each time you change the code. | ||
|
||
The tests simply take various parameters and compare the generated file from the init container with the expected files. |
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 |
---|---|---|
|
@@ -28,4 +28,36 @@ The init container accept the following environmenmt variables: | |
| `-e/--encrypted-folders <path>` | true | Encrypted files folder paths, comma seperated (the volumes mounted with the config map) | | | ||
| `-d/--decrypted-path <path>` | false | Decrypted file/s folder path mounted. Pass this argument to create one decrypted file per encrypted secret | | | ||
| `-n/--decrypted-file-name <name>` | false | Decrypted file name. Pass this argument to create one configuration file with the encrypted secrets. | | | ||
| `-f/--output-format <format>` | false | The format of the output file. Supported types: json, cfg, cfg-strict (surround strings with quotation marks), files | JSON | | ||
| `-f/--output-format <format>` | false | The format of the output file. Supported types: json, cfg, cfg-strict (surround strings with quotation marks), files, custom (see above for more bellow) | JSON | | ||
|
||
## Custom templating support | ||
In case you need something more complicated than the support output format, you can provide your own template. | ||
The init container support [EJS](http://ejs.co/) templates, a powerful template engine for nodejs. | ||
To use it, provide a key in the supplied config map called "template.ejs": | ||
```yaml | ||
apiVersion: v1 | ||
kind: ConfigMap | ||
metadata: | ||
name: encrypted-secrets-cm | ||
data: | ||
key: 4AD7lM6lc4dGvE3oF+5w8g==:WrcckiNNOAlMhuWHaM0kTw== | ||
template.ejs: | | ||
<%= secrets["key"] %> | ||
hello | ||
``` | ||
|
||
This will result in the following file created by the init container: | ||
``` | ||
<decrypted value> | ||
hello | ||
``` | ||
|
||
Look on EJS docummentation for more details, or on one of the existing [templates](/~https://github.com/Soluto/kamus/tree/master/init-container/templates) for ideas on how you can use it. The template input is: | ||
``` | ||
{ | ||
"secrets": [] //array of the decretyped items, key value pairs. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unclear what is |
||
"stringifyIfJson": function //apply JSON.stringify if the value is object. | ||
} | ||
``` | ||
|
||
Because the init container support multiple config maps, you can create shared template using config map and mount them where needed. Have a common template? we'll appreciate PRs! |
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 @@ | ||
ezBR+Ew+Itwg6fA/tQjxzg==:/DH+kSV3UN8eRUxT/cJp5w== |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<%_ Object.keys(secrets).forEach(function(key){ -%> | ||
<%_ if(typeof(secrets[key]) === "string") { -%> | ||
<%= key %>="<%- secrets[key] %>" | ||
<%_ } else { -%> | ||
<%= key %>=<%- stringifyIfJson(secrets[key]) %> | ||
<%_ } -%> | ||
<%_ }); -%> |
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,3 @@ | ||
<%_ Object.keys(secrets).forEach(function(key){ -%> | ||
<%= key %>=<%- stringifyIfJson(secrets[key]) %> | ||
<%_ }); -%> |
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,9 @@ | ||
{ | ||
<%_ Object.keys(secrets).forEach(function(key, index){ -%> | ||
<%_ if(typeof(secrets[key]) === "object") { -%> | ||
"<%= key %>":<%- JSON.stringify(secrets[key]) -%> <%_ if(Object.keys(secrets).length - 1 > index) { -%> , <%_ } %> | ||
<%_ } else { -%> | ||
"<%= key %>":"<%- secrets[key] -%>" <%_ if(Object.keys(secrets).length - 1 > index) { -%> , <%_ } %> | ||
<%_ } -%> | ||
<%_ }); -%> | ||
} |
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,2 @@ | ||
super-secret-from-value | ||
hello |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
key.json={"secret_key":"secret_value"} | ||
key1="super-secret-from-value" | ||
key2="super-secret" | ||
key2="super-secret" |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
key.json={"secret_key":"secret_value"} | ||
key1=super-secret-from-value | ||
key2=super-secret | ||
key2=super-secret |
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
{"key.json":{"secret_key":"secret_value"},"key1":"super-secret-from-value","key2":"super-secret"} | ||
{ | ||
"key.json":{"secret_key":"secret_value"} , | ||
"key1":"super-secret-from-value" , | ||
"key2":"super-secret" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<%= secrets["key1"] %> | ||
hello |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain what is
encrypted
? is a folder? is it a file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's a folder - I put it there with some ready-made encrypted content that is ready for debugging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, it needs to be more clear in the MD.