Skip to content

db

ArkDPADBSecretsService

Bases: ArkService

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
class ArkDPADBSecretsService(ArkService):
    def __init__(self, isp_auth: ArkISPAuth) -> None:
        super().__init__(isp_auth)
        self.__isp_auth = isp_auth
        self.__client: ArkISPServiceClient = ArkISPServiceClient.from_isp_auth(self.__isp_auth, 'dpa')

    def __list_secrets_with_filters(
        self,
        secret_type: Optional[ArkDPADBSecretType] = None,
        tags: Optional[List[ArkDPADBTag]] = None,
    ) -> ArkDPADBSecretMetadataList:
        params = {}
        if secret_type:
            params['secret_type'] = secret_type.value
        if tags:
            params.update({t.key: t.value for t in tags})
        resp: Response = self.__client.get(SECRETS_ROUTE, params=params)
        if resp.status_code == HTTPStatus.OK:
            try:
                return ArkDPADBSecretMetadataList.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse list secrets response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse list secrets response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to list secrets [{resp.text}] - [{resp.status_code}]')

    def add_secret(self, add_secret: ArkDPADBAddSecret) -> ArkDPADBSecretMetadata:
        """
        Adds a new DB secret to the secret store.

        Args:
            add_secret (ArkDPADBAddSecret): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBSecretMetadata: _description_
        """
        self._logger.info('Adding new db secret')
        add_secret_dict = add_secret.dict(
            exclude_none=True,
            exclude={
                'store_type',
                'username',
                'password',
                'pam_safe',
                'pam_account_name',
                'iam_username',
                'iam_account',
                'iam_access_key_id',
                'iam_secret_access_key',
                'atlas_public_key',
                'atlas_private_key',
            },
        )
        if not add_secret.store_type:
            add_secret.store_type = SECRET_TYPE_TO_STORE_DICT[add_secret.secret_type]
        add_secret_dict['secret_store'] = {
            'store_type': add_secret.store_type.value,
        }
        if add_secret.secret_type == ArkDPADBSecretType.UsernamePassword:
            if not add_secret.username or not add_secret.password:
                raise ArkServiceException(
                    'When specifying a username password type, both username and password parameters must be supplied'
                )
            add_secret_dict['secret_data'] = {
                'username': add_secret.username,
                'password': add_secret.password.get_secret_value(),
            }
        elif add_secret.secret_type == ArkDPADBSecretType.CyberArkPAM:
            if not add_secret.pam_account_name or not add_secret.pam_safe:
                raise ArkServiceException(
                    'When specifying a pam secret type, both pam safe and pam account name parameters must be supplied'
                )
            add_secret_dict['secret_link'] = {
                'safe': add_secret.pam_safe,
                'account_name': add_secret.pam_account_name,
            }
        elif add_secret.secret_type == ArkDPADBSecretType.IAMUser:
            if (
                not add_secret.iam_access_key_id
                or not add_secret.iam_secret_access_key
                or not add_secret.iam_account
                or not add_secret.iam_username
            ):
                raise ArkServiceException('When specifying a iam user secret type, all iam parameters must be supplied')
            add_secret_dict['secret_data'] = {
                'account': add_secret.iam_account,
                'username': add_secret.iam_username,
                'access_key_id': add_secret.iam_access_key_id.get_secret_value(),
                'secret_access_key': add_secret.iam_secret_access_key.get_secret_value(),
            }
        elif add_secret.secret_type == ArkDPADBSecretType.AtlasAccessKeys:
            if not add_secret.atlas_public_key or not add_secret.atlas_private_key:
                raise ArkServiceException(
                    'When specifying an atlas secret type, both private key and public key parameters must be supplied'
                )
            add_secret_dict['secret_data'] = {
                'public_key': add_secret.atlas_public_key,
                'private_key': add_secret.atlas_private_key.get_secret_value(),
            }
        resp: Response = self.__client.post(
            SECRETS_ROUTE,
            json=add_secret_dict,
        )
        if resp.status_code == HTTPStatus.CREATED:
            try:
                return ArkDPADBSecretMetadata.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse add db secret response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse add db secret response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to add db secret [{resp.text}] - [{resp.status_code}]')

    def update_secret(self, update_secret: ArkDPADBUpdateSecret) -> ArkDPADBSecretMetadata:
        """
        Updates a DB secret.

        Args:
            update_secret (ArkDPADBUpdateSecret): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBSecretMetadata: _description_
        """
        if update_secret.secret_name and not update_secret.secret_id:
            update_secret.secret_id = (
                self.list_secrets_by(
                    secrets_filter=ArkDPADBSecretsFilter(secret_name=update_secret.secret_name),
                )
                .secrets[0]
                .secret_id
            )
        self._logger.info(f'Updating existing db secret with id [{update_secret.secret_id}]')
        update_secret_dict = update_secret.dict(
            exclude_none=True,
            exclude={
                'secret_id',
                'secret_name',
                'new_secret_name',
                'username',
                'password',
                'pam_safe',
                'pam_account_name',
                'iam_username',
                'iam_account',
                'iam_access_key_id',
                'iam_secret_access_key',
                'atlas_public_key',
                'atlas_private_key',
            },
        )
        if update_secret.new_secret_name:
            update_secret_dict['secret_name'] = update_secret.new_secret_name
        if update_secret.pam_account_name or update_secret.pam_safe:
            if not update_secret.pam_account_name or not update_secret.pam_safe:
                raise ArkServiceException('When specifying a pam secret, both pam safe and pam account name parameters must be supplied')
            update_secret_dict['secret_link'] = {
                'safe': update_secret.pam_safe,
                'account_name': update_secret.pam_account_name,
            }
        if update_secret.username or update_secret.password:
            if not update_secret.username or not update_secret.password:
                raise ArkServiceException(
                    'When specifying a username password secret, both username and password name parameters must be supplied'
                )
            update_secret_dict['secret_data'] = {
                'username': update_secret.username,
                'password': update_secret.password.get_secret_value(),
            }
        if (
            update_secret.iam_access_key_id
            or update_secret.iam_secret_access_key
            or update_secret.iam_account
            or update_secret.iam_username
        ):
            if (
                not update_secret.iam_access_key_id
                or not update_secret.iam_secret_access_key
                or not update_secret.iam_account
                or not update_secret.iam_username
            ):
                raise ArkServiceException('When specifying a iam user secret type, all iam parameters must be supplied')
            update_secret_dict['secret_data'] = {
                'account': update_secret.iam_account,
                'username': update_secret.iam_username,
                'access_key_id': update_secret.iam_access_key_id.get_secret_value(),
                'secret_access_key': update_secret.iam_secret_access_key.get_secret_value(),
            }
        if update_secret.atlas_public_key or update_secret.atlas_private_key:
            if not update_secret.atlas_public_key or not update_secret.atlas_private_key:
                raise ArkServiceException('When specifying an atlas secret, both private key and public key parameters must be supplied')
            update_secret_dict['secret_data'] = {
                'public_key': update_secret.atlas_public_key,
                'private_key': update_secret.atlas_private_key.get_secret_value(),
            }

        resp: Response = self.__client.patch(
            SECRET_ROUTE.format(secret_id=update_secret.secret_id),
            json=update_secret_dict,
        )
        if resp.status_code == HTTPStatus.OK:
            try:
                return ArkDPADBSecretMetadata.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse db secret response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse db secret response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to update db secret [{resp.text}] - [{resp.status_code}]')

    def delete_secret(self, delete_secret: ArkDPADBDeleteSecret) -> None:
        """
        Deletes a DB secret.

        Args:
            delete_secret (ArkDPADBDeleteSecret): _description_

        Raises:
            ArkServiceException: _description_
        """
        if delete_secret.secret_name and not delete_secret.secret_id:
            delete_secret.secret_id = (
                self.list_secrets_by(
                    secrets_filter=ArkDPADBSecretsFilter(secret_name=delete_secret.secret_name),
                )
                .secrets[0]
                .secret_id
            )
        self._logger.info(f'Deleting db secret by id [{delete_secret.secret_id}]')
        resp: Response = self.__client.delete(SECRET_ROUTE.format(secret_id=delete_secret.secret_id))
        if resp.status_code != HTTPStatus.NO_CONTENT:
            raise ArkServiceException(f'Failed to delete db secret [{resp.text}] - [{resp.status_code}]')

    def list_secrets(self) -> ArkDPADBSecretMetadataList:
        """
        Lists all tenant DB secrets.

        Returns:
            ArkDPADBSecretMetadataList: _description_
        """
        self._logger.info('Listing all db secrets')
        return self.__list_secrets_with_filters()

    def list_secrets_by(self, secrets_filter: ArkDPADBSecretsFilter) -> ArkDPADBSecretMetadataList:
        """
        Lists DB secrets that match the specified filters.

        Args:
            secrets_filter (ArkDPADBSecretsFilter): _description_

        Returns:
            ArkDPADBSecretMetadataList: _description_
        """
        self._logger.info(f'Listing db secrets by filters [{secrets_filter}]')
        secrets = self.__list_secrets_with_filters(secrets_filter.secret_type, secrets_filter.tags)

        # Filter by secret types
        if secrets_filter.store_type:
            secrets.secrets = [s for s in secrets.secrets if s.secret_store.store_type == secrets_filter.store_type]

        # Filter by name
        if secrets_filter.secret_name:
            secrets.secrets = [s for s in secrets.secrets if s.secret_name and fnmatch(s.secret_name, secrets_filter.secret_name)]

        # Filter by is active
        if secrets_filter.is_active is not None:
            secrets.secrets = [s for s in secrets.secrets if s.is_active == secrets_filter.is_active]
        secrets.total_count = len(secrets.secrets)

        return secrets

    def enable_secret(self, enable_secret: ArkDPADBEnableSecret) -> ArkDPADBSecretMetadata:
        """
        Enables a DB secret.

        Args:
            enable_secret (ArkDPADBEnableSecret): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBSecretMetadata: _description_
        """
        if enable_secret.secret_name and not enable_secret.secret_id:
            enable_secret.secret_id = (
                self.list_secrets_by(
                    secrets_filter=ArkDPADBSecretsFilter(secret_name=enable_secret.secret_name),
                )
                .secrets[0]
                .secret_id
            )
        self._logger.info(f'Enabling db secret by id [{enable_secret.secret_id}]')
        resp: Response = self.__client.post(ENABLE_SECRET_ROUTE.format(secret_id=enable_secret.secret_id))
        if resp.status_code != HTTPStatus.OK:
            raise ArkServiceException(f'Failed to enable db secret [{resp.text}] - [{resp.status_code}]')

    def disable_secret(self, disable_secret: ArkDPADBDisableSecret) -> ArkDPADBSecretMetadata:
        """
        Disables a DB secret.

        Args:
            disable_secret (ArkDPADBDisableSecret): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBSecretMetadata: _description_
        """
        if disable_secret.secret_name and not disable_secret.secret_id:
            disable_secret.secret_id = (
                self.list_secrets_by(
                    secrets_filter=ArkDPADBSecretsFilter(secret_name=disable_secret.secret_name),
                )
                .secrets[0]
                .secret_id
            )
        self._logger.info(f'Disabling db secret by id [{disable_secret.secret_id}]')
        resp: Response = self.__client.post(DISABLE_SECRET_ROUTE.format(secret_id=disable_secret.secret_id))
        if resp.status_code != HTTPStatus.OK:
            raise ArkServiceException(f'Failed to disable db secret [{resp.text}] - [{resp.status_code}]')

    def secret(self, get_secret: ArkDPADBGetSecret) -> ArkDPADBSecretMetadata:
        """
        Retrieves a DB secret.

        Args:
            get_secret (ArkDPADBGetSecret): _description_

        Raises:
            ArkServiceException: _description_

        Returns:
            ArkDPADBSecretMetadata: _description_
        """
        if get_secret.secret_name and not get_secret.secret_id:
            get_secret.secret_id = (
                self.list_secrets_by(
                    secrets_filter=ArkDPADBSecretsFilter(secret_name=get_secret.secret_name),
                )
                .secrets[0]
                .secret_id
            )
        self._logger.info(f'Retrieving db secret by id [{get_secret.secret_id}]')
        resp: Response = self.__client.get(SECRET_ROUTE.format(secret_id=get_secret.secret_id))
        if resp.status_code == HTTPStatus.OK:
            try:
                return ArkDPADBSecretMetadata.parse_obj(resp.json())
            except (ValidationError, JSONDecodeError) as ex:
                self._logger.exception(f'Failed to parse db secret response [{str(ex)}] - [{resp.text}]')
                raise ArkServiceException(f'Failed to parse db secret response [{str(ex)}]') from ex
        raise ArkServiceException(f'Failed to retrieve db secret [{resp.text}] - [{resp.status_code}]')

    def secrets_stats(self) -> ArkDPADBSecretsStats:
        """
        Calculates DB secrets statistics.

        Returns:
            ArkDPADBSecretsStats: _description_
        """
        self._logger.info('Calculating secrets statistics')
        secrets = self.list_secrets()
        secrets_stats = ArkDPADBSecretsStats.construct()
        secrets_stats.secrets_count = len(secrets.secrets)
        secrets_stats.active_secrets_count = len([s for s in secrets.secrets if s.is_active])
        secrets_stats.inactive_secrets_count = len([s for s in secrets.secrets if not s.is_active])

        # Count secrets per secret type
        secret_types: Set[ArkDPADBSecretType] = {s.secret_type for s in secrets.secrets if s.secret_type}
        secrets_stats.secrets_count_by_secret_type = {
            st: len([s for s in secrets.secrets if s.secret_type and s.secret_type == st]) for st in secret_types
        }

        # Count secrets per store type
        store_types: Set[ArkDPADBStoreType] = {s.secret_store.store_type for s in secrets.secrets if s.secret_store.store_type}
        secrets_stats.secrets_count_by_store_type = {
            st: len([s for s in secrets.secrets if s.secret_store.store_type and s.secret_store.store_type == st]) for st in store_types
        }

        return secrets_stats

    @staticmethod
    @overrides
    def service_config() -> ArkServiceConfig:
        return SERVICE_CONFIG

