Пример #1
0
        public static PolicyCollection GetAllPolicies(CxRestContext ctx,
                                                      CancellationToken token)
        {
            return(WebOperation.ExecuteGet <PolicyCollection>(
                       ctx.Json.CreateMnoClient
                       , (response) =>
            {
                JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                               (response.Content.ReadAsStreamAsync().Result)));

                return ParsePolicies(ctx, token, jt);
            }
                       , CxRestContext.MakeUrl(ctx.MnoUrl, POLICY_LIST_URL_SUFFIX)
                       , ctx
                       , token
                       , exceptionErrorLogic: (ex) =>
            {
                if (ex is System.AggregateException)
                {
                    foreach (var x in (ex as System.AggregateException).InnerExceptions)
                    {
                        if (x is System.Net.Http.HttpRequestException)
                        {
                            return false;
                        }
                    }
                }

                return true;
            }));
        }
Пример #2
0
        public static PolicyCollection GetAllPolicies(CxRestContext ctx,
                                                      CancellationToken token)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     POLICY_LIST_URL_SUFFIX), token).Result)
                    {
                        if (!policyPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ("Unable to retrieve policies.");
                        }

                        JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                       (policyPayload.Content.ReadAsStreamAsync().Result)));

                        return(ParsePolicies(ctx, token, jt));
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static ViolatedPolicyCollection GetViolations(CxRestContext ctx,
                                                             CancellationToken token, int projectId, PolicyCollection policies)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var violationsPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                         String.Format(URL_SUFFIX, projectId)), token).Result)
                    {
                        if (!violationsPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve rule violations for project {projectId}.");
                        }

                        using (var sr = new StreamReader
                                            (violationsPayload.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseViolatedRules(policies, projectId, jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static GenStatus GetReportGenerationStatus(CxRestContext ctx,
                                                          CancellationToken token, String reportId)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                {
                    using (var scanReportStatus = client.GetAsync(
                               CxRestContext.MakeUrl(ctx.Url,
                                                     String.Format(URL_SUFFIX, reportId)), token).Result)
                    {
                        if (!scanReportStatus.IsSuccessStatusCode)
                        {
                            return(GenStatus.None);
                        }

                        using (var sr = new StreamReader
                                            (scanReportStatus.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ReadStatus(jt));
                            }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #5
0
        public static ScanSummary GetReport(CxRestContext ctx, CancellationToken token,
                                            String scanId)
        {
            try
            {
                String url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "scanId", Convert.ToString(scanId) }
                });


                using (var client = ctx.Json.CreateSastClient())
                    using (var scanSummary = client.GetAsync(url, token).Result)
                    {
                        if (!scanSummary.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(scanSummary.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (scanSummary.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseScanSummary(jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #6
0
        public static IEnumerable <int> GetPolicyIdsForProject(CxRestContext ctx,
                                                               CancellationToken token, int projectId)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     String.Format(PROJECT_POLICY_URL_SUFFIX, projectId)), token).Result)
                    {
                        if (!policyPayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve policies for project {projectId}.");
                        }

                        JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                       (policyPayload.Content.ReadAsStreamAsync().Result)));

                        LinkedList <int> policyIds = new LinkedList <int>();

                        using (JTokenReader reader = new JTokenReader(jt))
                            while (JsonUtils.MoveToNextProperty(reader, "id"))
                            {
                                policyIds.AddLast(Convert.ToInt32(((JProperty)reader.CurrentToken).Value));
                            }

                        return(policyIds);
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #7
0
        public static IEnumerable <Project> GetProjects(CxRestContext ctx, CancellationToken token)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                    using (var projects = client.GetAsync(
                               CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX), token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        if (!projects.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(projects.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (projects.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);

                                return(new ProjectReader(jt, ctx, token));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
        public static String GetGeneratedReportId(CxRestContext ctx, CancellationToken token,
                                                  String scanId, ReportTypes type)
        {
            var dict = new Dictionary <String, String>()
            {
                { "reportType", type.ToString() },
                { "scanId", scanId }
            };

            return(WebOperation.ExecutePost <String>(
                       ctx.Json.CreateSastClient
                       , (response) =>
            {
                using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
                    using (var jtr = new JsonTextReader(sr))
                    {
                        JToken jt = JToken.Load(jtr);
                        return ReadReportId(jt);
                    }
            }
                       , CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX)
                       , () => new FormUrlEncodedContent(dict)
                       , ctx
                       , token));
        }
Пример #9
0
        /// <summary>
        /// The main logic for invoking a transformation.  It does not return until a sweep
        /// for new scans is performed across all projects.
        /// </summary>
        /// <param name="concurrentThreads">The number of concurrent scan transformation threads.</param>
        /// <param name="previousStatePath">A folder path where files will be created to store any state
        /// data required to resume operations across program runs.</param>
        /// <param name="ctx"></param>
        /// <param name="outFactory">The factory implementation for making IOutput instances
        /// used for outputting various record types.</param>
        /// <param name="records">The names of the supported record types that will be used by
        /// the IOutputFactory to create the correct output implementation instance.</param>
        /// <param name="token">A cancellation token that can be used to stop processing of data if
        /// the task needs to be interrupted.</param>
        public static void DoTransform(int concurrentThreads, String previousStatePath, String instanceId,
                                       CxRestContext ctx, IOutputFactory outFactory, RecordNames records, CancellationToken token)
        {
            try
            {
                Transformer xform = new Transformer(ctx, token, previousStatePath)
                {
                    ThreadOpts = new ParallelOptions()
                    {
                        CancellationToken      = token,
                        MaxDegreeOfParallelism = concurrentThreads
                    },
                    ProjectInfoOut           = outFactory.newInstance(records.ProjectInfo),
                    SastScanSummaryOut       = outFactory.newInstance(records.SASTScanSummary),
                    SastScanDetailOut        = outFactory.newInstance(records.SASTScanDetail),
                    PolicyViolationDetailOut = outFactory.newInstance(records.PolicyViolations),
                    ScaScanSummaryOut        = outFactory.newInstance(records.SCAScanSummary),
                    ScaScanDetailOut         = outFactory.newInstance(records.SCAScanDetail),
                    InstanceId = instanceId
                };

                xform.ExecuteSweep();
            }
            catch (Exception ex)
            {
                _log.Error("Unhandled exception caught.", ex);
            }
        }
Пример #10
0
        public static Stream GetVulnerabilities(CxRestContext ctx,
                                                CancellationToken token, String reportId)
        {
            return(WebOperation.ExecuteGet <Stream>(
                       ctx.Xml.CreateSastClient
                       , (response) =>
            {
                var report = response.Content.ReadAsStreamAsync().Result;
                var mem = new SharedMemoryStream(response.Content.Headers.ContentLength.Value);

                int readAmount = 0;
                byte[] buffer = new byte[BUFFER_SIZE];

                do
                {
                    readAmount = report.Read(buffer, 0, BUFFER_SIZE);

                    mem.Write(buffer, 0, readAmount);

                    if (readAmount < BUFFER_SIZE)
                    {
                        mem.Seek(0, SeekOrigin.Begin);
                    }
                } while (readAmount == BUFFER_SIZE);

                return mem;
            }
                       , CxRestContext.MakeUrl(ctx.Url, String.Format(URL_SUFFIX, reportId))
                       , ctx
                       , token));
        }
Пример #11
0
 internal ProjectReader(JToken json, CxRestContext ctx, CancellationToken token)
 {
     _json   = json;
     _ctx    = ctx;
     _reader = new JTokenReader(_json);
     _token  = token;
 }
Пример #12
0
        public static IEnumerable <RuleDescriptor> GetRulesForPolicy(CxRestContext ctx,
                                                                     CancellationToken token, int policyId)
        {
            try
            {
                using (var client = ctx.Json.CreateMnoClient())
                    using (var rulePayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                   String.Format(URL_SUFFIX, policyId)), token).Result)
                    {
                        if (!rulePayload.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException
                                      ($"Unable to retrieve rules for policy {policyId}.");
                        }

                        using (var sr = new StreamReader
                                            (rulePayload.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(ParseRules(ctx, token, jt));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #13
0
        private Transformer(CxRestContext ctx, CancellationToken token,
                            String previousStatePath, IProjectFilter filter)
        {
            RestContext = ctx;
            CancelToken = token;
            Filter      = filter;
            _state      = new CrawlState(previousStatePath);

            ResolveScans().Wait();
        }
Пример #14
0
        public static IEnumerable <Library> GetLibraries(CxRestContext ctx, CancellationToken token,
                                                         String scanId)
        {
            int curPage = 1;

            List <Library> returnLibs = new List <Library>();

            try
            {
                Func <int, String> url = (pg) => CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "scanId", Convert.ToString(scanId) },
                    { "page", Convert.ToString(pg) },
                    { "itemsPerPage", PAGE_SIZE }
                });

                while (true)
                {
                    using (var client = ctx.Json.CreateSastClient())
                        using (var libraryResponse = client.GetAsync(url(curPage++), token).Result)
                        {
                            if (token.IsCancellationRequested)
                            {
                                return(null);
                            }

                            if (!libraryResponse.IsSuccessStatusCode)
                            {
                                throw new InvalidOperationException(libraryResponse.ReasonPhrase);
                            }

                            using (var sr = new StreamReader
                                                (libraryResponse.Content.ReadAsStreamAsync().Result))
                                using (var jtr = new JsonTextReader(sr))
                                {
                                    JToken jt          = JToken.Load(jtr);
                                    var    beforeCount = returnLibs.Count;
                                    returnLibs.AddRange(new LibrariesReader(jt));
                                    if (returnLibs.Count == beforeCount)
                                    {
                                        break;
                                    }
                                }
                        }
                }

                return(returnLibs);
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #15
0
 public static bool CalculateViolations(CxRestContext ctx,
                                        CancellationToken token, int projectId)
 {
     return(WebOperation.ExecutePost <bool>(
                ctx.Json.CreateMnoClient
                , (response) => response.StatusCode == HttpStatusCode.Created
                , CxRestContext.MakeUrl(ctx.MnoUrl, String.Format(URL_SUFFIX, projectId))
                , null
                , ctx
                , token));
 }
Пример #16
0
        static void Main(string[] args)
        {
            appLog.Info("Start");

            appLog.InfoFormat("CWD: {0}", Directory.GetCurrentDirectory());


            var builder = new CxRestContext.CxRestContextBuilder();

            builder.WithSASTServiceURL(Config.Connection.URL)
            .WithOpTimeout(Config.Connection.TimeoutSeconds)
            .WithSSLValidate(Config.Connection.ValidateCertificates)
            .WithUsername(Config.Credentials.Username)
            .WithPassword(Config.Credentials.Password).
            WithMNOServiceURL(Config.Connection.MNOUrl);

            using (CancellationTokenSource t = new CancellationTokenSource())
            {
                try
                {
                    var outFactory = MakeFactory();

                    CxRestContext ctx = builder.Build();
                    Transformer.DoTransform(Config.Service.ConcurrentThreads,
                                            Config.Service.StateDataStoragePath, Config.Service.InstanceIdentifier,
                                            ctx,
                                            outFactory,
                                            new RecordNames()
                    {
                        SASTScanSummary  = Config.Service.SASTScanSummaryRecordName,
                        SASTScanDetail   = Config.Service.SASTScanDetailRecordName,
                        SCAScanSummary   = Config.Service.SCAScanSummaryRecordName,
                        SCAScanDetail    = Config.Service.SCAScanDetailRecordName,
                        ProjectInfo      = Config.Service.ProjectInfoRecordName,
                        PolicyViolations = Config.Service.PolicyViolationsRecordName
                    },
                                            t.Token);


                    AuditTrailCrawler.CrawlAuditTrails(outFactory, t.Token);
                }
                catch (ProcessFatalException pfe)
                {
                    appLog.Error("Fatal exception caught, program ending.", pfe);
                }
                catch (Exception ex)
                {
                    appLog.Error("Unhandled exception caught, program ending.", ex);
                }
            }

            appLog.Info("End");
        }
Пример #17
0
        private static IEnumerable <RuleDescriptor> ParseRules(CxRestContext ctx,
                                                               CancellationToken token, JToken rulePayload)
        {
            using (var reader = new JTokenReader(rulePayload))
            {
                LinkedList <RuleDescriptor> rules = new LinkedList <RuleDescriptor>();

                while (JsonUtils.MoveToNextProperty(reader, "ruleId"))
                {
                    RuleDescriptor rule = new RuleDescriptor()
                    {
                        RuleId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value)
                    };


                    if (!JsonUtils.MoveToNextProperty(reader, "name"))
                    {
                        continue;
                    }
                    rule.Name = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "description"))
                    {
                        continue;
                    }
                    rule.Description = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "scanType"))
                    {
                        continue;
                    }
                    rule.ScanProduct = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "ruleType"))
                    {
                        continue;
                    }
                    rule.RuleType = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "createdOn"))
                    {
                        continue;
                    }
                    rule.CreatedOn = JsonUtils.UtcEpochTimeToDateTime
                                         (Convert.ToInt64(((JProperty)reader.CurrentToken).Value) / 1000);

                    rules.AddLast(rule);
                }

                return(rules);
            }
        }
Пример #18
0
        public static String GetProjectPoliciesSingleField(CxRestContext ctx,
                                                           CancellationToken token, int projectId)
        {
            return(WebOperation.ExecuteGet <String>(
                       ctx.Json.CreateMnoClient
                       , (response) =>
            {
                JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                               (response.Content.ReadAsStreamAsync().Result)));

                return GetFlatPolicyNames(jt);
            }
                       , CxRestContext.MakeUrl(ctx.MnoUrl, String.Format(PROJECT_POLICY_URL_SUFFIX, projectId))
                       , ctx
                       , token));
        }
