Created
December 31, 2024 05:01
-
-
Save yammerjp/4223fbb3497fee8bb7b83866d1a72dd4 to your computer and use it in GitHub Desktop.
*.envの内容を1passwordに登録し、Secretのテンプレートファイルを生成
This file contains hidden or 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
| # Usage: | |
| # $ gawk -f env2optpl.gawk namespace.name.env > name-secret.yaml.1password.tpl | |
| # $ op inject - name-secret.yaml.1password.tpl > name-secret.yaml | |
| # Example: namespace.name.env | |
| # SECRET_KEY=SECRET_VALUE | |
| # SECRET_KEY2="DOUBLE_QUOTED_VALUE" | |
| # SECRET_KEY3="VALUE MUST NOT INCLUDE EQUAL CHARACTER" | |
| BEGIN{ | |
| FS="=" | |
| } | |
| FNR==1 { | |
| basename = FILENAME | |
| gsub(/.*\//, "", basename); | |
| dirname = FILENAME | |
| gsub(/[^\/]+$/, "", dirname); | |
| if (dirname == "") { | |
| dirname = "." | |
| } | |
| split(basename, splitted, "."); | |
| if (splitted[2] == "") { | |
| namespace = "default" | |
| name = splitted[1] | |
| } else { | |
| namespace = splitted[1] | |
| name = splitted[2] | |
| } | |
| vaultName = ENVIRON["OP_VAULT"] | |
| content = sprintf("apiVersion: v1\n"); | |
| content = content sprintf("kind: Secret\n"); | |
| content = content sprintf("metadata:\n"); | |
| content = content sprintf("name: %s\n", name); | |
| content = content sprintf(" namespace: %s\n", namespace); | |
| content = content sprintf("type: Opaque\n"); | |
| content = content sprintf("stringData:\n"); | |
| command = sprintf("op item create --category Password --vault %s --title %s.%s", vaultName, namespace, name) | |
| } | |
| { | |
| key = $1; | |
| value = $2; | |
| gsub(/^"/, "", value); | |
| gsub(/"$/, "", value); | |
| command = command sprintf(" '%s=%s'", key, value) | |
| content = content sprintf(" %s: \"{{ op://%s/%s.%s/%s }}\"\n", key, vaultName, namespace, name, key); | |
| } | |
| END { | |
| tplFile = sprintf("%s/%s-secret.yaml.1password.tpl", dirname, name) | |
| printf "env2optpl.gawk: exec: %s\n", command | |
| printf "env2optpl.gawk: exit-code: %s\n", system(command) | |
| printf "env2optpl.gawk: build template file: %s\n", tplFile | |
| printf "%s", content > tplFile | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment