get-hc-secrets 1.3__tar.gz → 1.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: get_hc_secrets
3
- Version: 1.3
3
+ Version: 1.4
4
4
  Summary: A package to read secrets from Hashicorp vault
5
5
  Home-page: https://github.com/xmayeur/getSecrets
6
6
  Author: Xavier Mayeur
@@ -18,21 +18,23 @@ Requires-Dist: requests
18
18
 
19
19
  # getSecrets package
20
20
 
21
-
22
- getSecrets is a simple package that reads from the 'secret' repository of a Hashicorp vault
21
+ getSecrets is a simple package that reads from the given engine ('secret' by default) of a Hashicorp vault
23
22
 
24
23
  usage:
25
24
 
26
25
  ```
27
- from get_secrets import get_secret
26
+ from get_secrets import *
28
27
 
29
- data = get_secret(<id>)
30
- ```
28
+ data = get_secret(<id>, [<secret>])
29
+
30
+ usr_pwd = get_user_pwd(<id>, <new k_v_dict> , [<secret>])
31
31
 
32
- If the secret is a single key/value pair, data is a type tuple(key, value)
33
- else, data is a dictionary
32
+ list = list_secret([<secret>]
33
+
34
+ ```
34
35
 
35
36
  Vault parameters are stored in a config file ~/.config/.vault/.vault.yml
37
+
36
38
  ```
37
39
  vault:
38
40
  token: "<access token>"
@@ -0,0 +1,25 @@
1
+ # getSecrets package
2
+
3
+ getSecrets is a simple package that reads from the given engine ('secret' by default) of a Hashicorp vault
4
+
5
+ usage:
6
+
7
+ ```
8
+ from get_secrets import *
9
+
10
+ data = get_secret(<id>, [<secret>])
11
+
12
+ usr_pwd = get_user_pwd(<id>, <new k_v_dict> , [<secret>])
13
+
14
+ list = list_secret([<secret>]
15
+
16
+ ```
17
+
18
+ Vault parameters are stored in a config file ~/.config/.vault/.vault.yml
19
+
20
+ ```
21
+ vault:
22
+ token: "<access token>"
23
+ vault_addr: "https://vault:8200"
24
+ certs: "<path>/bundle.pem"
25
+ ```
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: get_hc_secrets
3
- Version: 1.3
3
+ Version: 1.4
4
4
  Summary: A package to read secrets from Hashicorp vault
5
5
  Home-page: https://github.com/xmayeur/getSecrets
6
6
  Author: Xavier Mayeur
@@ -18,21 +18,23 @@ Requires-Dist: requests
18
18
 
19
19
  # getSecrets package
20
20
 
21
-
22
- getSecrets is a simple package that reads from the 'secret' repository of a Hashicorp vault
21
+ getSecrets is a simple package that reads from the given engine ('secret' by default) of a Hashicorp vault
23
22
 
24
23
  usage:
25
24
 
26
25
  ```
27
- from get_secrets import get_secret
26
+ from get_secrets import *
28
27
 
29
- data = get_secret(<id>)
30
- ```
28
+ data = get_secret(<id>, [<secret>])
29
+
30
+ usr_pwd = get_user_pwd(<id>, <new k_v_dict> , [<secret>])
31
31
 
32
- If the secret is a single key/value pair, data is a type tuple(key, value)
33
- else, data is a dictionary
32
+ list = list_secret([<secret>]
33
+
34
+ ```
34
35
 
35
36
  Vault parameters are stored in a config file ~/.config/.vault/.vault.yml
37
+
36
38
  ```
37
39
  vault:
38
40
  token: "<access token>"
@@ -2,7 +2,7 @@
2
2
  name = "get_hc_secrets"
3
3
  dynamic = ["version"]
4
4
  authors = [
5
- { name="Xavier Mayeur", email="xavier@mayeur.be" }
5
+ { name = "Xavier Mayeur", email = "xavier@mayeur.be" }
6
6
  ]
7
7
  description = "A package to read secrets from Hashicorp vault"
8
8
  readme = "README.md"
@@ -2,7 +2,7 @@ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name='get_hc_secrets',
5
- version='1.3',
5
+ version='1.4',
6
6
  packages=['src'],
7
7
  url='https://github.com/xmayeur/getSecrets',
8
8
  license='',
@@ -0,0 +1,136 @@
1
+ import logging
2
+ from os import getenv
3
+ from os.path import join
4
+
5
+ import requests
6
+ import yaml
7
+
8
+ _config_file = "~/.config/.vault/vault.yml"
9
+ _home = getenv("HOME")
10
+ _config = yaml.safe_load(open(join(_home, _config_file.replace("~/", ''))))
11
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s',
12
+ datefmt='%m/%d/%Y %I:%M:%S %p')
13
+
14
+
15
+ def get_secret(id: str, repo: str = 'secret') -> dict:
16
+ """
17
+ :param id: The ID of the secret to retrieve
18
+ :param repo: The name of the secrets repository to retrieve the secret from - defaults to 'secret'
19
+ :return: a json object with key/value pairs
20
+ or an empty object if the secret retrieval fails
21
+
22
+ This method retrieves a secret from a Vault server using the provided ID.
23
+ If the request is successful (status code 200), the method extracts the key-value pairs JSON object.
24
+ If the request fails, the method logs an HTTP error message and returns a n empty json {}.
25
+ """
26
+
27
+ base_url = _config['vault']['vault_addr']
28
+ certs = join(_home, _config['vault']['certs'].replace("~/", ''))
29
+ token = _config['vault']['token']
30
+
31
+ headers = {"X-Vault-Token": token}
32
+ uri = f"/v1/{repo}/data/"
33
+ url = f"{base_url}{uri}{id}"
34
+ resp = requests.get(url, headers=headers, verify=certs)
35
+ if resp.status_code == 200:
36
+ secret = resp.json()["data"]["data"]
37
+ return secret
38
+
39
+ else:
40
+ print(f"http error {resp.status_code}")
41
+ logging.error(f"Vault api error {resp}")
42
+ return {}
43
+
44
+
45
+ def get_user_pwd(id: str, repo: str = 'secret') -> tuple:
46
+ """
47
+ :param id: The ID of the secret to retrieve
48
+ :param repo: The name of the secret repository to retrieve the seret from - defaults to 'secret'
49
+ :return: a tuple username, password values if the secrets has such keys, else None, None
50
+
51
+ This method retrieves a secret from a Vault server using the provided ID.
52
+ If the request is successful (status code 200), the method extracts the username and password key value
53
+ if such keys exist.
54
+ If the request fails, the method prints an HTTP error message and returns (None, None).
55
+ """
56
+
57
+ base_url = _config['vault']['vault_addr']
58
+ certs = join(_home, _config['vault']['certs'].replace("~/", ''))
59
+ token = _config['vault']['token']
60
+
61
+ headers = {"X-Vault-Token": token}
62
+ uri = f"/v1/{repo}/data/"
63
+ url = f"{base_url}{uri}{id}"
64
+ resp = requests.get(url, headers=headers, verify=certs)
65
+ if resp.status_code == 200:
66
+ secret = resp.json()["data"]["data"]
67
+ if 'username' in secret and 'password' in secret:
68
+ return secret['username'], secret['password']
69
+ else:
70
+ return None, None
71
+
72
+ else:
73
+ print(f"http error {resp.status_code}")
74
+ logging.error(f"Vault api error {resp}")
75
+ return None, None
76
+
77
+
78
+ def list_secret(repo: str = 'secret'):
79
+ """
80
+ :param repo: The name of a secret repository to retrieve the secret from - defaults to 'secret'
81
+ :return: A list containing all items keys from the repository
82
+
83
+ """
84
+
85
+ base_url = _config['vault']['vault_addr']
86
+ certs = join(_home, _config['vault']['certs'].replace("~/", ''))
87
+ token = _config['vault']['token']
88
+
89
+ headers = {"X-Vault-Token": token}
90
+ uri = f"/v1/{repo}/metadata"
91
+ url = f"{base_url}{uri}"
92
+ resp = requests.request('LIST', url, headers=headers, verify=certs)
93
+ if resp.status_code == 200:
94
+ return resp.json()["data"]["keys"]
95
+
96
+ else:
97
+ print(f"http error {resp.status_code}")
98
+ logging.error(f"Vault api error {resp}")
99
+ return None, None
100
+
101
+
102
+ def upd_secret(id: str, data, repo: str = 'secret'):
103
+ """
104
+ :param id: The ID of the secret to retrieve
105
+ :param data: The data to be uploaded in place of the exitisting one
106
+ :param repo: The name of the repository to retrieve the secret from - defaults to 'secret'
107
+ :return: the response status code from the vault - 200 if successful.
108
+
109
+ """
110
+
111
+ base_url = _config['vault']['vault_addr']
112
+ certs = join(_home, _config['vault']['certs'].replace("~/", ''))
113
+ token = _config['vault']['token']
114
+
115
+ headers = {"X-Vault-Token": token}
116
+ uri = f"/v1/{repo}/data/"
117
+ url = f"{base_url}{uri}{id}"
118
+ resp = requests.request('GET', url, headers=headers, verify=certs)
119
+ if resp.status_code == 200:
120
+ version = resp.json()["data"]['metadata']['version']
121
+ obj = {
122
+ "options": {
123
+ "cas": version
124
+ },
125
+ "data": data
126
+ }
127
+
128
+ resp2 = requests.request('POST', url, headers=headers, json=obj, verify=certs)
129
+ if resp2.status_code != 200:
130
+ logging.warning(f"Vault update error for {id} with new {data}")
131
+ return resp2.status_code
132
+
133
+ else:
134
+ print(f"http error {resp.status_code}")
135
+ logging.error(f"Vault api error {resp}")
136
+ return None, None
@@ -1,23 +0,0 @@
1
- # getSecrets package
2
-
3
-
4
- getSecrets is a simple package that reads from the 'secret' repository of a Hashicorp vault
5
-
6
- usage:
7
-
8
- ```
9
- from get_secrets import get_secret
10
-
11
- data = get_secret(<id>)
12
- ```
13
-
14
- If the secret is a single key/value pair, data is a type tuple(key, value)
15
- else, data is a dictionary
16
-
17
- Vault parameters are stored in a config file ~/.config/.vault/.vault.yml
18
- ```
19
- vault:
20
- token: "<access token>"
21
- vault_addr: "https://vault:8200"
22
- certs: "<path>/bundle.pem"
23
- ```
@@ -1,46 +0,0 @@
1
- import logging
2
- from os import getenv
3
- from os.path import join
4
- import yaml
5
- import requests
6
-
7
- _config_file = "~/.config/.vault/vault.yml"
8
- _home = getenv("HOME")
9
- _config = yaml.safe_load(open(join(_home, _config_file.replace("~/", ''))))
10
- logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s',
11
- datefmt='%m/%d/%Y %I:%M:%S %p')
12
-
13
-
14
- def get_secret(id: str):
15
- """
16
- :param id: The ID of the secret to retrieve
17
- :return: A tuple containing the key-value pairs of the secret or a json object for complex secrets
18
- or (None, None) if the secret retrieval fails
19
-
20
- This method retrieves a secret from a Vault server using the provided ID. I
21
- t sends a GET request to the Vault server with the necessary headers and authentication token.
22
- If the request is successful (status code 200), the method extracts the key-value pairs
23
- from the response JSON and returns them as a tuple.
24
- If the request fails, the method prints an HTTP error message and returns (None, None).
25
- """
26
-
27
- base_url = _config['vault']['vault_addr']
28
- certs = join(_home, _config['vault']['certs'].replace("~/", ''))
29
- token = _config['vault']['token']
30
-
31
- headers = {"X-Vault-Token": token}
32
- uri = "/v1/secret/data/"
33
- url = f"{base_url}{uri}{id}"
34
- resp = requests.get(url, headers=headers, verify=certs)
35
- if resp.status_code == 200:
36
- secret = resp.json()["data"]["data"]
37
- if len(secret.values()) == 1:
38
- for k, v in secret.items():
39
- return k, v
40
- else:
41
- return secret
42
-
43
- else:
44
- print(f"http error {resp.status_code}")
45
- logging.error(f"Vault api error {resp}")
46
- return None, None
File without changes
File without changes