def _setUp(self, subfolder):
     session = boto3.Session()
     pill = placebo.attach(session,
                           data_path=os.path.join(self.PLACEBO_PATH,
                                                  subfolder))
     pill.playback()
     self.ssm_client = session.client('ssm')
     SSMParameter.set_ssm_client(self.ssm_client)
    def test_with_valid_client(self):
        """ Test invalid client (without required methods) """

        # pylint: disable=unused-argument,no-self-use
        class MyValidClient(object):
            """ This client has all the required methods """
            def get_parameters(self, *args, **kwargs):
                """ Mock method """
                return {
                    'InvalidParameters': [],
                    'Parameters': [
                        {
                            "Type": "String",
                            "Name": "my_param",
                            "Value": "abc123",
                            "Version": 1
                        },
                    ],
                }

            def get_parameters_by_path(self, *args, **kwargs):
                """ Mock method """
                return {
                    "Parameters": [
                        {
                            "Type": "String",
                            "Name": "/foo/bar/1",
                            "Value": "abc123",
                            "Version": 1
                        },
                        {
                            "Type": "String",
                            "Name": "/foo/bar/2",
                            "Value": "abc123",
                            "Version": 1
                        },
                    ]
                }

        client = MyValidClient()
        SSMParameter.set_ssm_client(client)

        param = SSMParameter("my_param")
        self.assertEqual(param.value, self.PARAM_VALUE)

        group = SSMParameterGroup()
        param = group.parameter("my_param")
        self.assertEqual(param.value, self.PARAM_VALUE)

        params = group.parameters("/foo/bar/")
        self.assertEqual(len(params), 2)
        for param in params:
            self.assertEqual(param.value, self.PARAM_VALUE)
    def test_with_placebo(self):
        """ Test that set_ssm_client works fine with Placebo """
        session = boto3.Session()
        pill = placebo.attach(session, data_path=self.PLACEBO_PATH)
        pill.playback()

        client = session.client('ssm')

        SSMParameter.set_ssm_client(client)

        param = SSMParameter("my_param")
        self.assertEqual(param.value, self.PARAM_VALUE)
    def test_with_illegal_client(self):
        """ Test invalid client (without required methods) """
        with self.assertRaises(TypeError):
            SSMParameter.set_ssm_client(42)

        # pylint: disable=too-few-public-methods
        class MyInvalidClient(object):
            """ This client only has get_parameters """
            def get_parameters(self):
                """ Empty method """

        with self.assertRaises(TypeError):
            client = MyInvalidClient()
            SSMParameter.set_ssm_client(client)
예제 #5
0
    def _setUp(self, class_name, test_name):
        os.environ['CHAOS_PARAM'] = SSM_CONFIG_FILE
        session = boto3.Session()
        dir_name = os.path.join(self.PLACEBO_PATH, class_name, test_name)
        pill = placebo.attach(session,
                              data_path=os.path.join(self.PLACEBO_PATH,
                                                     class_name, test_name))

        if PLACEBO_MODE == "record":
            try:
                os.makedirs(dir_name)
            except FileExistsError:
                print("Directory already exists")
            print("Recording")
            pill.record()
        else:
            pill.playback()

        self.ssm_client = session.client('ssm')
        SSMParameter.set_ssm_client(self.ssm_client)
예제 #6
0
import boto3
from pathlib import Path
from ssm_cache import SSMParameterGroup, SSMParameter, InvalidParameterError

VALID_PARAM_TYPES = ["String", "SecureString", "StringList"]

# make sure we're using the same client as the SSMParameter objects
ssm = boto3.client('ssm')
SSMParameter.set_ssm_client(ssm)


def get_stages(project):
    project_path = (Path("/") / project).as_posix()
    group = SSMParameterGroup(base_path=project_path)
    param_paths = [Path(x.full_name) for x in group.parameters("/")]
    stage_names = set(p.parts[2] for p in param_paths)
    return [Stage(project, x) for x in stage_names]


def create_param_path(project, stage_name, param_name):
    return (Path("/") / project / stage_name / param_name).as_posix()


class ParameterNotFound(Exception):
    pass


class ParamSchemaValidationError(Exception):
    def __init__(self, message=None, errors=[]):
        super(ParamSchemaValidationError, self).__init__(message)
        self.errors = errors
예제 #7
0
 def setUp(self):
     session = boto3.Session()
     pill = placebo.attach(session, data_path=self.PLACEBO_PATH)
     pill.playback()
     ssm_client = session.client('ssm')
     SSMParameter.set_ssm_client(ssm_client)