forked from lunar/snyk_exporter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnyk.go
162 lines (145 loc) · 3.94 KB
/
snyk.go
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"github.com/prometheus/common/log"
)
type client struct {
httpClient *http.Client
token string
baseURL string
}
func (c *client) getOrganizations() (orgsResponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/orgs", c.baseURL), nil)
if err != nil {
return orgsResponse{}, err
}
response, err := c.do(req)
if err != nil {
return orgsResponse{}, err
}
var orgs orgsResponse
err = json.NewDecoder(response.Body).Decode(&orgs)
if err != nil {
return orgsResponse{}, err
}
return orgs, nil
}
func (c *client) getProjects(organization string) (projectsResponse, error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/org/%s/projects", c.baseURL, organization), nil)
if err != nil {
return projectsResponse{}, err
}
response, err := c.do(req)
if err != nil {
return projectsResponse{}, err
}
var projects projectsResponse
err = json.NewDecoder(response.Body).Decode(&projects)
if err != nil {
return projectsResponse{}, err
}
return projects, nil
}
func (c *client) getIssues(organizationID, projectID string) (issuesResponse, error) {
postData := issuesPostData{
Filters: issueFilters{
Severities: []string{
"high", "medium", "low",
},
},
}
var reader bytes.Buffer
err := json.NewEncoder(&reader).Encode(&postData)
if err != nil {
return issuesResponse{}, err
}
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/org/%s/project/%s/issues", c.baseURL, organizationID, projectID), &reader)
if err != nil {
return issuesResponse{}, err
}
response, err := c.do(req)
if err != nil {
return issuesResponse{}, err
}
var issues issuesResponse
err = json.NewDecoder(response.Body).Decode(&issues)
if err != nil {
return issuesResponse{}, err
}
return issues, nil
}
func (c *client) do(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.token))
response, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
if response.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Errorf("read body failed: %v", err)
body = []byte("failed to read body")
}
requestDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Debugf("Failed to dump request for logging")
} else {
log.Debugf("Failed request dump: %s", requestDump)
}
return nil, fmt.Errorf("request not OK: %s: body: %s", response.Status, body)
}
return response, nil
}
type orgsResponse struct {
Orgs []org `json:"orgs,omitempty"`
}
type org struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Group *struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
} `json:"group,omitempty"`
}
type projectsResponse struct {
Org projectOrg `json:"org,omitempty"`
Projects []project `json:"projects,omitempty"`
}
type projectOrg struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
}
type project struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
}
type issuesResponse struct {
Issues issues `json:"issues,omitempty"`
}
type issues struct {
Vulnerabilities []vulnerability `json:"vulnerabilities,omitempty"`
Licenses []license `json:"licenses,omitempty"`
}
type vulnerability struct {
ID string `json:"id,omitempty"`
Severity string `json:"severity,omitempty"`
Title string `json:"title,omitempty"`
Ignored bool `json:"isIgnored"`
Upgradeable bool `json:"isUpgradable"`
Patchable bool `json:"isPatchable"`
}
type license struct{}
type issuesPostData struct {
Filters issueFilters `json:"filters,omitempty"`
}
type issueFilters struct {
Severities []string `json:"severities,omitempty"`
Types []string `json:"types,omitempty"`
Ignored bool `json:"ignored,omitempty"`
Patched bool `json:"patched,omitempty"`
}