Ejemplo n.º 1
0
        public void Test_fromJson()
        {
            // Loads the JSON string.
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/dockerconfig.json").ToURI());

            // Deserializes into a docker config JSON object.
            DockerConfig dockerConfig =
                new DockerConfig(JsonTemplateMapper.ReadJsonFromFile <DockerConfigTemplate>(jsonFile));

            Assert.AreEqual("some:auth", DecodeBase64(dockerConfig.GetAuthFor("some registry")));
            Assert.AreEqual(
                "some:other:auth", DecodeBase64(dockerConfig.GetAuthFor("some other registry")));
            Assert.AreEqual("token", DecodeBase64(dockerConfig.GetAuthFor("registry")));
            Assert.AreEqual("token", DecodeBase64(dockerConfig.GetAuthFor("https://registry")));
            Assert.IsNull(dockerConfig.GetAuthFor("just registry"));

            Assert.AreEqual(
                Paths.Get("docker-credential-some credential store"),
                dockerConfig.GetCredentialHelperFor("some registry").GetCredentialHelper());
            Assert.AreEqual(
                Paths.Get("docker-credential-some credential store"),
                dockerConfig.GetCredentialHelperFor("some other registry").GetCredentialHelper());
            Assert.AreEqual(
                Paths.Get("docker-credential-some credential store"),
                dockerConfig.GetCredentialHelperFor("just registry").GetCredentialHelper());
            Assert.AreEqual(
                Paths.Get("docker-credential-some credential store"),
                dockerConfig.GetCredentialHelperFor("with.protocol").GetCredentialHelper());
            Assert.AreEqual(
                Paths.Get("docker-credential-another credential helper"),
                dockerConfig.GetCredentialHelperFor("another registry").GetCredentialHelper());
            Assert.IsNull(dockerConfig.GetCredentialHelperFor("unknonwn registry"));
        }
Ejemplo n.º 2
0
        /**
         * Extract an {@link ErrorCodes} response from the error object encoded in an {@link
         * HttpResponseException}.
         *
         * @param httpResponseException the response exception
         * @return the parsed {@link ErrorCodes} if found
         * @throws HttpResponseException rethrows the original exception if an error object could not be
         *     parsed, if there were multiple error objects, or if the error code is unknown.
         */
        public static async Task <ErrorCode> GetErrorCodeAsync(HttpResponseMessage httpResponse)
        {
            httpResponse = httpResponse ?? throw new ArgumentNullException(nameof(httpResponse));
            // Obtain the error response code.
            string errorContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            if (errorContent == null)
            {
                throw new HttpResponseException(httpResponse);
            }

            try
            {
                ErrorResponseTemplate errorResponse =
                    JsonTemplateMapper.ReadJson <ErrorResponseTemplate>(errorContent);
                IReadOnlyList <ErrorEntryTemplate> errors = errorResponse?.Errors;
                // There may be multiple error objects
                if (errors?.Count == 1)
                {
                    var errorCode = errors[0].Code;
                    // May not get an error code back.
                    if (errorCode.HasValue)
                    {
                        return(errorCode.GetValueOrDefault());
                    }
                }
            }
            catch (Exception e) when(e is IOException || e is ArgumentException)
            {
                // Parse exception: either isn't an error object or unknown error code
            }

            // rethrow the original exception
            throw new HttpResponseException(httpResponse);
        }
