
                              S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr	  SSKJ
r
  SSKJr  SS	KJr  SSKr\R                  " S
5      rSr " S S\	R$                  5      r " S S\5      rSS jrS rS r  SS jrg)z:Utility library for configuring docker credential helpers.    )absolute_import)division)unicode_literalsN)
client_lib)	constants)files)semverz1.13credHelpersc                       \ rS rSrSrSrg)DockerConfigUpdateError#   zCError thrown for issues updating Docker configuration file updates. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/googlecloudsdk/core/docker/credential_utils.pyr   r   #   s    Kr   r   c                   t    \ rS rSrSrSS jrS r\S 5       rS r	S r
S	 rS
 rSS jrS r\SS j5       rSrg)Configuration'   zCFull Docker configuration configuration file and related meta-data.Nc                 *    Xl         X l        S U l        g N)contentspath_version)selfconfig_datar   s      r   __init__Configuration.__init__*   s    MIDMr   c                 t    U R                   UR                   :H  =(       a    U R                  UR                  :H  $ r   )r   r   )r   others     r   __eq__Configuration.__eq__/   s,    MMU^^+ $II#%r   c                     U(       a  UR                  5       (       a  0 nO[        R                  " U5      n[        X25      $ )zBuild a Configuration object from a JSON string.

Args:
  json_string: string, json content for Configuration
  path: string, file path to Docker Configuation File

Returns:
  a Configuration object
)isspacejsonloadsr   )clsjson_stringr   config_dicts       r   FromJsonConfiguration.FromJson3   s3     +--//kJJ{+k++r   c                 @    [         R                  " U R                  SS9$ )z9Get this Configuration objects contents as a JSON string.   )indent)r)   dumpsr   r   s    r   ToJsonConfiguration.ToJsonD   s    ::dmmA..r   c                     U R                   (       dD  [        R                  " [        R                  " 5       5      n[
        R                  " U5      U l         U R                   $ r   )r   six	text_typeclient_utilsGetDockerVersionr	   LooseVersion)r   version_strs     r   DockerVersionConfiguration.DockerVersionH   s<    ==MM,"?"?"ABk))+6dm==r   c                 @     U R                  5       [        :  $ !    g= f)z?Returns True unless Docker is confirmed to not support helpers.T)r>    MIN_DOCKER_CONFIG_HELPER_VERSIONr4   s    r   SupportsRegistryHelpers%Configuration.SupportsRegistryHelpersN   s&    !%EEEs    c                     U R                   (       a-  [        U R                   ;   a  [        U R                   [           0$ 0 $ )zReturns credential helpers entry from the Docker config file.

Returns:
  'credHelpers' entry if it is specified in the Docker configuration or
  empty dict if the config does not contain a 'credHelpers' key.

)r   CREDENTIAL_HELPER_KEYr4   s    r   GetRegisteredCredentialHelpers,Configuration.GetRegisteredCredentialHelpersV   s1     }}.$--?#T]]3H%IJJIr   c                 X   U=(       d    [        [        5       5      n[        U[        5      (       d  [	        SR                  U5      5      eU R                  5       (       d(  [        SR                  U R                  5       5      5      eXR                  [        '   U R                  5         g)a  Adds Docker 'credHelpers' entry to this configuration.

Adds Docker 'credHelpers' entry to this configuration and writes updated
configuration to disk.

Args:
  mappings_dict: The dict of 'credHelpers' mappings ({registry: handler}) to
    add to the Docker configuration. If not set, use the values from
    BuildOrderedCredentialHelperRegistries(DefaultAuthenticatedRegistries())

Raises:
  ValueError: mappings are not a valid dict.
  DockerConfigUpdateError: Configuration does not support 'credHelpers'.
