Skip to content

Commit

Permalink
secrets: add new fixtures for secret store
Browse files Browse the repository at this point in the history
  • Loading branch information
okraskaj committed Jun 28, 2019
1 parent d20d1c3 commit 6d5a924
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 7 deletions.
61 changes: 60 additions & 1 deletion pytest_reana/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

from __future__ import absolute_import, print_function

import base64
import json
import os
import shutil
from uuid import uuid4

import pkg_resources
import pytest
from kombu import Connection, Exchange, Producer, Queue
from mock import ANY, patch
from kubernetes import client
from mock import ANY, Mock, patch
from reana_commons.consumer import BaseConsumer
from reana_db.models import Base, User, Workflow
from sqlalchemy import create_engine
Expand Down Expand Up @@ -527,3 +530,59 @@ def sample_condition_for_starting_queued_workflows():
def sample_condition_for_requeueing_workflows():
"""Sample always false condition."""
return False


@pytest.fixture
def user_secrets():
"""Test user secrets dictionary."""
keytab_file = base64.b64encode(b'keytab file.')
user_secrets = {
"username": {"value": "reanauser",
"type": "env"},
"password": {"value": "1232456",
"type": "env"},
".keytab": {"value": keytab_file,
"type": "file"}
}
return user_secrets


@pytest.fixture
def empty_user_secrets():
"""Empty user secrets dictionary."""
return {}


@pytest.fixture
def corev1_api_client_with_user_secrets(default_user):
"""Kubernetes CoreV1 api client with user secrets in K8s secret store.
Scope: function
Adds the CoreV1APIClient with example user secrets.
"""
def make_corev1_api_client_with_user_secrets(user_secrets):
"""Callable to return.
Should be used with one of the secret store fixtures.
"""
corev1_api_client = Mock()
metadata = client.V1ObjectMeta(name=str(default_user.id_))
metadata.annotations = {'secrets_types': '{}'}
user_secrets_values = {}
secrets_types = {}
for secret_name in user_secrets:
# Add type metadata to secret store
secrets_types[secret_name] = \
user_secrets[secret_name]['type']
user_secrets_values[secret_name] = \
user_secrets[secret_name]['value']
metadata.annotations['secrets_types'] = json.dumps(secrets_types)
k8s_secrets_store = client.V1Secret(
api_version="v1",
metadata=metadata,
data=user_secrets_values)
corev1_api_client.read_namespaced_secret = \
lambda name, namespace: k8s_secrets_store
return corev1_api_client
return make_corev1_api_client_with_user_secrets
11 changes: 6 additions & 5 deletions pytest_reana/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@


from .fixtures import (ConsumerBase, ConsumerBaseOnMessageMock, app,
consume_queue, cwl_workflow_with_name,
cwl_workflow_without_name, db_engine, default_exchange,
default_in_memory_producer, default_queue, default_user,
consume_queue, corev1_api_client_with_user_secrets,
cwl_workflow_with_name, cwl_workflow_without_name,
db_engine, default_exchange, default_in_memory_producer,
default_queue, default_user, empty_user_secrets,
in_memory_queue_connection,
sample_serial_workflow_in_db, sample_workflow_workspace,
sample_yadage_workflow_in_db, serial_workflow, session,
tmp_shared_volume_path, yadage_workflow_with_name,
yadage_workflow_without_name)
tmp_shared_volume_path, user_secrets,
yadage_workflow_with_name, yadage_workflow_without_name)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
'mock>=2.0',
'pika>=0.12.0,<0.13',
'pytest>=3.8.0',
'reana-commons>=0.5.0.dev20190416',
'reana-commons[kubernetes]>=0.5.0.dev20190416',
'reana-db>=0.5.0.dev20190416',
]

Expand Down

0 comments on commit 6d5a924

Please sign in to comment.