Skip to content

Commit 88bf4f2

Browse files
Merge pull request #2 from jamaalscarlett/add_ruff_formatting
Run black and ruff formatters
2 parents c92e0ff + 097ac56 commit 88bf4f2

18 files changed

+2298
-1766
lines changed

.github/workflows/tests.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
branches: [ master ]
77
paths:
88
- '**.py'
9-
- 'run_tests.sh'
9+
- 'download_vault.sh'
1010
- 'tox.ini'
1111
- 'requirements.txt'
1212
- 'pyproject.toml'
@@ -35,18 +35,18 @@ jobs:
3535
- name: Install dependencies
3636
run: |
3737
python -m pip install --upgrade pip
38-
pip install uv
38+
pip install uv ruff
39+
- name: Run Ruff
40+
run: ruff check .
3941
- name: Install tox
4042
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
4143
- name: Install Python
4244
if: startsWith(matrix.env, '3.') && matrix.env != '3.13'
4345
run: uv python install --python-preference only-managed ${{ matrix.env }}
4446
- name: Make test script executable
45-
run: chmod +x ./run_tests.sh
47+
run: chmod +x ./download_vault.sh
4648
- name: Run test script
47-
# env:
48-
# PATH: "root/.local/bin:/root/.local/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
49-
run: ./run_tests.sh
49+
run: ./download_vault.sh
5050
- name: Run test suite vault 1.16.3
5151
run: tox run --skip-pkg-install -e py${{ matrix.env }}-vault1163
5252
- name: Run test suite vault 1.17.6

Dockerfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
FROM python:3.12-slim-bullseye
22

3-
COPY run_tests.sh /run_tests.sh
3+
COPY download_vault.sh /download_vault.sh
4+
COPY docker_tests.sh /docker_tests.sh
45
COPY async_hvac/ /src/async_hvac/
56
COPY test/ /src/test/
67
COPY tox.ini /src/
78
COPY requirements-dev.txt /src/
89

910
RUN apt update
1011
RUN apt install -y wget zip gcc
11-
RUN pip install uv tox
12+
RUN pip install uv
1213
RUN uv tool install tox --with tox-uv
1314
ENV PATH=/root/.local/bin:$PATH
1415
WORKDIR /src
1516

16-
CMD ["/run_tests.sh"]
17+
CMD ["/docker_tests.sh"]

Makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
test: version
1+
SHELL := /bin/bash
2+
3+
test: version download_vault
24
tox
35

6+
download_vault:
7+
if [[ ! -f "/tmp/async-hvac/vault_1.16.3" || \
8+
! -f "/tmp/async-hvac/vault_1.17.6" || \
9+
! -f "/tmp/async-hvac/vault_1.18.3" ]]; then \
10+
./download_vault.sh; \
11+
fi
12+
413
version:
514
cp version async_hvac/version
615

async_hvac/__init__.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,35 @@ def wrapper(*args, **kwargs):
1111
return f(*args, **kwargs)
1212
coro = coroutine(f)
1313
future = coro(*args, **kwargs)
14-
return self._executor.submit(
15-
self._loop.run_until_complete,
16-
future).result()
14+
return self._executor.submit(self._loop.run_until_complete, future).result()
15+
1716
return wrapper
1817

1918

2019
# Just for test and try and do not work properly in some cases... not part of the Async API
2120
class Client(AsyncClient):
2221

23-
def __init__(self, url='http://127.0.0.1:8200', token=None,
24-
cert=None, verify=True, timeout=30, proxies=None,
25-
allow_redirects=True, session=None, sync=True,
26-
loop=None):
22+
def __init__(
23+
self,
24+
url="http://127.0.0.1:8200",
25+
token=None,
26+
cert=None,
27+
verify=True,
28+
timeout=30,
29+
proxies=None,
30+
allow_redirects=True,
31+
session=None,
32+
sync=True,
33+
loop=None,
34+
):
2735
super(Client, self).__init__(
28-
url, token, cert, verify, timeout,
29-
proxies, allow_redirects, session, loop)
36+
url, token, cert, verify, timeout, proxies, allow_redirects, session, loop
37+
)
3038
self._sync = sync
3139
if sync:
3240
for attr in AsyncClient.__dict__:
3341
attr_obj = getattr(AsyncClient, attr)
34-
if callable(attr_obj) and not attr.startswith('_'):
42+
if callable(attr_obj) and not attr.startswith("_"):
3543
setattr(self, attr, async_to_sync(self, getattr(self, attr)))
3644
self._executor = concurrent.futures.ThreadPoolExecutor(
3745
max_workers=3,
@@ -48,37 +56,37 @@ def seal_status(self):
4856
if not self._sync or self._loop.is_running():
4957
return super(Client, self).seal_status
5058
return self._executor.submit(
51-
self._loop.run_until_complete,
52-
super(Client, self).seal_status).result()
59+
self._loop.run_until_complete, super(Client, self).seal_status
60+
).result()
5361

5462
@property
5563
def generate_root_status(self):
5664
if not self._sync or self._loop.is_running():
5765
return super(Client, self).generate_root_status
5866
return self._executor.submit(
59-
self._loop.run_until_complete,
60-
super(Client, self).generate_root_status).result()
67+
self._loop.run_until_complete, super(Client, self).generate_root_status
68+
).result()
6169

