forked from snyk-playground/cx-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-function-splunk.cs
111 lines (96 loc) · 4.48 KB
/
azure-function-splunk.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json.Linq;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
//log.LogInformation("data: " + requestBody);
string responseMessage = "No valid payload received!";
if (data.project != null)
{
int x = 1;
string count = data.newIssues.Count.ToString();
string projectName = data.project.name;
string[] projectNameParts = projectName.Split(':');
string containerImage = projectName;
if (projectNameParts.Length > 0)
{
containerImage = projectNameParts[1] + ":" + data.project.imageTag;
}
log.LogInformation(projectName + ", data.newIssues.Count: " + count);
responseMessage = "No new issues found. Nothing to process!";
name = name ?? data?.name;
string browseUrl = data.project.browseUrl;
StringBuilder sbDimensions = new StringBuilder();
sbDimensions.Append(" , \"dimensions\": {");
sbDimensions.Append(" \"projectName\": \"" + projectName + "\",");
sbDimensions.Append(" \"browseUrl\": \"" + browseUrl + "\",");
sbDimensions.Append(" \"imageId\": \"" + data.project.imageId + "\",");
sbDimensions.Append(" \"imageTag\": \"" + data.project.imageTag + "\",");
sbDimensions.Append(" \"imagePlatform\": \"" + data.project.imagePlatform + "\",");
sbDimensions.Append(" \"imageBaseImage\": \"" + data.project.imageBaseImage + "\",");
sbDimensions.Append(" \"containerImage\": \"" + data.project.imageBaseImage + "\"");
sbDimensions.Append(" }");
// send data to Splunk
StringBuilder sb = new StringBuilder();
sb.Append("{");
sb.Append(" \"gauge\": [");
sb.Append(" {");
sb.Append(" \"metric\": \"Snyk.issueCountsBySeverityLow\",");
sb.Append(" \"value\": " + data.project.issueCountsBySeverity.low + "");
sb.Append(sbDimensions.ToString());
sb.Append(" },");
sb.Append(" {");
sb.Append(" \"metric\": \"Snyk.issueCountsBySeverityHigh\",");
sb.Append(" \"value\": " + data.project.issueCountsBySeverity.high + "");
sb.Append(sbDimensions.ToString());
sb.Append(" },");
sb.Append(" {");
sb.Append(" \"metric\": \"Snyk.issueCountsBySeverityMedium\",");
sb.Append(" \"value\": " + data.project.issueCountsBySeverity.medium + "");
sb.Append(sbDimensions.ToString());
sb.Append(" },");
sb.Append(" {");
sb.Append(" \"metric\": \"Snyk.issueCountsBySeverityCritical\",");
sb.Append(" \"value\": " + data.project.issueCountsBySeverity.critical + "");
sb.Append(sbDimensions.ToString());
sb.Append(" }");
sb.Append(" ]");
sb.Append("}");
string payload = sb.ToString();
//log.LogInformation("content: " + payload);
var content = new StringContent(payload);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var SPLUNK_EVENTS_URL = Environment.GetEnvironmentVariable("SPLUNK_EVENTS_URL");
var SPLUNK_ACCESS_TOKEN = Environment.GetEnvironmentVariable("SPLUNK_ACCESS_TOKEN");
var url = SPLUNK_EVENTS_URL;
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-SF-TOKEN", SPLUNK_ACCESS_TOKEN);
var response = await client.PostAsync(url, content);
string result = response.Content.ReadAsStringAsync().Result;
log.LogInformation("response.StatusCode: " + response.StatusCode);
if (response.StatusCode == HttpStatusCode.OK)
{
x++;
}
//log.LogInformation("result: " + result);
// write output as summary
string output = "Successfully processed " + x + " issues.";
log.LogInformation(output);
responseMessage = output;
}
else
{
log.LogInformation("No project found!");
}
return new OkObjectResult(responseMessage);
}