Beispiel #1
0
def load(options, path, format='yaml', validate=True, vars=None):
    # should we do os.path.expanduser here?
    if not os.path.exists(path):
        raise IOError("Invalid path for config %r" % path)

    load_resources()
    data = utils.load_file(path, format=format, vars=vars)

    if format == 'json':
        validate = False

    if isinstance(data, list):
        log.warning('yaml in invalid format. The "policies:" line is probably missing.')
        return None

    # Test for empty policy file
    if not data or data.get('policies') is None:
        return None

    if validate:
        from c7n.schema import validate
        errors = validate(data)
        if errors:
            raise Exception(
                "Failed to validate on policy %s \n %s" % (
                    errors[1], errors[0]))

    collection = PolicyCollection.from_data(data, options)
    if validate:
        # non schema validation of policies
        [p.validate() for p in collection]
    return collection
Beispiel #2
0
def load(options, path, format='yaml', validate=True, vars=None):
    # should we do os.path.expanduser here?
    if not os.path.exists(path):
        raise IOError("Invalid path for config %r" % path)

    load_resources()
    data = utils.load_file(path, format=format, vars=vars)

    if format == 'json':
        validate = False

    if isinstance(data, list):
        log.warning('yaml in invalid format. The "policies:" line is probably missing.')
        return None

    # Test for empty policy file
    if not data or data.get('policies') is None:
        return None

    if validate:
        from c7n.schema import validate
        errors = validate(data)
        if errors:
            raise Exception(
                "Failed to validate on policy %s \n %s" % (
                    errors[1], errors[0]))

    collection = PolicyCollection.from_data(data, options)
    if validate:
        # non schema validation of policies
        [p.validate() for p in collection]
    return collection
Beispiel #3
0
def load(options, path, format=None, validate=True, vars=None):
    # should we do os.path.expanduser here?
    if not os.path.exists(path):
        raise IOError("Invalid path for config %r" % path)

    from c7n.schema import validate, StructureParser
    data = utils.load_file(path, format=format, vars=vars)

    structure = StructureParser()
    structure.validate(data)
    load_resources(structure.get_resource_types(data))

    if isinstance(data, list):
        log.warning(
            'yaml in invalid format. The "policies:" line is probably missing.'
        )
        return None

    if validate:
        errors = validate(data)
        if errors:
            raise PolicyValidationError("Failed to validate policy %s \n %s" %
                                        (errors[1], errors[0]))

    # Test for empty policy file
    if not data or data.get('policies') is None:
        return None

    collection = PolicyCollection.from_data(data, options)
    if validate:
        # non schema validation of policies
        [p.validate() for p in collection]
    return collection
Beispiel #4
0
    def test_load_file(self):
        # Basic load
        yml_file = os.path.join(os.path.dirname(__file__), 'data', 'vars-test.yml')
        data = utils.load_file(yml_file)
        self.assertTrue(len(data['policies']) == 1)

        # Load with vars
        resource = 'ec2'
        data = utils.load_file(yml_file, vars={'resource': resource})
        self.assertTrue(data['policies'][0]['resource'] == resource)

        # Fail to substitute
        self.assertRaises(utils.VarsSubstitutionError, utils.load_file, yml_file, vars={'foo': 'bar'})

        # JSON load
        json_file = os.path.join(os.path.dirname(__file__), 'data', 'ec2-instance.json')
        data = utils.load_file(json_file)
        self.assertTrue(data['InstanceId'] == 'i-1aebf7c0')
Beispiel #5
0
 def load_file(self, path, resource_key):
     data = load_file(path)
     if resource_key:
         data = jmespath.search(resource_key, data)
     if not isinstance(data, list):
         raise PolicyExecutionError(
             "found disk records at %s in non list format %s" %
             (path, type(data)))
     return DataFile(path, resource_key, data)
    def test_load_file(self):
        # Basic load
        yml_file = os.path.join(os.path.dirname(__file__), 'data', 'vars-test.yml')
        data = utils.load_file(yml_file)
        self.assertTrue(len(data['policies']) == 1)

        # Load with vars
        resource = 'ec2'
        data = utils.load_file(yml_file, vars={'resource': resource})
        self.assertTrue(data['policies'][0]['resource'] == resource)

        # Fail to substitute
        self.assertRaises(utils.VarsSubstitutionError, utils.load_file, yml_file, vars={'foo': 'bar'})

        # JSON load
        json_file = os.path.join(os.path.dirname(__file__), 'data', 'ec2-instance.json')
        data = utils.load_file(json_file)
        self.assertTrue(data['InstanceId'] == 'i-1aebf7c0')
Beispiel #7
0
    def test_load_file(self):
        # Basic load
        yml_file = os.path.join(os.path.dirname(__file__), "data", "vars-test.yml")
        data = utils.load_file(yml_file)
        self.assertTrue(len(data["policies"]) == 1)

        # Load with vars
        resource = "ec2"
        data = utils.load_file(yml_file, vars={"resource": resource})
        self.assertTrue(data["policies"][0]["resource"] == resource)

        # Fail to substitute
        self.assertRaises(
            utils.VarsSubstitutionError, utils.load_file, yml_file, vars={"foo": "bar"}
        )

        # JSON load
        json_file = os.path.join(os.path.dirname(__file__), "data", "ec2-instance.json")
        data = utils.load_file(json_file)
        self.assertTrue(data["InstanceId"] == "i-1aebf7c0")
Beispiel #8
0
def _load_vars(options):
    vars = None
    if options.vars:
        try:
            vars = load_file(options.vars)
        except IOError as e:
            log.error('Problem loading vars file "{}": {}'.format(options.vars, e.strerror))
            sys.exit(1)

    # TODO - provide builtin vars here (such as account)

    return vars
def _load_vars(options):
    vars = None
    if options.vars:
        try:
            vars = load_file(options.vars)
        except IOError as e:
            log.error('Problem loading vars file "{}": {}'.format(options.vars, e.strerror))
            sys.exit(1)

    # TODO - provide builtin vars here (such as account)

    return vars
Beispiel #10
0
def load(options, path, format='yaml', validate=True, vars=None):
    # should we do os.path.expanduser here?
    if not os.path.exists(path):
        raise IOError("Invalid path for config %r" % path)

    load_resources()
    data = utils.load_file(path, format=format, vars=vars)

    if format == 'json':
        validate = False

    # Test for empty policy file
    if not data or data.get('policies') is None:
        return None

    if validate:
        from c7n.schema import validate
        errors = validate(data)
        if errors:
            raise Exception("Failed to validate on policy %s \n %s" % (errors[1], errors[0]))

    collection = PolicyCollection.from_data(data, options)
    return collection
Beispiel #11
0
 def load_file(self, file_path, format=None):
     # should we do os.path.expanduser here?
     if not os.path.exists(file_path):
         raise IOError("Invalid path for config %r" % file_path)
     policy_data = load_file(file_path, format=format)
     return self.load_data(policy_data, file_path)