-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Prepped for adding tests * Handle malformed timestamps, improve handling for missing timestamps * Add tests for vault_time module * Update poetry.lock with correct dependency setup * Bump version: 0.3.8 → 0.3.9 * Correct UTC handling by swapping to timezone aware usage of datetime * Apply formatting * Update pytest execution to properly include coverage
- Loading branch information
1 parent
3e142ae
commit 493fd1a
Showing
8 changed files
with
126 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ on: | |
workflow_dispatch: | ||
|
||
env: | ||
VERSION: 0.3.8 | ||
VERSION: 0.3.9 | ||
|
||
jobs: | ||
release: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "vault-assesment-prometheus-exporter" | ||
version = "0.3.8" | ||
version = "0.3.9" | ||
description = "Prometheus exporter to monitor custom metadata for KV2 secrets for (self-imposed) expiration." | ||
authors = ["Eugene Davis <[email protected]>"] | ||
readme = "README.md" | ||
|
@@ -31,10 +31,9 @@ types-requests = "2.27.25" | |
types-PyYAML = "6.0.7" | ||
bandit = "^1.7.4" | ||
bump2version = "^1.0.1" | ||
pytest-mock = "3.6.1" | ||
pytest-mock = "^3.7.0" | ||
mock = "4.0.3" | ||
|
||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" | ||
|
@@ -161,3 +160,4 @@ ignore-imports = true | |
[tool.pytest.ini_options] | ||
minversion = "6.2.5" | ||
testpaths = ["tests"] | ||
addopts = "--cov=vault_monitor --cov-report term-missing" |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import datetime | ||
|
||
import pytest | ||
from pytest_mock import mocker | ||
|
||
from vault_monitor.secret_expiration_monitor.vault_time import ExpirationMetadata | ||
|
||
|
||
@pytest.mark.parametrize("weeks, days, hours, minutes, seconds", [(1, 1, 1, 2, 4), (8, 10, 1105, 20, 4444)]) | ||
def test_from_duration_get_seralized_expiration(weeks, days, hours, minutes, seconds): | ||
""" | ||
Tests that creating from duration results in matching metadata | ||
""" | ||
last_renewed_time = datetime.datetime.now(tz=datetime.timezone.utc) | ||
expiration_delta = datetime.timedelta(weeks=weeks, days=days, hours=hours, minutes=minutes, seconds=seconds) | ||
expiration = last_renewed_time + expiration_delta | ||
expiration_metadata = ExpirationMetadata.from_duration(weeks, days, hours, minutes, seconds) | ||
|
||
metadata_dict = expiration_metadata.get_serialized_expiration_metadata() | ||
|
||
# Give 50 milliseconds grace period between calling utcnow here and the code calling it | ||
assert datetime.datetime.fromisoformat(metadata_dict["last_renewed_timestamp"][:-1]) - last_renewed_time < datetime.timedelta(milliseconds=50) | ||
assert datetime.datetime.fromisoformat(metadata_dict["expiration_timestamp"][:-1]) - expiration < datetime.timedelta(milliseconds=50) | ||
|
||
|
||
def test_from_metadata_get_seralized_expiration(): | ||
""" | ||
Tests that creating from metadata results in matching metadata being available | ||
""" | ||
metadata = {"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "2022-05-02T09:49:41.415869Z"} | ||
|
||
expiration_metadata = ExpirationMetadata.from_metadata(metadata) | ||
assert expiration_metadata.get_serialized_expiration_metadata() == metadata | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input, output", | ||
[ | ||
# Missing last_renewed_timestamp | ||
( | ||
{ | ||
"expiration_timestamp": "2022-08-08T09:49:41.415869Z", | ||
}, | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
# Missing expiration_timestamp | ||
( | ||
{ | ||
"last_renewed_timestamp": "2022-08-08T09:49:41.415869Z", | ||
}, | ||
{"last_renewed_timestamp": "2022-08-08T09:49:41.415869Z", "expiration_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
# Timezone dropped from expiration_timestamp | ||
( | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869", "last_renewed_timestamp": "2022-05-02T09:49:41.415869Z"}, | ||
{"expiration_timestamp": "1970-01-01T00:00:00+00:00Z", "last_renewed_timestamp": "2022-05-02T09:49:41.415869Z"}, | ||
), | ||
# Timezone dropped from last_renewed_timestamp | ||
( | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "2022-05-02T09:49:41.415869"}, | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
# Malformed expiration_timestamp | ||
( | ||
{"expiration_timestamp": "2022-08-008T09:49:41.415869Z", "last_renewed_timestamp": "2022-05-02T09:49:41.415869Z"}, | ||
{"expiration_timestamp": "1970-01-01T00:00:00+00:00Z", "last_renewed_timestamp": "2022-05-02T09:49:41.415869Z"}, | ||
), | ||
# Malformed last_renewed_timestamp | ||
( | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "2022-05-002T09:49:41.415869Z"}, | ||
{"expiration_timestamp": "2022-08-08T09:49:41.415869Z", "last_renewed_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
# Missing both | ||
( | ||
{}, | ||
{"expiration_timestamp": "1970-01-01T00:00:00+00:00Z", "last_renewed_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
# Malformed both | ||
( | ||
{"expiration_timestamp": "2022-008-08T09:49:41.415869Z", "last_renewed_timestamp": "2022-05-002T09:49:41.415869Z"}, | ||
{"expiration_timestamp": "1970-01-01T00:00:00+00:00Z", "last_renewed_timestamp": "1970-01-01T00:00:00+00:00Z"}, | ||
), | ||
], | ||
) | ||
def test_missing_fieldname_and_malformed_timestamp_is_zero(input, output): | ||
""" | ||
Tests that missing or malformed timestamps get set to zero (the beginning of the Unix epoch) | ||
""" | ||
expiration_metadata_object = ExpirationMetadata.from_metadata(input) | ||
assert expiration_metadata_object.get_serialized_expiration_metadata() == output |
This file contains 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