How to use the c7n.utils function in c7n

To help you get started, we’ve selected a few c7n examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cloud-custodian / cloud-custodian / tests / test_sqsexec.py View on Github external
"QueueUrl"
        ]
        self.addCleanup(client.delete_queue, QueueUrl=reduce_queue)

        with SQSExecutor(session_factory, map_queue, reduce_queue) as w:
            w.op_sequence_start = 699723
            w.op_sequence = 699723
            # Submit work
            futures = []
            for i in range(10):
                futures.append(w.submit(int_processor, i))

            # Manually process and send results
            messages = MessageIterator(client, map_queue, limit=10)
            for m in messages:
                d = utils.loads(m["Body"])
                self.assertEqual(
                    m["MessageAttributes"]["op"]["StringValue"],
                    "tests.test_sqsexec:int_processor",
                )
                client.send_message(
                    QueueUrl=reduce_queue,
                    MessageBody=utils.dumps([d["args"], int_processor(*d["args"])]),
                    MessageAttributes=m["MessageAttributes"],
                )
            w.gather()
            results = [
                json.loads(r.result()["Body"]) for r in list(as_completed(futures))
            ]
            self.assertEqual(list(sorted(results))[-1], [[9], 18])
github cloud-custodian / cloud-custodian / tests / test_utils.py View on Github external
def test_format_event(self):
        event = {"message": "This is a test", "timestamp": 1234567891011}
        event_json = (
            '{\n  "timestamp": 1234567891011, \n' '  "message": "This is a test"\n}'
        )
        self.assertEqual(json.loads(utils.format_event(event)), json.loads(event_json))
github cloud-custodian / cloud-custodian / tests / test_utils.py View on Github external
def test_generate_arn(self):
        self.assertEqual(
            utils.generate_arn("s3", "my_bucket"), "arn:aws:s3:::my_bucket"
        )

        self.assertEqual(
            utils.generate_arn("s3", "my_bucket", region="us-gov-west-1"),
            "arn:aws-us-gov:s3:::my_bucket"
        )

        self.assertEqual(
            utils.generate_arn(
                "cloudformation",
                "MyProductionStack/abc9dbf0-43c2-11e3-a6e8-50fa526be49c",
                region="us-east-1",
                account_id="123456789012",
                resource_type="stack",
            ),
            "arn:aws:cloudformation:us-east-1:123456789012:"
github cloud-custodian / cloud-custodian / tests / test_utils.py View on Github external
def test_format_date(self):
        d = parse_date("2018-02-02 12:00")
        self.assertEqual("{}".format(utils.FormatDate(d)), "2018-02-02 12:00:00")

        self.assertEqual("{:%Y-%m-%d}".format(utils.FormatDate(d)), "2018-02-02")

        self.assertEqual("{:+5h%H}".format(utils.FormatDate(d)), "17")

        self.assertEqual("{:+5d%d}".format(utils.FormatDate(d)), "07")

        self.assertEqual("{:+5M%M}".format(utils.FormatDate(d)), "05")
github cloud-custodian / cloud-custodian / tests / test_utils.py View on Github external
"default": {"type": "object"},
                    "key": {"type": "string"},
                    "op": {"enum": ["regex", "ni", "gt", "not-in"]},
                    "value": {
                        "oneOf": [
                            {"type": "array"},
                            {"type": "string"},
                            {"type": "boolean"},
                            {"type": "number"},
                        ]
                    },
                },
                "required": ["key"],
            }

        ret = utils.reformat_schema(FakeResource)
        self.assertIsInstance(ret, dict)

        # Test error conditions
        # Instead of testing for specific keywords, just make sure that strings
        # are returned instead of a dictionary.
        FakeResource.schema = {}
        ret = utils.reformat_schema(FakeResource)
        self.assertIsInstance(ret, six.text_type)

        delattr(FakeResource, "schema")
        ret = utils.reformat_schema(FakeResource)
        self.assertIsInstance(ret, six.text_type)
