Example #1
0
 def test_hex_encoded_cap(self, urandom):
     """
     generate_capability will hex encode the random bytes from os.urandom.
     """
     urandom.return_value = '\xde\xad\xbe\xef'
     (_v, cap) = generate_capability()
     self.assertEqual(cap, 'deadbeef')
Example #2
0
 def test_hex_encoded_cap(self, urandom):
     """
     generate_capability will hex encode the random bytes from os.urandom.
     """
     urandom.return_value = '\xde\xad\xbe\xef'
     (_v, cap) = generate_capability()
     self.assertEqual(cap, 'deadbeef')
Example #3
0
 def test_urandom_32_bytes(self, urandom):
     """
     generate_capability will use os.urandom to get 32 bytes of random
     data.
     """
     (_v, _cap) = generate_capability()
     urandom.assert_called_once_with(32)
Example #4
0
    def create_webhooks(self, policy_id, data):
        """
        see :meth:`otter.models.interface.IScalingGroup.create_webhooks`
        """
        if self.error is not None:
            return defer.fail(self.error)

        if policy_id in self.policies:
            max_webhooks = config_value('limits.absolute.maxWebhooksPerPolicy')
            curr_webhooks = len(self.webhooks.get(policy_id, []))
            if len(data) + curr_webhooks > max_webhooks:
                return defer.fail(
                    WebhooksOverLimitError(self.tenant_id, self.uuid,
                                           policy_id, max_webhooks,
                                           curr_webhooks, len(data)))

            created = []
            for webhook_input in data:
                webhook_real = {'metadata': {}}
                webhook_real.update(webhook_input)
                webhook_real['capability'] = {}

                (webhook_real['capability']['version'],
                 webhook_real['capability']['hash']) = generate_capability()

                uuid = str(uuid4())
                self.webhooks[policy_id][uuid] = webhook_real
                # return a copy so this store doesn't get mutated
                created.append(dict(id=uuid, **webhook_real))

            return defer.succeed(created)
        else:
            return defer.fail(NoSuchPolicyError(self.tenant_id,
                                                self.uuid, policy_id))
Example #5
0
 def test_urandom_32_bytes(self, urandom):
     """
     generate_capability will use os.urandom to get 32 bytes of random
     data.
     """
     (_v, _cap) = generate_capability()
     urandom.assert_called_once_with(32)
Example #6
0
File: mock.py Project: alex/otter
    def create_webhooks(self, policy_id, data):
        """
        see :meth:`otter.models.interface.IScalingGroup.create_webhooks`
        """
        if self.error is not None:
            return defer.fail(self.error)

        if policy_id in self.policies:
            created = {}
            for webhook_input in data:
                webhook_real = {'metadata': {}}
                webhook_real.update(webhook_input)
                webhook_real['capability'] = {}

                (webhook_real['capability']['version'],
                 webhook_real['capability']['hash']) = generate_capability()

                uuid = str(uuid4())
                self.webhooks[policy_id][uuid] = webhook_real
                # return a copy so this store doesn't get mutated
                created[uuid] = webhook_real.copy()

            return defer.succeed(created)
        else:
            return defer.fail(NoSuchPolicyError(self.tenant_id,
                                                self.uuid, policy_id))
Example #7
0
    def create_webhooks(self, policy_id, data):
        """
        see :meth:`otter.models.interface.IScalingGroup.create_webhooks`
        """
        if self.error is not None:
            return defer.fail(self.error)

        if policy_id in self.policies:
            created = {}
            for webhook_input in data:
                webhook_real = {'metadata': {}}
                webhook_real.update(webhook_input)
                webhook_real['capability'] = {}

                (webhook_real['capability']['version'],
                 webhook_real['capability']['hash']) = generate_capability()

                uuid = str(uuid4())
                self.webhooks[policy_id][uuid] = webhook_real
                # return a copy so this store doesn't get mutated
                created[uuid] = webhook_real.copy()

            return defer.succeed(created)
        else:
            return defer.fail(
                NoSuchPolicyError(self.tenant_id, self.uuid, policy_id))