Ejemplo n.º 3
0
        public async Task WriteToAsync(Stream @out)
        {
            TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
            DockerLoadManifestEntryTemplate manifestTemplate = new DockerLoadManifestEntryTemplate();

            // Adds all the layers to the tarball and manifest.
            foreach (ILayer layer in image.GetLayers())
            {
                string layerName = layer.GetBlobDescriptor().GetDigest().GetHash() + LAYER_FILE_EXTENSION;

                tarStreamBuilder.AddBlobEntry(
                    layer.GetBlob(), layer.GetBlobDescriptor().GetSize(), layerName);
                manifestTemplate.AddLayerFile(layerName);
            }

            // Adds the container configuration to the tarball.
            ContainerConfigurationTemplate containerConfiguration =
                new ImageToJsonTranslator(image).GetContainerConfiguration();

            tarStreamBuilder.AddByteEntry(
                JsonTemplateMapper.ToByteArray(containerConfiguration),
                CONTAINER_CONFIGURATION_JSON_FILE_NAME);

            // Adds the manifest to tarball.
            manifestTemplate.SetRepoTags(imageReference.ToStringWithTag());
            tarStreamBuilder.AddByteEntry(
                JsonTemplateMapper.ToByteArray(new List <DockerLoadManifestEntryTemplate> {
                manifestTemplate
            }),
                MANIFEST_JSON_FILE_NAME);

            await tarStreamBuilder.WriteAsTarArchiveToAsync(@out).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public void TestReadJsonWithLock()
        {
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/basic.json").ToURI());

            // Deserializes into a metadata JSON object.
            TestJson testJson = JsonTemplateMapper.ReadJsonFromFileWithLock <TestJson>(jsonFile);

            Assert.AreEqual(testJson.Number, 54);
            Assert.AreEqual(testJson.Text, "crepecake");
            Assert.AreEqual(
                testJson.Digest,
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"));
            Assert.IsInstanceOf <TestJson.InnerObjectClass>(testJson.InnerObject);
            Assert.AreEqual(testJson.InnerObject.Number, 23);

            Assert.AreEqual(
                testJson.InnerObject.Texts, new[] { "first text", "second text" });

            Assert.AreEqual(testJson.InnerObject.Digests, new[]
            {
                DescriptorDigest.FromDigest("sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10"),
                DescriptorDigest.FromHash("4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236")
            });

            // ignore testJson.list
        }
Ejemplo n.º 5
0
        public void TestReadListOfJson()
        {
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/basic_list.json").ToURI());

            string           jsonString  = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));
            IList <TestJson> listofJsons = JsonTemplateMapper.ReadListOfJson <TestJson>(jsonString);
            TestJson         json1       = listofJsons[0];
            TestJson         json2       = listofJsons[1];

            DescriptorDigest digest1 =
                DescriptorDigest.FromDigest(
                    "sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10");
            DescriptorDigest digest2 =
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad");

            Assert.AreEqual(1, json1.Number);
            Assert.AreEqual(2, json2.Number);
            Assert.AreEqual("text1", json1.Text);
            Assert.AreEqual("text2", json2.Text);
            Assert.AreEqual(digest1, json1.Digest);
            Assert.AreEqual(digest2, json2.Digest);
            Assert.AreEqual(10, json1.InnerObject.Number);
            Assert.AreEqual(20, json2.InnerObject.Number);
            Assert.AreEqual(2, json1.List.Count);
            Assert.IsTrue(json2.List.Count == 0);
        }
Ejemplo n.º 6
0
        public static async Task <BlobDescriptor> ComputeJsonDigestAsync(object template, Stream outStream)
        {
            async Task ContentsAsync(Stream contentsOut) =>
            await JsonTemplateMapper.WriteToAsync(template, contentsOut).ConfigureAwait(false);

            return(await ComputeDigestAsync(ContentsAsync, outStream).ConfigureAwait(false));
        }
        public void TestToJson()
        {
            // Loads the expected JSON string.
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource("core/json/containerconfig.json").ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            // Creates the JSON object to serialize.
            ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate
            {
                Created      = "1970-01-01T00:00:20Z",
                Architecture = "wasm",
                Os           = "js"
            };

            containerConfigJson.SetContainerEnvironment(new[] { "VAR1=VAL1", "VAR2=VAL2" });
            containerConfigJson.SetContainerEntrypoint(new[] { "some", "entrypoint", "command" });
            containerConfigJson.SetContainerCmd(new[] { "arg1", "arg2" });
            containerConfigJson.SetContainerHealthCheckTest(new[] { "CMD-SHELL", "/checkhealth" });
            containerConfigJson.SetContainerHealthCheckInterval(3000000000L);
            containerConfigJson.SetContainerHealthCheckTimeout(1000000000L);
            containerConfigJson.SetContainerHealthCheckStartPeriod(2000000000L);
            containerConfigJson.SetContainerHealthCheckRetries(3);
            containerConfigJson.SetContainerExposedPorts(
                new Dictionary <string, IDictionary <object, object> >
            {
                ["1000/tcp"] =
                    ImmutableDictionary.Create <object, object>(),
                ["2000/tcp"] =
                    ImmutableDictionary.Create <object, object>(),
                ["3000/udp"] =
                    ImmutableDictionary.Create <object, object>()
            }.ToImmutableSortedDictionary());
            containerConfigJson.SetContainerLabels(ImmutableDic.Of("key1", "value1", "key2", "value2"));
            containerConfigJson.SetContainerVolumes(
                ImmutableDic.Of <string, IDictionary <object, object> >(
                    "/var/job-result-data", ImmutableDictionary.Create <object, object>(), "/var/log/my-app-logs", ImmutableDictionary.Create <object, object>()));
            containerConfigJson.SetContainerWorkingDir("/some/workspace");
            containerConfigJson.SetContainerUser("tomcat");

            containerConfigJson.AddLayerDiffId(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"));
            containerConfigJson.AddHistoryEntry(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(0))
                .SetAuthor("Bazel")
                .SetCreatedBy("bazel build ...")
                .SetEmptyLayer(true)
                .Build());
            containerConfigJson.AddHistoryEntry(
                HistoryEntry.CreateBuilder()
                .SetCreationTimestamp(Instant.FromUnixTimeSeconds(20))
                .SetAuthor("Fib")
                .SetCreatedBy("fib")
                .Build());

            // Serializes the JSON object.
            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(containerConfigJson));
        }
