merge-helm-values 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ergon-public
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ Metadata-Version: 2.4
2
+ Name: merge-helm-values
3
+ Version: 1.0.0
4
+ Summary: Load, decrypt (ansible-vault), and deep-merge Helm value YAML files.
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ License-File: LICENSE
8
+ Requires-Dist: ansible-vault
9
+ Requires-Dist: mergedeep
10
+ Requires-Dist: oyaml
11
+ Provides-Extra: test
12
+ Requires-Dist: pytest; extra == "test"
13
+ Dynamic: license-file
@@ -0,0 +1,150 @@
1
+ # merge-helm-values
2
+
3
+ Managing Helm values across multiple environments (development, integration, production) quickly becomes error-prone. Configuration is scattered, secrets end up duplicated or stored insecurely, and there is no single source of truth for what a deployment actually receives.
4
+
5
+ **merge-helm-values** solves this by giving you:
6
+
7
+ - **A structured, multi-environment value layout** — split configuration by concern (database, cache, frontend, ...) and by environment, with shared defaults that reduce duplication
8
+ - **Inline secret encryption** — sensitive values are encrypted with [ansible-vault](https://docs.ansible.com/ansible/latest/vault_guide/index.html) directly inside your YAML files, right next to the configuration they belong to. No separate secret files, no manual decryption steps
9
+ - **Deterministic deep merge** — folders and files are merged in a well-defined order (lexicographic within folders, folders left to right), producing a single, predictable values file for `helm upgrade`
10
+
11
+ The result is a reliable, auditable, and version-controllable source for all your Helm values — plain and secret — across all environments.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install merge-helm-values
17
+ ```
18
+
19
+ ## How it works
20
+
21
+ 1. Point the library at one or more folders containing YAML files
22
+ 2. Files are loaded in **lexicographic order** within each folder, folders are processed in the order given
23
+ 3. Values tagged with `!vault` are **decrypted inline** during YAML parsing
24
+ 4. All loaded dictionaries are **deep-merged** — later values override earlier ones
25
+
26
+ ## Encrypting values
27
+
28
+ Use `ansible-vault encrypt_string` to create inline encrypted values:
29
+
30
+ ```bash
31
+ # Encrypt a simple value
32
+ echo -n "my-secret-password" | ansible-vault encrypt_string --stdin-name password
33
+
34
+ # Output (paste this into your YAML file):
35
+ # password: !vault |
36
+ # $ANSIBLE_VAULT;1.1;AES256
37
+ # 6163326163...
38
+ ```
39
+
40
+ The `!vault` tag tells the loader to decrypt the value during parsing. Untagged values pass through unchanged.
41
+
42
+ ## Value file structure
43
+
44
+ Organize your Helm values into folders by concern — common defaults, environment-specific overrides, and secrets:
45
+
46
+ ```
47
+ values/
48
+ ├── common/ # shared defaults (loaded first)
49
+ │ ├── backend.yaml
50
+ │ ├── database.yaml
51
+ │ └── frontend.yaml
52
+ ├── development/ # env overrides + secrets
53
+ │ ├── backend.yaml
54
+ │ ├── database_vault.yaml # contains !vault encrypted values
55
+ │ └── frontend.yaml
56
+ └── production/
57
+ ├── backend.yaml
58
+ ├── database_vault.yaml
59
+ └── frontend.yaml
60
+ ```
61
+
62
+ Files are sorted lexicographically within each folder. A common convention is to suffix files containing secrets with `_vault`.
63
+
64
+ ## Inline vault example
65
+
66
+ A file mixing plain and encrypted values:
67
+
68
+ ```yaml
69
+ backend:
70
+ database:
71
+ host: "db.prod.example.com"
72
+ port: 5432
73
+ username: myapp
74
+ password: !vault |
75
+ $ANSIBLE_VAULT;1.1;AES256
76
+ 6163326163613032373733...
77
+ cache:
78
+ host: "redis.prod.internal"
79
+ password: !vault |
80
+ $ANSIBLE_VAULT;1.1;AES256
81
+ 3430613966353766366538...
82
+ ```
83
+
84
+ After decryption and merge, this produces a flat dictionary with all secrets resolved to their plaintext values.
85
+
86
+ ## Python API
87
+
88
+ ### `make_vault_loader(passphrase)`
89
+
90
+ Creates a YAML loader that decrypts `!vault` tagged values inline.
91
+
92
+ ```python
93
+ import oyaml as yaml
94
+ from merge_helm_values import make_vault_loader
95
+
96
+ loader = make_vault_loader("my-vault-passphrase")
97
+ with open("values.yaml") as f:
98
+ data = yaml.load(f, Loader=loader)
99
+ ```
100
+
101
+ ### `merge_value_folders(passphrase, folders)`
102
+
103
+ Loads and deep-merges all YAML/YML files from the given folders. Files within each folder are sorted lexicographically. Folders are processed in order — later folders override earlier ones.
104
+
105
+ ```python
106
+ from merge_helm_values import merge_value_folders
107
+
108
+ merged = merge_value_folders("my-vault-passphrase", [
109
+ "values/common",
110
+ "values/production",
111
+ ])
112
+ ```
113
+
114
+ ## CLI wrapper example
115
+
116
+ The library provides the building blocks. You write a thin CLI wrapper that handles passphrase resolution for your setup:
117
+
118
+ ```python
119
+ #!/usr/bin/env python3
120
+ import os
121
+ import sys
122
+ import oyaml as yaml
123
+ from merge_helm_values import merge_value_folders
124
+
125
+ passphrase = os.environ.get("ANSIBLE_VAULT_PASSWORD")
126
+ if not passphrase:
127
+ # resolve from your secret manager, e.g. 1Password, AWS SSM, etc.
128
+ raise SystemExit("Set ANSIBLE_VAULT_PASSWORD")
129
+
130
+ merged = merge_value_folders(passphrase, sys.argv[1:])
131
+ print(yaml.dump(merged))
132
+ ```
133
+
134
+ Use it in your deploy script:
135
+
136
+ ```bash
137
+ merge-values.py values/common/ values/$ENVIRONMENT/ > merged.yaml
138
+ helm upgrade --install myapp ./chart --values merged.yaml
139
+ ```
140
+
141
+ ## Merge behavior
142
+
143
+ - **Deep merge**: nested dictionaries are merged recursively, not replaced
144
+ - **Lexicographic order**: within a folder, `a.yaml` is loaded before `b.yaml`
145
+ - **Folder order**: folders are processed left to right — the last folder wins on conflicts
146
+ - **File extensions**: only `.yaml` and `.yml` files are loaded, everything else is ignored
147
+
148
+ ## License
149
+
150
+ MIT
@@ -0,0 +1,13 @@
1
+ Metadata-Version: 2.4
2
+ Name: merge-helm-values
3
+ Version: 1.0.0
4
+ Summary: Load, decrypt (ansible-vault), and deep-merge Helm value YAML files.
5
+ License-Expression: MIT
6
+ Requires-Python: >=3.10
7
+ License-File: LICENSE
8
+ Requires-Dist: ansible-vault
9
+ Requires-Dist: mergedeep
10
+ Requires-Dist: oyaml
11
+ Provides-Extra: test
12
+ Requires-Dist: pytest; extra == "test"
13
+ Dynamic: license-file
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ merge_helm_values.py
4
+ pyproject.toml
5
+ merge_helm_values.egg-info/PKG-INFO
6
+ merge_helm_values.egg-info/SOURCES.txt
7
+ merge_helm_values.egg-info/dependency_links.txt
8
+ merge_helm_values.egg-info/requires.txt
9
+ merge_helm_values.egg-info/top_level.txt
10
+ tests/test_merge_helm_values.py
@@ -0,0 +1,6 @@
1
+ ansible-vault
2
+ mergedeep
3
+ oyaml
4
+
5
+ [test]
6
+ pytest
@@ -0,0 +1 @@
1
+ merge_helm_values
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env python3
2
+ # Read and merge all values to a single value structure.
3
+ # Values tagged with !vault are decrypted inline using ansible-vault.
4
+ # Please note, the files in the folders are sorted lexicographically
5
+ # and are processed in this order.
6
+
7
+ import os.path
8
+ from os import listdir
9
+ from ansible_vault import Vault
10
+ import oyaml as yaml
11
+ from mergedeep import merge
12
+
13
+
14
+ def make_vault_loader(passphrase):
15
+ """Create a YAML loader that decrypts !vault tagged values inline."""
16
+ loader = type('VaultLoader', (yaml.SafeLoader,), {})
17
+
18
+ def vault_constructor(loader, node):
19
+ encrypted = loader.construct_scalar(node)
20
+ if not encrypted.startswith("$ANSIBLE_VAULT"):
21
+ encrypted = "$ANSIBLE_VAULT;1.1;AES256\n" + encrypted
22
+ vault = Vault(passphrase)
23
+ return vault.load_raw(encrypted.encode()).decode("utf-8")
24
+
25
+ loader.add_constructor('!vault', vault_constructor)
26
+ return loader
27
+
28
+
29
+ def merge_value_folders(passphrase, value_folders):
30
+ """Load and deep-merge all YAML files from the given folders (lexicographic order)."""
31
+ vault_loader = make_vault_loader(passphrase)
32
+
33
+ value_dics = []
34
+ for value_folder in value_folders:
35
+ for file in sorted(listdir(value_folder)):
36
+ if not (file.endswith(".yml") or file.endswith(".yaml")):
37
+ continue
38
+
39
+ values_path = os.path.join(value_folder, file)
40
+ with open(values_path) as fh:
41
+ content = yaml.load(fh, Loader=vault_loader)
42
+ value_dics.append(content)
43
+
44
+ return merge(*value_dics)
@@ -0,0 +1,21 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "merge-helm-values"
7
+ version = "1.0.0"
8
+ description = "Load, decrypt (ansible-vault), and deep-merge Helm value YAML files."
9
+ license = "MIT"
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "ansible-vault",
13
+ "mergedeep",
14
+ "oyaml",
15
+ ]
16
+
17
+ [project.optional-dependencies]
18
+ test = ["pytest"]
19
+
20
+ [tool.setuptools]
21
+ py-modules = ["merge_helm_values"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,164 @@
1
+ import os
2
+ import textwrap
3
+ import pytest
4
+ from ansible_vault import Vault
5
+ import oyaml as yaml
6
+ from mergedeep import merge
7
+
8
+ from merge_helm_values import make_vault_loader, merge_value_folders
9
+
10
+
11
+ VAULT_PASSPHRASE = "test-passphrase-for-unit-tests"
12
+ FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "fixtures")
13
+ ENCRYPTION_FIXTURES = os.path.join(FIXTURES_DIR, "encryption")
14
+ MERGE_FIXTURES = os.path.join(FIXTURES_DIR, "merge")
15
+
16
+
17
+ def encrypt_full(value: str) -> str:
18
+ """Encrypt a string and return the full vault blob including header."""
19
+ vault = Vault(VAULT_PASSPHRASE)
20
+ encrypted = vault.dump_raw(value.encode())
21
+ if isinstance(encrypted, bytes):
22
+ encrypted = encrypted.decode("utf-8")
23
+ return encrypted.strip()
24
+
25
+
26
+ def encrypt_no_header(value: str) -> str:
27
+ """Encrypt a string and return the ciphertext without the header line."""
28
+ full = encrypt_full(value)
29
+ lines = full.splitlines()
30
+ assert lines[0].startswith("$ANSIBLE_VAULT")
31
+ return "\n".join(lines[1:])
32
+
33
+
34
+ def indent_vault_block(ciphertext: str, spaces: int) -> str:
35
+ """Indent every line of a vault ciphertext block for YAML literal scalar embedding."""
36
+ prefix = " " * spaces
37
+ return "\n".join(prefix + line for line in ciphertext.splitlines())
38
+
39
+
40
+ def load_yaml(text: str, passphrase: str = VAULT_PASSPHRASE) -> dict:
41
+ loader = make_vault_loader(passphrase)
42
+ return yaml.load(text, Loader=loader)
43
+
44
+
45
+ class TestInlineVaultDecryption:
46
+ def test_single_inline_vault_value(self):
47
+ secret = "super-secret-key"
48
+ vault_block = indent_vault_block(encrypt_full(secret), 4)
49
+ doc = (
50
+ "backend:\n"
51
+ " accessKey: !vault |\n"
52
+ f"{vault_block}\n"
53
+ " bucket: plain-value\n"
54
+ )
55
+ result = load_yaml(doc)
56
+ assert result["backend"]["accessKey"] == secret
57
+ assert result["backend"]["bucket"] == "plain-value"
58
+
59
+ def test_multiple_inline_vault_values(self):
60
+ access = "my-access-key"
61
+ secret = "my-secret-key"
62
+ access_block = indent_vault_block(encrypt_full(access), 4)
63
+ secret_block = indent_vault_block(encrypt_full(secret), 4)
64
+ doc = (
65
+ "s3:\n"
66
+ " accessKey: !vault |\n"
67
+ f"{access_block}\n"
68
+ " secretKey: !vault |\n"
69
+ f"{secret_block}\n"
70
+ ' endpoint: "https://minio.example.com/"\n'
71
+ )
72
+ result = load_yaml(doc)
73
+ assert result["s3"]["accessKey"] == access
74
+ assert result["s3"]["secretKey"] == secret
75
+ assert result["s3"]["endpoint"] == "https://minio.example.com/"
76
+
77
+ def test_vault_without_header_is_prepended(self):
78
+ secret = "no-header-secret"
79
+ vault_block = indent_vault_block(encrypt_no_header(secret), 2)
80
+ doc = (
81
+ "key: !vault |\n"
82
+ f"{vault_block}\n"
83
+ )
84
+ result = load_yaml(doc)
85
+ assert result["key"] == secret
86
+
87
+ def test_no_vault_tags_passes_through(self):
88
+ doc = textwrap.dedent("""\
89
+ plain:
90
+ nested: value
91
+ number: 42
92
+ """)
93
+ result = load_yaml(doc)
94
+ assert result == {"plain": {"nested": "value", "number": 42}}
95
+
96
+ def test_wrong_passphrase_raises(self):
97
+ vault_block = indent_vault_block(encrypt_full("secret"), 2)
98
+ doc = (
99
+ "key: !vault |\n"
100
+ f"{vault_block}\n"
101
+ )
102
+ wrong_loader = make_vault_loader("wrong-passphrase")
103
+ with pytest.raises(Exception):
104
+ yaml.load(doc, Loader=wrong_loader)
105
+
106
+
107
+ class TestMergeWithFixtureFiles:
108
+ def test_deep_merge_plain_and_vault_files(self):
109
+ """Load fixture files from disk, merge them, and verify the result."""
110
+ plain_path = os.path.join(ENCRYPTION_FIXTURES, "plain_config.yaml")
111
+ vault_path = os.path.join(ENCRYPTION_FIXTURES, "vault_config.yaml")
112
+
113
+ plain_dict = yaml.safe_load(open(plain_path))
114
+
115
+ loader = make_vault_loader(VAULT_PASSPHRASE)
116
+ with open(vault_path) as fh:
117
+ vault_dict = yaml.load(fh, Loader=loader)
118
+
119
+ merged = merge(plain_dict, vault_dict)
120
+
121
+ assert merged["backend"]["storage"]["s3"]["endpoint"] == "https://minio.example.com/"
122
+ assert merged["backend"]["storage"]["s3"]["bucket"] == "my-app-dev"
123
+ assert merged["backend"]["storage"]["s3"]["region"] == "us-east-1"
124
+ assert merged["backend"]["storage"]["s3"]["accessKey"] == "SEMI_SECRET"
125
+ assert merged["backend"]["storage"]["s3"]["secretKey"] == "TOP_SECRET"
126
+
127
+ def test_vault_on_different_levels(self):
128
+ """Decrypt inline vault values at different nesting depths and compare to expected output."""
129
+ input_path = os.path.join(ENCRYPTION_FIXTURES, "on_different_levels_input.yaml")
130
+ expected_path = os.path.join(ENCRYPTION_FIXTURES, "on_different_levels_expected.yaml")
131
+
132
+ loader = make_vault_loader(VAULT_PASSPHRASE)
133
+ with open(input_path) as fh:
134
+ actual = yaml.load(fh, Loader=loader)
135
+
136
+ expected = yaml.safe_load(open(expected_path))
137
+
138
+ assert actual == expected
139
+
140
+
141
+ class TestMergeMultipleFiles:
142
+ def test_lexicographic_deep_merge(self):
143
+ """Merge multiple input files lexicographically and compare to expected output."""
144
+ input_dir = os.path.join(MERGE_FIXTURES, "test1", "input")
145
+ expected_path = os.path.join(MERGE_FIXTURES, "test1", "expected.yml")
146
+
147
+ actual = merge_value_folders(VAULT_PASSPHRASE, [input_dir])
148
+ expected = yaml.safe_load(open(expected_path))
149
+
150
+ assert actual == expected
151
+
152
+ def test_three_layer_merge_common_env_secrets(self):
153
+ """Simulate real deploy: common/ + environment/ + secrets/ folders merged in order."""
154
+ base = os.path.join(MERGE_FIXTURES, "test2", "input")
155
+ expected_path = os.path.join(MERGE_FIXTURES, "test2", "expected.yaml")
156
+
157
+ actual = merge_value_folders(VAULT_PASSPHRASE, [
158
+ os.path.join(base, "common"),
159
+ os.path.join(base, "environment"),
160
+ os.path.join(base, "secrets"),
161
+ ])
162
+ expected = yaml.safe_load(open(expected_path))
163
+
164
+ assert actual == expected