Skip to content

Commit 0447bc9

Browse files
authored
refactor(database)!: Use class for databases and implement ApiCredential (#320)
1 parent 8563064 commit 0447bc9

25 files changed

Lines changed: 709 additions & 290 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,4 @@ dist
108108
.vscode/
109109

110110
temp/
111+
.claude

docs/database/nvd.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ NVD stand for <kbd>National Vulnerability Database</kbd>, which is the U.S. gove
44

55
## Implementation Notes
66

7-
The NVD integration uses the REST API (v2.0) available at [services.nvd.nist.gov](https://services.nvd.nist.gov/rest/json/cves/2.0).
7+
The NVD integration uses the REST API (v2.0) available at [services.nvd.nist.gov](https://services.nvd.nist.gov/rest/json/cves/2.0).
88

99
### Search Parameters
1010

@@ -30,7 +30,27 @@ export interface NVD {
3030

3131
## API
3232

33-
### findOne(parameters: NVDApiParameter): Promise< NVD[] >
33+
### Constructor
34+
35+
```ts
36+
import * as vulnera from "@nodesecure/vulnera";
37+
38+
const db = new vulnera.Database.NVD({
39+
credential: new vulnera.ApiCredential({
40+
type: "querystring",
41+
name: "apiKey",
42+
value: "your-api-key"
43+
})
44+
});
45+
```
46+
47+
```ts
48+
export interface NVDOptions {
49+
credential?: ApiCredential;
50+
}
51+
```
52+
53+
### `findOne(parameters: NVDApiParameter): Promise<NVD[]>`
3454
Find the vulnerabilities of a given package using available NVD API parameters.
3555

3656
```ts
@@ -43,19 +63,20 @@ export type NVDApiParameter = {
4363
};
4464
```
4565

46-
### findOneBySpec(spec: string): Promise< NVD[] >
66+
### `findOneBySpec(spec: string): Promise<NVD[]>`
4767
Find the vulnerabilities of a given package using the NPM spec format like `packageName@version`.
4868

4969
```ts
50-
import * as vulnera from "@nodesecure/vulnera";
51-
52-
const vulns = await vulnera.Database.nvd.findOneBySpec(
53-
"express@4.0.0"
54-
);
70+
const vulns = await db.findOneBySpec("express@4.0.0");
5571
console.log(vulns);
5672
```
5773

58-
### findMany< T extends string >(specs: T[]): Promise< Record< T, NVD[] > >
74+
### `findMany<T extends string>(specs: T[]): Promise<Record<T, NVD[]>>`
5975
Find the vulnerabilities of many packages using the spec format.
6076

61-
Returns a Record where keys are equals to the provided specs.
77+
Returns a Record where keys are equals to the provided specs.
78+
79+
```ts
80+
const vulns = await db.findMany(["express@4.0.0", "lodash@4.17.0"]);
81+
console.log(vulns);
82+
```

docs/database/osv.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,21 @@ export interface OSV {
3838

3939
## API
4040

41-
### findOne(parameters: OSVApiParameter): Promise< OSV[] >
41+
### Constructor
42+
43+
```ts
44+
import * as vulnera from "@nodesecure/vulnera";
45+
46+
const db = new vulnera.Database.OSV();
47+
```
48+
49+
```ts
50+
export interface OSVOptions {
51+
credential?: ApiCredential;
52+
}
53+
```
54+
55+
### `findOne(parameters: OSVApiParameter): Promise<OSV[]>`
4256
Find the vulnerabilities of a given package using available OSV API parameters.
4357

4458
```ts
@@ -54,19 +68,20 @@ export type OSVApiParameter = {
5468
}
5569
```
5670
57-
### findOneBySpec(spec: string): Promise< OSV[] >
71+
### `findOneBySpec(spec: string): Promise<OSV[]>`
5872
Find the vulnerabilities of a given package using the NPM spec format like `packageName@version`.
5973
6074
```ts
61-
import * as vulnera from "@nodesecure/vulnera";
62-
63-
const vulns = await vulnera.Database.osv.findOneBySpec(
64-
"01template1"
65-
);
75+
const vulns = await db.findOneBySpec("01template1");
6676
console.log(vulns);
6777
```
6878

69-
### findMany< T extends string >(specs: T[]): Promise< Record< T, OSV[] > >
79+
### `findMany<T extends string>(specs: T[]): Promise<Record<T, OSV[]>>`
7080
Find the vulnerabilities of many packages using the spec format.
7181

7282
Return a Record where keys are equals to the provided specs.
83+
84+
```ts
85+
const vulns = await db.findMany(["express@4.0.0", "lodash@4.17.0"]);
86+
console.log(vulns);
87+
```

docs/database/snyk.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Snyk
2+
3+
[Snyk](https://snyk.io/fr) Snyk Limited is a developer-oriented cybersecurity company, specializing in securing custom developed code, open-source dependencies and cloud infrastructure.
4+
5+
## Implementation Notes
6+
7+
The Snyk integration uses the REST API (v1) available at [snyk.io](https://snyk.io/api/v1/test/npm) to perform security audit.
8+
9+
### Authentication
10+
11+
The `Snyk` constructor requires an `org` and a `credential`. These are generated when you create an organization on Snyk.
12+
13+
- `org`: Your Snyk organization ID
14+
- `credential`: An `ApiCredential` instance using the `token` type (passed as `Authorization: token <token>` header)
15+
16+
### Format
17+
18+
The Snyk interface is exported as root like `SnykAuditResponse`.
19+
20+
```ts
21+
export interface SnykAuditResponse {
22+
/** Does this package have one or more issues? **/
23+
ok: boolean;
24+
/** The issues found. **/
25+
issues: {
26+
vulnerabilities: SnykVulnerability[];
27+
licenses: SnykVulnerability[];
28+
};
29+
/** The number of dependencies the package has. **/
30+
dependencyCount: number;
31+
/** The organization this test was carried out for. **/
32+
org: {
33+
id: string;
34+
name: string;
35+
};
36+
/** The organization's licenses policy used for this test **/
37+
licensesPolicy: null | object;
38+
/** The package manager for this package **/
39+
packageManager: string;
40+
}
41+
```
42+
43+
## API
44+
45+
### Constructor
46+
47+
```ts
48+
import * as vulnera from "@nodesecure/vulnera";
49+
50+
const db = new vulnera.Database.Snyk({
51+
org: process.env.SNYK_ORG,
52+
credential: new vulnera.ApiCredential(process.env.SNYK_TOKEN)
53+
});
54+
```
55+
56+
```ts
57+
export interface SnykOptions {
58+
org: string;
59+
credential: ApiCredential;
60+
}
61+
```
62+
63+
### `findOne(parameters: SnykFindOneParameters): Promise<SnykAuditResponse>`
64+
65+
Find the vulnerabilities of a given package using available SnykFindOneParameters API parameters.
66+
67+
```ts
68+
export type SnykFindOneParameters = {
69+
files: {
70+
target: {
71+
contents: string;
72+
};
73+
additional?: {
74+
contents: string;
75+
}[];
76+
};
77+
};
78+
```
79+
80+
```ts
81+
import * as vulnera from "@nodesecure/vulnera";
82+
83+
const db = new vulnera.Database.Snyk({
84+
org: process.env.SNYK_ORG,
85+
credential: new vulnera.ApiCredential({
86+
type: "token",
87+
token: process.env.SNYK_TOKEN
88+
})
89+
});
90+
const result = await db.findOne({
91+
files: {
92+
target: { contents: packageJsonBase64 }
93+
}
94+
});
95+
```

docs/database/sonatype.md

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,84 @@
22

33
Sonatype provides software supply chain security and repository management tools to help organizations manage risks in their open source dependencies.
44

5-
### Implementation Notes
5+
## Implementation Notes
66

77
The Sonatype integration uses the REST API (v3) available at [ossindex.sonatype.org](https://ossindex.sonatype.org/api/v3/component-report).
88

9+
### Authentication
10+
11+
`Sonatype` supports optional basic auth credentials for higher rate limits. Without credentials, the API is still accessible at reduced rate limits.
12+
913
### Format
1014

11-
the Sonatype interface is exported as root like `SonatypeResponse`.
15+
The Sonatype interface is exported as root like `SonatypeResponse`.
16+
17+
```ts
18+
export type SonatypeResponse = {
19+
coordinates: string;
20+
vulnerabilities: SonatypeVulnerability[];
21+
};
22+
```
23+
24+
## API
25+
26+
### Constructor
27+
28+
```ts
29+
import * as vulnera from "@nodesecure/vulnera";
30+
31+
const db = new vulnera.Database.Sonatype({
32+
credential: new vulnera.ApiCredential({
33+
type: "basic",
34+
username: process.env.SONATYPE_USERNAME,
35+
password: process.env.SONATYPE_PASSWORD
36+
})
37+
});
38+
```
1239

1340
```ts
14-
export type SonatypeResponse = {
15-
coordinates: string; vulnerabilities: SonatypeVulnerability[];
16-
};
41+
export interface SonatypeOptions {
42+
credential?: ApiCredential;
43+
}
1744
```
18-
### API
1945

20-
### findOne(parameters: SonaTypeFindOneParameters): Promise< SonatypeResponse[] >
46+
### `findOne(parameters: SonaTypeFindOneParameters): Promise<SonatypeResponse[]>`
47+
48+
Find the vulnerabilities of a given package using available Sonatype API parameters.
2149

2250
```ts
2351
export type SonaTypeFindOneParameters = {
2452
coordinates: string[];
2553
};
2654
```
2755

28-
Find the vulnerabilities of a given package using available Sonatype API parameters.
56+
```ts
57+
import * as vulnera from "@nodesecure/vulnera";
2958

30-
### findMany(parameters: SonaTypeFindManyParameters): Promise< SonatypeResponse[] > >
59+
const db = new vulnera.Database.Sonatype();
60+
const vulns = await db.findOne({ coordinates: ["pkg:npm/express@4.0.0"] });
61+
console.log(vulns);
62+
```
63+
64+
### `findMany(parameters: SonaTypeFindManyParameters): Promise<SonatypeResponse[]>`
65+
66+
Find the vulnerabilities of many packages.
3167

3268
```ts
3369
export type SonaTypeFindManyParameters = {
3470
coordinates: string[][];
3571
};
3672
```
3773

38-
Find the vulnerabilities of many packages.
74+
```ts
75+
import * as vulnera from "@nodesecure/vulnera";
76+
77+
const db = new vulnera.Database.Sonatype();
78+
const vulns = await db.findMany({
79+
coordinates: [
80+
["pkg:npm/express@4.0.0"],
81+
["pkg:npm/lodash@4.17.0"]
82+
]
83+
});
84+
console.log(vulns);
85+
```

docs/database/synk.md

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/constants.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export const NPM_TOKEN = typeof process.env.NODE_SECURE_TOKEN === "string" ?
22
{ token: process.env.NODE_SECURE_TOKEN } : {};
3-
export const SNYK_ORG = process.env.SNYK_ORG;
4-
export const SNYK_TOKEN = process.env.SNYK_TOKEN;
53

64
export const VULN_MODE = Object.freeze({
75
GITHUB_ADVISORY: "github-advisory",

0 commit comments

Comments
 (0)