コード例 #1
0
 /**
  * Returns a {@link DockerCredentialHelperFactory} that checks given parameters upon creating a
  * {@link DockerCredentialHelper} instance.
  *
  * @param expectedRegistry the expected registry given to the factory
  * @param expectedCredentialHelper the expected credential helper path given to the factory
  * @param returnedCredentialHelper the mock credential helper to return
  * @return a new {@link DockerCredentialHelperFactory}
  */
 private static DockerCredentialHelperFactory GetTestFactory(
     string expectedRegistry,
     SystemPath expectedCredentialHelper,
     IDockerCredentialHelper returnedCredentialHelper)
 {
     return((registry, credentialHelper) =>
     {
         Assert.AreEqual(expectedRegistry, registry);
         Assert.AreEqual(expectedCredentialHelper, credentialHelper);
         return returnedCredentialHelper;
     });
 }
コード例 #2
0
        /**
         * Retrieves credentials for a registry alias from a {@link DockerConfig}.
         *
         * @param dockerConfig the {@link DockerConfig} to retrieve from
         * @param logger a consumer for handling log events
         * @return the retrieved credentials, or {@code Optional#empty} if none are found
         */

        public Maybe <Credential> Retrieve(IDockerConfig dockerConfig, Action <LogEvent> logger)
        {
            logger = logger ?? throw new ArgumentNullException(nameof(logger));
            foreach (string registryAlias in RegistryAliasGroup.GetAliasesGroup(registry))
            {
                // First, tries to find defined auth.
                string auth = dockerConfig.GetAuthFor(registryAlias);
                if (auth != null)
                {
                    // 'auth' is a basic authentication token that should be parsed back into credentials
                    string usernameColonPassword = Encoding.UTF8.GetString(Convert.FromBase64String(auth));
                    int    colonIndex            = usernameColonPassword.IndexOf(":", StringComparison.Ordinal);
                    string username = usernameColonPassword.Substring(0, colonIndex);

                    string password = usernameColonPassword.Substring(colonIndex + 1);
                    return(Maybe.Of(Credential.From(username, password)));
                }

                // Then, tries to use a defined credHelpers credential helper.
                IDockerCredentialHelper dockerCredentialHelper =
                    dockerConfig.GetCredentialHelperFor(registryAlias);
                if (dockerCredentialHelper != null)
                {
                    try
                    {
                        // Tries with the given registry alias (may be the original registry).
                        return(Maybe.Of(dockerCredentialHelper.Retrieve()));
                    }
                    catch (Exception ex) when(ex is IOException || ex is CredentialHelperUnhandledServerUrlException || ex is CredentialHelperNotFoundException)
                    {
                        // Warns the user that the specified credential helper cannot be used.
                        if (ex.Message != null)
                        {
                            logger(LogEvent.Warn(ex.Message));
                            if (ex.InnerException?.Message != null)
                            {
                                logger(LogEvent.Warn("  Caused by: " + ex.InnerException.Message));
                            }
                        }
                    }
                }
            }
            return(Maybe.Empty <Credential>());
        }