예제 #1
0
        public async Task CopyBlobAsync(string name, string reference, Registry baseRegistry, string baseName)
        {
            // Create bucket to POST blob
            var uri     = new Uri(_registryUri, $"/v2/{name}/blobs/uploads/");
            var request = new HttpRequestMessage(HttpMethod.Post, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                throw new RegistryException();
            }

            // POST blob into new registry
            var location = response.Headers.GetValues("Location").FirstOrDefault();

            uri             = new Uri(_registryUri, $"{location}&digest={reference}");
            request         = new HttpRequestMessage(HttpMethod.Put, uri);
            request.Content = new StreamContent(await baseRegistry.GetBlobStream(baseName, reference));
            request.AddBasicAuthorizationHeader(_username, _password);
            response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new RegistryException();
            }
        }
예제 #2
0
        public async Task <long> PutConfigBlobAsync(string name, string config, string configDigest)
        {
            var uri     = new Uri(_registryUri, $"/v2/{name}/blobs/uploads/");
            var request = new HttpRequestMessage(HttpMethod.Post, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.Accepted)
            {
                throw new RegistryException();
            }
            var location = response.Headers.GetValues("Location").FirstOrDefault();

            uri = new Uri(_registryUri, $"{location}&digest={configDigest}");
            Console.WriteLine("Pusing config to " + uri.ToString());
            request         = new HttpRequestMessage(HttpMethod.Post, uri);
            request.Content = new StringContent(config);
            var returnVal = request.Content.Headers.ContentLength.Value;

            request.AddBasicAuthorizationHeader(_username, _password);
            response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new RegistryException();
            }

            return(returnVal);
        }
예제 #3
0
        public async Task <ApiVersion> GetApiVersionAsync()
        {
            var uri     = new Uri(_registryUri, "/v2/");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new RegistryException();
            }
            return(ApiVersion.v2);
        }
예제 #4
0
        private async Task <Stream> GetBlobStream(string name, string reference)
        {
            var uri     = new Uri(_registryUri, $"/v2/{name}/blobs/{reference}");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);

            if (response.IsSuccessStatusCode)
            {
                return(await response.Content.ReadAsStreamAsync());
            }
            else
            {
                throw new RegistryException();
            }
        }
예제 #5
0
        public async Task PutManifestAsync(string name, string reference, Manifest manifest)
        {
            var uri         = new Uri(_registryUri, $"/v2/{name}/manifests/{reference}");
            var request     = new HttpRequestMessage(HttpMethod.Put, uri);
            var jsonContent = JsonConvert.SerializeObject(manifest);

            request.Content = new StringContent(jsonContent);
            request.Content.Headers.ContentType.MediaType = manifest.MediaType;
            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                Console.WriteLine("ERR PutManifest: " + response.ReasonPhrase);
                var responseJObject = JObject.Parse(await response.Content.ReadAsStringAsync());
                throw CreateException(responseJObject);
            }
            return;
        }
예제 #6
0
        public async Task <string> GetDigestFromReference(string name, string reference, ManifestType manifestType)
        {
            var uri = new Uri(_registryUri, $"/v2/{name}/manifests/{reference}");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, uri);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(manifestType.MediaType));
            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new RegistryException();
            }
            var digest = response.Headers.GetValues("Docker-Content-Digest").FirstOrDefault();

            return(digest);
        }
예제 #7
0
        public async Task <Manifest> GetManifestAsync(string name, string reference, ManifestType manifestType)
        {
            var uri     = new Uri(_registryUri, $"/v2/{name}/manifests/{reference}");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(manifestType.MediaType));
            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            var responseJObject = JObject.Parse(await response.Content.ReadAsStringAsync());

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw CreateException(responseJObject);
            }
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw CreateException(responseJObject);
            }
            return(responseJObject.ToObject <Manifest>());
        }
예제 #8
0
        public async Task <bool> CheckIfLayerExistsAsync(string name, string reference)
        {
            var uri = new Uri(_registryUri, $"/v2/{name}/blobs/{reference}");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Head, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                return(true);
            }
            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                return(false);
            }
            else
            {
                throw new RegistryException();
            }
        }
예제 #9
0
        public async Task <ConfigurationManifest> GetConfigBlobAsync(string name, string reference)
        {
            var uri     = new Uri(_registryUri, $"/v2/{name}/blobs/{reference}");
            var request = new HttpRequestMessage(HttpMethod.Get, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            var responseString = await response.Content.ReadAsStringAsync();

            var responseJObject = JObject.Parse(responseString);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw CreateException(responseJObject);
            }
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw CreateException(responseJObject);
            }
            return(responseJObject.ToObject <ConfigurationManifest>());
        }
예제 #10
0
        public async Task <(long, string)> PostConfigBlobAsync(string name, ConfigurationManifest config)
        {
            // This operation makes two calls:

            // 1. POST to create a bucket to place the config
            var uri     = new Uri(_registryUri, $"/v2/{name}/blobs/uploads/");
            var request = new HttpRequestMessage(HttpMethod.Post, uri);

            request.AddBasicAuthorizationHeader(_username, _password);
            var response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new RegistryException();
            }

            // 2. POST again with actual contents of the config
            var location         = response.Headers.GetValues("Location").FirstOrDefault();
            var serializedConfig = JsonConvert.SerializeObject(config, Formatting.None);
            var configDigest     = SHAHelpers.ComputeSHA256(serializedConfig);

            uri = new Uri(_registryUri, $"{location}&digest={configDigest}");
            Console.WriteLine("Pusing config to " + uri.ToString());
            request         = new HttpRequestMessage(HttpMethod.Put, uri);
            request.Content = new StringContent(serializedConfig);
            var configSize = request.Content.Headers.ContentLength.Value;

            request.AddBasicAuthorizationHeader(_username, _password);
            response = await _httpClient.SendAsync(request);

            if (!response.IsSuccessStatusCode)
            {
                throw new RegistryException();
            }

            return(configSize, configDigest);
        }
 public static void AddBasicAuthorizationHeader(this HttpRequestMessage message, NetworkCredential networkCredential)
 {
     message.AddBasicAuthorizationHeader(networkCredential.UserName, networkCredential.Password);
 }