|
| 1 | +#!/usr/bin/env python |
| 2 | +"""LDAP methods module.""" |
| 3 | + |
| 4 | +from hvac import utils |
| 5 | +from hvac.api.vault_api_base import VaultApiBase |
| 6 | + |
| 7 | +DEFAULT_MOUNT_POINT = "ldap" |
| 8 | + |
| 9 | + |
| 10 | +class Ldap(VaultApiBase): |
| 11 | + """LDAP Secrets Engine (API). |
| 12 | + Reference: https://www.vaultproject.io/api/secret/ldap/index.html |
| 13 | + """ |
| 14 | + |
| 15 | + def configure( |
| 16 | + self, |
| 17 | + binddn=None, |
| 18 | + bindpass=None, |
| 19 | + url=None, |
| 20 | + password_policy=None, |
| 21 | + schema=None, |
| 22 | + userdn=None, |
| 23 | + userattr=None, |
| 24 | + upndomain=None, |
| 25 | + mount_point=DEFAULT_MOUNT_POINT, |
| 26 | + ): |
| 27 | + """Configure shared information for the ldap secrets engine. |
| 28 | +
|
| 29 | + Supported methods: |
| 30 | + POST: /{mount_point}/config. Produces: 204 (empty body) |
| 31 | +
|
| 32 | + :param binddn: Distinguished name of object to bind when performing user and group search. |
| 33 | + :type binddn: str | unicode |
| 34 | + :param bindpass: Password to use along with binddn when performing user search. |
| 35 | + :type bindpass: str | unicode |
| 36 | + :param url: Base DN under which to perform user search. |
| 37 | + :type url: str | unicode |
| 38 | + :param userdn: Base DN under which to perform user search. |
| 39 | + :type userdn: str | unicode |
| 40 | + :param upndomain: userPrincipalDomain used to construct the UPN string for the authenticating user. |
| 41 | + :type upndomain: str | unicode |
| 42 | + :param password_policy: The name of the password policy to use to generate passwords. |
| 43 | + :type password_policy: str | unicode |
| 44 | + :param schema: The LDAP schema to use when storing entry passwords. Valid schemas include ``openldap``, ``ad``, and ``racf``. |
| 45 | + :type schema: str | unicode |
| 46 | + :param mount_point: The "path" the method/backend was mounted on. |
| 47 | + :type mount_point: str | unicode |
| 48 | + :return: The response of the request. |
| 49 | + :rtype: requests.Response |
| 50 | + """ |
| 51 | + params = utils.remove_nones( |
| 52 | + { |
| 53 | + "binddn": binddn, |
| 54 | + "bindpass": bindpass, |
| 55 | + "url": url, |
| 56 | + "userdn": userdn, |
| 57 | + "userattr": userattr, |
| 58 | + "upndomain": upndomain, |
| 59 | + "password_policy": password_policy, |
| 60 | + "schema": schema, |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + api_path = utils.format_url("/v1/{mount_point}/config", mount_point=mount_point) |
| 65 | + return self._adapter.post( |
| 66 | + url=api_path, |
| 67 | + json=params, |
| 68 | + ) |
| 69 | + |
| 70 | + def read_config(self, mount_point=DEFAULT_MOUNT_POINT): |
| 71 | + """Read the configured shared information for the ldap secrets engine. |
| 72 | +
|
| 73 | + Credentials will be omitted from returned data. |
| 74 | +
|
| 75 | + Supported methods: |
| 76 | + GET: /{mount_point}/config. Produces: 200 application/json |
| 77 | +
|
| 78 | + :param mount_point: The "path" the method/backend was mounted on. |
| 79 | + :type mount_point: str | unicode |
| 80 | + :return: The JSON response of the request. |
| 81 | + :rtype: dict |
| 82 | + """ |
| 83 | + api_path = utils.format_url("/v1/{mount_point}/config", mount_point=mount_point) |
| 84 | + return self._adapter.get( |
| 85 | + url=api_path, |
| 86 | + ) |
| 87 | + |
| 88 | + def rotate_root(self, mount_point=DEFAULT_MOUNT_POINT): |
| 89 | + """Rotate the root password for the binddn entry used to manage the ldap secrets engine. |
| 90 | +
|
| 91 | + Supported methods: |
| 92 | + POST: /{mount_point}/rotate root. Produces: 200 application/json |
| 93 | +
|
| 94 | + :param mount_point: The "path" the method/backend was mounted on. |
| 95 | + :type mount_point: str | unicode |
| 96 | + :return: The JSON response of the request. |
| 97 | + :rtype: dict |
| 98 | + """ |
| 99 | + api_path = utils.format_url( |
| 100 | + "/v1/{mount_point}/rotate-root", mount_point=mount_point |
| 101 | + ) |
| 102 | + return self._adapter.post(url=api_path) |
| 103 | + |
| 104 | + def create_or_update_static_role( |
| 105 | + self, |
| 106 | + name, |
| 107 | + username=None, |
| 108 | + dn=None, |
| 109 | + rotation_period=None, |
| 110 | + mount_point=DEFAULT_MOUNT_POINT, |
| 111 | + ): |
| 112 | + """This endpoint creates or updates the ldap static role definition. |
| 113 | +
|
| 114 | + :param name: Specifies the name of an existing static role against which to create this ldap credential. |
| 115 | + :type name: str | unicode |
| 116 | + :param username: The name of a pre-existing service account in LDAP that maps to this static role. |
| 117 | + This value is required on create and cannot be updated. |
| 118 | + :type username: str | unicode |
| 119 | + :param dn: Distinguished name of the existing LDAP entry to manage password rotation for (takes precedence over username). |
| 120 | + Optional but cannot be modified after creation. |
| 121 | + :type dn: str | unicode |
| 122 | + :param rotation_period: How often Vault should rotate the password. |
| 123 | + This is provided as a string duration with a time suffix like "30s" or "1h" or as seconds. |
| 124 | + If not provided, the default Vault rotation_period is used. |
| 125 | + :type rotation_period: str | unicode |
| 126 | + :param mount_point: Specifies the place where the secrets engine will be accessible (default: ad). |
| 127 | + :type mount_point: str | unicode |
| 128 | + :return: The response of the request. |
| 129 | + :rtype: requests.Response |
| 130 | + """ |
| 131 | + api_path = utils.format_url("/v1/{}/static-role/{}", mount_point, name) |
| 132 | + params = {"username": username, "rotation_period": rotation_period} |
| 133 | + params.update(utils.remove_nones({"dn": dn})) |
| 134 | + return self._adapter.post( |
| 135 | + url=api_path, |
| 136 | + json=params, |
| 137 | + ) |
| 138 | + |
| 139 | + def read_static_role(self, name, mount_point=DEFAULT_MOUNT_POINT): |
| 140 | + """This endpoint queries for information about an ldap static role with the given name. |
| 141 | + If no role exists with that name, a 404 is returned. |
| 142 | + :param name: Specifies the name of the static role to query. |
| 143 | + :type name: str | unicode |
| 144 | + :param mount_point: Specifies the place where the secrets engine will be accessible (default: ad). |
| 145 | + :type mount_point: str | unicode |
| 146 | + :return: The response of the request. |
| 147 | + :rtype: requests.Response |
| 148 | + """ |
| 149 | + api_path = utils.format_url("/v1/{}/static-role/{}", mount_point, name) |
| 150 | + return self._adapter.get( |
| 151 | + url=api_path, |
| 152 | + ) |
| 153 | + |
| 154 | + def list_static_roles(self, mount_point=DEFAULT_MOUNT_POINT): |
| 155 | + """This endpoint lists all existing static roles in the secrets engine. |
| 156 | + :return: The response of the request. |
| 157 | + :rtype: requests.Response |
| 158 | + """ |
| 159 | + api_path = utils.format_url("/v1/{}/static-role", mount_point) |
| 160 | + return self._adapter.list( |
| 161 | + url=api_path, |
| 162 | + ) |
| 163 | + |
| 164 | + def delete_static_role(self, name, mount_point=DEFAULT_MOUNT_POINT): |
| 165 | + """This endpoint deletes an ldap static role with the given name. |
| 166 | + Even if the role does not exist, this endpoint will still return a successful response. |
| 167 | + :param name: Specifies the name of the role to delete. |
| 168 | + :type name: str | unicode |
| 169 | + :param mount_point: Specifies the place where the secrets engine will be accessible (default: ad). |
| 170 | + :type mount_point: str | unicode |
| 171 | + :return: The response of the request. |
| 172 | + :rtype: requests.Response |
| 173 | + """ |
| 174 | + api_path = utils.format_url("/v1/{}/static-role/{}", mount_point, name) |
| 175 | + return self._adapter.delete( |
| 176 | + url=api_path, |
| 177 | + ) |
| 178 | + |
| 179 | + def generate_static_credentials(self, name, mount_point=DEFAULT_MOUNT_POINT): |
| 180 | + """This endpoint retrieves the previous and current LDAP password for |
| 181 | + the associated account (or rotate if required) |
| 182 | +
|
| 183 | + :param name: Specifies the name of the static role to request credentials from. |
| 184 | + :type name: str | unicode |
| 185 | + :param mount_point: Specifies the place where the secrets engine will be accessible (default: ad). |
| 186 | + :type mount_point: str | unicode |
| 187 | + :return: The response of the request. |
| 188 | + :rtype: requests.Response |
| 189 | + """ |
| 190 | + api_path = utils.format_url("/v1/{}/static-cred/{}", mount_point, name) |
| 191 | + return self._adapter.get( |
| 192 | + url=api_path, |
| 193 | + ) |
0 commit comments