예제 #1
0
    def setup_subparser(cls, subparser):
        # FIXME (derek.wang) Refactor this into a more robust command-nesting framework
        template = '''\
Available Sub-Commands:

{}

Examples:

    manage.py lookup-tables [describe-tables|get|set]
'''
        subcommands = cls._subcommands()
        set_parser_epilog(
            subparser,
            epilog=(
                template.format('\n'.join([
                    '\t{command: <{pad}}{description}'.format(
                        command=command,
                        pad=30,

                        # FIXME (Derek.wang)
                        #   Ryan suggested that we could implement a __str__ or __repr__ function
                        #   for each of the CLICommand classes
                        description=subcommand.description
                    )
                    for command, subcommand
                    in subcommands.items()
                ]))
            )
        )

        lookup_tables_subparsers = subparser.add_subparsers()

        for subcommand in subcommands.values():
            subcommand.setup_subparser(lookup_tables_subparsers)
예제 #2
0
    def setup_subparser(cls, subparser):
        """Add the create-cluster-alarm subparser: manage.py create-cluster-alarm [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Other Constraints:

                    The product of the value for period multiplied by the value for evaluation
                    periods cannot exceed 86,400. 86,400 is the number of seconds in one day and
                    an alarm's total current evaluation period can be no longer than one day.

                Example:

                    manage.py create-cluster-alarm FailedParsesAlarm \\
                      --metric FailedParses \\
                      --comparison-operator GreaterThanOrEqualToThreshold \\
                      --evaluation-periods 1 \\
                      --period 300 \\
                      --threshold 1.0 \\
                      --clusters prod \\
                      --statistic Sum \\
                      --alarm-description 'Alarm for any failed parses that occur \
    within a 5 minute period in the prod cluster'

                Resources:

                    AWS:        https://docs.aws.amazon.com/AmazonCloudWatch/\
    latest/APIReference/API_PutMetricAlarm.html
                    Terraform:  https://www.terraform.io/docs/providers/aws/r/\
    cloudwatch_metric_alarm.html
                '''))

        _add_default_metric_alarms_args(subparser, clustered=True)

        # Add the option to specify cluster(s)
        add_clusters_arg(subparser, required=True)
예제 #3
0
    def setup_subparser(cls, subparser):
        """Add the deploy subparser: manage.py deploy [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py deploy --function rule alert
                '''))

        # Flag to manually bypass rule staging for new rules upon deploy
        # This only has an effect if rule staging is enabled
        subparser.add_argument(
            '--skip-rule-staging',
            action='store_true',
            help='Skip staging of new rules so they go directly into production'
        )

        # flag to manually demote specific rules to staging during deploy
        subparser.add_argument(
            '--stage-rules',
            action=MutuallyExclusiveStagingAction,
            default=set(),
            help='Stage the rules provided in a space-separated list',
            nargs='+')

        # flag to manually bypass rule staging for specific rules during deploy
        subparser.add_argument(
            '--unstage-rules',
            action=MutuallyExclusiveStagingAction,
            default=set(),
            help='Unstage the rules provided in a space-separated list',
            nargs='+')

        add_default_lambda_args(subparser)
예제 #4
0
    def setup_subparser(cls, subparser):
        get_parser = generate_subparser(
            subparser,
            'get',
            description='Retrieves a key from the requested LookupTable',
            subcommand=True
        )

        set_parser_epilog(
            get_parser,
            epilog=(
                '''\
                Examples:

                    manage.py lookup-tables get -t [table] -k [key]
                '''
            )
        )

        get_parser.add_argument(
            '-t',
            '--table',
            help='Name of the LookupTable',
            required=True
        )

        get_parser.add_argument(
            '-k',
            '--key',
            help='Key to fetch on the LookupTable',
            required=True
        )
예제 #5
0
    def _setup_athena_rebuild_subparser(cls, subparsers):
        """
        Add the athena rebuild-partitions subparser:

        $ manage.py athena rebuild-partitions [options]
        """
        athena_rebuild_parser = generate_subparser(
            subparsers,
            'rebuild-partitions',
            description='Rebuild the partitions for an Athena table',
            subcommand=True
        )

        set_parser_epilog(
            athena_rebuild_parser,
            epilog=(
                '''\
                Examples:

                    manage.py athena rebuild-partitions \\
                      --bucket s3.bucket.name \\
                      --table-name my_athena_table
                '''
            )
        )

        cls._add_default_athena_args(athena_rebuild_parser)
예제 #6
0
    def setup_subparser(cls, subparser):
        """Add kinesis subparser: manage.py kinesis [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py kinesis disable-events --clusters corp prod
                '''))

        actions = ['disable-events', 'enable-events']
        subparser.add_argument(
            'action',
            metavar='ACTION',
            choices=actions,
            help='One of the following actions to be performed: {}'.format(
                ', '.join(actions)))

        # Add the option to specify cluster(s)
        add_clusters_arg(subparser)

        subparser.add_argument(
            '-s',
            '--skip-terraform',
            action='store_true',
            help='Only update the config options and do not run Terraform')