Ejemplo n.º 8
0
        public void TestToBlob_listOfJson()
        {
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/basic_list.json").ToURI());

            string          jsonString = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));
            List <TestJson> listOfJson = JsonTemplateMapper.ReadListOfJson <TestJson>(jsonString);

            Assert.AreEqual(jsonString, JsonTemplateMapper.ToUtf8String(listOfJson));
        }
Ejemplo n.º 9
0
        public void TestGetAuthFor_correctSuffixMatching()
        {
            SystemPath json =
                Paths.Get(TestResources.GetResource("core/json/dockerconfig_extra_matches.json").ToURI());

            DockerConfig dockerConfig =
                new DockerConfig(JsonTemplateMapper.ReadJsonFromFile <DockerConfigTemplate>(json));

            Assert.IsNull(dockerConfig.GetAuthFor("example"));
        }
Ejemplo n.º 10
0
        public void TestGetCredentialHelperFor_withProtocolAndSuffix()
        {
            SystemPath json = Paths.Get(TestResources.GetResource("core/json/dockerconfig.json").ToURI());

            DockerConfig dockerConfig =
                new DockerConfig(JsonTemplateMapper.ReadJsonFromFile <DockerConfigTemplate>(json));

            Assert.AreEqual(
                Paths.Get("docker-credential-some credential store"),
                dockerConfig.GetCredentialHelperFor("with.protocol.and.suffix").GetCredentialHelper());
        }
        public void TestFromJson()
        {
            // Loads the JSON string.
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/containerconfig.json").ToURI());

            // Deserializes into a manifest JSON object.
            ContainerConfigurationTemplate containerConfigJson =
                JsonTemplateMapper.ReadJsonFromFile <ContainerConfigurationTemplate>(jsonFile);

            Assert.AreEqual("1970-01-01T00:00:20Z", containerConfigJson.Created);
            Assert.AreEqual("wasm", containerConfigJson.Architecture);
            Assert.AreEqual("js", containerConfigJson.Os);
            Assert.AreEqual(
                new[] { "VAR1=VAL1", "VAR2=VAL2" }, containerConfigJson.GetContainerEnvironment());
            Assert.AreEqual(
                new[] { "some", "entrypoint", "command" },
                containerConfigJson.GetContainerEntrypoint());
            Assert.AreEqual(new[] { "arg1", "arg2" }, containerConfigJson.GetContainerCmd());

            Assert.AreEqual(
                new[] { "CMD-SHELL", "/checkhealth" }, containerConfigJson.GetContainerHealthTest());
            Assert.IsNotNull(containerConfigJson.GetContainerHealthInterval());
            Assert.AreEqual(3000000000L, containerConfigJson.GetContainerHealthInterval().GetValueOrDefault());
            Assert.IsNotNull(containerConfigJson.GetContainerHealthTimeout());
            Assert.AreEqual(1000000000L, containerConfigJson.GetContainerHealthTimeout().GetValueOrDefault());
            Assert.IsNotNull(containerConfigJson.GetContainerHealthStartPeriod());
            Assert.AreEqual(
                2000000000L, containerConfigJson.GetContainerHealthStartPeriod().GetValueOrDefault());
            Assert.IsNotNull(containerConfigJson.GetContainerHealthRetries());
            Assert.AreEqual(3, containerConfigJson.GetContainerHealthRetries().GetValueOrDefault());

            Assert.AreEqual(
                ImmutableDic.Of("key1", "value1", "key2", "value2"),
                containerConfigJson.GetContainerLabels());
            Assert.AreEqual("/some/workspace", containerConfigJson.GetContainerWorkingDir());
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                containerConfigJson.GetLayerDiffId(0));
            Assert.AreEqual(
                ImmutableArray.Create(
                    HistoryEntry.CreateBuilder()
                    .SetCreationTimestamp(Instant.FromUnixTimeSeconds(0))
                    .SetAuthor("Bazel")
                    .SetCreatedBy("bazel build ...")
                    .SetEmptyLayer(true)
                    .Build(),
                    HistoryEntry.CreateBuilder()
                    .SetCreationTimestamp(Instant.FromUnixTimeSeconds(20))
                    .SetAuthor("Fib")
                    .SetCreatedBy("fib")
                    .Build()),
                containerConfigJson.History);
        }
        /**
         * Retrieves credentials for a registry. Tries all possible known aliases.
         *
         * @param logger a consumer for handling log events
         * @return {@link Credential} found for {@code registry}, or {@link Optional#empty} if not found
         * @throws IOException if failed to parse the config JSON
         */
        public Maybe <Credential> Retrieve(Action <LogEvent> logger)
        {
            if (!Files.Exists(dockerConfigFile))
            {
                return(Maybe.Empty <Credential>());
            }
            DockerConfig dockerConfig =
                new DockerConfig(
                    JsonTemplateMapper.ReadJsonFromFile <DockerConfigTemplate>(dockerConfigFile));

            return(Retrieve(dockerConfig, logger));
        }