Пример #19
0
 public static bool CalculateViolations(CxRestContext ctx,
                                        CancellationToken token, int projectId)
 {
     try
     {
         using (var client = ctx.Json.CreateMnoClient())
             using (var calculationResponse = client.PostAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                     String.Format(URL_SUFFIX, projectId)), null, token).Result)
                 return(calculationResponse.StatusCode == HttpStatusCode.Created);
     }
     catch (HttpRequestException hex)
     {
         _log.Error("Communication error.", hex);
         throw hex;
     }
 }
Пример #20
0
 public static T ExecuteGet <T>(Func <CxRestClient.IO.CxRestClient> clientFactory, Func <HttpResponseMessage, T> onSuccess,
                                String url, CxRestContext ctx, CancellationToken token, Func <HttpResponseMessage, Boolean> responseErrorLogic = null,
                                Func <Exception, Boolean> exceptionErrorLogic = null)
 {
     return(ExecuteOperation <T>(
                clientFactory
                , onSuccess
                , (client) =>
     {
         _log.Trace($"Executing GET operation at {url}");
         return client.GetAsync(url, token).Result;
     }
                , ctx
                , token
                , responseErrorLogic
                , exceptionErrorLogic));
 }
Пример #21
0
 public static IEnumerable <Preset> GetPresets(CxRestContext ctx, CancellationToken token)
 {
     return(WebOperation.ExecuteGet <PresetReader>(
                ctx.Json.CreateSastClient
                , (response) =>
     {
         using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
             using (var jtr = new JsonTextReader(sr))
             {
                 JToken jt = JToken.Load(jtr);
                 return new PresetReader(jt);
             }
     }
                , CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX)
                , ctx
                , token));
 }
