Пример #1
0
        private async Task <ClairVulnerabiltyResponse> GetVulnerabilitesFromClair(JsonSerializer serializer, string vulnerabilitesApiUrl, IVulnerabilityDockerBlob blob, ClairPostResponse response)
        {
            try
            {
                var             vRequest  = this.CreateGetWebRequest(string.Format(vulnerabilitesApiUrl, response.Layer.Name));
                HttpWebResponse vResponse = (HttpWebResponse)await vRequest.GetResponseAsync();

                if (vResponse.StatusCode == HttpStatusCode.OK || vResponse.StatusCode == HttpStatusCode.Created)
                {
                    using (var responseStream = vResponse.GetResponseStream())
                        using (var textReader = new StreamReader(responseStream, InedoLib.UTF8Encoding))
                            using (var jsonTextReader = new JsonTextReader(textReader))
                            {
                                var vulnerabilities = serializer.Deserialize <ClairVulnerabiltyResponse>(jsonTextReader);
                                this.LogDebug($"Layer {blob.Digest} pulled vulnerabilities from clair", JsonConvert.SerializeObject(vulnerabilities));
                                return(vulnerabilities);
                            }
                }
                else if ((int)vResponse.StatusCode == 422)
                {
                    this.LogInformation($"Clair could not process layer {blob.Digest}, request was valid");
                    return(null);
                }
                else
                {
                    this.LogWarning($"Clair returned error {vResponse.StatusCode} for layer {blob.Digest}");
                    return(null);
                }
            }
            catch (WebException webEx)
            {
                var webResponse = (HttpWebResponse)webEx.Response;
                if ((int)webResponse.StatusCode == 422)
                {
                    this.LogInformation($"Clair could not process layer {blob.Digest}, request was valid.", webEx.ToString());
                    return(null);
                }
                else
                {
                    this.LogWarning($"Clair returned error {webResponse.StatusCode} for layer {blob.Digest}.", webEx.ToString());
                    return(null);
                }
            }
            catch (Exception ex)
            {
                this.LogError(ex.Message, ex.ToString());
                return(null);
            }
        }
Пример #2
0
        private async Task <ClairPostResponse> PushLayerToClair(JsonSerializer serializer, WebRequest request, IVulnerabilityDockerBlob blob)
        {
            var clairRequest = new ClairPostRequest();

            clairRequest.Layer = new ClairPostRequest.ClairLayer
            {
                Format  = "Docker",
                Name    = blob.Digest,
                Path    = blob.DownloadUrl,
                Headers = new ClairPostRequest.ClairHeaders()
            };


            if (AuthenticationHeader != null)
            {
                clairRequest.Layer.Headers.Autorization = AH.Unprotect(AuthenticationHeader);
            }
            var requestStream = await request.GetRequestStreamAsync().ConfigureAwait(false);

            using (var textWriter = new StreamWriter(requestStream, InedoLib.UTF8Encoding, 16, true))
            {
                serializer.Serialize(textWriter, clairRequest);
                //request.ContentLength = stream.Length;
                //stream.Position = 0;

                textWriter.Flush();
                requestStream.Close();
            }
            try
            {
                var response = (HttpWebResponse)(await request.GetResponseAsync().ConfigureAwait(false));

                if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.Created)
                {
                    using (var responseStream = response.GetResponseStream())
                        using (var textReader = new StreamReader(responseStream, InedoLib.UTF8Encoding))
                            using (var jsonTextReader = new JsonTextReader(textReader))
                            {
                                var scan = serializer.Deserialize <ClairPostResponse>(jsonTextReader);
                                this.LogDebug($"Layer {blob.Digest} pushed to clair", JsonConvert.SerializeObject(scan));
                                return(scan);
                            }
                }
                else if ((int)response.StatusCode == 422)
                {
                    this.LogInformation($"Clair could not process layer {blob.Digest}, request was valid");
                    return(null);
                }
                else
                {
                    this.LogWarning($"Clair returned error {response.StatusCode} for layer {blob.Digest}");
                    return(null);
                }
            }
            catch (WebException webEx)
            {
                var response = (HttpWebResponse)webEx.Response;
                if ((int)response.StatusCode == 422)
                {
                    this.LogInformation($"Clair could not process layer {blob.Digest}, request was valid.", webEx.ToString());
                    return(null);
                }
                else
                {
                    this.LogWarning($"Clair returned error {response.StatusCode} for layer {blob.Digest}.", webEx.ToString());
                    return(null);
                }
            }
            catch (Exception ex)
            {
                this.LogError(ex.Message, ex.ToString());
                return(null);
            }
        }