Ejemplo n.º 13
0
        public void TestGetContainerConfiguration()
        {
            SetUp(ManifestFormat.V22);

            // Loads the expected JSON string.
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource("core/json/containerconfig.json").ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            // Translates the image to the container configuration and writes the JSON string.
            ContainerConfigurationTemplate containerConfiguration = imageToJsonTranslator.GetContainerConfiguration();

            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(containerConfiguration));
        }
Ejemplo n.º 14
0
        public void SetUp()
        {
            mockEventHandlers    = Mock.Of <IEventHandlers>();
            v22manifestJsonFile  = Paths.Get(TestResources.GetResource("core/json/v22manifest.json").ToURI());
            fakeManifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <V22ManifestTemplate>(v22manifestJsonFile);

            testManifestPusher =
                new ManifestPusher(
                    new RegistryEndpointRequestProperties("someServerUrl", "someImageName"),
                    fakeManifestTemplate,
                    "test-image-tag",
                    mockEventHandlers);
        }
Ejemplo n.º 15
0
        public void TestWriteJson()
        {
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource("core/json/basic.json").ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            TestJson testJson = new TestJson
            {
                Number = 54,
                Text   = "crepecake",
                Digest =
                    DescriptorDigest.FromDigest(
                        "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                InnerObject = new TestJson.InnerObjectClass
                {
                    Number = 23,
                    Texts  = new List <string> {
                        "first text", "second text"
                    },
                    Digests = new[]
                    {
                        DescriptorDigest.FromDigest(
                            "sha256:91e0cae00b86c289b33fee303a807ae72dd9f0315c16b74e6ab0cdbe9d996c10"),
                        DescriptorDigest.FromHash(
                            "4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236")
                    }
                }
            };

            TestJson.InnerObjectClass innerObject1 = new TestJson.InnerObjectClass
            {
                Number = 42,
                Texts  = new List <string>()
            };
            TestJson.InnerObjectClass innerObject2 = new TestJson.InnerObjectClass
            {
                Number = 99,
                Texts  = new List <string> {
                    "some text"
                },
                Digests =
                    new List <DescriptorDigest> {
                    DescriptorDigest.FromDigest(
                        "sha256:d38f571aa1c11e3d516e0ef7e513e7308ccbeb869770cb8c4319d63b10a0075e")
                }
            };
            testJson.List = new[] { innerObject1, innerObject2 };

            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(testJson));
        }
