Ejemplo n.º 1
0
        public Task <ManifestQueryResult> GetManifestAsync(string image, IRegistryCredentialsHost credsHost, bool isDryRun)
        {
            if (isDryRun)
            {
                return(Task.FromResult(new ManifestQueryResult("", new JsonObject())));
            }

            ImageName imageName = ImageName.Parse(image, autoResolveImpliedNames: true);

            BasicAuthenticationCredentials?basicAuthCreds = null;

            // Lookup the credentials, if any, for the registry where the image is located
            if (credsHost.Credentials.TryGetValue(imageName.Registry !, out RegistryCredentials? registryCreds))
            {
                basicAuthCreds = new BasicAuthenticationCredentials
                {
                    UserName = registryCreds.Username,
                    Password = registryCreds.Password
                };
            }

            // Docker Hub's registry has a separate host name for its API
            string apiRegistry = imageName.Registry == DockerHelper.DockerHubRegistry ?
                                 DockerHelper.DockerHubApiRegistry :
                                 imageName.Registry !;

            RegistryServiceClient registryClient = new(apiRegistry, _httpClient, basicAuthCreds);

            return(registryClient.GetManifestAsync(imageName.Repo, (imageName.Tag ?? imageName.Digest) !));
        }
Ejemplo n.º 2
0
        public async Task <string?> GetImageDigestAsync(string image, IRegistryCredentialsHost credsHost, bool isDryRun)
        {
            IEnumerable <string> digests = DockerHelper.GetImageDigests(image, isDryRun);

            // A digest will not exist for images that have been built locally or have been manually installed
            if (!digests.Any())
            {
                return(null);
            }

            string digestSha = await _manifestToolService.GetManifestDigestShaAsync(image, credsHost, isDryRun);

            if (digestSha is null)
            {
                return(null);
            }

            string digest = DockerHelper.GetDigestString(DockerHelper.GetRepo(image), digestSha);

            if (!digests.Contains(digest))
            {
                throw new InvalidOperationException(
                          $"Found published digest '{digestSha}' for tag '{image}' but could not find a matching digest value from " +
                          $"the set of locally pulled digests for this tag: { string.Join(", ", digests) }. This most likely means that " +
                          "this tag has been updated since it was last pulled.");
            }

            return(digest);
        }
Ejemplo n.º 3
0
        public static async Task <IEnumerable <string> > GetImageLayersAsync(
            this IManifestService manifestService, string tag, IRegistryCredentialsHost credsHost, bool isDryRun)
        {
            ManifestQueryResult manifestResult = await manifestService.GetManifestAsync(tag, credsHost, isDryRun);

            if (isDryRun)
            {
                return(Enumerable.Empty <string>());
            }

            if (!manifestResult.Manifest.ContainsKey("layers"))
            {
                JsonArray manifests = (JsonArray)(manifestResult.Manifest["manifests"] ??
                                                  throw new InvalidOperationException("Expected manifests property"));
                throw new InvalidOperationException(
                          $"'{tag}' is expected to be a concrete tag with 1 manifest. It has '{manifests.Count}' manifests.");
            }

            return(((JsonArray)manifestResult.Manifest["layers"] !)
                   .Select(layer => (layer !["digest"] ?? throw new InvalidOperationException("Expected digest property")).ToString())
Ejemplo n.º 4
0
 public Task <IEnumerable <string> > GetImageManifestLayersAsync(string image, IRegistryCredentialsHost credsHost, bool isDryRun) =>
 _manifestToolService.GetImageLayersAsync(image, credsHost, isDryRun);