Пример #22
0
        private static PolicyCollection ParsePolicies(CxRestContext ctx,
                                                      CancellationToken token, JToken policyPayload)
        {
            PolicyCollection result = new PolicyCollection();

            using (JTokenReader reader = new JTokenReader(policyPayload))
                while (JsonUtils.MoveToNextProperty(reader, "id"))
                {
                    PolicyDescriptor policy = new PolicyDescriptor()
                    {
                        PolicyId = Convert.ToInt32(((JProperty)reader.CurrentToken).Value)
                    };

                    if (!JsonUtils.MoveToNextProperty(reader, "name"))
                    {
                        continue;
                    }
                    policy.Name = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "description"))
                    {
                        continue;
                    }
                    policy.Description = ((JProperty)reader.CurrentToken).Value.ToString();

                    if (!JsonUtils.MoveToNextProperty(reader, "isActive"))
                    {
                        continue;
                    }
                    policy.isActive = Convert.ToBoolean(((JProperty)reader.CurrentToken).Value);

                    if (!JsonUtils.MoveToNextProperty(reader, "createdOn"))
                    {
                        continue;
                    }
                    policy.CreatedOn = JsonUtils.UtcEpochTimeToDateTime
                                           (Convert.ToInt64(((JProperty)reader.CurrentToken).Value) / 1000);

                    var rules = CxMnoPolicyRules.GetRulesForPolicy(ctx, token, policy.PolicyId);
                    policy.AddRule(rules);

                    result.AddPolicy(policy);
                }

            return(result);
        }