Ejemplo n.º 16
0
 /**
  * Writes a json template to the destination path by writing to a temporary file then moving the
  * file.
  *
  * @param jsonTemplate the json template
  * @param destination the destination path
  * @throws IOException if an I/O exception occurs
  */
 public static async Task WriteMetadataAsync(object jsonTemplate, SystemPath destination)
 {
     destination = destination ?? throw new ArgumentNullException(nameof(destination));
     using (TemporaryFile temporaryFile = Files.CreateTempFile(destination.GetParent()))
     {
         using (Stream outputStream = Files.NewOutputStream(temporaryFile.Path))
         {
             await JsonTemplateMapper.WriteToAsync(jsonTemplate, outputStream).ConfigureAwait(false);
         }
         Files.Move(
             temporaryFile.Path,
             destination,
             StandardCopyOption.REPLACE_EXISTING);
     }
 }
Ejemplo n.º 17
0
        public void TestGetAuthFor_orderOfMatchPreference()
        {
            SystemPath json =
                Paths.Get(TestResources.GetResource("core/json/dockerconfig_extra_matches.json").ToURI());

            DockerConfig dockerConfig =
                new DockerConfig(JsonTemplateMapper.ReadJsonFromFile <DockerConfigTemplate>(json));

            Assert.AreEqual("my-registry: exact match", dockerConfig.GetAuthFor("my-registry"));
            Assert.AreEqual("cool-registry: with https", dockerConfig.GetAuthFor("cool-registry"));
            Assert.AreEqual(
                "awesome-registry: starting with name", dockerConfig.GetAuthFor("awesome-registry"));
            Assert.AreEqual(
                "dull-registry: starting with name and with https",
                dockerConfig.GetAuthFor("dull-registry"));
        }
Ejemplo n.º 18
0
        /** Tests translation of image to {@link BuildableManifestTemplate}. */
        private async Task TestGetManifestAsync(
            ManifestFormat manifestTemplateClass, string translatedJsonFilename)
        {
            // Loads the expected JSON string.
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource(translatedJsonFilename).ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            // Translates the image to the manifest and writes the JSON string.
            ContainerConfigurationTemplate containerConfiguration = imageToJsonTranslator.GetContainerConfiguration();
            BlobDescriptor blobDescriptor = await Digests.ComputeJsonDescriptorAsync(containerConfiguration).ConfigureAwait(false);

            IBuildableManifestTemplate manifestTemplate =
                imageToJsonTranslator.GetManifestTemplate(manifestTemplateClass, blobDescriptor);

            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(manifestTemplate));
        }
Ejemplo n.º 19
0
        public async Task TestHandleHttpResponseExceptionAsync()
        {
            ErrorResponseTemplate emptyErrorResponseTemplate =
                new ErrorResponseTemplate()
                .AddError(new ErrorEntryTemplate(ErrorCode.BlobUnknown, "some message"));

            using (HttpResponseMessage mockHttpResponseException = new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(JsonTemplateMapper.ToUtf8String(emptyErrorResponseTemplate))
            })
            {
                bool result =
                    await testBlobChecker.HandleHttpResponseExceptionAsync(mockHttpResponseException).ConfigureAwait(false);

                Assert.IsFalse(result);
            }
        }
Ejemplo n.º 20
0
        public async Task TestWriteMetadata_v21Async()
        {
            SystemPath manifestJsonFile =
                Paths.Get(TestResources.GetResource("core/json/v21manifest.json").ToURI());
            V21ManifestTemplate manifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <V21ManifestTemplate>(manifestJsonFile);
            ImageReference imageReference = ImageReference.Parse("image.reference/project/thing:tag");

            await new CacheStorageWriter(cacheStorageFiles).WriteMetadataAsync(imageReference, manifestTemplate).ConfigureAwait(false);

            SystemPath savedManifestPath =
                cacheRoot.Resolve("images/image.reference/project/thing!tag/manifest.json");

            Assert.IsTrue(Files.Exists(savedManifestPath));

            V21ManifestTemplate savedManifest =
                JsonTemplateMapper.ReadJsonFromFile <V21ManifestTemplate>(savedManifestPath);

            Assert.AreEqual("amd64", savedManifest.GetContainerConfiguration().Get().Architecture);
        }
        public void TestToJson()
        {
            // Loads the expected JSON string.
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource("core/json/loadmanifest.json").ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            DockerLoadManifestEntryTemplate template = new DockerLoadManifestEntryTemplate();

            template.SetRepoTags(
                ImageReference.Of("testregistry", "testrepo", "testtag").ToStringWithTag());
            template.AddLayerFile("layer1.tar.gz");
            template.AddLayerFile("layer2.tar.gz");
            template.AddLayerFile("layer3.tar.gz");

            List <DockerLoadManifestEntryTemplate> loadManifest = new List <DockerLoadManifestEntryTemplate> {
                template
            };

            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(loadManifest));
        }
