def test_instantiate_bad_socket_path():
    with pytest.raises(ArgumentError) as exception:
        DefaultWorkloadApiClient(spiffe_socket='/invalid')

    assert (
        str(exception.value) ==
        'Invalid DefaultWorkloadApiClient configuration: SPIFFE endpoint socket: scheme must be set.'
    )
def test_instantiate_default_without_var():
    with pytest.raises(ArgumentError) as exception:
        DefaultWorkloadApiClient()

    assert (
        str(exception.value) ==
        'Invalid DefaultWorkloadApiClient configuration: SPIFFE endpoint socket: socket must be set.'
    )
def test_instantiate_default_with_bad_var():
    os.environ[SPIFFE_SOCKET_ENV] = '/invalid'
    with pytest.raises(ArgumentError) as exception:
        DefaultWorkloadApiClient()

    assert (
        str(exception.value) ==
        'Invalid DefaultWorkloadApiClient configuration: SPIFFE endpoint socket: scheme must be set.'
    )
    del os.environ[SPIFFE_SOCKET_ENV]
Example #4
0
    def __init__(
        self,
        workload_api_client: WorkloadApiClient = None,
        spiffe_socket_path: str = None,
        timeout_in_seconds: float = None,
    ) -> None:
        """Creates a new DefaultJwtSource.

           It blocks until the initial update has been received from the Workload API or until timeout_in_seconds is reached.
           In case the underlying Workload API connection returns an unretryable error, the source will be closed and
           no methods on the source will be available.

        Args:
            workload_api_client: A WorkloadApiClient object that will be used to fetch the JWT materials from the Workload API.
                                 In case it's not provided, a default client will be created.
            spiffe_socket_path: Path to Workload API UDS. This will be used in case a the workload_api_client is not provided.
                           If not specified, the SPIFFE_ENDPOINT_SOCKET environment variable will be used and thus, must be set.
            timeout_in_seconds: Time to wait for the first update of the Workload API. If not provided, and
                                the connection with the Workload API fails, it will block indefinitely while
                                the connection is retried.

        Returns:
            DefaultJwtSource: New DefaultJwtSource object, initialized with the JwtBundleSet fetched from the Workload API.

        Raises:
            ArgumentError: If spiffe_socket_path is invalid or not provided and SPIFFE_ENDPOINT_SOCKET env variable is not set.
            JwtSourceError: In case a timeout was configured and it was reached during the source initialization waiting
                             for the first update from the Workload API.
        """

        self._initialized = threading.Event()
        self._lock = threading.Lock()
        self._closed = False
        self._workload_api_client = (
            workload_api_client if workload_api_client else
            DefaultWorkloadApiClient(spiffe_socket_path))

        # set the watcher that will keep the source updated and log the underlying errors
        self._client_cancel_handler = self._workload_api_client.watch_jwt_bundles(
            self._set_jwt_data, self._on_error)
        self._initialized.wait(timeout_in_seconds)

        if not self._initialized.is_set():
            self._client_cancel_handler.cancel()
            raise JwtSourceError(
                'Could not initialize JWT Source: reached timeout waiting for the first update'
            )
def get_client():
    return DefaultWorkloadApiClient(spiffe_socket='unix:///tmp/agent.sock')
def test_instantiate_bad_socket_path():
    with pytest.raises(ValueError):
        DefaultWorkloadApiClient(spiffe_socket='/invalid')
def test_instantiate_default_with_bad_var():
    os.environ[SPIFFE_SOCKET_ENV] = '/invalid'
    with pytest.raises(ValueError):
        DefaultWorkloadApiClient()
    del os.environ[SPIFFE_SOCKET_ENV]
def test_instantiate_socket_path():
    wlapi = DefaultWorkloadApiClient(spiffe_socket='unix:///tmp/agent.sock')
    assert wlapi.get_spiffe_endpoint_socket() == 'unix:///tmp/agent.sock'
def test_instantiate_default_with_var():
    os.environ[SPIFFE_SOCKET_ENV] = 'unix:///tmp/agent.sock'
    wlapi = DefaultWorkloadApiClient()
    del os.environ[SPIFFE_SOCKET_ENV]
    assert wlapi.get_spiffe_endpoint_socket() == 'unix:///tmp/agent.sock'
def test_instantiate_default_without_var():
    with pytest.raises(ValueError):
        DefaultWorkloadApiClient()
import os
import pytest

from pyspiffe.exceptions import ArgumentError
from pyspiffe.workloadapi.default_workload_api_client import DefaultWorkloadApiClient

SPIFFE_SOCKET_ENV = 'SPIFFE_ENDPOINT_SOCKET'
WORKLOAD_API_CLIENT = DefaultWorkloadApiClient('unix:///dummy.path')


# No SPIFFE_ENDPOINT_SOCKET, and no path passed, raises exception
def test_instantiate_default_without_var():
    with pytest.raises(ArgumentError) as exception:
        DefaultWorkloadApiClient()

    assert (
        str(exception.value) ==
        'Invalid DefaultWorkloadApiClient configuration: SPIFFE endpoint socket: socket must be set.'
    )


# With SPIFFE_ENDPOINT_SOCKET, and no path passed, succeeds
def test_instantiate_default_with_var():
    os.environ[SPIFFE_SOCKET_ENV] = 'unix:///tmp/agent.sock'
    wlapi = DefaultWorkloadApiClient()
    del os.environ[SPIFFE_SOCKET_ENV]
    assert wlapi.get_spiffe_endpoint_socket() == 'unix:///tmp/agent.sock'


# Pass socket path
def test_instantiate_socket_path():