forked from projectdiscovery/vulnx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
425 lines (394 loc) · 13.9 KB
/
client.go
File metadata and controls
425 lines (394 loc) · 13.9 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
// Package vulnx provides a robust, idiomatic Go client for interacting
// with the ProjectDiscovery vulnx REST API. The client focuses on
// the "/v2/vulnerability" endpoints, exposing high-level helper methods that
// handle authentication, request construction, network-level retries, and JSON
// decoding so that callers can concentrate on business logic.
//
// # Quick Start
//
// The snippet below demonstrates a minimal, production-ready workflow. While
// authentication is optional, using an API key is strongly recommended to
// avoid rate limiting and ensure better performance:
//
// ctx := context.Background()
//
// client, err := vulnx.New(
// vulnx.WithKeyFromEnv(), // or vulnx.WithPDCPKey("<YOUR_KEY>")
// )
// if err != nil {
// log.Fatal(err)
// }
//
// out, err := client.SearchVulnerabilities(ctx, vulnx.SearchParams{
// Query: vulnx.Ptr("id:CVE-2023-4799"),
// Limit: vulnx.Ptr(10),
// })
// if err != nil {
// log.Fatal(err)
// }
//
// fmt.Println(len(out.Vulnerabilities))
//
// # Rate Limiting
//
// Unauthenticated requests are subject to strict rate limits. If you encounter
// 429 (Too Many Requests) errors, configure an API key using WithPDCPKey() or
// WithKeyFromEnv() to get higher rate limits and better service reliability.
//
// The client is safe for concurrent use by multiple goroutines.
//
// For complete API semantics refer to https://api.projectdiscovery.io/docs.
package vulnx
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/projectdiscovery/gologger"
retryablehttp "github.com/projectdiscovery/retryablehttp-go"
"github.com/projectdiscovery/utils/auth/pdcp"
"github.com/projectdiscovery/utils/errkit"
)
const (
// DefaultBaseURL is the default base URL for the API.
DefaultBaseURL = "https://api.projectdiscovery.io"
// UserAgent is the default user agent for the client.
UserAgent = "vulnx-client/1.0"
// BaseURLEnvVar is the environment variable name for overriding the base URL.
BaseURLEnvVar = "VULNX_API_URL"
)
// Client errors
var (
ErrBadRequest = errkit.New("bad request: client sent an invalid request")
ErrUnauthorized = errkit.New("unauthorized: invalid or missing API key")
ErrNotFound = errkit.New("not found: resource does not exist")
ErrTooManyRequests = errkit.New("too many requests: rate limit exceeded - consider using an API key for higher limits")
ErrInternalServerError = errkit.New("internal server error: something went wrong on the server")
ErrUnknownAPIError = errkit.New("unknown api error")
ErrRequestBuildFailure = errkit.New("failed to build request")
ErrRequestFailed = errkit.New("request failed")
ErrMarshalBody = errkit.New("failed to marshal request body")
ErrCreateHTTPRequest = errkit.New("failed to create http request")
ErrDecodeResponse = errkit.New("failed to decode response")
)
// Option represents a functional option that mutates a *Client* during
// construction. It follows the standard "functional options" pattern popularised
// by Google and is the preferred way to add optional parameters without an
// explosion of constructor variants.
//
// A typical call site looks like this:
//
// client, err := vulnx.New(
// vulnx.WithPDCPKey("<YOUR_KEY>"),
// vulnx.WithRetryableHTTPOptions(retryablehttp.Options{RetryMax: 5}),
// )
// if err != nil {
// // handle error
// }
type Option func(*Client)
// Client provides high-level helpers around the vulnx API. It is safe for
// concurrent use. Zero values for *Client* fields are not meaningful—always use
// the *New* constructor.
type Client struct {
baseURL string
apiKey string
httpc *retryablehttp.Client
userAgent string
// Optional debug hooks
debugRequest func(*http.Request)
debugResponse func(*http.Response)
}
// New returns a new *Client* configured by the supplied *Option*s. Authentication
// is optional but strongly recommended - unauthenticated requests are subject
// to strict rate limits. Use *WithPDCPKey* or *WithKeyFromEnv* to configure
// an API key for better performance and higher rate limits.
//
// The returned client is ready for immediate use:
//
// c, err := vulnx.New(vulnx.WithPDCPKey("<YOUR_KEY>"))
// if err != nil { /* handle */ }
//
// // Or without authentication (subject to rate limits):
// c, err := vulnx.New()
// if err != nil { /* handle */ }
//
// Custom HTTP behaviour (timeouts, retries, logging) can be injected via
// *WithClient* or *WithRetryableHTTPOptions*.
func New(opts ...Option) (*Client, error) {
// Check for base URL override from environment
baseURL := DefaultBaseURL
if envURL := os.Getenv(BaseURLEnvVar); envURL != "" {
baseURL = envURL
}
c := &Client{
baseURL: baseURL,
userAgent: UserAgent,
httpc: retryablehttp.NewClient(retryablehttp.DefaultOptionsSingle), // Default retryablehttp client
}
for _, opt := range opts {
opt(c)
}
// API key is now optional - authentication is not required but recommended
// If a custom httpc was not provided, set the default retryablehttp client
if c.httpc == nil {
c.httpc = retryablehttp.NewClient(retryablehttp.DefaultOptionsSingle)
}
return c, nil
}
// WithClient overrides the default *retryablehttp.Client* used for all network
// operations. It enables advanced users to specify custom transports, proxy
// settings, or instrumentation hooks.
func WithClient(hc *retryablehttp.Client) Option {
return func(c *Client) {
c.httpc = hc
}
}
// WithPDCPKey sets the ProjectDiscovery Cloud Platform (PDCP) API key that will
// be sent in the `X-PDCP-Key` HTTP header.
func WithPDCPKey(apiKey string) Option {
return func(c *Client) {
c.apiKey = apiKey
}
}
// WithKeyFromEnv attempts to discover a PDCP API key from the local credential
// store (managed by `pdcp`) or the `PDCP_API_KEY` environment variable.
func WithKeyFromEnv() Option {
return func(c *Client) {
pch := pdcp.PDCPCredHandler{}
if creds, err := pch.GetCreds(); err == nil {
c.apiKey = creds.APIKey
return
}
}
}
// WithBaseURL points the client at an alternative endpoint—useful for testing
// against staging or mock servers.
func WithBaseURL(url string) Option {
return func(c *Client) {
c.baseURL = url
}
}
// WithRetryableHTTPOptions constructs a fresh *retryablehttp.Client* with the
// supplied options and wires it into the *Client* instance.
func WithRetryableHTTPOptions(clientOpts retryablehttp.Options) Option {
return func(c *Client) {
c.httpc = retryablehttp.NewClient(clientOpts)
}
}
// WithDebugRequest sets a callback that is invoked with the *http.Request before it is sent.
func WithDebugRequest(cb func(*http.Request)) Option {
return func(c *Client) {
c.debugRequest = cb
}
}
// WithDebugResponse sets a callback that is invoked with the *http.Response after it is received (before decoding).
func WithDebugResponse(cb func(*http.Response)) Option {
return func(c *Client) {
c.debugResponse = cb
}
}
// SearchVulnerabilities performs a full-text search across all vulnerability
// documents and returns a paginated *SearchResponse*.
//
// The behaviour of the search is controlled via *SearchParams*; see that type
// for field-level documentation.
//
// SearchVulnerabilities may contact the network multiple times if retries are
// enabled on the underlying HTTP client. It is safe to call concurrently.
func (c *Client) SearchVulnerabilities(ctx context.Context, params SearchParams) (SearchResponse, error) {
var resp SearchResponse
req, err := c.newRequest(ctx, http.MethodGet, "/v2/vulnerability/search", paramsToQuery(params), nil)
if err != nil {
return resp, errkit.Append(ErrRequestBuildFailure, err)
}
err = c.do(req, &resp)
if err != nil {
return resp, errkit.Append(ErrRequestFailed, err)
}
return resp, nil
}
// GetVulnerabilityByID fetches a single vulnerability document identified by
// its canonical ID (for example "CVE-2023-1234").
//
// When *params* is non-nil the *Fields* slice can be used to limit the response
// payload to a subset of fields, reducing bandwidth.
func (c *Client) GetVulnerabilityByID(ctx context.Context, id string, params *GetByIDParams) (VulnerabilityResponse, error) {
var resp VulnerabilityResponse
path := fmt.Sprintf("/v2/vulnerability/%s", id)
var query url.Values
if params != nil && len(params.Fields) > 0 {
query = make(url.Values)
query.Set("fields", strings.Join(params.Fields, ","))
}
req, err := c.newRequest(ctx, http.MethodGet, path, query, nil)
if err != nil {
return resp, errkit.Append(ErrRequestBuildFailure, err)
}
err = c.do(req, &resp)
if err != nil {
return resp, errkit.Append(ErrRequestFailed, err)
}
return resp, nil
}
// GetVulnerabilityFilters lists all filter definitions that can be applied to
// search queries. Filters are stable identifiers used for building rich UI
// facets or powering autocomplete experiences.
func (c *Client) GetVulnerabilityFilters(ctx context.Context) ([]VulnerabilityFilter, error) {
var filters []VulnerabilityFilter
req, err := c.newRequest(ctx, http.MethodGet, "/v2/vulnerability/filters", nil, nil)
if err != nil {
return nil, errkit.Append(ErrRequestBuildFailure, err)
}
err = c.do(req, &filters)
if err != nil {
return nil, errkit.Append(ErrRequestFailed, err)
}
return filters, nil
}
// IsAuthenticated returns true if the client has an API key configured.
// This can be used to provide better UX messaging about rate limits.
func (c *Client) IsAuthenticated() bool {
return c.apiKey != ""
}
// newRequest builds an HTTP request with authentication and query params.
func (c *Client) newRequest(ctx context.Context, method, path string, query url.Values, body any) (*http.Request, error) {
requestURL := c.baseURL + path
if len(query) > 0 {
requestURL += "?" + query.Encode()
}
var req *http.Request
var err error
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, errkit.Append(ErrMarshalBody, err)
}
req, err = http.NewRequestWithContext(ctx, method, requestURL, strings.NewReader(string(b)))
if err != nil {
return nil, errkit.Append(ErrCreateHTTPRequest, err)
}
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequestWithContext(ctx, method, requestURL, nil)
if err != nil {
return nil, errkit.Append(ErrCreateHTTPRequest, err)
}
}
// Only set API key header if one is provided
if c.apiKey != "" {
req.Header.Set("X-PDCP-Key", c.apiKey)
}
req.Header.Set("User-Agent", c.userAgent)
return req, nil
}
// do executes the HTTP request and decodes the JSON response.
func (c *Client) do(req *http.Request, out any) error {
if c.debugRequest != nil {
c.debugRequest(req)
}
resp, err := c.httpc.HTTPClient.Do(req)
if err != nil {
return errkit.Append(ErrRequestFailed, err)
}
if c.debugResponse != nil {
c.debugResponse(resp)
}
defer func() {
if err := resp.Body.Close(); err != nil {
gologger.Error().Msgf("Failed to close response body: %s", err)
}
}()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return c.handleAPIError(resp)
}
if out != nil {
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(out); err != nil {
return errkit.Append(ErrDecodeResponse, err)
}
}
return nil
}
// handleAPIError processes non-2xx HTTP responses, normalising them into the
// rich errkit error hierarchy so that callers can unwrap and inspect the root
// cause.
func (c *Client) handleAPIError(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusNotFound:
return ErrNotFound // Return ErrNotFound directly for 404 as it indicates no content
}
bodyBytes, readErr := io.ReadAll(resp.Body)
if readErr != nil {
return errkit.Wrap(ErrUnknownAPIError, fmt.Sprintf("api error (failed to read response body): %s", resp.Status))
}
var errorBody map[string]interface{}
unmarshalErr := json.Unmarshal(bodyBytes, &errorBody)
var detailedErr error
if unmarshalErr == nil {
// Check for common error message keys
if errMsg, ok := errorBody["error"]; ok {
detailedErr = errkit.New("api error", "status_code", resp.StatusCode, "error", errMsg)
} else if errMsg, ok := errorBody["msg"]; ok {
detailedErr = errkit.New("api error", "status_code", resp.StatusCode, "error", errMsg)
} else if errMsg, ok := errorBody["message"]; ok {
detailedErr = errkit.New("api error", "status_code", resp.StatusCode, "error", errMsg)
} else if errMsg, ok := errorBody["cause"]; ok {
detailedErr = errkit.New("api error", "status_code", resp.StatusCode, "error", errMsg)
}
}
if detailedErr == nil {
// Fallback to raw body if no specific keys found or unmarshalling failed
detailedErr = errkit.New("api error", "status_code", resp.StatusCode, "error", strings.TrimSpace(string(bodyBytes)))
}
switch resp.StatusCode {
case http.StatusBadRequest:
return errkit.Append(ErrBadRequest, detailedErr)
case http.StatusUnauthorized:
return errkit.Append(ErrUnauthorized, detailedErr)
case http.StatusTooManyRequests:
return errkit.Append(ErrTooManyRequests, detailedErr)
case http.StatusInternalServerError:
return errkit.Append(ErrInternalServerError, detailedErr)
default:
return errkit.Append(ErrUnknownAPIError, detailedErr)
}
}
// paramsToQuery converts SearchParams to url.Values for query string.
func paramsToQuery(params SearchParams) url.Values {
q := make(url.Values)
if params.Limit != nil {
q.Set("limit", fmt.Sprintf("%d", *params.Limit))
}
if params.Offset != nil {
q.Set("offset", fmt.Sprintf("%d", *params.Offset))
}
if params.SortAsc != nil {
q.Set("sort_asc", *params.SortAsc)
}
if params.SortDesc != nil {
q.Set("sort_desc", *params.SortDesc)
}
if len(params.Fields) > 0 {
q.Set("fields", strings.Join(params.Fields, ","))
}
if len(params.TermFacets) > 0 {
q.Set("term_facets", strings.Join(params.TermFacets, ","))
}
if len(params.RangeFacets) > 0 {
q.Set("range_facets", strings.Join(params.RangeFacets, ","))
}
if params.Query != nil {
q.Set("q", *params.Query)
}
if params.Highlight != nil {
q.Set("highlight", fmt.Sprintf("%t", *params.Highlight))
}
if params.FacetSize != nil {
q.Set("facet_size", fmt.Sprintf("%d", *params.FacetSize))
}
return q
}