shopcloud_secrethub 2.19.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) 2020 Talk-Point GmbH
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,104 @@
1
+ Metadata-Version: 2.4
2
+ Name: shopcloud_secrethub
3
+ Version: 2.19.0
4
+ Summary: CLI tool for the Shopcloud SecretHub
5
+ Author-email: Konstantin Stoldt <konstantin.stoldt@talk-point.de>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Talk-Point/shopcloud-secrethub-cli
8
+ Keywords: CLI
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE.txt
12
+ Requires-Dist: requests>=2.28
13
+ Dynamic: license-file
14
+
15
+ # Shopcloud SecretHub CLI
16
+
17
+ The SecretHub CLI provides the command-line interface to interact with the SecretHub API.
18
+
19
+ ## install
20
+
21
+ ```sh
22
+ pip install shopcloud_secrethub
23
+ ```
24
+
25
+ Or with uv:
26
+
27
+ ```sh
28
+ uv add shopcloud_secrethub
29
+ ```
30
+
31
+ ### Usage
32
+
33
+
34
+ __Reading and writing secrets:__
35
+
36
+ ```sh
37
+ $ secrethub auth
38
+ $ secrethub read <secret-name>
39
+ $ secrethub write <secret-name> <value>
40
+ ```
41
+
42
+
43
+ __Provisioning your applications with secrets:__
44
+
45
+ Provision a template file
46
+
47
+ ```sh
48
+ $ secrethub inject -i app.temp.yaml -o app.yaml
49
+
50
+ # app.temp.yaml
51
+ env_variables:
52
+ ENV: {{ talk-point/test-repo/env }}
53
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
54
+
55
+ ```
56
+
57
+ Provision to the environment
58
+
59
+ ```sh
60
+ $ eval `secrethub printenv -i app.temp.yaml`
61
+
62
+ # app.temp.yaml
63
+ env_variables:
64
+ ENV: {{ talk-point/test-repo/env }}
65
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
66
+
67
+ ```
68
+
69
+ __in Code:__
70
+
71
+ ```py
72
+ from shopcloud_secrethub import SecretHub
73
+ hub = SecretHub(user_app="test-script", api_token='<TOKEN>')
74
+ hub.read('talk-point/test-repo/secret_key')
75
+ ```
76
+
77
+ ### Contributing
78
+
79
+ Install uv if you don't have it:
80
+
81
+ ```sh
82
+ curl -LsSf https://astral.sh/uv/install.sh | sh
83
+ ```
84
+
85
+ Set up the project:
86
+
87
+ ```sh
88
+ uv sync
89
+ ```
90
+
91
+ Run tests:
92
+
93
+ ```sh
94
+ uv run pytest
95
+ ```
96
+
97
+ ### Deploy to PyPI
98
+
99
+ Deployment is handled automatically by CI when pushing to `master`. To build and publish manually:
100
+
101
+ ```sh
102
+ uv build
103
+ uv publish
104
+ ```
@@ -0,0 +1,90 @@
1
+ # Shopcloud SecretHub CLI
2
+
3
+ The SecretHub CLI provides the command-line interface to interact with the SecretHub API.
4
+
5
+ ## install
6
+
7
+ ```sh
8
+ pip install shopcloud_secrethub
9
+ ```
10
+
11
+ Or with uv:
12
+
13
+ ```sh
14
+ uv add shopcloud_secrethub
15
+ ```
16
+
17
+ ### Usage
18
+
19
+
20
+ __Reading and writing secrets:__
21
+
22
+ ```sh
23
+ $ secrethub auth
24
+ $ secrethub read <secret-name>
25
+ $ secrethub write <secret-name> <value>
26
+ ```
27
+
28
+
29
+ __Provisioning your applications with secrets:__
30
+
31
+ Provision a template file
32
+
33
+ ```sh
34
+ $ secrethub inject -i app.temp.yaml -o app.yaml
35
+
36
+ # app.temp.yaml
37
+ env_variables:
38
+ ENV: {{ talk-point/test-repo/env }}
39
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
40
+
41
+ ```
42
+
43
+ Provision to the environment
44
+
45
+ ```sh
46
+ $ eval `secrethub printenv -i app.temp.yaml`
47
+
48
+ # app.temp.yaml
49
+ env_variables:
50
+ ENV: {{ talk-point/test-repo/env }}
51
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
52
+
53
+ ```
54
+
55
+ __in Code:__
56
+
57
+ ```py
58
+ from shopcloud_secrethub import SecretHub
59
+ hub = SecretHub(user_app="test-script", api_token='<TOKEN>')
60
+ hub.read('talk-point/test-repo/secret_key')
61
+ ```
62
+
63
+ ### Contributing
64
+
65
+ Install uv if you don't have it:
66
+
67
+ ```sh
68
+ curl -LsSf https://astral.sh/uv/install.sh | sh
69
+ ```
70
+
71
+ Set up the project:
72
+
73
+ ```sh
74
+ uv sync
75
+ ```
76
+
77
+ Run tests:
78
+
79
+ ```sh
80
+ uv run pytest
81
+ ```
82
+
83
+ ### Deploy to PyPI
84
+
85
+ Deployment is handled automatically by CI when pushing to `master`. To build and publish manually:
86
+
87
+ ```sh
88
+ uv build
89
+ uv publish
90
+ ```
@@ -0,0 +1,40 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "shopcloud_secrethub"
7
+ version = "2.19.0"
8
+ description = "CLI tool for the Shopcloud SecretHub"
9
+ readme = "README.md"
10
+ license = { text = "MIT" }
11
+ authors = [{ name = "Konstantin Stoldt", email = "konstantin.stoldt@talk-point.de" }]
12
+ keywords = ["CLI"]
13
+ requires-python = ">=3.11"
14
+ dependencies = ["requests>=2.28"]
15
+
16
+ [project.urls]
17
+ Homepage = "https://github.com/Talk-Point/shopcloud-secrethub-cli"
18
+
19
+ [project.scripts]
20
+ secrethub = "shopcloud_secrethub.__main__:main"
21
+
22
+ [dependency-groups]
23
+ dev = ["pytest>=8"]
24
+
25
+ [tool.ruff]
26
+ select = [
27
+ "E", # pycodestyle errors
28
+ "W", # pycodestyle warnings
29
+ "F", # pyflakes
30
+ "I", # isort
31
+ "UP", # pyupgrade
32
+ "C", # flake8-comprehensions
33
+ "B", # flake8-bugbear
34
+ ]
35
+ ignore = [
36
+ "E501", # line too long
37
+ "B008", # do not perform function calls in argument defaults
38
+ "F401",
39
+ "C901", # too complex
40
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ from .app import * # noqa: F403
@@ -0,0 +1,54 @@
1
+ import argparse
2
+ import sys
3
+
4
+ from . import cli
5
+
6
+
7
+ def main():
8
+ parser = argparse.ArgumentParser(
9
+ description='SecretHub',
10
+ prog='shopcloud-secrethub'
11
+ )
12
+
13
+ subparsers = parser.add_subparsers(help='commands', title='commands')
14
+
15
+ parser_auth = subparsers.add_parser('auth', help='generate auth file')
16
+ parser_auth.set_defaults(which='auth')
17
+
18
+ parser_auth = subparsers.add_parser('token-refresh', help='generate new token')
19
+ parser_auth.set_defaults(which='token-refresh')
20
+
21
+ parser_read = subparsers.add_parser('read', help='read a secret')
22
+ parser_read.add_argument('name', type=str, help='secret name')
23
+ parser_read.add_argument('--output', '-o', help='Output Format', choices=['text', 'json', 'raw'])
24
+ parser_read.set_defaults(which='read')
25
+
26
+ parser_write = subparsers.add_parser('write', help='write a secret')
27
+ parser_write.add_argument('name', type=str, help='secret name')
28
+ parser_write.add_argument('value', type=str, help='secret value', nargs='?')
29
+ parser_write.add_argument(
30
+ '--in-file',
31
+ '-i',
32
+ nargs='?',
33
+ help='Use the contents of this file as the value of the secret.'
34
+ )
35
+ parser_write.set_defaults(which='write')
36
+
37
+ parser_inject = subparsers.add_parser('inject', help='inject the secrets')
38
+ parser_inject.add_argument('-i', required=True)
39
+ parser_inject.add_argument('-o', required=True)
40
+ parser_inject.set_defaults(which='inject')
41
+
42
+ parser_printenv = subparsers.add_parser('printenv', help='print as env export')
43
+ parser_printenv.add_argument('-i', required=True)
44
+ parser_printenv.set_defaults(which='printenv')
45
+
46
+ args = parser.parse_args()
47
+ if len(sys.argv) == 1:
48
+ parser.print_help(sys.stderr)
49
+ sys.exit(1)
50
+ cli.main(args)
51
+
52
+
53
+ if __name__ == '__main__':
54
+ main()
@@ -0,0 +1,145 @@
1
+ import configparser
2
+ import os
3
+ from typing import List
4
+
5
+ import requests
6
+
7
+ from . import exceptions
8
+
9
+
10
+ class ConfigFile:
11
+ def __init__(self, path, **kwargs):
12
+ self.path = path
13
+ self._token = kwargs.get('api_token')
14
+
15
+ if self._token is None:
16
+ self._load_from_env()
17
+ if self._token is None:
18
+ self._load_from_config()
19
+
20
+ def _load_from_env(self):
21
+ token = os.environ.get('SECRETHUB_TOKEN')
22
+ if token is not None:
23
+ self._token = token
24
+
25
+ def _load_from_config(self):
26
+ config = configparser.ConfigParser()
27
+ config.read(self.path)
28
+ if 'default' not in config:
29
+ raise Exception(f'Error loading file {self.path} use auth to generate')
30
+
31
+ self._token = config['default']['token']
32
+
33
+ @property
34
+ def token(self) -> str:
35
+ if self._token is None:
36
+ raise Exception('No Auth Token exists')
37
+ return self._token
38
+
39
+ @staticmethod
40
+ def generate(path: str, token: str):
41
+ config = configparser.ConfigParser()
42
+ config.add_section('default')
43
+ config['default']['token'] = token
44
+
45
+ with open(path, 'w') as configfile:
46
+ config.write(configfile)
47
+
48
+
49
+ class App:
50
+ def __init__(self, path, **kwargs):
51
+ self.config = ConfigFile(path, api_token=kwargs.get('api_token'))
52
+ self.endpoint = kwargs.get('endpoint', 'shopcloud-secrethub.ey.r.appspot.com')
53
+
54
+ def read(self, secretname, **kwargs) -> List:
55
+ headers = {
56
+ 'Authorization': self.config.token.strip(),
57
+ 'User-Agent': kwargs.get('user_agent', 'secrethub-cli'),
58
+ 'User-App': kwargs.get('user_app')
59
+ }
60
+ response = requests.get(
61
+ f'https://{self.endpoint}/hub/api/secrets/',
62
+ headers=headers,
63
+ params={
64
+ 'q': secretname,
65
+ }
66
+ )
67
+ if not (200 <= response.status_code <= 299):
68
+ if response.status_code == 403:
69
+ raise exceptions.APIAuthError()
70
+ raise exceptions.APIError()
71
+
72
+ return response.json().get('results', [])
73
+
74
+ def write(self, secretname, value, **kwargs):
75
+ headers = {
76
+ 'Authorization': self.config.token,
77
+ 'User-Agent': kwargs.get('user_agent', 'secrethub-cli'),
78
+ 'User-App': kwargs.get('user_app')
79
+ }
80
+ response = requests.post(
81
+ f'https://{self.endpoint}/hub/api/secrets/write/',
82
+ headers=headers,
83
+ json={
84
+ 'name': secretname,
85
+ 'value': value
86
+ }
87
+ )
88
+
89
+ if not (200 <= response.status_code <= 299):
90
+ if response.status_code == 404:
91
+ raise exceptions.APIAuthError()
92
+ raise exceptions.APIError()
93
+
94
+ return response.json()
95
+
96
+ def token_refresh(self, **kwargs) -> str:
97
+ headers = {
98
+ 'Authorization': self.config.token,
99
+ 'User-Agent': kwargs.get('user_agent', 'secrethub-cli'),
100
+ 'User-App': kwargs.get('user_app')
101
+ }
102
+ response = requests.post(
103
+ f'https://{self.endpoint}/hub/api/tokens/refresh/',
104
+ headers=headers,
105
+ json={}
106
+ )
107
+
108
+ if not (200 <= response.status_code <= 299):
109
+ if response.status_code == 403:
110
+ raise exceptions.APIAuthError()
111
+ raise exceptions.APIError()
112
+
113
+ return response.json().get('token', {}).get('value')
114
+
115
+
116
+ class SecretHub:
117
+ def __init__(self, **kwargs):
118
+ path = os.path.expanduser('~/.secrethub')
119
+ self.app = App(path, api_token=kwargs.get('api_token'))
120
+ self.secrets = {}
121
+ self.user_app = kwargs.get('user_app')
122
+
123
+ def read(self, secretname: str):
124
+ secret = self.secrets.get(secretname)
125
+ if secret is not None:
126
+ return secret.get('value')
127
+
128
+ secrets = self.app.read(
129
+ secretname,
130
+ user_agent='secrethub-code',
131
+ user_app=self.user_app
132
+ )
133
+ if len(secrets) == 0:
134
+ raise Exception(f'can not access secret `{secretname}`')
135
+ secret = secrets[0]
136
+ self.secrets[secret.get('name')] = secret
137
+ return secrets[0].get('value')
138
+
139
+ def write(self, secretname: str, value: str):
140
+ return self.app.write(
141
+ secretname,
142
+ value,
143
+ user_agent='secrethub-code',
144
+ user_app=self.user_app
145
+ )
@@ -0,0 +1,121 @@
1
+ import getpass
2
+ import json
3
+ import os
4
+ import pwd
5
+ import re
6
+
7
+ from . import exceptions
8
+ from .app import App, ConfigFile
9
+
10
+
11
+ def get_username():
12
+ try:
13
+ return pwd.getpwuid( os.getuid())[0]
14
+ except Exception:
15
+ return None
16
+
17
+
18
+ def main(args):
19
+ path = os.path.expanduser('~/.secrethub')
20
+ username = get_username()
21
+ command = args.which
22
+
23
+ if command == 'auth':
24
+ token = getpass.getpass('Token:')
25
+ ConfigFile.generate(path, token)
26
+ elif command == 'token-refresh':
27
+ app = App(path)
28
+ new_token = app.token_refresh()
29
+ ConfigFile.generate(path, new_token)
30
+ elif command == 'read':
31
+ app = App(path)
32
+ try:
33
+ secrets = app.read(args.name, user_app=username)
34
+ except exceptions.APIAuthError:
35
+ print('Authentication Error - Token invalid')
36
+ exit(1)
37
+ except exceptions.APIError:
38
+ print('API Error')
39
+ exit(1)
40
+ except Exception as e:
41
+ print(f'Error {e}')
42
+ exit(1)
43
+ if len(secrets) == 0:
44
+ raise Exception(f'can not access secret `{args.name}`')
45
+ if args.output is None:
46
+ for secret in secrets:
47
+ value = secret.get('value').encode('unicode_escape').decode('utf-8')
48
+ print(f"{secret.get('name')}={value}")
49
+ elif args.output == 'raw':
50
+ for secret in secrets:
51
+ value = secret.get('value').encode('unicode_escape').decode('utf-8')
52
+ print(value.replace('\\n', "\n"))
53
+ elif args.output == 'json':
54
+ for secret in secrets:
55
+ print(json.dumps(secret))
56
+ elif command == 'write':
57
+ app = App(path)
58
+
59
+ if args.in_file is not None:
60
+ with open(args.in_file) as f:
61
+ s = f.read()
62
+ try:
63
+ app.write(args.name, s, user_app=username)
64
+ except exceptions.APIAuthError:
65
+ print('Authentication Error - Token invalid')
66
+ exit(1)
67
+ except exceptions.APIError:
68
+ print('API Error')
69
+ exit(1)
70
+ except Exception as e:
71
+ print(f'Error {e}')
72
+ exit(1)
73
+ else:
74
+ if args.value is None:
75
+ raise Exception("value must set")
76
+ try:
77
+ app.write(args.name, args.value, user_app=username)
78
+ except exceptions.APIAuthError:
79
+ print('Authentication Error - Token invalid')
80
+ exit(1)
81
+ except exceptions.APIError:
82
+ print('API Error')
83
+ exit(1)
84
+ except Exception as e:
85
+ print(f'Error {e}')
86
+ exit(1)
87
+ elif command == 'inject' or command == 'printenv':
88
+ app = App(path)
89
+
90
+ with open(args.i) as f:
91
+ template = f.read()
92
+
93
+ def extract(x):
94
+ name = x.replace("{{", '').replace('}}', '').strip()
95
+ try:
96
+ secrets = app.read(name, user_app=username)
97
+ except exceptions.APIAuthError:
98
+ print('Authentication Error - Token invalid')
99
+ exit(1)
100
+ except exceptions.APIError:
101
+ print('API Error')
102
+ exit(1)
103
+ except Exception as e:
104
+ print(f'Error {e}')
105
+ exit(1)
106
+ if len(secrets) == 0:
107
+ raise AttributeError(f"Secret not found {name}")
108
+ key_s = "/".join(name.split('/')[-1:]).upper().replace('/', '_').replace('-', '_')
109
+ return (x, key_s, secrets[0].get('value'))
110
+
111
+ variables = [extract(x) for x in re.findall('{{.*}}', template)]
112
+
113
+ if command == 'printenv':
114
+ for _, key, value in variables:
115
+ value = value.replace('$', r'\$')
116
+ print(f"export {key}=\"{value}\"")
117
+ else:
118
+ for key, _, value in variables:
119
+ template = template.replace(key, f"\"{value}\"")
120
+ with open(args.o, 'w') as writer:
121
+ writer.write(template)
@@ -0,0 +1,6 @@
1
+ class APIAuthError(Exception):
2
+ pass
3
+
4
+
5
+ class APIError(Exception):
6
+ pass
@@ -0,0 +1,6 @@
1
+ def inc(x):
2
+ return x + 1
3
+
4
+
5
+ def test_answer():
6
+ assert inc(3) == 4
@@ -0,0 +1,104 @@
1
+ Metadata-Version: 2.4
2
+ Name: shopcloud_secrethub
3
+ Version: 2.19.0
4
+ Summary: CLI tool for the Shopcloud SecretHub
5
+ Author-email: Konstantin Stoldt <konstantin.stoldt@talk-point.de>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Talk-Point/shopcloud-secrethub-cli
8
+ Keywords: CLI
9
+ Requires-Python: >=3.11
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE.txt
12
+ Requires-Dist: requests>=2.28
13
+ Dynamic: license-file
14
+
15
+ # Shopcloud SecretHub CLI
16
+
17
+ The SecretHub CLI provides the command-line interface to interact with the SecretHub API.
18
+
19
+ ## install
20
+
21
+ ```sh
22
+ pip install shopcloud_secrethub
23
+ ```
24
+
25
+ Or with uv:
26
+
27
+ ```sh
28
+ uv add shopcloud_secrethub
29
+ ```
30
+
31
+ ### Usage
32
+
33
+
34
+ __Reading and writing secrets:__
35
+
36
+ ```sh
37
+ $ secrethub auth
38
+ $ secrethub read <secret-name>
39
+ $ secrethub write <secret-name> <value>
40
+ ```
41
+
42
+
43
+ __Provisioning your applications with secrets:__
44
+
45
+ Provision a template file
46
+
47
+ ```sh
48
+ $ secrethub inject -i app.temp.yaml -o app.yaml
49
+
50
+ # app.temp.yaml
51
+ env_variables:
52
+ ENV: {{ talk-point/test-repo/env }}
53
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
54
+
55
+ ```
56
+
57
+ Provision to the environment
58
+
59
+ ```sh
60
+ $ eval `secrethub printenv -i app.temp.yaml`
61
+
62
+ # app.temp.yaml
63
+ env_variables:
64
+ ENV: {{ talk-point/test-repo/env }}
65
+ SECRET_KEY: {{ talk-point/test-repo/secret_key }}
66
+
67
+ ```
68
+
69
+ __in Code:__
70
+
71
+ ```py
72
+ from shopcloud_secrethub import SecretHub
73
+ hub = SecretHub(user_app="test-script", api_token='<TOKEN>')
74
+ hub.read('talk-point/test-repo/secret_key')
75
+ ```
76
+
77
+ ### Contributing
78
+
79
+ Install uv if you don't have it:
80
+
81
+ ```sh
82
+ curl -LsSf https://astral.sh/uv/install.sh | sh
83
+ ```
84
+
85
+ Set up the project:
86
+
87
+ ```sh
88
+ uv sync
89
+ ```
90
+
91
+ Run tests:
92
+
93
+ ```sh
94
+ uv run pytest
95
+ ```
96
+
97
+ ### Deploy to PyPI
98
+
99
+ Deployment is handled automatically by CI when pushing to `master`. To build and publish manually:
100
+
101
+ ```sh
102
+ uv build
103
+ uv publish
104
+ ```
@@ -0,0 +1,15 @@
1
+ LICENSE.txt
2
+ README.md
3
+ pyproject.toml
4
+ shopcloud_secrethub/__init__.py
5
+ shopcloud_secrethub/__main__.py
6
+ shopcloud_secrethub/app.py
7
+ shopcloud_secrethub/cli.py
8
+ shopcloud_secrethub/exceptions.py
9
+ shopcloud_secrethub/test_cli.py
10
+ shopcloud_secrethub.egg-info/PKG-INFO
11
+ shopcloud_secrethub.egg-info/SOURCES.txt
12
+ shopcloud_secrethub.egg-info/dependency_links.txt
13
+ shopcloud_secrethub.egg-info/entry_points.txt
14
+ shopcloud_secrethub.egg-info/requires.txt
15
+ shopcloud_secrethub.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ secrethub = shopcloud_secrethub.__main__:main
@@ -0,0 +1 @@
1
+ shopcloud_secrethub