/// <inheritdoc />
        public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname)
        {
            this.logger.SearchingDockerRegistryCredential("CredsStore");

            if (!this.IsApplicable(hostname))
            {
                return(null);
            }

            var credentialProviderOutput = DockerCredentialProcess.Get(this.rootElement.GetString(), hostname);

            if (string.IsNullOrEmpty(credentialProviderOutput))
            {
                return(null);
            }

            AuthConfig authConfig;

            try
            {
                authConfig = JsonSerializer.Deserialize <AuthConfig>(JsonDocument.Parse(credentialProviderOutput).RootElement.GetRawText(), JsonSerializerOptions);
            }
            catch (JsonException)
            {
                return(null);
            }

            this.logger.DockerRegistryCredentialFound(hostname);
            return(new DockerRegistryAuthenticationConfiguration(authConfig.ServerAddress, authConfig.Username, authConfig.Password));
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public IDockerRegistryAuthenticationConfiguration GetAuthConfig(string hostname)
        {
            this.logger.SearchingDockerRegistryCredential("CredHelpers");

            if (!this.IsApplicable(hostname))
            {
                return(null);
            }

#if NETSTANDARD2_1_OR_GREATER
            var registryEndpointProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.Contains(hostname, StringComparison.OrdinalIgnoreCase));
#else
            var registryEndpointProperty = this.rootElement.EnumerateObject().LastOrDefault(property => property.Name.IndexOf(hostname, StringComparison.OrdinalIgnoreCase) >= 0);
#endif

            if (!JsonValueKind.String.Equals(registryEndpointProperty.Value.ValueKind))
            {
                return(null);
            }

            if (string.IsNullOrEmpty(registryEndpointProperty.Value.GetString()))
            {
                return(null);
            }

            var credentialProviderOutput = DockerCredentialProcess.Get(registryEndpointProperty.Value.GetString(), hostname);
            if (string.IsNullOrEmpty(credentialProviderOutput))
            {
                return(null);
            }

            JsonElement credential;

            try
            {
                credential = JsonDocument.Parse(credentialProviderOutput).RootElement;
            }
            catch (JsonException)
            {
                return(null);
            }

            var username = credential.TryGetProperty("Username", out var usernameProperty) ? usernameProperty.GetString() : null;

            var password = credential.TryGetProperty("Secret", out var passwordProperty) ? passwordProperty.GetString() : null;

            this.logger.DockerRegistryCredentialFound(hostname);

            if ("<token>".Equals(username, StringComparison.OrdinalIgnoreCase))
            {
                return(new DockerRegistryAuthenticationConfiguration(hostname, null, null, password));
            }
            else
            {
                return(new DockerRegistryAuthenticationConfiguration(hostname, username, password));
            }
        }