-
Notifications
You must be signed in to change notification settings - Fork 7
/
cveClientlib.js
169 lines (168 loc) · 4.41 KB
/
cveClientlib.js
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
163
164
165
166
167
168
169
class cveClient {
constructor(org,user,key,url) {
this.org = org;
this.user = user;
this.key = key;
this.url = url;
this.user_path = "/org/" + this.org + "/user/" + this.user;
this._version = "1.0.15";
}
publishadp(cve,adp) {
let path = "/cve/" + cve + "/adp";
let opts = {method: "PUT"};
return this.putjson(path,opts,null,adp);
}
publishcve(cve,cnajson,update,rejected) {
/* Create or Update a CVE */
let opts = null;
if(update)
opts = {method: "PUT"};
let path = "/cve/" + cve + "/cna";
if(rejected)
path = "/cve/" + cve + "/reject";
return this.putjson(path,opts,null,{cnaContainer:cnajson});
}
reservecve(amount,cve_year,batch_type) {
let path = "/cve-id/";
let opts = {method: 'POST'};
let valid_batch_types = {"nonsequential":1,"sequential":1};
let qvars = {amount: amount ? amount: 1,
short_name:this.org,
cve_year: cve_year ? cve_year : new Date().getFullYear()};
if(amount > 1) {
if(batch_type && batch_type in valid_batch_types) {
qvars['batch_type'] = batch_type;
} else {
qvars['batch_type'] = "sequential";
}
};
return this.putjson(path,opts,qvars);
}
getcvedetail(cve) {
return this.getjson("/cve/" + cve);
}
getcve(cve) {
return this.getjson("/cve-id/" + cve);
}
getcvefilter(year,state,reserved_before,reserved_after,
modified_before,modified_after) {
let qvars = {};
let path = "/cve-id/";
let arg_map = ["cve_id_year","state","time_reserved.lt",
"time_reserved.gt","time_modified.lt",
"time_modified.gt"];
let largs = arguments;
arg_map.forEach(function(f,i) {
if(largs[i])
qvars[f] = largs[i];
});
return this.getjson(path,null,qvars);
}
getquota() {
return this.getjson("/org/" + this.org + "/id_quota");
}
getcveids(cve,opts,qvars) {
let path = "/cve-id/";
if(cve)
path = path + cve;
return this.getjson(path,opts,qvars);
}
getuser(username) {
let path = this.user_path;
if(username)
path = "/org/"+ this.org + "/user/" + username;
return this.rfetch(path);
}
resetuser(username) {
let path = this.user_path+"/reset_secret";
if(username)
path = "/org/" + this.org + "/user/" + username + "/reset_secret";
return this.putjson(path,{method:'PUT'});
}
createuser(userdata) {
let path = "/org/" + this.org + "/user";
return this.putjson(path,null,null,userdata);
}
updateuser(username,userdata) {
if(!username)
username = this.user;
let path = "/org/" + this.org + "/user/" + username;
return this.putjson(path,{method:'PUT'},userdata,null);
}
listusers(path,opts,qvars) {
/* Overwrite the path variable */
path = "/org/" + this.org + "/users";
return this.getjson(path,opts,qvars);
}
getorg(worg) {
if(!worg)
worg = this.org;
let path = "/org/" + worg;
return this.getjson(path);
}
gethealth() {
return this.rfetch("/health-check");
}
getjson(path,opts,qvars) {
return this.rfetch(path,opts,qvars).then(function(j) {
if(j && j.ok)
return j.json();
});
}
putjson(path,opts,qvars,pvars) {
if(!opts)
opts = {method: 'POST'};
if(!('headers' in opts))
opts.headers = {};
opts.headers["Content-Type"] = 'application/json';
if(pvars)
opts.body = JSON.stringify(pvars);
return this.rfetch(path,opts,qvars).then(function(j) {
return j.json();
});
}
rfetch(path,opts,qvars) {
let url;
try {
url = new URL(this.url);
delete this.error;
} catch(err) {
this.error = err;
return;
}
url.pathname = url.pathname + path;
if(!opts) {
opts = {method:'GET'};
}
if(qvars) {
var qstr = new URLSearchParams();
Object.keys(qvars).forEach(function(x) {
/* Remove empty values in query_string
strange issue #11 when changing user's information
see https://github.com/CERTCC/cveClient/issues/11
*/
if(qvars[x] != "")
qstr.append(x,qvars[x]);
});
url.search = qstr.toString();
}
if(!('headers' in opts))
opts.headers = {};
opts.headers = Object.assign({},opts.headers,
{'CVE-API-KEY': this.key,
'CVE-API-ORG': this.org,
'CVE-API-USER': this.user });
let client = this;
return fetch(url.toString(),opts).then(function(r) {
client.response = r;
if(r.ok) {
delete client.error;
return r;
}
client.error = "Error see client.response for full error";
}).catch(function(err) {
delete client.response;
client.error = err;
});
}
}