o
    ɶd                     @   s   d dl mZ d dlZd dlZd dlZd dlmZ ddlmZ dZ	dZ
dZd	d
iZeeZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd ZdS )    )absolute_importN)requests   )%InstancePrincipalsSecurityTokenSignerOCI_RESOURCE_PRINCIPAL_RPT_PATHOCI_RESOURCE_PRINCIPAL_RPT_IDz%/20180711/resourcePrincipalToken/{id}ZAuthorizationzBearer Oraclec                   @   s   e Zd ZdZdd ZdS )RptPathProviderInterfacez9An informal interface which returns the complete RPT pathc                 C      d S N selfr   r   ?usr/lib/python3.10/site-packages/oci/auth/rpt_path_providers.pyget_path      z!RptPathProviderInterface.get_pathN)__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   c                   @   s   e Zd Zdd Zdd ZdS )StringRptPathProviderc                 C   s   || _ td| d S )Nz+Found String RPT Path Provider with path {})pathloggerdebugformat)r   r   r   r   r   __init__   s   zStringRptPathProvider.__init__c                 C      | j S r
   )r   r   r   r   r   r   !      zStringRptPathProvider.get_pathN)r   r   r   r   r   r   r   r   r   r      s    r   c                   @   s4   e Zd ZdZejZdd Zdd Zej	dd Z
dS )	AbstractRptPathProviderzThis abstract path provider has a string template with placeholders like {id},
    and provides a way to get a mapping of substitutions to fill inc                 C   s
   || _ d S r
   )path_templater   r   r   r   r   r   *   s   
z AbstractRptPathProvider.__init__c                 C   s.   |   }| jjdi |}td| |S )NzUsing path {}r   )get_replacementsr   r   r   r   )r   replacementsr   r   r   r   r   -   s   z AbstractRptPathProvider.get_pathc                 C   r	   r
   r   r   r   r   r   r    3   s   z(AbstractRptPathProvider.get_replacementsN)r   r   r   r   abcABCMetaZ__metaclass__r   r   abstractmethodr    r   r   r   r   r   %   s    r   c                       4   e Zd Z fddZdd Zdd Zdd Z  ZS )	ImdsRptPathProviderc                    "   t t| |   |  | _d S r
   )superr&   r   get_path_templatebuild_replacementsr!   r   	__class__r   r   r   9      zImdsRptPathProvider.__init__c                 C   r   r
   r!   r   r   r   r   r    =   r   z$ImdsRptPathProvider.get_replacementsc                 C   s   t S r
   )IMDS_PATH_TEMPLATEr   r   r   r   r)   @   r   z%ImdsRptPathProvider.get_path_templatec                 C   s
   dt  iS Nid)get_instance_id_from_imdsr   r   r   r   r*   C   s   
z&ImdsRptPathProvider.build_replacementsr   r   r   r   r    r)   r*   __classcell__r   r   r+   r   r&   8   
    r&   c                       r%   )	EnvRptPathProviderc                    r'   r
   )r(   r6   r   r)   r*   r!   r   r+   r   r   r   I   r-   zEnvRptPathProvider.__init__c                 C   r   r
   r.   r   r   r   r   r    M   r   z#EnvRptPathProvider.get_replacementsc                 C   s   t jtS r
   )osenvirongetr   r   r   r   r   r)   P   s   z$EnvRptPathProvider.get_path_templatec                 C   s   t jt}|rd|iS d S r0   )r7   r8   r9   r   )r   Zrpt_idr   r   r   r*   S   s   z%EnvRptPathProvider.build_replacementsr3   r   r   r+   r   r6   H   r5   r6   c                       s8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
DefaultRptPathProvidera-  
    This path provider makes sure the behavior happens with the correct fallback.

    For the path,
    Use the contents of the OCI_RESOURCE_PRINCIPAL_RPT_PATH environment variable, if set.
    Otherwise, use the current path: "/20180711/resourcePrincipalToken/{id}"

    For the resource id,
    Use the contents of the OCI_RESOURCE_PRINCIPAL_RPT_ID environment variable, if set.
    Otherwise, use IMDS to get the instance id

    This path provider is used when the caller doesn't provide a specific path provider to the resource principals signer
    c                    s<   t d t | _t | _tt| | 	  | 
 | _d S )Nz?A path provider was not specified, using DefaultRptPathProvider)r   r   r6   env_rpt_path_providerr&   imds_rpt_path_providerr(   r:   r   r)   r*   r!   r   r+   r   r   r   i   s
   
zDefaultRptPathProvider.__init__c                 C   <   | j  }|stdt | j }td| |S )NzEUnable to get path template from {} env variable, using IMDS templatezThe path template is {})r;   r)   r   r   r   r   r<   r   r   r   r   r)   p      

z(DefaultRptPathProvider.get_path_templatec                 C   r=   )NzOUnable to get replacements from {} env variable, getting replacements from IMDSzThe replacement dict is {})r;   r*   r   r   r   r   r<   )r   r!   r   r   r   r*   x   r>   z)DefaultRptPathProvider.build_replacementsc                 C   r   r
   r.   r   r   r   r   r       r   z'DefaultRptPathProvider.get_replacements)	r   r   r   r   r   r)   r*   r    r4   r   r   r+   r   r:   Z   s    r:   c                  C   s.   d tj} d}tj| |td}|j  S )Nz{}/instance/id)
   <   )timeoutheaders)	r   r   ZMETADATA_URL_BASEr   r9   METADATA_AUTH_HEADERStextstriplower)ZendpointrA   responser   r   r   r2      s   r2   )
__future__r   r"   r7   loggingZoci._vendorr   Z1signers.instance_principals_security_token_signerr   r   r   r/   rC   	getLoggerr   r   objectr   r   r   r&   r6   r:   r2   r   r   r   r   <module>   s$   
	*