Example #8
0
def _build_webhooks(bare_webhooks, webhooks_table, queries, cql_parameters,
                    output):
    """
    Because inserting many values into a table with compound keys with one
    insert statement is hard. This builds a bunch of insert statements and a
    dictionary matching different parameter names to different policies.

    :param bare_webhooks: a list of webhook data without ID or webhook keys,
        or any generated capability hash info
    :type bare_webhooks: ``list`` of ``dict``

    :param webhooks_table: the name of the webhooks table
    :type webhooks_table: ``str``

    :param queries: a list of existing CQL queries to add to
    :type queries: ``list`` of ``str``

    :param cql_parameters: the dictionary of named parameters and values passed
        in addition to the query to execute the query - additional parameters
        will be added to this dictionary
    :type cql_paramters: ``dict``

    :param output: a dictionary to which to insert the created policies
        along with their generated IDs
    :type output: ``dict``
    """
    for i, webhook in enumerate(bare_webhooks):
        name = "webhook{0}".format(i)
        webhook_id = generate_key_str('webhook')
        queries.append(_cql_insert_webhook.format(cf=webhooks_table,
                                                  name=name))

        # generate the real data that will be stored, which includes the webhook
        # token, the capability stuff, and metadata by default
        bare_webhooks[i].setdefault('metadata', {})
        version, cap_hash = generate_capability()

        cql_parameters[name] = serialize_json_data(webhook, 1)
        cql_parameters['{0}Id'.format(name)] = webhook_id
        cql_parameters['{0}Key'.format(name)] = cap_hash
        cql_parameters['{0}Capability'.format(name)] = serialize_json_data(
            {version: cap_hash}, 1)

        output[webhook_id] = webhook.copy()
        output[webhook_id]['capability'] = {
            'hash': cap_hash,
            'version': version
        }
Example #9
0
def _build_webhooks(bare_webhooks, webhooks_table, queries, cql_parameters):
    """
    Because inserting many values into a table with compound keys with one
    insert statement is hard. This builds a bunch of insert statements and a
    dictionary matching different parameter names to different policies.

    :param bare_webhooks: a list of webhook data without ID or webhook keys,
        or any generated capability hash info
    :type bare_webhooks: ``list`` of ``dict``

    :param webhooks_table: the name of the webhooks table
    :type webhooks_table: ``str``

    :param queries: a list of existing CQL queries to add to
    :type queries: ``list`` of ``str``

    :param cql_parameters: the dictionary of named parameters and values passed
        in addition to the query to execute the query - additional parameters
        will be added to this dictionary
    :type cql_paramters: ``dict``

    :returns: ``list`` of the created webhooks along with their IDs
    """
    output = []
    for i, webhook in enumerate(bare_webhooks):
        name = "webhook{0}".format(i)
        webhook_id = generate_key_str('webhook')
        queries.append(_cql_insert_webhook.format(cf=webhooks_table,
                                                  name=name))

        # generate the real data that will be stored, which includes the webhook
        # token, the capability stuff, and metadata by default
        bare_webhooks[i].setdefault('metadata', {})
        version, cap_hash = generate_capability()

        cql_parameters[name] = serialize_json_data(webhook, 1)
        cql_parameters['{0}Id'.format(name)] = webhook_id
        cql_parameters['{0}Key'.format(name)] = cap_hash
        cql_parameters['{0}Capability'.format(name)] = serialize_json_data(
            {version: cap_hash}, 1)

        output.append(dict(id=webhook_id,
                           capability={'hash': cap_hash, 'version': version},
                           **webhook))
    return output
Example #10
0
 def test_version_1(self):
     """
     generate_capability returns a version 1 capability.
     """
     (v, _cap) = generate_capability()
     self.assertEqual(v, "1")
Example #11
0
 def test_version_1(self):
     """
     generate_capability returns a version 1 capability.
     """
     (v, _cap) = generate_capability()
     self.assertEqual(v, "1")