add_secret(add_secret)

Adds a new DB secret to the secret store.

Parameters:

Name Type Description Default
add_secret ArkDPADBAddSecret

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBSecretMetadata ArkDPADBSecretMetadata

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
def add_secret(self, add_secret: ArkDPADBAddSecret) -> ArkDPADBSecretMetadata:
    """
    Adds a new DB secret to the secret store.

    Args:
        add_secret (ArkDPADBAddSecret): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBSecretMetadata: _description_
    """
    self._logger.info('Adding new db secret')
    add_secret_dict = add_secret.dict(
        exclude_none=True,
        exclude={
            'store_type',
            'username',
            'password',
            'pam_safe',
            'pam_account_name',
            'iam_username',
            'iam_account',
            'iam_access_key_id',
            'iam_secret_access_key',
            'atlas_public_key',
            'atlas_private_key',
        },
    )
    if not add_secret.store_type:
        add_secret.store_type = SECRET_TYPE_TO_STORE_DICT[add_secret.secret_type]
    add_secret_dict['secret_store'] = {
        'store_type': add_secret.store_type.value,
    }
    if add_secret.secret_type == ArkDPADBSecretType.UsernamePassword:
        if not add_secret.username or not add_secret.password:
            raise ArkServiceException(
                'When specifying a username password type, both username and password parameters must be supplied'
            )
        add_secret_dict['secret_data'] = {
            'username': add_secret.username,
            'password': add_secret.password.get_secret_value(),
        }
    elif add_secret.secret_type == ArkDPADBSecretType.CyberArkPAM:
        if not add_secret.pam_account_name or not add_secret.pam_safe:
            raise ArkServiceException(
                'When specifying a pam secret type, both pam safe and pam account name parameters must be supplied'
            )
        add_secret_dict['secret_link'] = {
            'safe': add_secret.pam_safe,
            'account_name': add_secret.pam_account_name,
        }
    elif add_secret.secret_type == ArkDPADBSecretType.IAMUser:
        if (
            not add_secret.iam_access_key_id
            or not add_secret.iam_secret_access_key
            or not add_secret.iam_account
            or not add_secret.iam_username
        ):
            raise ArkServiceException('When specifying a iam user secret type, all iam parameters must be supplied')
        add_secret_dict['secret_data'] = {
            'account': add_secret.iam_account,
            'username': add_secret.iam_username,
            'access_key_id': add_secret.iam_access_key_id.get_secret_value(),
            'secret_access_key': add_secret.iam_secret_access_key.get_secret_value(),
        }
    elif add_secret.secret_type == ArkDPADBSecretType.AtlasAccessKeys:
        if not add_secret.atlas_public_key or not add_secret.atlas_private_key:
            raise ArkServiceException(
                'When specifying an atlas secret type, both private key and public key parameters must be supplied'
            )
        add_secret_dict['secret_data'] = {
            'public_key': add_secret.atlas_public_key,
            'private_key': add_secret.atlas_private_key.get_secret_value(),
        }
    resp: Response = self.__client.post(
        SECRETS_ROUTE,
        json=add_secret_dict,
    )
    if resp.status_code == HTTPStatus.CREATED:
        try:
            return ArkDPADBSecretMetadata.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse add db secret response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse add db secret response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to add db secret [{resp.text}] - [{resp.status_code}]')