Пример #23
0
        public static IEnumerable <Vulnerability> GetVulnerabilities
            (CxRestContext ctx, CancellationToken token, String scanId)
        {
            int curPage = 1;
            List <Vulnerability> returnVulns = new List <Vulnerability>();

            Func <int, String> url = (pg) => CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
            {
                { "scanId", Convert.ToString(scanId) },
                { "page", Convert.ToString(pg) },
                { "itemsPerPage", PAGE_SIZE }
            });


            while (true)
            {
                if (token.IsCancellationRequested)
                {
                    return(null);
                }

                var beforeCount = returnVulns.Count;

                returnVulns.AddRange(WebOperation.ExecuteGet <IEnumerable <Vulnerability> >(
                                         ctx.Json.CreateSastClient
                                         , (response) =>
                {
                    using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
                        using (var jtr = new JsonTextReader(sr))
                        {
                            JToken jt = JToken.Load(jtr);
                            return(new VulnerabilityReader(jt));
                        }
                }
                                         , url(curPage++)
                                         , ctx
                                         , token));

                if (returnVulns.Count == beforeCount)
                {
                    break;
                }
            }

            return(returnVulns);
        }
Пример #24
0
        public static IEnumerable <Scan> GetScans(CxRestContext ctx, CancellationToken token,
                                                  int projectId)
        {
            try
            {
                String url = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX, new Dictionary <String, String>()
                {
                    { "projectId", Convert.ToString(projectId) },
                    { "itemsPerPage", "5000" }
                });

                using (var client = ctx.Json.CreateSastClient())
                    using (var scans = client.GetAsync(url, token).Result)
                    {
                        if (token.IsCancellationRequested)
                        {
                            return(null);
                        }

                        // If no OSA license, result is 403.  Return an empty set of scans.
                        if (scans.StatusCode == System.Net.HttpStatusCode.Forbidden)
                        {
                            return(new ScansReader());
                        }

                        if (!scans.IsSuccessStatusCode)
                        {
                            throw new InvalidOperationException(scans.ReasonPhrase);
                        }

                        using (var sr = new StreamReader
                                            (scans.Content.ReadAsStreamAsync().Result))
                            using (var jtr = new JsonTextReader(sr))
                            {
                                JToken jt = JToken.Load(jtr);
                                return(new ScansReader(jt, projectId));
                            }
                    }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
 public static GenStatus GetReportGenerationStatus(CxRestContext ctx,
                                                   CancellationToken token, String reportId)
 {
     return(WebOperation.ExecuteGet <GenStatus>(
                ctx.Json.CreateSastClient
                , (response) =>
     {
         using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
             using (var jtr = new JsonTextReader(sr))
             {
                 JToken jt = JToken.Load(jtr);
                 return ReadStatus(jt);
             }
     }
                , CxRestContext.MakeUrl(ctx.Url, String.Format(URL_SUFFIX, reportId))
                , ctx
                , token));
 }
Пример #26
0
 public static ViolatedPolicyCollection GetViolations(CxRestContext ctx,
                                                      CancellationToken token, int projectId, PolicyCollection policies)
 {
     return(WebOperation.ExecuteGet <ViolatedPolicyCollection>(
                ctx.Json.CreateMnoClient
                , (response) =>
     {
         using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
             using (var jtr = new JsonTextReader(sr))
             {
                 JToken jt = JToken.Load(jtr);
                 return ParseViolatedRules(policies, projectId, jt);
             }
     }
                , CxRestContext.MakeUrl(ctx.MnoUrl, String.Format(URL_SUFFIX, projectId))
                , ctx
                , token));
 }
Пример #27
0
 public static IEnumerable <RuleDescriptor> GetRulesForPolicy(CxRestContext ctx,
                                                              CancellationToken token, int policyId)
 {
     return(WebOperation.ExecuteGet <IEnumerable <RuleDescriptor> >(
                ctx.Json.CreateMnoClient
                , (response) =>
     {
         using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
             using (var jtr = new JsonTextReader(sr))
             {
                 JToken jt = JToken.Load(jtr);
                 return ParseRules(ctx, token, jt);
             }
     }
                , CxRestContext.MakeUrl(ctx.MnoUrl, String.Format(URL_SUFFIX, policyId))
                , ctx
                , token));
 }
Пример #28
0
        public static ScanSettings GetScanSettings(CxRestContext ctx, CancellationToken token, int projectId)
        {
            String restUrl = CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX);

            return(WebOperation.ExecuteGet <ScanSettings>(
                       ctx.Json.CreateSastClient
                       , (response) =>
            {
                using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
                    using (var jtr = new JsonTextReader(sr))
                    {
                        JToken jt = JToken.Load(jtr);
                        return new ScanSettings(jt);
                    }
            }
                       , CxRestContext.MakeUrl(restUrl, Convert.ToString(projectId))
                       , ctx
                       , token));
        }
