o |nh<@svdZddlmZmZddlZddlZddlmZddlm Z GdddedZ Gd d d e Z Gd d d e Z e Z dS) z HTTP Client Library Adapters )ABCMetaabstractmethodN)utils) DEFAULT_URLc @seZdZdZeddZedddddddddddf dd Zed d Z d d Z ddZ ddZ ddZ ddZddZddZd ddZeddZed!ddZdS)"AdapterzRAbstract base class used when constructing adapters for use with the Client class.cCsP||j|j|jd|jd|jd|jd|j|j|j|j|j|j d S)aCreate a new adapter based on an existing Adapter instance. This can be used to create a new type of adapter that inherits the properties of an existing one. :param adapter: The existing Adapter instance. :type adapter: hvac.Adapters.Adapter certverifytimeoutproxies) base_uritokenrrr r allow_redirectssession namespaceignore_exceptions strict_httprequest_header) r r _kwargsgetr rrrrr)clsadapterr8/usr/local/lib/python3.10/dist-packages/hvac/adapters.py from_adapters     zAdapter.from_adapterNTFc Cs|st}||||_|_|_n|jr|j}|jr|j}|jr$|j}||_||_| |_||_||_ | |_ | |_ | |_ ||||d|_ dS)aiCreate a new request adapter instance. :param base_uri: Base URL for the Vault instance being addressed. :type base_uri: str :param token: Authentication token to include in requests sent to Vault. :type token: str :param cert: Certificates for use in requests sent to the Vault instance. This should be a tuple with the certificate and then key. :type cert: tuple :param verify: Either a boolean to indicate whether TLS verification should be performed when sending requests to Vault, or a string pointing at the CA bundle to use for verification. See http://docs.python-requests.org/en/master/user/advanced/#ssl-cert-verification. :type verify: Union[bool,str] :param timeout: The timeout value for requests sent to Vault. :type timeout: int :param proxies: Proxies to use when preforming requests. See: http://docs.python-requests.org/en/master/user/advanced/#proxies :type proxies: dict :param allow_redirects: Whether to follow redirects when sending requests to Vault. :type allow_redirects: bool :param session: Optional session object to use when performing request. :type session: request.Session :param namespace: Optional Vault Namespace. :type namespace: str :param ignore_exceptions: If True, _always_ return the response object for a given request. I.e., don't raise an exception based on response status code, etc. :type ignore_exceptions: bool :param strict_http: If True, use only standard HTTP verbs in request with additional params, otherwise process as is :type strict_http: bool :param request_header: If true, add the X-Vault-Request header to all requests to protect against SSRF vulnerabilities. :type request_header: bool )rrr r N)requestsSessionrrr r r rrr rrrr) selfr r rrr r r rrrrrrrr__init__,s,. zAdapter.__init__cGsdtdd|S)a0Joins given arguments into a url. Trailing and leading slashes are stripped for each argument. :param args: Multiple parts of a URL to be combined into one string. :type args: str | unicode :return: Full URL combining all provided arguments :rtype: str | unicode /cSst|dS)Nr)strstrip)xrrrsz!Adapter.urljoin..)joinmap)argsrrrurljoinws zAdapter.urljoincCs|jdS)z&Close the underlying Requests session.N)rclose)rrrrr(sz Adapter.closecK|jd|fi|S)aPerforms a GET request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response rrequestrurlkwargsrrrr z Adapter.getcKr))aPerforms a POST request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response postr*r,rrrr0r/z Adapter.postcKr))aPerforms a PUT request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response putr*r,rrrr1r/z Adapter.putcKr))aPerforms a DELETE request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response deleter*r,rrrr2r/zAdapter.deletecKr))aPerforms a LIST request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response listr*r,rrrr3r/z Adapter.listcKr))aPerforms a HEAD request. :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response headr*r,rrrr4r/z Adapter.headcKs&|j|fi|}|r|||_|S)ahPerform a login request. Associated request is typically to a path prefixed with "/v1/auth") and optionally stores the client token sent in the resulting Vault response for use by the :py:meth:`hvac.adapters.Adapter` instance under the _adapter Client attribute. :param url: Path to send the authentication request to. :type url: str | unicode :param use_token: if True, uses the token in the response received from the auth request to set the "token" attribute on the the :py:meth:`hvac.adapters.Adapter` instance under the _adapter Client attribute. :type use_token: bool :param kwargs: Additional keyword arguments to include in the params sent with the request. :type kwargs: dict :return: The response of the auth request. :rtype: requests.Response )r0get_login_tokenr )rr- use_tokenr.responserrrlogins z Adapter.logincCstS)zExtracts the client token from a login response. :param response: The response object returned by the login method. :return: A client token. :rtype: str NotImplementedErrorrr7rrrr5szAdapter.get_login_tokencKst)a{Main method for routing HTTP requests to the configured Vault base_uri. Intended to be implement by subclasses. :param method: HTTP method to use with the request. E.g., GET, POST, etc. :type method: str :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param headers: Additional headers to include with the request. :type headers: dict :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :param raise_exception: If True, raise an exception via utils.raise_for_error(). Set this parameter to False to bypass this functionality. :type raise_exception: bool :return: The response of the request. :rtype: requests.Response r9)rmethodr-headersraise_exceptionr.rrrr+szAdapter.request)TNT)__name__ __module__ __qualname____doc__ classmethodrrr staticmethodr'r(rr0r1r2r3r4r8rr5r+rrrrrs>  K         r) metaclassc@s:eZdZdZdededejfddZddZd d d Z d S) RawAdapterz The RawAdapter adapter class. This adapter adds Vault-specific headers as required and optionally raises exceptions on errors, but always returns Response objects for requests. r<r-r7c Csd}}}}z|j}Wn tyYnw|jddkr4z|}Wn ty.Ynw|d}|dur:|}tj|||j||||ddS)Nz Content-Typezapplication/jsonerrors)rHtextjson)rI Exceptionr=rrJrraise_for_error status_code)rr<r-r7msgrJrIrHrrr_raise_for_errors0      zRawAdapter._raise_for_errorcCs|}|ddS)zExtracts the client token from a login response. :param response: The response object returned by the login method. :type response: requests.Response :return: A client token. :rtype: str auth client_token)rJ)rr7 response_jsonrrrr50s zRawAdapter.get_login_tokenNTc Ksd|vr|dd}d|vs||j|}|si}|jr d|d<|jr(|j|d<|jr0|j|d<|dd}|r>t||d <|j }| ||j rj| d vrj| d i}| d krfd }| d di||d <|jjd||||jd|} | js|r|js|||| | S)aSMain method for routing HTTP requests to the configured Vault base_uri. :param method: HTTP method to use with the request. E.g., GET, POST, etc. :type method: str :param url: Partial URL path to send the request to. This will be joined to the end of the instance's base_uri attribute. :type url: str | unicode :param headers: Additional headers to include with the request. :type headers: dict :param raise_exception: If True, raise an exception via utils.raise_for_error(). Set this parameter to False to bypass this functionality. :type raise_exception: bool :param kwargs: Additional keyword arguments to include in the requests call. :type kwargs: dict :return: The response of the request. :rtype: requests.Response z//rtruezX-Vault-Requestz X-Vault-TokenzX-Vault-Namespacewrap_ttlNzX-Vault-Wrap-TTL)r3paramsr3r)r<r-r=r r)replacer'r rr rpopr rcopyupdaterlowerrrr+r okrrO) rr<r-r=r>r.rTrrUr7rrrr+;sD         zRawAdapter.requestr?) r@rArBrCr rResponserOr5r+rrrrrG s  rGcs(eZdZdZddZfddZZS) JSONAdapterz The JSONAdapter adapter class. This adapter works just like the RawAdapter adapter except that HTTP 200 responses are returned as JSON dicts. All non-200 responses are returned as Response objects. cCs |ddS)zExtracts the client token from a login response. :param response: The response object returned by the login method. :type response: dict | requests.Response :return: A client token. :rtype: str rPrQrr;rrrr5s zJSONAdapter.get_login_tokencs@tj|i|}|jdkrz|WStyY|Sw|S)aMain method for routing HTTP requests to the configured Vault base_uri. :param args: Positional arguments to pass to RawAdapter.request. :type args: list :param kwargs: Keyword arguments to pass to RawAdapter.request. :type kwargs: dict :return: Dict on HTTP 200 with JSON body, otherwise the response object. :rtype: dict | requests.Response )superr+rMrJ ValueError)rr&r.r7 __class__rrr+s   zJSONAdapter.request)r@rArBrCr5r+ __classcell__rrrarr]}s r])rCabcrrrrequests.exceptionshvacrhvac.constants.clientrrrGr]Requestrrrrs  p&