|
| 1 | +import os |
| 2 | +from http import HTTPStatus |
| 3 | +from typing import Final |
| 4 | + |
| 5 | +from overrides import overrides |
| 6 | +from requests import Response |
| 7 | + |
| 8 | +from ark_sdk_python.auth.ark_isp_auth import ArkISPAuth |
| 9 | +from ark_sdk_python.common.isp import ArkISPServiceClient |
| 10 | +from ark_sdk_python.models import ArkServiceException |
| 11 | +from ark_sdk_python.models.services import ArkServiceConfig |
| 12 | +from ark_sdk_python.models.services.sia.ssh_ca import ArkSIAGetSSHPublicKey |
| 13 | +from ark_sdk_python.services.ark_service import ArkService |
| 14 | + |
| 15 | +SERVICE_CONFIG: Final[ArkServiceConfig] = ArkServiceConfig( |
| 16 | + service_name='sia-ssh-ca', required_authenticator_names=['isp'], optional_authenticator_names=[] |
| 17 | +) |
| 18 | + |
| 19 | +# SSH CA Key Rotation |
| 20 | +GENERATE_NEW_CA_KEY_API: Final[str] = 'api/public-keys/rotation/generate-new' |
| 21 | +DEACTIVATE_PREVIOUS_CA_KEY_API: Final[str] = 'api/public-keys/rotation/deactivate-previous' |
| 22 | +REACTIVATE_PREVIOUS_CA_KEY_API: Final[str] = 'api/public-keys/rotation/reactivate-previous' |
| 23 | + |
| 24 | +PUBLIC_KEYS_API: Final[str] = 'api/public-keys' |
| 25 | +PUBLIC_KEYS_SCRIPT_API: Final[str] = 'api/public-keys/scripts' |
| 26 | + |
| 27 | + |
| 28 | +class ArkSIASSHCAService(ArkService): |
| 29 | + def __init__(self, isp_auth: ArkISPAuth) -> None: |
| 30 | + super().__init__(isp_auth) |
| 31 | + self.__isp_auth = isp_auth |
| 32 | + self.__client: ArkISPServiceClient = ArkISPServiceClient.from_isp_auth( |
| 33 | + isp_auth=self.__isp_auth, |
| 34 | + service_name='dpa', |
| 35 | + refresh_connection_callback=self.__refresh_sia_auth, |
| 36 | + ) |
| 37 | + |
| 38 | + def __refresh_sia_auth(self, client: ArkISPServiceClient) -> None: |
| 39 | + ArkISPServiceClient.refresh_client(client, self.__isp_auth) |
| 40 | + |
| 41 | + def generate_new_ca(self) -> None: |
| 42 | + """ |
| 43 | + Generate new SSH CA key version |
| 44 | +
|
| 45 | + Raises: |
| 46 | + ArkServiceException: _description_ |
| 47 | + """ |
| 48 | + self._logger.info('Generate new CA key version') |
| 49 | + resp: Response = self.__client.post(GENERATE_NEW_CA_KEY_API) |
| 50 | + if resp.status_code != HTTPStatus.CREATED: |
| 51 | + raise ArkServiceException(f'Failed to generate new CA key [{resp.text}] - [{resp.status_code}]') |
| 52 | + |
| 53 | + def deactivate_previous_ca(self) -> None: |
| 54 | + """ |
| 55 | + Deactivate previous SSH CA key version |
| 56 | +
|
| 57 | + Raises: |
| 58 | + ArkServiceException: _description_ |
| 59 | + """ |
| 60 | + self._logger.info('Deactivate previous CA key version') |
| 61 | + resp: Response = self.__client.post(DEACTIVATE_PREVIOUS_CA_KEY_API) |
| 62 | + if resp.status_code != HTTPStatus.OK: |
| 63 | + raise ArkServiceException(f'Failed to deactivate previous CA key [{resp.text}] - [{resp.status_code}]') |
| 64 | + |
| 65 | + def reactivate_previous_ca(self) -> None: |
| 66 | + """ |
| 67 | + Reactivate previous SSH CA key version |
| 68 | +
|
| 69 | + Raises: |
| 70 | + ArkServiceException: _description_ |
| 71 | + """ |
| 72 | + self._logger.info('Reactivate previous CA key version') |
| 73 | + resp: Response = self.__client.post(REACTIVATE_PREVIOUS_CA_KEY_API) |
| 74 | + if resp.status_code != HTTPStatus.OK: |
| 75 | + raise ArkServiceException(f'Failed to reactivate previous CA key [{resp.text}] - [{resp.status_code}]') |
| 76 | + |
| 77 | + def public_key(self, get_public_key: ArkSIAGetSSHPublicKey) -> str: |
| 78 | + """ |
| 79 | + Retrieves the public key used for SIA SSH connections trust with customer env |
| 80 | +
|
| 81 | + Args: |
| 82 | + get_public_key (ArkSIAGetSSHPublicKey): _description_ |
| 83 | +
|
| 84 | + Raises: |
| 85 | + ArkNotSupportedException: _description_ |
| 86 | + ArkServiceException: _description_ |
| 87 | +
|
| 88 | + Returns: |
| 89 | + str |
| 90 | + """ |
| 91 | + self._logger.info('Getting public key') |
| 92 | + resp: Response = self.__client.get(PUBLIC_KEYS_API) |
| 93 | + if resp.status_code == HTTPStatus.OK: |
| 94 | + if get_public_key.output_file: |
| 95 | + os.makedirs(os.path.dirname(get_public_key.output_file), exist_ok=True) |
| 96 | + with open(get_public_key.output_file, 'w', encoding='utf-8') as f: |
| 97 | + f.write(resp.text) |
| 98 | + return resp.text |
| 99 | + raise ArkServiceException(f'Failed to get public key [{resp.text}] - [{resp.status_code}]') |
| 100 | + |
| 101 | + def public_key_script(self, get_public_key: ArkSIAGetSSHPublicKey) -> str: |
| 102 | + """ |
| 103 | + Retrieves the public key script used for SIA SSH connections trust with customer env |
| 104 | + The script can be run to install the public key in needed ssh configuration files |
| 105 | +
|
| 106 | + Args: |
| 107 | + get_public_key (ArkSIAGetSSHPublicKey): _description_ |
| 108 | +
|
| 109 | + Raises: |
| 110 | + ArkNotSupportedException: _description_ |
| 111 | + ArkServiceException: _description_ |
| 112 | +
|
| 113 | + Returns: |
| 114 | + str |
| 115 | + """ |
| 116 | + self._logger.info('Getting public key script') |
| 117 | + resp: Response = self.__client.get( |
| 118 | + PUBLIC_KEYS_SCRIPT_API, |
| 119 | + ) |
| 120 | + if resp.status_code == HTTPStatus.OK: |
| 121 | + if get_public_key.output_file: |
| 122 | + os.makedirs(os.path.dirname(get_public_key.output_file), exist_ok=True) |
| 123 | + with open(get_public_key.output_file, 'w', encoding='utf-8') as f: |
| 124 | + f.write(resp.text) |
| 125 | + return resp.text |
| 126 | + raise ArkServiceException(f'Failed to get public key script [{resp.text}] - [{resp.status_code}]') |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + @overrides |
| 130 | + def service_config() -> ArkServiceConfig: |
| 131 | + return SERVICE_CONFIG |
0 commit comments