6270
@property
6371
def key_status(self):
6472
if not self._sync or self._loop.is_running():
6573
return super(Client, self).key_status
6674
return self._executor.submit(
67-
self._loop.run_until_complete,
68-
super(Client, self).key_status).result()
75+
self._loop.run_until_complete, super(Client, self).key_status
76+
).result()
6977

7078
@property
7179
def rekey_status(self):
7280
if not self._sync or self._loop.is_running():
7381
return super(Client, self).rekey_status
7482
return self._executor.submit(
75-
self._loop.run_until_complete,
76-
super(Client, self).rekey_status).result()
83+
self._loop.run_until_complete, super(Client, self).rekey_status
84+
).result()
7785

7886
@property
7987
def ha_status(self):
8088
if not self._sync or self._loop.is_running():
8189
return super(Client, self).ha_status
8290
return self._executor.submit(
83-
self._loop.run_until_complete,
84-
super(Client, self).ha_status).result()
91+
self._loop.run_until_complete, super(Client, self).ha_status
92+
).result()

async_hvac/aws_utils.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,45 @@ def __init__(self, access_key, secret_key, session_token=None):
1111

1212
def add_auth(self, method, headers, body):
1313
if body:
14-
headers['Content-Length'] = str(len(body))
15-
timestamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
16-
headers['X-Amz-Date'] = timestamp
14+
headers["Content-Length"] = str(len(body))
15+
timestamp = datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
16+
headers["X-Amz-Date"] = timestamp
1717

1818
if self.session_token:
19-
headers['X-Amz-Security-Token'] = self.session_token
19+
headers["X-Amz-Security-Token"] = self.session_token
2020

2121
# https://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
22-
canonical_headers = ''.join('{0}:{1}\n'.format(k.lower(), headers[k]) for k in sorted(headers))
23-
signed_headers = ';'.join(k.lower() for k in sorted(headers))
24-
payload_hash = sha256(body.encode('utf-8')).hexdigest()
25-
canonical_request = '\n'.join([method, '/', '', canonical_headers, signed_headers, payload_hash])
22+
canonical_headers = "".join(
23+
"{0}:{1}\n".format(k.lower(), headers[k]) for k in sorted(headers)
24+
)
25+
signed_headers = ";".join(k.lower() for k in sorted(headers))
26+
payload_hash = sha256(body.encode("utf-8")).hexdigest()
27+
canonical_request = "\n".join(
28+
[method, "/", "", canonical_headers, signed_headers, payload_hash]
29+
)
2630

2731
# https://docs.aws.amazon.com/general/latest/gr/sigv4-create-string-to-sign.html
28-
algorithm = 'AWS4-HMAC-SHA256'
29-
credential_scope = '/'.join([timestamp[0:8], 'us-east-1', 'sts', 'aws4_request'])
30-
canonical_request_hash = sha256(canonical_request.encode('utf-8')).hexdigest()
31-
string_to_sign = '\n'.join([algorithm, timestamp, credential_scope, canonical_request_hash])
32+
algorithm = "AWS4-HMAC-SHA256"
33+
credential_scope = "/".join(
34+
[timestamp[0:8], "us-east-1", "sts", "aws4_request"]
35+
)
36+
canonical_request_hash = sha256(canonical_request.encode("utf-8")).hexdigest()
37+
string_to_sign = "\n".join(
38+
[algorithm, timestamp, credential_scope, canonical_request_hash]
39+
)
3240

3341
# https://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
34-
key = 'AWS4{0}'.format(self.secret_key).encode('utf-8')
35-
key = hmac.new(key, timestamp[0:8].encode('utf-8'), sha256).digest()
36-
key = hmac.new(key, 'us-east-1'.encode('utf-8'), sha256).digest()
37-
key = hmac.new(key, 'sts'.encode('utf-8'), sha256).digest()
38-
key = hmac.new(key, 'aws4_request'.encode('utf-8'), sha256).digest()
39-
signature = hmac.new(key, string_to_sign.encode('utf-8'), sha256).hexdigest()
42+
key = "AWS4{0}".format(self.secret_key).encode("utf-8")
43+
key = hmac.new(key, timestamp[0:8].encode("utf-8"), sha256).digest()
44+
key = hmac.new(key, "us-east-1".encode("utf-8"), sha256).digest()
45+
key = hmac.new(key, "sts".encode("utf-8"), sha256).digest()
46+
key = hmac.new(key, "aws4_request".encode("utf-8"), sha256).digest()
47+
signature = hmac.new(key, string_to_sign.encode("utf-8"), sha256).hexdigest()
4048

4149
# https://docs.aws.amazon.com/general/latest/gr/sigv4-add-signature-to-request.html
42-
authorization = '{0} Credential={1}/{2}, SignedHeaders={3}, Signature={4}'.format(
43-
algorithm, self.access_key, credential_scope, signed_headers, signature)
44-
headers['Authorization'] = authorization
50+
authorization = (
51+
"{0} Credential={1}/{2}, SignedHeaders={3}, Signature={4}".format(
52+
algorithm, self.access_key, credential_scope, signed_headers, signature
53+
)
54+
)
55+
headers["Authorization"] = authorization

async_hvac/exceptions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class VaultError(Exception):
22
def __init__(self, message=None, errors=None):
33
if errors:
4-
message = ', '.join(errors)
4+
message = ", ".join(errors)
55

66
self.errors = errors
77

0 commit comments

Comments
 (0)