Beispiel #1
0
    def do_run(self, args):
        try:
            # add arguments
            do_parser = self.arg_run()
            do_args = do_parser.parse_args(shlex.split(args))

            if not do_args:
                return 2

            if not self.check_overlay_option_is_allowed(
                    do_args.name, do_args.overlay):
                return 2

            local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir(
                self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY,
                constants.SCAN_BINARY_NAME)

            try:
                self.upload_and_launch_scan_binary(self.login, self.password,
                                                   None, do_args,
                                                   local_uforge_scan_path,
                                                   self.api.getUrl())
            except AttributeError:
                self.upload_and_launch_scan_binary(self.login, None,
                                                   self.apikeys, do_args,
                                                   local_uforge_scan_path,
                                                   self.api.getUrl())

            # delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out("Searching scan on uforge ...")
            running = True
            while running:
                my_scanned_instances = self.api.Users(
                    self.login).Scannedinstances.Getall(Includescans="true",
                                                        Name=do_args.name)
                my_scanned_instances = my_scanned_instances.scannedInstances.scannedInstance
                if my_scanned_instances is None or not my_scanned_instances:
                    time.sleep(5)
                else:
                    if len(my_scanned_instances) > 1:
                        printer.out("A scan with the same name already exists",
                                    printer.ERROR)
                    my_scanned_instance = my_scanned_instances[0]
                    if not my_scanned_instance.scans.scan:
                        time.sleep(5)
                    else:
                        running = self.handle_scan_run_status(
                            my_scanned_instance, running)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_run()
        except Exception as e:
            return hammr_utils.handle_uforge_exception(e)
Beispiel #2
0
    def do_run(self, args):
        try:
            # add arguments
            do_parser = self.arg_run()
            do_args = do_parser.parse_args(shlex.split(args))

            if not do_args:
                return 2

            if not self.check_overlay_option_is_allowed(do_args.name, do_args.overlay):
                return 2

            local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir(self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY, constants.SCAN_BINARY_NAME)

            try:
                self.upload_and_launch_scan_binary(
                    self.login, self.password, None, do_args, local_uforge_scan_path, self.api.getUrl())
            except AttributeError:
                self.upload_and_launch_scan_binary(
                    self.login, None, self.apikeys, do_args, local_uforge_scan_path, self.api.getUrl())

            # delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out("Searching scan on uforge ...")
            running = True
            while running:
                my_scanned_instances = self.api.Users(self.login).Scannedinstances.Getall(Includescans="true",
                                                                                          Name=do_args.name)
                my_scanned_instances = my_scanned_instances.scannedInstances.scannedInstance
                if my_scanned_instances is None or not my_scanned_instances:
                    time.sleep(5)
                else:
                    if len(my_scanned_instances) > 1:
                        printer.out("A scan with the same name already exists", printer.ERROR)
                    my_scanned_instance = my_scanned_instances[0]
                    if not my_scanned_instance.scans.scan:
                        time.sleep(5)
                    else:
                        running = self.handle_scan_run_status(my_scanned_instance, running)


        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_run()
        except Exception as e:
            return hammr_utils.handle_uforge_exception(e)
Beispiel #3
0
    def do_launch(self, args):
        try:
            # add arguments
            do_parser = self.arg_launch()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            try:
                uforge_password = self.password
            except AttributeError:
                printer.out("Using API keys with command 'hammr migration launch' is not yet supported. Please use password.", printer.ERROR)
                return 2

            migration_config = migration_utils.retrieve_migration_configuration(do_args.file)
            target_format = migration_utils.retrieve_target_format(self.api, self.login, migration_config["target"]["builder"]["type"])
            image = migration_utils.retrieve_image(migration_config["target"]["builder"], target_format, self.api, self.login)
            if image is None:
                return 2
            cred_account = migration_utils.retrieve_account(self.api, self.login, migration_config["target"]["builder"]["account"]["name"])
            publish_image = migration_utils.build_publish_image(migration_config["target"]["builder"], target_format, cred_account)
            if publish_image is None:
                return 2

            migration = self.create_migration(migration_config["name"], migration_config["os"], target_format.name, image, publish_image)
            self.api.Users(self.login).Migrations.Create(body=migration, element_name="ns1:migration")

            local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir(self.api, constants.TMP_WORKING_DIR, constants.URI_MIGRATION_BINARY, constants.MIGRATION_BINARY_NAME)

            self.upload_and_launch_migration_binary(self.login, uforge_password, migration_config, local_uforge_migration_path, self.api.getUrl())

            # delete temp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out("Migration launched successfully, please go to the platform to follow steps of the migration.", printer.OK)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_launch()
        except Exception as e:
            if hammr_utils.is_uforge_exception(e):
                return hammr_utils.handle_uforge_exception(e)
            printer.out(str(e), printer.ERROR)

        return 0
Beispiel #4
0
    def test_download_binary_in_local_temp_dir_download_with_good_url_and_directory(
            self, mock_mkdir, mock_get_uforge_url, mock_download):
        # Given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        mock_get_uforge_url.return_value = "/url"
        # When
        local_binary_path = hammr_utils.download_binary_in_local_temp_dir(
            api, "/tmp/local/temp/dir", "/uri/binary", "binaryName")

        # Then
        mock_download.assert_called_with(
            "/url/uri/binary", "/tmp/local/temp/dir/binaryName",
            not api.getDisableSslCertificateValidation())
        self.assertEqual(local_binary_path, "/tmp/local/temp/dir/binaryName")
