#!/usr/bin/env python
"""Active Directory methods module."""
from hvac import utils
from hvac.api.vault_api_base import VaultApiBase
DEFAULT_MOUNT_POINT = "ad"
[docs]class ActiveDirectory(VaultApiBase):
"""Active Directory Secrets Engine (API).
Reference: https://www.vaultproject.io/api/secret/ad/index.html
"""
[docs] def read_config(self, mount_point=DEFAULT_MOUNT_POINT):
"""Read the configured shared information for the ad secrets engine.
Credentials will be omitted from returned data.
Supported methods:
GET: /{mount_point}/config. Produces: 200 application/json
:param mount_point: The "path" the method/backend was mounted on.
:type mount_point: str | unicode
:return: The JSON response of the request.
:rtype: dict
"""
api_path = utils.format_url("/v1/{mount_point}/config", mount_point=mount_point)
return self._adapter.get(
url=api_path,
)
[docs] def create_or_update_role(
self, name, service_account_name=None, ttl=None, mount_point=DEFAULT_MOUNT_POINT
):
"""This endpoint creates or updates the ad role definition.
:param name: Specifies the name of an existing role against which to create this ad credential.
:type name: str | unicode
:param service_account_name: The name of a pre-existing service account in Active Directory that maps to this role.
This value is required on create and optional on update.
:type service_account_name: str | unicode
:param ttl: Specifies the TTL for this role.
This is provided as a string duration with a time suffix like "30s" or "1h" or as seconds.
If not provided, the default Vault TTL is used.
:type ttl: str | unicode
:param mount_point: Specifies the place where the secrets engine will be accessible (default: ad).
:type mount_point: str | unicode
:return: The response of the request.
:rtype: requests.Response
"""
api_path = utils.format_url("/v1/{}/roles/{}", mount_point, name)
params = {
"name": name,
}
params.update(
utils.remove_nones(
{
"service_account_name": service_account_name,
"ttl": ttl,
}
)
)
return self._adapter.post(
url=api_path,
json=params,
)
[docs] def read_role(self, name, mount_point=DEFAULT_MOUNT_POINT):
"""This endpoint queries for information about a ad role with the given name.
If no role exists with that name, a 404 is returned.
:param name: Specifies the name of the role to query.
:type name: str | unicode
:param mount_point: Specifies the place where the secrets engine will be accessible (default: ad).
:type mount_point: str | unicode
:return: The response of the request.
:rtype: requests.Response
"""
api_path = utils.format_url("/v1/{}/roles/{}", mount_point, name)
return self._adapter.get(
url=api_path,
)
[docs] def list_roles(self, mount_point=DEFAULT_MOUNT_POINT):
"""This endpoint lists all existing roles in the secrets engine.
:return: The response of the request.
:rtype: requests.Response
"""
api_path = utils.format_url("/v1/{}/roles", mount_point)
return self._adapter.list(
url=api_path,
)
[docs] def delete_role(self, name, mount_point=DEFAULT_MOUNT_POINT):
"""This endpoint deletes a ad role with the given name.
Even if the role does not exist, this endpoint will still return a successful response.
:param name: Specifies the name of the role to delete.
:type name: str | unicode
:param mount_point: Specifies the place where the secrets engine will be accessible (default: ad).
:type mount_point: str | unicode
:return: The response of the request.
:rtype: requests.Response
"""
api_path = utils.format_url("/v1/{}/roles/{}", mount_point, name)
return self._adapter.delete(
url=api_path,
)
[docs] def generate_credentials(self, name, mount_point=DEFAULT_MOUNT_POINT):
"""This endpoint retrieves the previous and current LDAP password for
the associated account (or rotate if required)
:param name: Specifies the name of the role to request credentials from.
:type name: str | unicode
:param mount_point: Specifies the place where the secrets engine will be accessible (default: ad).
:type mount_point: str | unicode
:return: The response of the request.
:rtype: requests.Response
"""
api_path = utils.format_url("/v1/{}/creds/{}", mount_point, name)
return self._adapter.get(
url=api_path,
)