def set_up_certs(env: MilMoveEnv) -> None: """ Sets up certs for making requests to the mymove server :param env: MilMoveEnv that the target server is running in, e.g. MilMoveEnv.DP3 :return: None """ if is_local(env=env): return # We don't need to set up certs for a local run because they already exist host_upper = env.value.upper() try: deployed_tls_cert = os.environ[f"MOVE_MIL_{host_upper}_TLS_CERT"] deployed_tls_key = os.environ[f"MOVE_MIL_{host_upper}_TLS_KEY"] except KeyError: logger.debug( f"Unable to find cert and key values for environment: {MilMoveEnv}" ) raise ImplementationError( "Cannot run load testing in a deployed environment without the matching certificate and key." ) from None with open(DP3_CERT_KEY_PEM, "w") as f: f.write(deployed_tls_cert) f.write("\n") f.write(deployed_tls_key)
def get_request_kwargs(self, certs_required: bool = False, endpoint_name: str = "") -> RequestKwargsType: """ Grabs request kwargs that will be needed for the endpoint. :param certs_required: Boolean indicating if certs will be required. These will point to the paths for the TLS cert/key files, which change based on the environment being targetted. Possibly also includes a value that is either a boolean that indicates if the TLS certs should be verified, or a path to certs to use for verification. :param endpoint_name: name of endpoint, for locust request grouping :return: kwargs that can be passed to the request. """ kwargs = {"headers": get_json_headers()} if certs_required: if is_local(env=self.env): kwargs.update(deepcopy(LOCAL_TLS_CERT_KWARGS)) else: kwargs["cert"] = DP3_CERT_KEY_PEM if endpoint_name: kwargs["name"] = endpoint_name return kwargs
def form_internal_path(self, endpoint: str, include_prefix: bool = True) -> str: """ Returns a url pointing at the requested endpoint for the Internal API. :param endpoint: Endpoint to target, e.g. "/moves" :param include_prefix: Indicate if the internal prefix should be included or not :return: full url to use in requests """ if is_local(env=self.env): base_domain = self.form_base_domain( local_port=os.getenv("LOCAL_PORT", "8080"), local_protocol="http", local_subdomain="milmovelocal", ) else: base_domain = self.form_base_domain(deployed_subdomain="my") internal_path = base_domain if include_prefix: internal_path += self.INTERNAL_PATH_PREFIX return f"{internal_path}{endpoint}"
def remove_certs(env: MilMoveEnv) -> None: """ Removes certs that were set up for making requests to the mymove server :param env: MilMoveEnv that the target server is running in, e.g. MilMoveEnv.DP3 :return: None """ if is_local(env=env): return # We don't need to delete local certs since they're part of the repo try: os.remove(DP3_CERT_KEY_PEM) except FileNotFoundError: # FileNotFoundError means the file was already removed. pass
def form_support_path(self, endpoint: str) -> str: """ Returns a url pointing at the requested endpoint for the Support API. :param endpoint: Endpoint to target, e.g. "/moves" :return: full url to use in requests """ if is_local(env=self.env): base_domain = self.form_base_domain( local_port="9443", local_protocol="https", local_subdomain="primelocal", ) else: base_domain = self.form_base_domain(deployed_subdomain="api") return f"{base_domain}{self.SUPPORT_PATH_PREFIX}{endpoint}"
def test_is_local(env: MilMoveEnv, expected_result: bool) -> None: assert is_local(env=env) == expected_result
class MilMoveRequestPreparer: """ Class to help prepare for making requests to MilMove APIs based on the target environment. """ # The environment the host is running in (ex. locally or deployed). # Can be any of the values in MilMoveEnv. env: MilMoveEnv GHC_PATH_PREFIX = "/ghc/v1" INTERNAL_PATH_PREFIX = "/internal" PRIME_PATH_PREFIX = "/prime/v1" SUPPORT_PATH_PREFIX = "/support/v1" def get_request_kwargs(self, certs_required: bool = False, endpoint_name: str = "") -> RequestKwargsType: """ Grabs request kwargs that will be needed for the endpoint. :param certs_required: Boolean indicating if certs will be required. These will point to the paths for the TLS cert/key files, which change based on the environment being targetted. Possibly also includes a value that is either a boolean that indicates if the TLS certs should be verified, or a path to certs to use for verification. :param endpoint_name: name of endpoint, for locust request grouping :return: kwargs that can be passed to the request. """ kwargs = {"headers": get_json_headers()} if certs_required: if is_local(env=self.env): kwargs.update(deepcopy(LOCAL_TLS_CERT_KWARGS)) else: kwargs["cert"] = DP3_CERT_KEY_PEM if endpoint_name: kwargs["name"] = endpoint_name return kwargs def form_base_domain( self, deployed_subdomain: str = "", local_port: str = "", local_protocol: str = "", local_subdomain: str = "", ) -> str: """ Sets up the base domain for a request based on the environment we're running against and the subdomain we're targeting. Con optionally be overridden by setting an environment variable called BASE_DOMAIN that points to where you want it to point. :param deployed_subdomain: subdomain to target when deployed, e.g. "api" or "my". :param local_port: Port to use when running locally. :param local_protocol: local protocol to run against, e.g. "https" :param local_subdomain: subdomain to target when running locally, e.g. "primelocal" :return: base domain to use for requests, e.g. https://api.loadtest.dp3.us """ if base_domain := os.getenv("BASE_DOMAIN"): return base_domain if not is_local(env=self.env): # NOTE: deployed protocol is always https return f"https://{deployed_subdomain}.loadtest.dp3.us" port = str(local_port) # just in case an int was passed in if not (port.isdigit() and len(port) == 4): raise ImplementationError( "The local port must be a string of 4 digits.") return f"{local_protocol}://{local_subdomain}:{port}"