Пример #1
0
    def _validate_action_prefix(action):
        action_parts = action.split(":")
        if len(action_parts) == 1 and action_parts[0] != "*":
            raise MalformedPolicyDocument(
                "Actions/Conditions must be prefaced by a vendor, e.g., iam, sdb, ec2, etc."
            )
        elif len(action_parts) > 2:
            raise MalformedPolicyDocument(
                "Actions/Condition can contain only one colon.")

        vendor_pattern = re.compile(r"[^a-zA-Z0-9\-.]")
        if action_parts[0] != "*" and vendor_pattern.search(action_parts[0]):
            raise MalformedPolicyDocument(
                "Vendor {vendor} is not valid".format(vendor=action_parts[0]))
Пример #2
0
    def _validate_registry_policy_action(self, policy_text):
        # only CreateRepository & ReplicateImage actions are allowed
        VALID_ACTIONS = {"ecr:CreateRepository", "ecr:ReplicateImage"}

        policy = json.loads(policy_text)
        for statement in policy["Statement"]:
            if set(statement["Action"]) - VALID_ACTIONS:
                raise MalformedPolicyDocument()
Пример #3
0
    def validate(self):
        try:
            self._validate_syntax()
        except Exception:
            raise MalformedPolicyDocument("Syntax errors in policy.")
        try:
            self._validate_version()
        except Exception:
            raise MalformedPolicyDocument(
                "Policy document must be version 2012-10-17 or greater.")
        try:
            self._perform_first_legacy_parsing()
            self._validate_resources_for_formats()
            self._validate_not_resources_for_formats()
        except Exception:
            raise MalformedPolicyDocument("The policy failed legacy parsing")
        try:
            self._validate_sid_uniqueness()
        except Exception:
            raise MalformedPolicyDocument(
                "Statement IDs (SID) in a single policy must be unique.")
        try:
            self._validate_action_like_exist()
        except Exception:
            raise MalformedPolicyDocument(
                "Policy statement must contain actions.")
        try:
            self._validate_resource_exist()
        except Exception:
            raise MalformedPolicyDocument(
                "Policy statement must contain resources.")

        if self._resource_error != "":
            raise MalformedPolicyDocument(self._resource_error)

        self._validate_actions_for_prefixes()
        self._validate_not_actions_for_prefixes()