コード例 #1
0
def test_watch_jwt_bundle_no_retry_on_grpc_error_no_call(mocker):
    grpc_error = grpc.RpcError
    jwt_bundles = {
        'example.org': JWKS_1_EC_KEY,
        'domain.prod': JWKS_2_EC_1_RSA_KEYS
    }

    WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock(
        side_effect=[
            grpc_error,
            delayed_responses(
                [workload_pb2.JWTBundlesResponse(bundles=jwt_bundles)]),
        ])

    expected_error = FetchJwtBundleError(
        'Cannot process response from Workload API.')
    event = threading.Event()
    response_holder = ResponseHolder()

    WORKLOAD_API_CLIENT.watch_jwt_bundles(
        on_success=lambda r: handle_success(r, response_holder, event),
        on_error=lambda e: handle_error(e, response_holder, event),
    )

    event.wait(3)  # add timeout to prevent test from hanging

    assert not response_holder.success
    assert response_holder.error
    assert_error(response_holder.error, expected_error)
コード例 #2
0
def test_watch_jwt_bundle_success(mocker):
    jwt_bundles = {
        'example.org': JWKS_1_EC_KEY,
        'domain.prod': JWKS_2_EC_1_RSA_KEYS
    }
    jwt_bundles_2 = {'domain.dev': JWKS_1_EC_KEY}

    WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock(
        return_value=delayed_responses([
            workload_pb2.JWTBundlesResponse(bundles=jwt_bundles),
            workload_pb2.JWTBundlesResponse(bundles=jwt_bundles_2),
        ]))

    event = threading.Event()
    response_holder = ResponseHolder()

    WORKLOAD_API_CLIENT.watch_jwt_bundles(
        on_success=lambda r: handle_success(r, response_holder, event),
        on_error=lambda e: handle_error(e, response_holder, event),
    )

    event.wait(3)  # add timeout to prevent test from hanging

    assert not response_holder.error
    jwt_bundle_set = response_holder.success
    assert jwt_bundle_set
    jwt_bundle_1 = jwt_bundle_set.get(TrustDomain.parse('example.org'))
    assert jwt_bundle_1
    assert len(jwt_bundle_1.jwt_authorities()) == 1

    jwt_bundle_2 = jwt_bundle_set.get(TrustDomain.parse('domain.prod'))
    assert jwt_bundle_2
    assert len(jwt_bundle_2.jwt_authorities()) == 3

    # Wait to receive the second response from delayed_responses()
    time.sleep(1)

    assert not response_holder.error
    jwt_bundle_set = response_holder.success
    jwt_bundle = jwt_bundle_set.get(TrustDomain.parse('domain.dev'))
    assert jwt_bundle
    assert len(jwt_bundle.jwt_authorities()) == 1
コード例 #3
0
def test_watch_jwt_bundle_no_retry_on_error(mocker):
    some_error = Exception('Some Error')

    WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock(
        side_effect=some_error, )

    expected_error = FetchJwtBundleError(str(some_error))
    event = threading.Event()
    response_holder = ResponseHolder()

    WORKLOAD_API_CLIENT.watch_jwt_bundles(
        on_success=lambda r: handle_success(r, response_holder, event),
        on_error=lambda e: handle_error(e, response_holder, event),
    )

    event.wait(3)  # add timeout to prevent test from hanging

    assert not response_holder.success
    assert response_holder.error
    assert_error(response_holder.error, expected_error)
コード例 #4
0
def test_watch_jwt_bundle_no_retry_on_grpc_error(mocker):
    grpc_error = FakeCall()
    grpc_error._code = grpc.StatusCode.INVALID_ARGUMENT

    WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock(
        side_effect=[
            grpc_error,
        ])

    expected_error = FetchJwtBundleError(grpc_error.details())
    event = threading.Event()
    response_holder = ResponseHolder()

    WORKLOAD_API_CLIENT.watch_jwt_bundles(
        on_success=lambda r: handle_success(r, response_holder, event),
        on_error=lambda e: handle_error(e, response_holder, event),
    )

    event.wait(3)  # add timeout to prevent test from hanging

    assert not response_holder.success
    assert response_holder.error
    assert_error(response_holder.error, expected_error)
コード例 #5
0
def test_watch_jwt_bundle_retry_on_grpc_error(mocker):
    grpc_error = FakeCall()
    jwt_bundles = {
        'example.org': JWKS_1_EC_KEY,
        'domain.prod': JWKS_2_EC_1_RSA_KEYS
    }

    WORKLOAD_API_CLIENT._spiffe_workload_api_stub.FetchJWTBundles = mocker.Mock(
        side_effect=[
            grpc_error,
            delayed_responses(
                [workload_pb2.JWTBundlesResponse(bundles=jwt_bundles)]),
        ])

    expected_error = FetchJwtBundleError(grpc_error.details())
    event = threading.Event()
    response_holder = ResponseHolder()

    WORKLOAD_API_CLIENT.watch_jwt_bundles(
        on_success=lambda r: handle_success(r, response_holder, event),
        on_error=lambda e: assert_error(e, expected_error),
    )

    event.wait(3)  # add timeout to prevent test from hanging
    # Wait to receive the response from delayed_responses()
    time.sleep(1)

    jwt_bundle_set = response_holder.success
    assert jwt_bundle_set
    jwt_bundle_1 = jwt_bundle_set.get(TrustDomain.parse('example.org'))
    assert jwt_bundle_1
    assert len(jwt_bundle_1.jwt_authorities()) == 1

    jwt_bundle_2 = jwt_bundle_set.get(TrustDomain.parse('domain.prod'))
    assert jwt_bundle_2
    assert len(jwt_bundle_2.jwt_authorities()) == 3