def upload_analyzer_manifest(manifest: AnalyzerManifest) -> str: logger.info(f"Uploading manifest") analyzer_json = manifest.to_original_json() r = auth_post(f"{get_base_url()}/api/v1/analyzers/", json=analyzer_json) data = handle_request_with_error_message(r) link = data.get("links", {}).get("artifact_url") return link
def fetch_latest_version() -> Tuple[Optional[str], Optional[bool]]: try: url = f"{get_base_url()}/api/cli/version/latest" r = auth_get(url, timeout=2.0) # sec response_json = handle_request_with_error_message(r) return response_json.get("latest"), response_json.get("forceUpgrade") except Exception: return None, None
def fetch_latest_version() -> Optional[str]: try: org = get_default_org() url = f"{get_base_url()}/api/cli/version/latest" r = auth_get(url) response_json = handle_request_with_error_message(r) return response_json.get("latest") except Exception as e: return None
def fetch_registry_data(): org = get_default_org() url = f"{get_base_url()}/api/v1/analyzers/" r = auth_get(url) response_json = handle_request_with_error_message(r) if response_json["status"] == "success": return response_json["analyzers"] else: raise ValueError("Couldn't parse analyzer registry response")
def upload_inputset(ctx, input_set_file, verbose, debug): """ Uploads INPUT_SET_FILE to the r2c analysis platform as a custom input set. See http://docs.r2c.dev for how to properly format INPUT_SET_FILE. """ if verbose is True: # allow passing --verbose to run as well as globally set_verbose_flag(ctx, True) if debug is True: set_debug_flag(ctx, True) print_msg(f"Uploading input set defined in `{input_set_file}`...") with open(input_set_file) as f: try: input_set = json.load(f) except Exception: print_error_exit( "Could not parse input set. Make sure it's a proper json file!" ) r = auth_post(f"{get_base_url()}/api/v1/inputs/", input_set) handle_request_with_error_message(r) print_success("Uploaded new input set successfully")
def _upload_analyzer_manifest( manifest: AnalyzerManifest, force: bool, readme: Optional[str] = None ) -> str: get_logger().info(f"Uploading manifest") analyzer_json = manifest.to_json() if "readme" not in analyzer_json and readme: analyzer_json["readme"] = readme # this unwrapping thing is ugly, but we have to do it for backwards compatibility # since we can't add a manifest field r = auth_post( f"{get_base_url()}/api/v1/analyzers/", json={**analyzer_json, "force": force} ) data = handle_request_with_error_message(r) link = data.get("links", {}).get("artifact_url") return link
def push(ctx, analyzer_directory, env_args_string): """ Push the analyzer in the current directory to the R2C platform. You must log in to push analyzers. This command will validate your analyzer and privately publish your analyzer to your org with the name specified in analyzer.json. Your analyzer name must follow {org}/{name}. """ debug = ctx.obj["DEBUG"] env_args_dict = parse_remaining(env_args_string) manifest, analyzer_directory = find_and_open_analyzer_manifest( analyzer_directory, ctx ) analyzer_org = get_org_from_analyzer_name(manifest.analyzer_name) # TODO(ulzii): let's decide which source of truth we're using for analyzer_name above and/or check consistency. # can't have both dir name and what's in analyzer.json print_msg(f"Pushing analyzer in {analyzer_directory}...") default_org = get_default_org() if default_org != analyzer_org: print_error_exit( f"Attempting to push to organization: `{default_org}`. However, the org specified as the prefix of the analyzer name in `analyzer.json` does not match it. " + f"Replace `{analyzer_org}` with `{default_org}` and try again." + "Please ask for help from R2C support" ) try: # upload analyzer.json artifact_link = upload_analyzer_manifest(manifest) except Exception as e: message = getattr(e, "message", repr(e)) print_error_exit(f"There was an error uploading your analyzer: {message}") if artifact_link is None: print_error_exit( "There was an error uploading your analyzer. Please ask for help from R2C support" ) # get docker login creds creds = get_docker_creds(artifact_link) if creds is None: print_error_exit( "There was an error getting Docker credentials. Please ask for help from R2C support" ) # docker login successful_login = docker_login(creds) if not successful_login: print_error_exit( "There was an error logging into Docker. Please ask for help from R2C support" ) # docker build and tag abort_on_build_failure( build_docker( manifest.analyzer_name, manifest.version, os.path.relpath(analyzer_directory, os.getcwd()), env_args_dict={**DEFAULT_ENV_ARGS_TO_DOCKER, **env_args_dict}, verbose=debug, ) ) # docker push image_id = VersionedAnalyzer(manifest.analyzer_name, manifest.version).image_id successful_push = docker_push(image_id) if not successful_push: print_error_exit( "There was an error pushing the Docker image. Please ask for help from R2C support" ) # mark uploaded with API # TODO figure out how to determine org from analyzer.json try: uploaded_url = f"{get_base_url()}/api/v1/analyzers/{manifest.analyzer_name}/{manifest.version}/uploaded" r = auth_put(uploaded_url) data = handle_request_with_error_message(r) if data.get("status") == "uploaded": web_url = data["links"]["web_url"] # display status to user and give link to view in web UI print_success(f"Successfully uploaded analyzer! Visit: {web_url}") else: print_error_exit( "Error confirming analyzer was successfully uploaded. Please contact us with the following information: failed to confirm analyzer finished uploading." ) except Exception as e: message = getattr(e, "message", repr(e)) print_error_exit( f"Error confirming analyzer was successfully uploaded: {message}" )
def push(ctx, analyzer_directory, force, squash, env_args_string): """ Push the analyzer in the current directory to the R2C platform. You must log in to push analyzers. This command will validate your analyzer and privately publish your analyzer to your org with the name specified in analyzer.json. Your analyzer name must follow {org}/{name}. """ env_args_dict = parse_remaining(env_args_string) manifest, analyzer_directory = find_and_open_analyzer_manifest( analyzer_directory, ctx ) readme = find_and_open_analyzer_readme(analyzer_directory, ctx) analyzer_org = get_org_from_analyzer_name(manifest.analyzer_name) overwriting_message = ( " and forcing overwrite if the analyzer version exists and is pending upload." ) # TODO(ulzii): let's decide which source of truth we're using for analyzer_name above and/or check consistency. # can't have both dir name and what's in analyzer.json print_msg( f"📌 Pushing analyzer in {analyzer_directory}{overwriting_message if force else ''}..." ) default_org = get_default_org() if default_org is None: print_error_exit( f"You are not logged in. Please run `r2c login` to be able to push your analyzer" ) if default_org != analyzer_org: if analyzer_org != PLATFORM_ANALYZER_PREFIX: print_error_exit( f"You're logged in to the common r2c platform. The org specified as the prefix of your analyzer name in `analyzer.json` must be `{PLATFORM_ANALYZER_PREFIX}`. " + f"Replace `{analyzer_org}` with `{PLATFORM_ANALYZER_PREFIX}` and try again." + "Please ask for help from r2c support" ) try: # upload analyzer.json artifact_link = _upload_analyzer_manifest(manifest, force, readme) except Exception as e: print_exception_exit("There was an error uploading your analyzer", e) if artifact_link is None: print_error_exit( "There was an error uploading your analyzer. Please ask for help from R2C support" ) get_logger().info(f"using artifact link: {artifact_link}") # get docker login creds creds = get_docker_creds(artifact_link) if creds is None: print_error_exit( "There was an error getting Docker credentials. Please ask for help from R2C support" ) else: print_success_step("Successfully fetched credentials.") # docker login successful_login = docker_login(creds) if not successful_login: print_error_exit( "There was an error logging into Docker. Please ask for help from R2C support" ) else: print_success_step("Successfully logged in to docker.") print_msg("🔨 Building docker container") # docker build and tag abort_on_build_failure( build_docker( manifest.analyzer_name, manifest.version, os.path.relpath(analyzer_directory, os.getcwd()), env_args_dict=env_args_dict, squash=squash, ) ) # docker push image_id = VersionedAnalyzer(manifest.analyzer_name, manifest.version).image_id print_msg(f"Pushing docker container to `{analyzer_org}`") successful_push = _docker_push(image_id) if not successful_push: print_error_exit( "There was an error pushing the Docker image. Please ask for help from R2C support" ) else: print_success_step("Successfully pushed to R2C.") # mark uploaded with API # TODO figure out how to determine org from analyzer.json try: uploaded_url = f"{get_base_url()}/api/v1/analyzers/{manifest.analyzer_name}/{manifest.version}/uploaded" r = auth_put(uploaded_url) data = handle_request_with_error_message(r) if data.get("status") == "uploaded": web_url = data["links"]["web_url"] # display status to user and give link to view in web UI print_success( f"Upload finished successfully for analyzer! Visit: {web_url}" ) else: print_error_exit("Error confirming analyzer was successfully uploaded.") except Exception as e: print_exception_exit("Error confirming analyzer was successfully uploaded", e)