github cloud-custodian / cloud-custodian / tests / test_utils.py View on Github external
def test_camel_case(self):
        d = {
            "zebraMoon": [{"instanceId": 123}, "moon"],
            "color": {"yellow": 1, "green": 2},
        }
        self.assertEqual(
            utils.camelResource(d),
            {
                "ZebraMoon": [{"InstanceId": 123}, "moon"],
                "Color": {"Yellow": 1, "Green": 2},
            },
github cloud-custodian / cloud-custodian / c7n / policy.py View on Github external
def provision(self):
        if self.policy.data['resource'] == 'ec2':
            self.policy.data['mode']['resource-filter'] = 'Instance'
        elif self.policy.data['resource'] == 'iam-user':
            self.policy.data['mode']['resource-filter'] = 'AccessKey'
        return super(GuardDutyMode, self).provision()


@execution.register('config-rule')
class ConfigRuleMode(LambdaMode):
    """a lambda policy that executes as a config service rule.
        http://docs.aws.amazon.com/config/latest/APIReference/API_PutConfigRule.html
    """

    cfg_event = None
    schema = utils.type_schema('config-rule', rinherit=LambdaMode.schema)

    def validate(self):
        super(ConfigRuleMode, self).validate()
        if not self.policy.resource_manager.resource_type.config_type:
            raise PolicyValidationError(
                "policy:%s AWS Config does not support resource-type:%s" % (
                    self.policy.name, self.policy.resource_type))

    def resolve_resources(self, event):
        source = self.policy.resource_manager.get_source('config')
        return [source.load_resource(self.cfg_event['configurationItem'])]

    def run(self, event, lambda_context):
        self.cfg_event = json.loads(event['invokingEvent'])
        cfg_item = self.cfg_event['configurationItem']
        evaluation = None
github cloud-custodian / cloud-custodian / c7n / actions / webhook.py View on Github external
def _build_http_manager(self):
        pool_kwargs = {
            'cert_reqs': 'CERT_REQUIRED',
            'ca_certs': certifi and certifi.where() or None
        }

        proxy_url = utils.get_proxy_url(self.url)
        if proxy_url:
            return urllib3.ProxyManager(proxy_url, **pool_kwargs)
        else:
            return urllib3.PoolManager(**pool_kwargs)
github cloud-custodian / cloud-custodian / c7n / actions / invoke.py View on Github external
def process(self, resources, event=None):
        params = dict(FunctionName=self.data['function'])
        if self.data.get('qualifier'):
            params['Qualifier'] = self.data['Qualifier']

        if self.data.get('async', True):
            params['InvocationType'] = 'Event'

        config = Config(read_timeout=self.data.get(
            'timeout', 90), region_name=self.data.get('region', None))
        client = utils.local_session(
            self.manager.session_factory).client('lambda', config=config)

        payload = {
            'version': VERSION,
            'event': event,
            'action': self.data,
            'policy': self.manager.data}

        results = []
        for resource_set in utils.chunks(resources, self.data.get('batch_size', 250)):
            payload['resources'] = resource_set
            params['Payload'] = utils.dumps(payload)
            result = client.invoke(**params)
            result['Payload'] = result['Payload'].read()
            if isinstance(result['Payload'], bytes):
                result['Payload'] = result['Payload'].decode('utf-8')
github cloud-custodian / cloud-custodian / c7n / actions.py View on Github external
def process(self, resources, event=None):
        alias = utils.get_account_alias_from_sts(
            utils.local_session(self.manager.session_factory))
        message = {
            'event': event,
            'account_id': self.manager.config.account_id,
            'account': alias,
            'region': self.manager.config.region,
            'policy': self.manager.data}
        message['action'] = self.expand_variables(message)

        for batch in utils.chunks(resources, self.batch_size):
            message['resources'] = self.prepare_resources(batch)
            receipt = self.send_data_message(message)
            self.log.info("sent message:%s policy:%s template:%s count:%s" % (
                receipt, self.manager.data['name'],
                self.data.get('template', 'default'), len(batch)))