Ejemplo n.º 22
0
        public async Task TestHandleHttpResponseException_notBlobUnknownAsync()
        {
            ErrorResponseTemplate emptyErrorResponseTemplate = new ErrorResponseTemplate();

            using (HttpResponseMessage mockHttpResponseException = new HttpResponseMessage(HttpStatusCode.NotFound)
            {
                Content = new StringContent(JsonTemplateMapper.ToUtf8String(emptyErrorResponseTemplate))
            })
            {
                try
                {
                    await testBlobChecker.HandleHttpResponseExceptionAsync(mockHttpResponseException).ConfigureAwait(false);

                    Assert.Fail("Non-BLOB_UNKNOWN errors should not be handled");
                }
                catch (HttpResponseException ex)
                {
                    Assert.AreEqual(mockHttpResponseException, ex.Cause);
                }
            }
        }
Ejemplo n.º 23
0
        protected override IManifestTemplate GetManifestTemplateFromJson(string jsonString)
        {
            var token = JToken.Parse(jsonString);

            if (!(token is JObject obj))
            {
                throw new UnknownManifestFormatException(Resources.ManifestPullerNotJsonExceptionMessage);
            }
            if (!obj.ContainsKey("schemaVersion"))
            {
                throw new UnknownManifestFormatException(Resources.ManifestPullerMissingSchemaVersionExceptionMessage);
            }
            if (!obj.TryGetValue("schemaVersion", out JToken schemaVersionToken) || schemaVersionToken.Type != JTokenType.Integer)
            {
                throw new UnknownManifestFormatException(Resources.ManifestPullerSchemaVersionNotIntExceptionMessage);
            }
            int schemaVersion = schemaVersionToken.Value <int>();

            if (schemaVersion == 1)
            {
                return(JsonTemplateMapper.ReadJson <V21ManifestTemplate>(jsonString));
            }
            if (schemaVersion == 2)
            {
                // 'schemaVersion' of 2 can be either Docker V2.2 or OCI.
                string mediaType = obj.Value <string>("mediaType");
                if (V22ManifestTemplate.ManifestMediaType == mediaType)
                {
                    return(JsonTemplateMapper.ReadJson <V22ManifestTemplate>(jsonString));
                }
                if (OCIManifestTemplate.ManifestMediaType == mediaType)
                {
                    return(JsonTemplateMapper.ReadJson <OCIManifestTemplate>(jsonString));
                }
                throw new UnknownManifestFormatException("Unknown mediaType: " + mediaType);
            }
            throw new UnknownManifestFormatException(
                      "Unknown schemaVersion: " + schemaVersion + " - only 1 and 2 are supported");
        }
Ejemplo n.º 24
0
        public void TestToJson()
        {
            // Loads the expected JSON string.
            SystemPath jsonFile     = Paths.Get(TestResources.GetResource("core/json/ocimanifest.json").ToURI());
            string     expectedJson = Encoding.UTF8.GetString(Files.ReadAllBytes(jsonFile));

            // Creates the JSON object to serialize.
            OCIManifestTemplate manifestJson = new OCIManifestTemplate();

            manifestJson.SetContainerConfiguration(
                1000,
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"));

            manifestJson.AddLayer(
                1000_000,
                DescriptorDigest.FromHash(
                    "4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236"));

            // Serializes the JSON object.
            Assert.AreEqual(expectedJson, JsonTemplateMapper.ToUtf8String(manifestJson));
        }
Ejemplo n.º 25
0
        /**
         * Attempts to parse the container configuration JSON (of format {@code
         * application/vnd.docker.container.image.v1+json}) from the {@code v1Compatibility} value of the
         * first {@code history} entry, which corresponds to the latest layer.
         *
         * @return container configuration if the first history string holds it; {@code null} otherwise
         */
        public Maybe <ContainerConfigurationTemplate> GetContainerConfiguration()
        {
            try
            {
                if (History.Count == 0)
                {
                    return(Maybe.Empty <ContainerConfigurationTemplate>());
                }
                string v1Compatibility = History[0].V1Compatibility;
                if (v1Compatibility == null)
                {
                    return(Maybe.Empty <ContainerConfigurationTemplate>());
                }

                return(Maybe.Of(
                           JsonTemplateMapper.ReadJson <ContainerConfigurationTemplate>(v1Compatibility)));
            }
            catch (IOException)
            {
                // not a container configuration; ignore and continue
                return(Maybe.Empty <ContainerConfigurationTemplate>());
            }
        }
Ejemplo n.º 26
0
        public void TestFromJson()
        {
            // Loads the JSON string.
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/v21manifest.json").ToURI());

            // Deserializes into a manifest JSON object.
            V21ManifestTemplate manifestJson =
                JsonTemplateMapper.ReadJsonFromFile <V21ManifestTemplate>(jsonFile);

            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                manifestJson.FsLayers[0].GetDigest());

            ContainerConfigurationTemplate containerConfiguration =
                manifestJson.GetContainerConfiguration().OrElse(null);

            Assert.AreEqual(
                new[] { "JAVA_HOME=/opt/openjdk", "PATH=/opt/openjdk/bin" },
                containerConfiguration.GetContainerEnvironment());
            Assert.AreEqual(
                new[] { "/opt/openjdk/bin/java" }, containerConfiguration.GetContainerEntrypoint());
        }
