Exemple #1
0
    def create_pipeline(self, region, pipeline, tags):
        if pipeline["name"] in self.pipelines:
            raise InvalidStructureException(
                "A pipeline with the name '{0}' already exists in account '{1}'"
                .format(pipeline["name"], ACCOUNT_ID))

        try:
            role = self.iam_backend.get_role_by_arn(pipeline["roleArn"])
            service_principal = json.loads(
                role.assume_role_policy_document
            )["Statement"][0]["Principal"]["Service"]
            if "codepipeline.amazonaws.com" not in service_principal:
                raise IAMNotFoundException("")
        except IAMNotFoundException:
            raise InvalidStructureException(
                "CodePipeline is not authorized to perform AssumeRole on role {}"
                .format(pipeline["roleArn"]))

        if len(pipeline["stages"]) < 2:
            raise InvalidStructureException(
                "Pipeline has only 1 stage(s). There should be a minimum of 2 stages in a pipeline"
            )

        self.pipelines[pipeline["name"]] = CodePipeline(region, pipeline)

        if tags:
            self.pipelines[pipeline["name"]].validate_tags(tags)

            new_tags = {tag["key"]: tag["value"] for tag in tags}
            self.pipelines[pipeline["name"]].tags.update(new_tags)

        return pipeline, sorted(tags, key=lambda i: i["key"])
Exemple #2
0
def user_update_key_last_used(user, keyid, timestamp):
    """
    Allows to change LastUsedDate in moto IAM backend.

    :param user: string with user name
    :param keyid: string with keyId
    :param timestamp: datetime object with desired value for LastUsedDate
    :return: nothing, raises IAMNotFoundException if user/keyId was not found
    """
    for idx, key in enumerate(iam_backend.users[user].access_keys):
        if key.access_key_id == keyid:
            iam_backend.users[user].access_keys[idx].last_used = timestamp
            break
    else:
        raise IAMNotFoundException("AccessKeyId {0} not found".format(keyid))
Exemple #3
0
def user_update_key_create_date(user, keyid, timestamp):
    """
    Allows to change CreateDate in moto IAM backend.

    :param user: string with user name
    :param keyid: string with keyId
    :param timestamp: datetime object with desired value for CreateDate
    :return: nothing, raises IAMNotFoundException if user/keyId was not found
    """
    for idx, key in enumerate(iam_backend.users[user].access_keys):
        if key.access_key_id == keyid:
            iam_backend.users[user].access_keys[idx].create_date = datetime.strftime(
                timestamp,
                "%Y-%m-%dT%H:%M:%SZ"
            )
            break
    else:
        raise IAMNotFoundException("AccessKeyId {0} not found".format(keyid))
Exemple #4
0
def get_access_key_last_used(self):
    """
    Hook function for GetAccessKeyLastUsed API call
    """
    access_key_id = self._get_param('AccessKeyId')
    users = iam_backend.list_users(None, None, None)
    user_name = None
    for user in users:
        keys = user.get_all_access_keys()
        for key in keys:
            if access_key_id == key.access_key_id:
                user_name = user.name
                last_used = key.last_used.isoformat() if key.last_used else key.last_used
                break
    if not user_name:
        raise IAMNotFoundException("AccessKeyId {0} not found".format(access_key_id))

    template = self.response_template(GET_ALL_ACCESS_KEYS_TEMPLATE)
    return template.render(user_name=user_name, last_used=last_used)