Ejemplo n.º 1
0
        public void TestGetAliasesGroup_noKnownAliases()
        {
            IList <string> singleton = RegistryAliasGroup.GetAliasesGroup("something.gcr.io");

            Assert.AreEqual(1, singleton.Count);
            Assert.AreEqual("something.gcr.io", singleton[0]);
        }
Ejemplo n.º 2
0
        public void TestGetAliasesGroup_dockerHub()
        {
            ISet <string> aliases = new HashSet <string>
            {
                "registry.hub.docker.com",
                "index.docker.io",
                "registry-1.docker.io",
                "docker.io"
            };

            foreach (string alias in aliases)
            {
                CollectionAssert.AreEquivalent(aliases, new HashSet <string>(RegistryAliasGroup.GetAliasesGroup(alias)));
            }
        }
        /**
         * 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>());
        }