delete_secret(delete_secret)

Deletes a DB secret.

Parameters:

Name Type Description Default
delete_secret ArkDPADBDeleteSecret

description

required

Raises:

Type Description
ArkServiceException

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
def delete_secret(self, delete_secret: ArkDPADBDeleteSecret) -> None:
    """
    Deletes a DB secret.

    Args:
        delete_secret (ArkDPADBDeleteSecret): _description_

    Raises:
        ArkServiceException: _description_
    """
    if delete_secret.secret_name and not delete_secret.secret_id:
        delete_secret.secret_id = (
            self.list_secrets_by(
                secrets_filter=ArkDPADBSecretsFilter(secret_name=delete_secret.secret_name),
            )
            .secrets[0]
            .secret_id
        )
    self._logger.info(f'Deleting db secret by id [{delete_secret.secret_id}]')
    resp: Response = self.__client.delete(SECRET_ROUTE.format(secret_id=delete_secret.secret_id))
    if resp.status_code != HTTPStatus.NO_CONTENT:
        raise ArkServiceException(f'Failed to delete db secret [{resp.text}] - [{resp.status_code}]')

disable_secret(disable_secret)

Disables a DB secret.

Parameters:

Name Type Description Default
disable_secret ArkDPADBDisableSecret

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBSecretMetadata ArkDPADBSecretMetadata

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
def disable_secret(self, disable_secret: ArkDPADBDisableSecret) -> ArkDPADBSecretMetadata:
    """
    Disables a DB secret.

    Args:
        disable_secret (ArkDPADBDisableSecret): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBSecretMetadata: _description_
    """
    if disable_secret.secret_name and not disable_secret.secret_id:
        disable_secret.secret_id = (
            self.list_secrets_by(
                secrets_filter=ArkDPADBSecretsFilter(secret_name=disable_secret.secret_name),
            )
            .secrets[0]
            .secret_id
        )
    self._logger.info(f'Disabling db secret by id [{disable_secret.secret_id}]')
    resp: Response = self.__client.post(DISABLE_SECRET_ROUTE.format(secret_id=disable_secret.secret_id))
    if resp.status_code != HTTPStatus.OK:
        raise ArkServiceException(f'Failed to disable db secret [{resp.text}] - [{resp.status_code}]')