Пример #29
0
        public static String GetGeneratedReportId(CxRestContext ctx, CancellationToken token,
                                                  String scanId, ReportTypes type)
        {
            try
            {
                using (var client = ctx.Json.CreateSastClient())
                {
                    var dict = new Dictionary <String, String>()
                    {
                        { "reportType", type.ToString() },
                        { "scanId", scanId }
                    };

                    using (var payload = new FormUrlEncodedContent(dict))
                    {
                        using (var scanReportTicket = client.PostAsync(
                                   CxRestContext.MakeUrl(ctx.Url, URL_SUFFIX), payload, token).Result)
                        {
                            if (!scanReportTicket.IsSuccessStatusCode)
                            {
                                throw new InvalidOperationException
                                          ($"Scan report generation request for scan {scanId} returned " +
                                          $"{scanReportTicket.StatusCode}");
                            }

                            using (var sr = new StreamReader
                                                (scanReportTicket.Content.ReadAsStreamAsync().Result))
                                using (var jtr = new JsonTextReader(sr))
                                {
                                    JToken jt = JToken.Load(jtr);
                                    return(ReadReportId(jt));
                                }
                        }
                    }
                }
            }
            catch (HttpRequestException hex)
            {
                _log.Error("Communication error.", hex);
                throw hex;
            }
        }
Пример #30
0
        public static String GetProjectPoliciesSingleField(CxRestContext ctx,
                                                           CancellationToken token, int projectId)
        {
            using (var client = ctx.Json.CreateMnoClient())
            {
                using (var policyPayload = client.GetAsync(CxRestContext.MakeUrl(ctx.MnoUrl,
                                                                                 String.Format(PROJECT_POLICY_URL_SUFFIX, projectId)), token).Result)
                {
                    if (!policyPayload.IsSuccessStatusCode)
                    {
                        throw new InvalidOperationException
                                  ($"Unable to retrieve policies for project {projectId}.");
                    }

                    JToken jt = JToken.Load(new JsonTextReader(new StreamReader
                                                                   (policyPayload.Content.ReadAsStreamAsync().Result)));

                    return(GetFlatPolicyNames(jt));
                }
            }
        }