z-Invalid Docker credential helpers mappings {}zBCredential Helpers not supported for this Docker client version {}N)&BuildOrderedCredentialHelperRegistriesDefaultAuthenticatedRegistries
isinstancedict
ValueErrorformatrB   r   r>   r   rE   WriteToDisk)r   mappings_dicts     r   RegisterCredentialHelpers'Configuration.RegisterCredentialHelpersc   s     " *%K&(&*MmT**
9
@
@
OQ Q ''))# %??Ev(,(:(:(<@>? ? ,9MM'(r   c                     [         R                  " U R                  U R                  5       5        g! [        [
        [        [        4 a3  n[        SR                  [        R                  " U5      5      5      eSnAff = f)z$Writes Conifguration object to disk.z.Error writing Docker configuration to disk: {}N)r   WriteFileAtomicallyr   r5   	TypeErrorrM   OSErrorIOErrorr   rN   r8   r9   )r   errs     r   rO   Configuration.WriteToDisk   sd    N		4;;=9z7G4 N# %228&s9K2LN NNs   /2 A?.A::A?c           	      <   U=(       d    [         R                  " S5      S   n [         R                  " U5      nU " X!5      $ ! [        [         R                  4 a?  n[         R
                  " SR                  U[        R                  " U5      5      5      eSnAff = f)a-  Reads configuration file and meta-data from default Docker location.

Reads configuration file and meta-data from default Docker location. Returns
a Configuration object containing the full contents of the configuration
file, and the configuration file path.

Args:
  path: string, path to look for the Docker config file. If empty will
    attempt to read from the new config location (default).

Returns:
  A Configuration object

Raises:
  ValueError: path or is_new_format are not set.
  InvalidDockerConfigError: config file could not be read as JSON.
Tr   z<Docker configuration file [{}] could not be read as JSON: {}N)	r:   GetDockerConfigPathReadConfigurationFilerM   DockerErrorInvalidDockerConfigErrorrN   r8   r9   )r+   r   contentrX   s       r   ReadFromDiskConfiguration.ReadFromDisk   s    & <<33D9!<D.2248g w 001 .11IF4s+,. ..s   A B:BB)r   r   r   r   )r   r   r   r   r   r!   r%   classmethodr.   r5   r>   rB   rF   rQ   rO   r`   r   r   r   r   r   r   '   sV    K
% , , /:N  r   r   c                 r    U (       a!  [         R                  [         R                  -   $ [         R                  $ )z;Return list of default gcloud credential helper registires.)r   "DEFAULT_REGISTRIES_TO_AUTHENTICATEREGIONAL_AR_REGISTRIES)include_artifact_registrys    r   rJ   rJ      s(    77):Z:ZZZ777r   c                  "    [         R                  $ )zDReturn list of gcloud credential helper supported Docker registires.)r   ALL_SUPPORTED_REGISTRIESr   r   r   SupportedRegistriesri      s    		+	++r   c                 \    [         R                  " U  Vs/ s H  oS4PM     sn5      $ s  snf )a  Returns dict of gcloud helper mappings for the supplied repositories.

Returns ordered dict of Docker registry to gcloud helper mappings for the
supplied list of registries.

Ensures that the order in which credential helper registry entries are
processed is consistent.

Args:
    registries: list, the registries to create the mappings for.

Returns:
 OrderedDict of Docker registry to gcloud helper mappings.
gcloud)collectionsOrderedDict)
registriesregistrys     r   rI   rI      s6    $ 
	 	 +5"+5x:" 
  "s   )c                 L    [        U =(       d    [        U5      5      n[        U0$ )a  Gets the credHelpers Docker config entry for gcloud supported registries.

Returns a Docker configuration JSON entry that will register gcloud as the
credential helper for all Google supported Docker registries.

Args:
    registries: list, the registries to create the mappings for. If not
      supplied, will use DefaultAuthenticatedRegistries().
    include_artifact_registry: bool, whether to include all Artifact Registry
      domains as well as GCR domains registries when called with no list of
      registries to add.

Returns:
  The config used to register gcloud as the credential helper for all
  supported Docker registries.
)rI   rJ   rE   )rn   rf   registered_helperss      r   GetGcloudCredentialHelperConfigrr      s-    $ >M23LMO  !3	44r   )F)NF)r   
__future__r   r   r   rl   r)   googlecloudsdk.core.dockerr   r:   r   googlecloudsdk.core.utilr   r	   r8   r<   rA   rE   r]   r   objectr   rJ   ri   rI   rr   r   r   r   <module>rw      s    A &  '   B 0 * + 
#)#6#6v#>  % Ll66 L~F ~B8,
. 04>C5r   