enable_secret(enable_secret)

Enables a DB secret.

Parameters:

Name Type Description Default
enable_secret ArkDPADBEnableSecret

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBSecretMetadata ArkDPADBSecretMetadata

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
def enable_secret(self, enable_secret: ArkDPADBEnableSecret) -> ArkDPADBSecretMetadata:
    """
    Enables a DB secret.

    Args:
        enable_secret (ArkDPADBEnableSecret): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBSecretMetadata: _description_
    """
    if enable_secret.secret_name and not enable_secret.secret_id:
        enable_secret.secret_id = (
            self.list_secrets_by(
                secrets_filter=ArkDPADBSecretsFilter(secret_name=enable_secret.secret_name),
            )
            .secrets[0]
            .secret_id
        )
    self._logger.info(f'Enabling db secret by id [{enable_secret.secret_id}]')
    resp: Response = self.__client.post(ENABLE_SECRET_ROUTE.format(secret_id=enable_secret.secret_id))
    if resp.status_code != HTTPStatus.OK:
        raise ArkServiceException(f'Failed to enable db secret [{resp.text}] - [{resp.status_code}]')

list_secrets()

Lists all tenant DB secrets.

Returns:

Name Type Description
ArkDPADBSecretMetadataList ArkDPADBSecretMetadataList

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
274
275
276
277
278
279
280
281
282
def list_secrets(self) -> ArkDPADBSecretMetadataList:
    """
    Lists all tenant DB secrets.

    Returns:
        ArkDPADBSecretMetadataList: _description_
    """
    self._logger.info('Listing all db secrets')
    return self.__list_secrets_with_filters()