Ejemplo n.º 27
0
        public void TestFromJson()
        {
            // Loads the JSON string.
            SystemPath jsonFile = Paths.Get(TestResources.GetResource("core/json/ocimanifest.json").ToURI());

            // Deserializes into a manifest JSON object.
            OCIManifestTemplate manifestJson =
                JsonTemplateMapper.ReadJsonFromFile <OCIManifestTemplate>(jsonFile);

            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                manifestJson.GetContainerConfiguration().Digest);

            Assert.AreEqual(1000, manifestJson.GetContainerConfiguration().Size);

            Assert.AreEqual(
                DescriptorDigest.FromHash(
                    "4945ba5011739b0b98c4a41afe224e417f47c7c99b2ce76830999c9a0861b236"),
                manifestJson.Layers[0].Digest);

            Assert.AreEqual(1000_000, manifestJson.Layers[0].Size);
        }
Ejemplo n.º 28
0
        public async Task TestWriteMetadata_v22Async()
        {
            SystemPath containerConfigurationJsonFile =
                Paths.Get(
                    TestResources.GetResource("core/json/containerconfig.json").ToURI());
            ContainerConfigurationTemplate containerConfigurationTemplate =
                JsonTemplateMapper.ReadJsonFromFile <ContainerConfigurationTemplate>(
                    containerConfigurationJsonFile);
            SystemPath manifestJsonFile =
                Paths.Get(TestResources.GetResource("core/json/v22manifest.json").ToURI());
            IBuildableManifestTemplate manifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <V22ManifestTemplate>(manifestJsonFile);
            ImageReference imageReference = ImageReference.Parse("image.reference/project/thing:tag");

            await new CacheStorageWriter(cacheStorageFiles)
            .WriteMetadataAsync(imageReference, manifestTemplate, containerConfigurationTemplate).ConfigureAwait(false);

            SystemPath savedManifestPath =
                cacheRoot.Resolve("images/image.reference/project/thing!tag/manifest.json");
            SystemPath savedConfigPath =
                cacheRoot.Resolve("images/image.reference/project/thing!tag/config.json");

            Assert.IsTrue(Files.Exists(savedManifestPath));
            Assert.IsTrue(Files.Exists(savedConfigPath));

            V22ManifestTemplate savedManifest =
                JsonTemplateMapper.ReadJsonFromFile <V22ManifestTemplate>(savedManifestPath);

            Assert.AreEqual(
                "8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad",
                savedManifest.GetContainerConfiguration().Digest.GetHash());

            ContainerConfigurationTemplate savedContainerConfig =
                JsonTemplateMapper.ReadJsonFromFile <ContainerConfigurationTemplate>(savedConfigPath);

            Assert.AreEqual("wasm", savedContainerConfig.Architecture);
        }
