Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allows runtime configuration of vault provider retry delay and tries. #41

Merged
merged 15 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Changelog
***
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)

## Unreleased
***
## [3.3.6] - 2023-06-11

### Added
- `delay` and `tries` to `Vault` constructor for runtime configuration of `retry_call`
- `delay` default to 60 seconds and `tries` to 5

### Changed
- Removed `retry` decorator usage in `vault.py`
- Invokes `vault_client` calls through `retry_call` instead


## Released
***

# TODO

66 changes: 45 additions & 21 deletions gestalt/vault.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import os
from datetime import datetime, timedelta
from queue import Queue
from threading import Thread
from time import sleep
from gestalt.provider import Provider
from typing import Any, Dict, List, Optional, Tuple, Union

import hvac # type: ignore
import requests
from requests.exceptions import Timeout
from jsonpath_ng import parse # type: ignore
from typing import Optional, Tuple, Any, Dict, Union, List
import hvac # type: ignore
from queue import Queue
import os
from threading import Thread
from retry import retry
from requests.exceptions import Timeout
from retry.api import retry_call

from gestalt.provider import Provider


class Vault(Provider):
@retry((RuntimeError, Timeout), delay=2, tries=5) # type: ignore
def __init__(self,
cert: Optional[Tuple[str, str]] = None,
role: Optional[str] = None,
jwt: Optional[str] = None,
url: Optional[str] = os.environ.get("VAULT_ADDR"),
token: Optional[str] = os.environ.get("VAULT_TOKEN"),
verify: Optional[bool] = True,
scheme: str = "ref+vault://") -> None:
def __init__(
self,
cert: Optional[Tuple[str, str]] = None,
role: Optional[str] = None,
jwt: Optional[str] = None,
url: Optional[str] = os.environ.get("VAULT_ADDR"),
token: Optional[str] = os.environ.get("VAULT_TOKEN"),
verify: Optional[bool] = True,
scheme: str = "ref+vault://",
delay: int = 60,
tries: int = 5,
) -> None:
"""Initialized vault client and authenticates vault

Args:
Expand All @@ -44,8 +49,16 @@ def __init__(self,
self._secret_values: Dict[str, Union[str, int, float, bool,
List[Any]]] = dict()

self.delay = delay
self.tries = tries

try:
self.vault_client.is_authenticated()
retry_call(
self.vault_client.is_authenticated,
exceptions=(RuntimeError, Timeout),
delay=self.delay,
tries=self.tries,
)
except requests.exceptions.MissingSchema:
raise RuntimeError(
"Gestalt Error: Unable to connect to vault with the given configuration"
Expand All @@ -55,7 +68,13 @@ def __init__(self,
try:
hvac.api.auth_methods.Kubernetes(
self.vault_client.adapter).login(role=role, jwt=jwt)
token = self.vault_client.auth.token.lookup_self()
token = retry_call(
self.vault_client.auth.token.lookup_self,
exceptions=(RuntimeError, Timeout),
delay=self.delay,
tries=self.tries,
)

if token is not None:
kubes_token = (
"kubernetes",
Expand Down Expand Up @@ -85,7 +104,6 @@ def stop(self) -> None:
def __del__(self) -> None:
self.stop()

@retry((RuntimeError, Timeout), delay=3, tries=3) # type: ignore
def get(
self,
key: str,
Expand All @@ -112,7 +130,13 @@ def get(
return self._secret_values[key]

try:
response = self.vault_client.read(path)
response = retry_call(
self.vault_client.read,
fargs=[path],
exceptions=(RuntimeError, Timeout),
delay=self.delay,
tries=self.tries,
)
if response is None:
raise RuntimeError("Gestalt Error: No secrets found")
if response['lease_id']:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def readme():
reqs_list = list(map(lambda x: x.rstrip(), reqs))

setup(name='gestalt-cfg',
version='3.3.5',
version='3.3.6',
description='A sensible configuration library for Python',
long_description=readme(),
long_description_content_type="text/markdown",
Expand Down
Loading