list_secrets_by(secrets_filter)

Lists DB secrets that match the specified filters.

Parameters:

Name Type Description Default
secrets_filter ArkDPADBSecretsFilter

description

required

Returns:

Name Type Description
ArkDPADBSecretMetadataList ArkDPADBSecretMetadataList

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
def list_secrets_by(self, secrets_filter: ArkDPADBSecretsFilter) -> ArkDPADBSecretMetadataList:
    """
    Lists DB secrets that match the specified filters.

    Args:
        secrets_filter (ArkDPADBSecretsFilter): _description_

    Returns:
        ArkDPADBSecretMetadataList: _description_
    """
    self._logger.info(f'Listing db secrets by filters [{secrets_filter}]')
    secrets = self.__list_secrets_with_filters(secrets_filter.secret_type, secrets_filter.tags)

    # Filter by secret types
    if secrets_filter.store_type:
        secrets.secrets = [s for s in secrets.secrets if s.secret_store.store_type == secrets_filter.store_type]

    # Filter by name
    if secrets_filter.secret_name:
        secrets.secrets = [s for s in secrets.secrets if s.secret_name and fnmatch(s.secret_name, secrets_filter.secret_name)]

    # Filter by is active
    if secrets_filter.is_active is not None:
        secrets.secrets = [s for s in secrets.secrets if s.is_active == secrets_filter.is_active]
    secrets.total_count = len(secrets.secrets)

    return secrets