Ejemplo n.º 29
0
        public void TestToImage_v21()
        {
            // Loads the JSON string.
            SystemPath jsonFile =
                Paths.Get(TestResources.GetResource("core/json/v21manifest.json").ToURI());

            // Deserializes into a manifest JSON object.
            V21ManifestTemplate manifestTemplate =
                JsonTemplateMapper.ReadJsonFromFile <V21ManifestTemplate>(jsonFile);

            Image image = JsonToImageTranslator.ToImage(manifestTemplate);

            IList <ILayer> layers = image.GetLayers();

            Assert.AreEqual(2, layers.Count);
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:5bd451067f9ab05e97cda8476c82f86d9b69c2dffb60a8ad2fe3723942544ab3"),
                layers[0].GetBlobDescriptor().GetDigest());
            Assert.AreEqual(
                DescriptorDigest.FromDigest(
                    "sha256:8c662931926fa990b41da3c9f42663a537ccd498130030f9149173a0493832ad"),
                layers[1].GetBlobDescriptor().GetDigest());
        }
Ejemplo n.º 30
0
        /**
         * Pulls the base image.
         *
         * @param registryAuthorization authentication credentials to possibly use
         * @param progressEventDispatcher the {@link ProgressEventDispatcher} for emitting {@link
         *     ProgressEvent}s
         * @return the pulled image
         * @throws IOException when an I/O exception occurs during the pulling
         * @throws RegistryException if communicating with the registry caused a known error
         * @throws LayerCountMismatchException if the manifest and configuration contain conflicting layer
         *     information
         * @throws LayerPropertyNotFoundException if adding image layers fails
         * @throws BadContainerConfigurationFormatException if the container configuration is in a bad
         *     format
         */
        private async Task <Image> PullBaseImageAsync(
            Authorization registryAuthorization,
            ProgressEventDispatcher progressEventDispatcher)
        {
            RegistryClient registryClient =
                buildConfiguration
                .NewBaseImageRegistryClientFactory()
                .SetAuthorization(registryAuthorization)
                .NewRegistryClient();

            IManifestTemplate manifestTemplate =
                await registryClient.PullManifestAsync(buildConfiguration.GetBaseImageConfiguration().GetImageTag()).ConfigureAwait(false);

            // TODO: Make schema version be enum.
            switch (manifestTemplate.SchemaVersion)
            {
            case 1:
                V21ManifestTemplate v21ManifestTemplate = (V21ManifestTemplate)manifestTemplate;
                await buildConfiguration
                .GetBaseImageLayersCache()
                .WriteMetadataAsync(
                    buildConfiguration.GetBaseImageConfiguration().GetImage(), v21ManifestTemplate).ConfigureAwait(false);

                return(JsonToImageTranslator.ToImage(v21ManifestTemplate));

            case 2:
                IBuildableManifestTemplate buildableManifestTemplate =
                    (IBuildableManifestTemplate)manifestTemplate;
                if (buildableManifestTemplate.GetContainerConfiguration() == null ||
                    buildableManifestTemplate.GetContainerConfiguration().Digest == null)
                {
                    throw new UnknownManifestFormatException(
                              "Invalid container configuration in Docker V2.2/OCI manifest: \n"
                              + JsonTemplateMapper.ToUtf8String(buildableManifestTemplate));
                }

                DescriptorDigest containerConfigurationDigest =
                    buildableManifestTemplate.GetContainerConfiguration().Digest;

                using (ThrottledProgressEventDispatcherWrapper progressEventDispatcherWrapper =
                           new ThrottledProgressEventDispatcherWrapper(
                               progressEventDispatcher.NewChildProducer(),
                               "pull container configuration " + containerConfigurationDigest))
                {
                    string containerConfigurationString =
                        await Blobs.WriteToStringAsync(
                            registryClient.PullBlob(
                                containerConfigurationDigest,
                                progressEventDispatcherWrapper.SetProgressTarget,
                                progressEventDispatcherWrapper.DispatchProgress)).ConfigureAwait(false);

                    ContainerConfigurationTemplate containerConfigurationTemplate =
                        JsonTemplateMapper.ReadJson <ContainerConfigurationTemplate>(
                            containerConfigurationString);
                    await buildConfiguration
                    .GetBaseImageLayersCache()
                    .WriteMetadataAsync(
                        buildConfiguration.GetBaseImageConfiguration().GetImage(),
                        buildableManifestTemplate,
                        containerConfigurationTemplate).ConfigureAwait(false);

                    return(JsonToImageTranslator.ToImage(
                               buildableManifestTemplate, containerConfigurationTemplate));
                }
            }

            throw new InvalidOperationException(Resources.PullBaseImageStepUnknownManifestErrorMessage);
        }