示例#1
0
文件: manage.py 项目: wyaeld/lemur
def validate_sources(source_strings):
    sources = []
    if not source_strings:
        table = []
        for source in source_service.get_all():
            table.append([source.label, source.active, source.description])

        sys.stdout.write("No source specified choose from below:\n")
        sys.stdout.write(
            tabulate(table, headers=['Label', 'Active', 'Description']))
        sys.exit(1)

    if 'all' in source_strings:
        sources = source_service.get_all()

    for source_str in source_strings:
        source = source_service.get_by_label(source_str)

        if not source:
            sys.stderr.write(
                "Unable to find specified source with label: {0}".format(
                    source_str))

        sources.append(source)
    return sources
示例#2
0
文件: cli.py 项目: zarfide/lemur
def validate_sources(source_strings):
    sources = []
    if not source_strings:
        table = []
        for source in source_service.get_all():
            table.append([source.label, source.active, source.description])

        print("No source specified choose from below:")
        print(tabulate(table, headers=["Label", "Active", "Description"]))
        sys.exit(1)

    if "all" in source_strings:
        sources = source_service.get_all()
    else:
        for source_str in source_strings:
            source = source_service.get_by_label(source_str)

            if not source:
                print(
                    "Unable to find specified source with label: {0}".format(source_str)
                )
                sys.exit(1)

            sources.append(source)
    return sources
示例#3
0
文件: manage.py 项目: ejcx/lemur
    def run(self, sources, action):
        if not sources:
            table = []
            for source in source_service.get_all():
                table.append([source.label, source.active, source.description])

            sys.stdout.write(tabulate(table, headers=['Label', 'Active', 'Description']))
            sys.exit(1)

        for label in sources:
            source = source_service.get_by_label(label)

            if not source:
                sys.stderr.write("Unable to find specified source with label: {0}".format(label))

            if action == 'sync':
                self.sync(source)

            if action == 'clean':
                self.clean(source)
示例#4
0
文件: cli.py 项目: Netflix/lemur
def validate_sources(source_strings):
    sources = []
    if not source_strings:
        table = []
        for source in source_service.get_all():
            table.append([source.label, source.active, source.description])

        print("No source specified choose from below:")
        print(tabulate(table, headers=['Label', 'Active', 'Description']))
        sys.exit(1)

    if 'all' in source_strings:
        sources = source_service.get_all()
    else:
        for source_str in source_strings:
            source = source_service.get_by_label(source_str)

            if not source:
                print("Unable to find specified source with label: {0}".format(source_str))
                sys.exit(1)

            sources.append(source)
    return sources
示例#5
0
    def run(self, sources, action):
        if not sources:
            table = []
            for source in source_service.get_all():
                table.append([source.label, source.active, source.description])

            sys.stdout.write(
                tabulate(table, headers=['Label', 'Active', 'Description']))
            sys.exit(1)

        for label in sources:
            source = source_service.get_by_label(label)

            if not source:
                sys.stderr.write(
                    "Unable to find specified source with label: {0}".format(
                        label))

            if action == 'sync':
                self.sync(source)

            if action == 'clean':
                self.clean(source)
示例#6
0
def sync_source(source_label):
    """
    This celery task will sync the specified source.

    :param source:
    :return:
    """

    function = f"{__name__}.{sys._getframe().f_code.co_name}"
    logger = logging.getLogger(function)

    task_id = None
    if celery.current_task:
        task_id = celery.current_task.request.id

    log_data = {
        "source": source_label,
        "task_id": task_id,
    }

    if task_id and is_task_active(function, task_id, (source_label, )):
        logger.debug("Skipping task: Task is already active", extra=log_data)
        return

    logger.info("Starting syncing source", extra=log_data)

    user = user_service.get_by_username("lemur")
    source = source_service.get_by_label(source_label)
    if not source:
        raise RuntimeError(f"Source {source_label} not found")

    result = source_service.sync(source, user)
    log_data["result"] = result

    logger.info("Done syncing source", extra=log_data)
    return
示例#7
0
def enable_cloudfront(source_label):
    """
    Given the label of a legacy AWS source (without path or endpointType options), set up the source for CloudFront:

    #. Update the source options to the newest template, inheriting the existing values.
    #. Set ``path`` to "/" and ``endpointType`` to "elb" to restrict the source to discovering ELBs and related certs only.
    #. Create a new source (and destination) for the same accountNumber with ``path`` as "/cloudfront/" and ``endpointType`` as "cloudfront"

    :param source_strings:
    :return:
    """
    class ValidationError(Exception):
        pass

    try:
        source = source_service.get_by_label(source_label)
        if not source:
            raise ValidationError(
                f"Unable to find source with label: {source_label}")
        if source.plugin_name != "aws-source":
            raise ValidationError(
                f"Source '{source_label}' is not an AWS source")
        for opt_name in ["endpointType", "path"]:
            if get_plugin_option(opt_name, source.options) is not None:
                raise ValidationError(
                    f"Source '{source_label}' already sets option '{opt_name}'"
                )
        cloudfront_label = f"{source_label}-cloudfront"
        cloudfront_source = source_service.get_by_label(cloudfront_label)
        if cloudfront_source:
            raise ValidationError(
                f"A source named '{cloudfront_label}' already exists")

        p = plugins.get(source.plugin_name)
        new_options = deepcopy(p.options)
        for old_opt in source.options:
            name = old_opt["name"]
            value = get_plugin_option(name, source.options)
            set_plugin_option(name, value, new_options)
        set_plugin_option("path", "/", new_options)
        set_plugin_option("endpointType", "elb", new_options)
        source_service.update(source.id, source.label, source.plugin_name,
                              new_options, source.description)

        cloudfront_options = deepcopy(new_options)
        set_plugin_option("path", "/cloudfront/", cloudfront_options)
        set_plugin_option("endpointType", "cloudfront", cloudfront_options)
        source_service.create(
            cloudfront_label, source.plugin_name, cloudfront_options,
            f"CloudFront certificates and distributions for {source_label}")

        print(
            f"[+] Limited source {source_label} to discover ELBs and ELB certificates.\n"
        )
        print(
            f"[+] Created source {cloudfront_label} to discover CloudFront distributions and certificates.\n"
        )

    except ValidationError as e:
        print(f"[+] Error: {str(e)}")
        sys.exit(1)