secret(get_secret)

Retrieves a DB secret.

Parameters:

Name Type Description Default
get_secret ArkDPADBGetSecret

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBSecretMetadata ArkDPADBSecretMetadata

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
def secret(self, get_secret: ArkDPADBGetSecret) -> ArkDPADBSecretMetadata:
    """
    Retrieves a DB secret.

    Args:
        get_secret (ArkDPADBGetSecret): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBSecretMetadata: _description_
    """
    if get_secret.secret_name and not get_secret.secret_id:
        get_secret.secret_id = (
            self.list_secrets_by(
                secrets_filter=ArkDPADBSecretsFilter(secret_name=get_secret.secret_name),
            )
            .secrets[0]
            .secret_id
        )
    self._logger.info(f'Retrieving db secret by id [{get_secret.secret_id}]')
    resp: Response = self.__client.get(SECRET_ROUTE.format(secret_id=get_secret.secret_id))
    if resp.status_code == HTTPStatus.OK:
        try:
            return ArkDPADBSecretMetadata.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse db secret response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse db secret response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to retrieve db secret [{resp.text}] - [{resp.status_code}]')

secrets_stats()

Calculates DB secrets statistics.

Returns:

Name Type Description
ArkDPADBSecretsStats ArkDPADBSecretsStats

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
def secrets_stats(self) -> ArkDPADBSecretsStats:
    """
    Calculates DB secrets statistics.

    Returns:
        ArkDPADBSecretsStats: _description_
    """
    self._logger.info('Calculating secrets statistics')
    secrets = self.list_secrets()
    secrets_stats = ArkDPADBSecretsStats.construct()
    secrets_stats.secrets_count = len(secrets.secrets)
    secrets_stats.active_secrets_count = len([s for s in secrets.secrets if s.is_active])
    secrets_stats.inactive_secrets_count = len([s for s in secrets.secrets if not s.is_active])

    # Count secrets per secret type
    secret_types: Set[ArkDPADBSecretType] = {s.secret_type for s in secrets.secrets if s.secret_type}
    secrets_stats.secrets_count_by_secret_type = {
        st: len([s for s in secrets.secrets if s.secret_type and s.secret_type == st]) for st in secret_types
    }

    # Count secrets per store type
    store_types: Set[ArkDPADBStoreType] = {s.secret_store.store_type for s in secrets.secrets if s.secret_store.store_type}
    secrets_stats.secrets_count_by_store_type = {
        st: len([s for s in secrets.secrets if s.secret_store.store_type and s.secret_store.store_type == st]) for st in store_types
    }

    return secrets_stats

