from CommonServerPython import * """ IMPORTS """ import json import traceback import urllib.parse from collections.abc import Callable from copy import deepcopy from typing import Any import dateparser import httplib2 from google.auth import aws, exceptions, identity_pool from google.oauth2 import service_account from google_auth_httplib2 import AuthorizedHttp from googleapiclient import discovery from googleapiclient.errors import HttpError """ CONSTANTS """ SCOPES = ["https://www.googleapis.com/auth/cloud-platform"] SERVICE_NAME = "securitycenter" PUBSUB_SERVICE_NAME = "pubsub" CLOUD_ASSET_SERVICE_NAME = "cloudasset" SERVICE_VERSION = "v1" PUBSUB_SERVICE_VERSION = "v1" CLOUD_ASSET_SERVICE_VERSION = "v1" DEFAULT_MAX_FETCH_VALUE = "50" MAX_FETCH_VALUE = "200" DEFAULT_PAGE_SIZE = 10 MAX_PAGE_SIZE = 1000 INCIDENT_NAME_PREFIX = "GoogleCloudSCC" STATE_LIST = ["ACTIVE", "INACTIVE"] # List of state mentioned in API doc SEVERITY_LIST = ["LOW", "MEDIUM", "HIGH", "CRITICAL"] # List of severity mentioned in API doc ALLOWED_DATE_UNIT = ["minute", "minutes", "hour", "hours", "day", "days", "month", "months", "year", "years"] DATE_FORMAT = "%B %d, %Y at %I:%M:%S %p" ISO_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S.%fZ" MARKDOWN_LINK = "[{}]({})" TIMEOUT_TIME = 60 # in second # The maximum number of results to return in a single response. # (ref: https://cloud.google.com/security-command-center/docs/reference/rest/v1/organizations.sources.findings/list) MAX_ITERATION = 10 DEFAULT_MAX_ITERATION = 2 ERROR_MESSAGES: dict[str, str] = { "JSON_PARSE_ERROR": "Unable to parse json. Please check the {} parameter.", "INVALID_ORGANIZATION_ID": "Invalid Organization ID.", "INVALID_ORGANIZATION_OR_PERMISSION_DENIED_ERROR": "Organization Id is not valid or permission denied.", "INVALID_SERVICE_ACCOUNT": "Invalid service account credentials.", "BAD_REQUEST_ERROR": "An error occurred while fetching/submitting the data. Reason: {}", "AUTHENTICATION_ERROR": "Unauthenticated. Check the configured Service Account JSON. Reason: {}", "AUTHORIZATION_ERROR": "Request has insufficient privileges. Reason: {}", "NOT_FOUND_ERROR": "Not found. Reason: {}", "TOO_MANY_REQUESTS_ERROR": "Too many requests please try after sometime. Reason: {}", "INTERNAL_SERVER_ERROR": "The server encountered an internal error. Reason: {}", "CONFLICT_ERROR": "Conflict. Reason: {}", "TIMEOUT_ERROR": "Connection Timeout Error - potential reasons might be that the Server is not accessible " "from your host. Reason: {}", "PROXY_ERROR": "Proxy Error - if the 'Use system proxy' checkbox in the integration configuration is" " selected, try clearing the checkbox.", "UNKNOWN_ERROR": "An error occurred. Status: {}. Reason: {}", "NO_RECORDS_FOUND": "No {} record(s) found for the given argument(s).", "MAX_INCIDENT_ERROR": "The parameter Max Incidents must be a positive integer." f" Accepted values can be in the range of 1-{MAX_FETCH_VALUE}.", "INVALID_STATE_ERROR": "The state value must be ACTIVE or INACTIVE.", "INVALID_SEVERITY_ERROR": "The severity value must be LOW, MEDIUM, HIGH or CRITICAL.", "INVALID_PAGE_SIZE_ERROR": "Page size should be an integer between 1 to 1000.", "INVALID_SOURCE_PROPERTIES": "Invalid format provided in sourceProperties. Supported format: key1=value1,key2=" "value2. if the value contains ',' or '=' character then escape with extra '\\'.", "REQUIRED_PROJECT_ID": "Project ID is required for fetch incidents.", "REQUIRED_SUBSCRIPTION_ID": "Subscription ID is required for fetch incidents.", "INVALID_INCIDENT": "Error while parsing pub/sub message. Reason: {}", "INVALID_DATE_TIME": '{} should be in following format: (