예제 #7
0
    def setup_subparser(cls, subparser):
        """Add build subparser: manage.py build [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py build --target alert_processor_lambda
                '''))

        _add_default_tf_args(subparser, add_cluster_args=False)
예제 #8
0
    def setup_subparser(cls, subparser):
        """Add destroy subparser: manage.py destroy [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py destroy --target aws_s3_bucket-streamalerts
                '''))

        _add_default_tf_args(subparser)
예제 #9
0
    def setup_subparser(cls, subparser):
        """Add the rollback subparser: manage.py rollback [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py rollback --function rule
                '''))

        add_default_lambda_args(subparser)
예제 #10
0
    def setup_subparser(cls, subparser):
        set_parser = generate_subparser(
            subparser,
            'list-add',
            description='Sets a key on the requested LookupTable',
            subcommand=True
        )

        set_parser_epilog(
            set_parser,
            epilog=(
                '''\
                Examples:

                    manage.py lookup-tables list-add -t [table] -k [key] -v [value]
                '''
            )
        )

        set_parser.add_argument(
            '-t',
            '--table',
            help='Name of the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-k',
            '--key',
            help='Key to modify on the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-v',
            '--value',
            help='Value to add to the key',
            required=True
        )

        set_parser.add_argument(
            '-u',
            '--unique',
            help='Remove duplicate values from the final list',
            action='store_true'
        )

        set_parser.add_argument(
            '-s',
            '--sort',
            help='Sort the final list',
            action='store_true'
        )
예제 #11
0
    def _setup_athena_create_table_subparser(cls, subparsers):
        """Add the athena create-table subparser: manage.py athena create-table [options]"""
        athena_create_table_parser = generate_subparser(
            subparsers,
            'create-table',
            description='Create an Athena table',
            subcommand=True
        )

        set_parser_epilog(
            athena_create_table_parser,
            epilog=(
                '''\
                Examples:

                    manage.py athena create-table \\
                      --bucket s3.bucket.name \\
                      --table-name my_athena_table
                '''
            )
        )

        cls._add_default_athena_args(athena_create_table_parser)

        # Validate the provided schema-override options
        def _validate_override(val):
            """Make sure the input is in the format column_name=type"""
            err = ('Invalid override expression [{}]. The proper format is '
                   '"column_name=value_type"').format(val)
            if '=' not in val:
                raise athena_create_table_parser.error(err)

            if len(val.split('=')) != 2:
                raise athena_create_table_parser.error(err)

        athena_create_table_parser.add_argument(
            '--schema-override',
            nargs='+',
            help=(
                'Value types to override with new types in the log schema. '
                'The provided input should be space-separated '
                'directives like "column_name=value_type"'
            ),
            action=UniqueSortedListAction,
            default=[],
            type=_validate_override
        )
예제 #12
0
    def setup_subparser(cls, subparser):
        set_parser = generate_subparser(
            subparser,
            'set',
            description='Sets a key on the requested LookupTable',
            subcommand=True
        )

        set_parser_epilog(
            set_parser,
            epilog=(
                '''\
                Examples:

                    manage.py lookup-tables set -t [table] -k [key] -v [value]
                '''
            )
        )

        set_parser.add_argument(
            '-t',
            '--table',
            help='Name of the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-k',
            '--key',
            help='Key to set on the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-v',
            '--value',
            help='Value to save into LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-j',
            '--json',
            help='Interpret the value as a JSON-encoded string',
            action='store_true'
        )