update_secret(update_secret)

Updates a DB secret.

Parameters:

Name Type Description Default
update_secret ArkDPADBUpdateSecret

description

required

Raises:

Type Description
ArkServiceException

description

Returns:

Name Type Description
ArkDPADBSecretMetadata ArkDPADBSecretMetadata

description

Source code in ark_sdk_python/services/dpa/secrets/db/ark_dpa_db_secrets_service.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def update_secret(self, update_secret: ArkDPADBUpdateSecret) -> ArkDPADBSecretMetadata:
    """
    Updates a DB secret.

    Args:
        update_secret (ArkDPADBUpdateSecret): _description_

    Raises:
        ArkServiceException: _description_

    Returns:
        ArkDPADBSecretMetadata: _description_
    """
    if update_secret.secret_name and not update_secret.secret_id:
        update_secret.secret_id = (
            self.list_secrets_by(
                secrets_filter=ArkDPADBSecretsFilter(secret_name=update_secret.secret_name),
            )
            .secrets[0]
            .secret_id
        )
    self._logger.info(f'Updating existing db secret with id [{update_secret.secret_id}]')
    update_secret_dict = update_secret.dict(
        exclude_none=True,
        exclude={
            'secret_id',
            'secret_name',
            'new_secret_name',
            'username',
            'password',
            'pam_safe',
            'pam_account_name',
            'iam_username',
            'iam_account',
            'iam_access_key_id',
            'iam_secret_access_key',
            'atlas_public_key',
            'atlas_private_key',
        },
    )
    if update_secret.new_secret_name:
        update_secret_dict['secret_name'] = update_secret.new_secret_name
    if update_secret.pam_account_name or update_secret.pam_safe:
        if not update_secret.pam_account_name or not update_secret.pam_safe:
            raise ArkServiceException('When specifying a pam secret, both pam safe and pam account name parameters must be supplied')
        update_secret_dict['secret_link'] = {
            'safe': update_secret.pam_safe,
            'account_name': update_secret.pam_account_name,
        }
    if update_secret.username or update_secret.password:
        if not update_secret.username or not update_secret.password:
            raise ArkServiceException(
                'When specifying a username password secret, both username and password name parameters must be supplied'
            )
        update_secret_dict['secret_data'] = {
            'username': update_secret.username,
            'password': update_secret.password.get_secret_value(),
        }
    if (
        update_secret.iam_access_key_id
        or update_secret.iam_secret_access_key
        or update_secret.iam_account
        or update_secret.iam_username
    ):
        if (
            not update_secret.iam_access_key_id
            or not update_secret.iam_secret_access_key
            or not update_secret.iam_account
            or not update_secret.iam_username
        ):
            raise ArkServiceException('When specifying a iam user secret type, all iam parameters must be supplied')
        update_secret_dict['secret_data'] = {
            'account': update_secret.iam_account,
            'username': update_secret.iam_username,
            'access_key_id': update_secret.iam_access_key_id.get_secret_value(),
            'secret_access_key': update_secret.iam_secret_access_key.get_secret_value(),
        }
    if update_secret.atlas_public_key or update_secret.atlas_private_key:
        if not update_secret.atlas_public_key or not update_secret.atlas_private_key:
            raise ArkServiceException('When specifying an atlas secret, both private key and public key parameters must be supplied')
        update_secret_dict['secret_data'] = {
            'public_key': update_secret.atlas_public_key,
            'private_key': update_secret.atlas_private_key.get_secret_value(),
        }

    resp: Response = self.__client.patch(
        SECRET_ROUTE.format(secret_id=update_secret.secret_id),
        json=update_secret_dict,
    )
    if resp.status_code == HTTPStatus.OK:
        try:
            return ArkDPADBSecretMetadata.parse_obj(resp.json())
        except (ValidationError, JSONDecodeError) as ex:
            self._logger.exception(f'Failed to parse db secret response [{str(ex)}] - [{resp.text}]')
            raise ArkServiceException(f'Failed to parse db secret response [{str(ex)}]') from ex
    raise ArkServiceException(f'Failed to update db secret [{resp.text}] - [{resp.status_code}]')