Ejemplo n.º 1
0
def sanitize_input(args, regex=None):
    """Ensures that the values passed to us via flags aren't malicious
       Or attempts to at least! Also sets types of vars and other tweaks
       Optional regex: Match the input based on the regex too
                       Raise ValueError if no match
       TODO: this can definitely be smarter...see if docopt can do some of it
       This could also be leveraged: https://github.com/keleshev/schema
       It is recommended on docopt github to do validation

       Also sources dev config from either `mlt.conf` or conf file provided
    """
    # docker requires repo name to be in lowercase
    if args["<name>"] and args.get("init"):
        args["<name>"] = args["<name>"].lower()

        if not regex_checks.k8s_name_is_valid(args["<name>"], "pod"):
            raise ValueError("Name {} not valid.\nName must comply with the "
                             "Kubernetes naming restrictions, which means that"
                             " it must consist of lowercase alphanumeric "
                             "characters or '-' and must start and end with "
                             "an alphanumeric character.".format(
                                 args["<name>"]))

    # we only do logic on full flag name rather than alias
    if args["-i"]:
        args["--interactive"] = True
    if args["-l"]:
        args["--logs"] = True
    if args["-v"]:
        args["--verbose"] = True
    if args["-n"]:
        args["--count"] = True

    # docopt doesn't support type assignment:
    # https://github.com/docopt/docopt/issues/8
    args['--retries'] = int(args['--retries'])
    if args['<count>']:
        args['<count>'] = int(args['<count>'])

    # verify that the specified namespace is valid
    if args['--namespace'] and not regex_checks.k8s_name_is_valid(
            args['--namespace'], "namespace"):
        raise ValueError("Namespace {} not valid. See "
                         "https://kubernetes.io/docs/concepts/overview"
                         "/working-with-objects/names/#names".format(
                             args['--namespace']))

    # Set and Unset config commands require the name arg
    if (args.get('set') or args.get('remove')) and not args.get('<name>'):
        raise ValueError("Name of the configuration parameter must be "
                         "specified.")

    return args
Ejemplo n.º 2
0
def sanitize_input(args, regex=None):
    """Ensures that the values passed to us via flags aren't malicious
       Or attempts to at least! Also sets types of vars and other tweaks
       Optional regex: Match the input based on the regex too
                       Raise ValueError if no match
       TODO: this can definitely be smarter...see if docopt can do some of it
       This could also be leveraged: https://github.com/keleshev/schema
       It is recommended on docopt github to do validation
    """
    # docker requires repo name to be in lowercase
    if args["<name>"]:
        args["<name>"] = args["<name>"].lower()

        if not regex_checks.k8s_name_is_valid(args["<name>"], "pod"):
            raise ValueError("Name {} not valid.\nName must comply with the "
                             "Kubernetes naming restrictions, which means that"
                             " it must consist of lowercase alphanumeric "
                             "characters or '-' and must start and end with "
                             "an alphanumeric character.".format(
                                 args["<name>"]))

    # -i is an alias, so ensure that we only have to do logic on --interactive
    if args["-i"]:
        args["--interactive"] = True

    # docopt doesn't support type assignment:
    # https://github.com/docopt/docopt/issues/8
    args['--retries'] = int(args['--retries'])

    # verify that the specified namespace is valid
    if args['--namespace'] and not regex_checks.k8s_name_is_valid(
            args['--namespace'], "namespace"):
        raise ValueError("Namespace {} not valid. See "
                         "https://kubernetes.io/docs/concepts/overview"
                         "/working-with-objects/names/#names".format(
                             args['--namespace']))

    return args
Ejemplo n.º 3
0
def test_app_name_regex(app_name, resource_type, is_valid):
    assert k8s_name_is_valid(app_name, resource_type) == is_valid