예제 #13
0
    def setup_subparser(cls, subparser):
        describe_tables_parser = generate_subparser(
            subparser,
            'describe-tables',
            description='Shows metadata about all currently configured LookupTables',
            subcommand=True
        )

        set_parser_epilog(
            describe_tables_parser,
            epilog=(
                '''\
                Examples:

                    manage.py lookup-tables describe-tables
                '''
            )
        )
예제 #14
0
    def _setup_app_update_auth_subparser(cls, subparsers):
        """Add the app update-auth subparser: manage.py app update-auth [options]"""
        app_update_parser = generate_subparser(
            subparsers,
            'update-auth',
            description=
            'Update the authentication information for an existing app',
            subcommand=True)

        set_parser_epilog(app_update_parser,
                          epilog=('''\
                Example:

                    manage.py app update-auth \\
                      --cluster prod \\
                      --name duo_prod_collector
                '''))

        cls._add_default_app_args(app_update_parser)
예제 #15
0
    def setup_subparser(cls, subparser):
        """Add the metrics subparser: manage.py custom-metrics [options]"""
        set_parser_epilog(subparser,
                          epilog=('''\
                Example:

                    manage.py custom-metrics --enable --functions rule
                '''))

        available_metrics = metrics.MetricLogger.get_available_metrics()
        available_functions = [
            func for func, value in available_metrics.items() if value
        ]

        # allow the user to select 1 or more functions to enable metrics for
        subparser.add_argument(
            '-f',
            '--functions',
            choices=available_functions,
            metavar='FUNCTION',
            help=
            'One or more of the following functions for which to enable metrics: {}'
            .format(', '.join(available_functions)),
            nargs='+',
            required=True)

        # get the metric toggle value
        toggle_group = subparser.add_mutually_exclusive_group(required=True)

        toggle_group.add_argument('-e',
                                  '--enable',
                                  dest='enable_custom_metrics',
                                  help='Enable custom CloudWatch metrics',
                                  action='store_true')

        toggle_group.add_argument('-d',
                                  '--disable',
                                  dest='enable_custom_metrics',
                                  help='Disable custom CloudWatch metrics',
                                  action='store_false')

        # Add the option to specify cluster(s)
        add_clusters_arg(subparser)
예제 #16
0
    def _setup_app_new_subparser(cls, subparsers):
        """Add the app new subparser: manage.py app new [options]"""
        app_new_parser = generate_subparser(
            subparsers,
            'new',
            description=
            'Create a new StreamAlert app to poll logs from various services',
            subcommand=True)

        set_parser_epilog(app_new_parser,
                          epilog=('''\
                Example:

                    manage.py app new \\
                      duo_auth \\
                      --cluster prod \\
                      --name duo_prod_collector \\
                      --schedule-expression 'rate(2 hours)' \\
                      --timeout 60 \\
                      --memory 256
                '''))

        cls._add_default_app_args(app_new_parser)

        app_types = sorted(StreamAlertApp.get_all_apps())

        # App type options
        app_new_parser.add_argument(
            'type',
            choices=app_types,
            metavar='APP_TYPE',
            help='Type of app being configured: {}'.format(
                ', '.join(app_types)))

        # Function schedule expression (rate) arg
        add_schedule_expression_arg(app_new_parser)

        # Function timeout arg
        add_timeout_arg(app_new_parser)

        # Function memory arg
        add_memory_arg(app_new_parser)
예제 #17
0
    def setup_subparser(cls, subparser):
        set_parser = generate_subparser(
            subparser,
            'set-from-json-file',
            description='Pushes the contents of a given json file into the LookupTable key',
            subcommand=True
        )

        set_parser_epilog(
            set_parser,
            epilog=(
                '''\
                Examples:

                    manage.py lookup-tables set-from-json-file -t [table] -k [key] -f \
[path/to/file.json]
                '''
            )
        )

        set_parser.add_argument(
            '-t',
            '--table',
            help='Name of the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-k',
            '--key',
            help='Key to modify on the LookupTable',
            required=True
        )

        set_parser.add_argument(
            '-f',
            '--file',
            help='Path to the json file, relative to the current working directory',
            required=True
        )