Beispiel #5
0
    def do_launch(self, args):
        try:
            # add arguments
            do_parser = self.arg_launch()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            try:
                uforge_password = self.password
            except AttributeError:
                printer.out(
                    "Using API keys with command 'hammr migration launch' is not yet supported. Please use password.",
                    printer.ERROR)
                return 2

            migration_config = migration_utils.retrieve_migration_configuration(
                do_args.file)
            target_format = migration_utils.retrieve_target_format(
                self.api, self.login,
                migration_config["target"]["builder"]["type"])
            image = migration_utils.retrieve_image(
                migration_config["target"]["builder"], target_format, self.api,
                self.login)
            if image is None:
                return 2
            cred_account = migration_utils.retrieve_account(
                self.api, self.login,
                migration_config["target"]["builder"]["account"]["name"])
            publish_image = migration_utils.build_publish_image(
                migration_config["target"]["builder"], target_format,
                cred_account)
            if publish_image is None:
                return 2

            migration = self.create_migration(migration_config["name"],
                                              migration_config["os"],
                                              target_format.name, image,
                                              publish_image)
            self.api.Users(self.login).Migrations.Create(
                body=migration, element_name="ns1:migration")

            local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir(
                self.api, constants.TMP_WORKING_DIR,
                constants.URI_MIGRATION_BINARY,
                constants.MIGRATION_BINARY_NAME)

            self.upload_and_launch_migration_binary(
                self.login, uforge_password, migration_config,
                local_uforge_migration_path, self.api.getUrl())

            # delete temp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out(
                "Migration launched successfully, please go to the platform to follow steps of the migration.",
                printer.OK)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_launch()
        except Exception as e:
            if hammr_utils.is_uforge_exception(e):
                return hammr_utils.handle_uforge_exception(e)
            printer.out(str(e), printer.ERROR)

        return 0
Beispiel #6
0
    def do_run(self, args):
        try:
            # add arguments
            doParser = self.arg_run()
            doArgs = doParser.parse_args(shlex.split(args))

            #if the help command is called, parse_args returns None object
            if not doArgs:
                return 2

            if not self.check_overlay_option_is_allowed(
                    doArgs.name, doArgs.overlay):
                return 2

            local_uforge_scan_path = hammr_utils.download_binary_in_local_temp_dir(
                self.api, constants.TMP_WORKING_DIR, constants.URI_SCAN_BINARY,
                constants.SCAN_BINARY_NAME)

            try:
                self.upload_and_launch_scan_binary(self.login, self.password,
                                                   None, doArgs,
                                                   local_uforge_scan_path,
                                                   self.api.getUrl())
            except AttributeError:
                self.upload_and_launch_scan_binary(self.login, None,
                                                   self.apikeys, doArgs,
                                                   local_uforge_scan_path,
                                                   self.api.getUrl())

            # delete tmp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out("Searching scan on uforge ...")
            running = True
            while running:
                myScannedInstances = self.api.Users(
                    self.login).Scannedinstances.Getall(Includescans="true",
                                                        Name=doArgs.name)
                myScannedInstances = myScannedInstances.scannedInstances.scannedInstance
                if myScannedInstances is None or len(myScannedInstances) == 0:
                    time.sleep(5)
                else:
                    if len(myScannedInstances) > 1:
                        printer.out("A scan with the same name already exists",
                                    printer.ERROR)
                    myScannedInstance = myScannedInstances[0]
                    if len(myScannedInstance.scans.scan) == 0:
                        time.sleep(5)
                    else:
                        for scan in myScannedInstance.scans.scan:
                            if (not scan.status.complete
                                    and not scan.status.error
                                    and not scan.status.cancelled):
                                status = scan.status
                                statusWidget = progressbar_widget.Status()
                                statusWidget.status = status
                                widgets = [
                                    Bar('>'), ' ', statusWidget, ' ',
                                    ReverseBar('<')
                                ]
                                progress = ProgressBar(widgets=widgets,
                                                       maxval=100).start()
                                while not (status.complete or status.error
                                           or status.cancelled):
                                    statusWidget.status = status
                                    progress.update(status.percentage)
                                    status = (self.api.Users(
                                        self.login).Scannedinstances(
                                            myScannedInstance.dbId).Scans(
                                                scan.dbId).Get(
                                                    "false", "false", "false",
                                                    "false", None, None, None,
                                                    None, None)).status
                                    time.sleep(2)
                                statusWidget.status = status
                                progress.finish()
                                if status.error:
                                    printer.out(
                                        "Scan  error: " + status.message +
                                        "\n" + status.errorMessage,
                                        printer.ERROR)
                                    if status.detailedError:
                                        printer.out(status.detailedErrorMsg)
                                elif status.cancelled:
                                    printer.out(
                                        "Scan canceled: " + status.message,
                                        printer.WARNING)
                                else:
                                    printer.out("Scan successfully",
                                                printer.OK)
                                running = False
                                break
                            else:
                                pass

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_run()
        except Exception as e:
            return hammr_utils.handle_uforge_exception(e)