Skip to content

Commit 52f9e4a

Browse files
authored
Okhttp (#1)
* added support for okhttp3 * added support custom CA Certs
1 parent e57f4d8 commit 52f9e4a

18 files changed

+526
-148
lines changed

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Upcoming changes...
1313

14+
## [0.5.0] - 2023-07-26
15+
### Added
16+
- Switched to okhttp for REST communication
17+
- Added custom HTTP certificate support (`customCert`)
18+
- CLI option: `--ca-cert`
19+
### Fixed
20+
- Fixed issue with null json object printing
21+
1422
## [0.4.0] - 2023-07-07
1523

1624
### Added
1725
- Added long snippet generation check limit (`snippetLimit`)
1826
- Added command line option: `--snippet-limit` to support it
1927

2028
## [0.2.0] - 2023-07-04
21-
2229
### Added
2330
- First pass at the following Classes
2431
- Fingerprinting ([Winnowing](src/main/java/com/scanoss/Winnowing.java))
@@ -29,3 +36,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2936

3037
[0.2.0]: https://github.com/scanoss/scanoss.java/compare/v0.0.0...v0.2.0
3138
[0.4.0]: https://github.com/scanoss/scanoss.java/compare/v0.2.0...v0.4.0
39+
[0.5.0]: https://github.com/scanoss/scanoss.java/compare/v0.4.0...v0.5.0

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ The package also ships with a sample CLI. It can be run using the example script
5252
scanos-cli.sh -h
5353
```
5454

55+
### Custom Certificate
56+
In order to connect to a SCANOSS server with a custom (self-signed) certificate,
57+
the keychain will need to be imported onto the CA Certs into the instance of java before proceeding:
58+
59+
```bash
60+
keytool -cacerts -importcert -file custom-key-chain.pem
61+
```
62+
5563
## Development
5664

5765
Before starting with development of this project, please read our [CONTRIBUTING](CONTRIBUTING.md)

SBOM.json

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
{
2+
"bomFormat": "CycloneDX",
3+
"specVersion": "1.4",
4+
"serialNumber": "urn:uuid:36e7c0cb-8330-4fe5-82ce-11061851147e",
5+
"version": 1,
6+
"components": [
7+
{
8+
"type": "library",
9+
"name": "scanoss.java",
10+
"publisher": "scanoss",
11+
"version": "0.4.0",
12+
"purl": "pkg:github/scanoss/scanoss.java",
13+
"bom-ref": "pkg:github/scanoss/scanoss.java",
14+
"licenses": [
15+
{
16+
"license": {
17+
"id": "MIT"
18+
}
19+
}
20+
]
21+
},
22+
{
23+
"type": "library",
24+
"name": "junit",
25+
"publisher": "",
26+
"version": "4.13.1",
27+
"purl": "pkg:maven/junit/junit",
28+
"bom-ref": "pkg:maven/junit/junit",
29+
"licenses": [
30+
{
31+
"license": {
32+
"id": "EPL-1.0"
33+
}
34+
}
35+
]
36+
},
37+
{
38+
"type": "library",
39+
"name": "mockwebserver",
40+
"publisher": "",
41+
"version": "4.11.0",
42+
"purl": "pkg:maven/com.squareup.okhttp3/mockwebserver",
43+
"bom-ref": "pkg:maven/com.squareup.okhttp3/mockwebserver",
44+
"licenses": [
45+
{
46+
"license": {
47+
"name": "Apache-2.0"
48+
}
49+
}
50+
]
51+
},
52+
{
53+
"type": "library",
54+
"name": "lombok",
55+
"publisher": "",
56+
"version": "1.18.26",
57+
"purl": "pkg:maven/org.projectlombok/lombok",
58+
"bom-ref": "pkg:maven/org.projectlombok/lombok",
59+
"licenses": [
60+
{
61+
"license": {
62+
"name": "MIT"
63+
}
64+
}
65+
]
66+
},
67+
{
68+
"type": "library",
69+
"name": "okhttp",
70+
"publisher": "",
71+
"version": "4.11.0",
72+
"purl": "pkg:maven/com.squareup.okhttp3/okhttp",
73+
"bom-ref": "pkg:maven/com.squareup.okhttp3/okhttp",
74+
"licenses": [
75+
{
76+
"license": {
77+
"name": "Apache-2.0"
78+
}
79+
}
80+
]
81+
},
82+
{
83+
"type": "library",
84+
"name": "commons-codec",
85+
"publisher": "",
86+
"version": "1.15",
87+
"purl": "pkg:maven/commons-codec/commons-codec",
88+
"bom-ref": "pkg:maven/commons-codec/commons-codec",
89+
"licenses": [
90+
{
91+
"license": {
92+
"name": "Apache-2.0"
93+
}
94+
}
95+
]
96+
},
97+
{
98+
"type": "library",
99+
"name": "slf4j-api",
100+
"publisher": "",
101+
"version": "2.0.7",
102+
"purl": "pkg:maven/org.slf4j/slf4j-api",
103+
"bom-ref": "pkg:maven/org.slf4j/slf4j-api",
104+
"licenses": [
105+
{
106+
"license": {
107+
"name": "MIT"
108+
}
109+
}
110+
]
111+
},
112+
{
113+
"type": "library",
114+
"name": "slf4j-simple",
115+
"publisher": "",
116+
"version": "2.0.7",
117+
"purl": "pkg:maven/org.slf4j/slf4j-simple",
118+
"bom-ref": "pkg:maven/org.slf4j/slf4j-simple",
119+
"licenses": [
120+
{
121+
"license": {
122+
"name": "MIT"
123+
}
124+
}
125+
]
126+
},
127+
{
128+
"type": "library",
129+
"name": "tika-core",
130+
"publisher": "",
131+
"version": "2.7.0",
132+
"purl": "pkg:maven/org.apache.tika/tika-core",
133+
"bom-ref": "pkg:maven/org.apache.tika/tika-core",
134+
"licenses": [
135+
{
136+
"license": {
137+
"name": "Apache-2.0"
138+
}
139+
}
140+
]
141+
},
142+
{
143+
"type": "library",
144+
"name": "picocli",
145+
"publisher": "",
146+
"version": "4.7.3",
147+
"purl": "pkg:maven/info.picocli/picocli",
148+
"bom-ref": "pkg:maven/info.picocli/picocli",
149+
"licenses": [
150+
{
151+
"license": {
152+
"name": "Apache-2.0"
153+
}
154+
}
155+
]
156+
},
157+
{
158+
"type": "library",
159+
"name": "gson",
160+
"publisher": "",
161+
"version": "2.10.1",
162+
"purl": "pkg:maven/com.google.code.gson/gson",
163+
"bom-ref": "pkg:maven/com.google.code.gson/gson",
164+
"licenses": [
165+
{
166+
"license": {
167+
"id": "Apache-2.0"
168+
}
169+
}
170+
]
171+
},
172+
{
173+
"type": "library",
174+
"name": "jquery",
175+
"publisher": "OpenJS Foundation and other contributors",
176+
"version": "3.5.1",
177+
"purl": "pkg:npm/jquery",
178+
"bom-ref": "pkg:npm/jquery",
179+
"licenses": [
180+
{
181+
"license": {
182+
"id": "MIT"
183+
}
184+
}
185+
]
186+
}
187+
],
188+
"vulnerabilities": []
189+
}

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.scanoss</groupId>
88
<artifactId>scanoss</artifactId>
9-
<version>0.4.0</version>
9+
<version>0.5.0</version>
1010
<packaging>jar</packaging>
1111
<name>scanoss.java</name>
1212
<url>https://github.com/scanoss/scanoss.java</url>
@@ -78,6 +78,16 @@
7878
<version>1.18.26</version>
7979
<optional>true</optional>
8080
</dependency>
81+
<dependency>
82+
<groupId>com.squareup.okhttp3</groupId>
83+
<artifactId>okhttp</artifactId>
84+
<version>4.11.0</version>
85+
</dependency>
86+
<dependency>
87+
<groupId>com.squareup.okhttp3</groupId>
88+
<artifactId>okhttp-tls</artifactId>
89+
<version>4.11.0</version>
90+
</dependency>
8191
<dependency>
8292
<groupId>commons-codec</groupId>
8393
<artifactId>commons-codec</artifactId>

src/main/java/com/scanoss/Scanner.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.IOException;
3838
import java.nio.file.*;
3939
import java.nio.file.attribute.BasicFileAttributes;
40+
import java.time.Duration;
4041
import java.util.ArrayList;
4142
import java.util.List;
4243
import java.util.Objects;
@@ -45,6 +46,8 @@
4546
import java.util.concurrent.Executors;
4647
import java.util.concurrent.Future;
4748

49+
import static com.scanoss.ScanossConstants.*;
50+
4851
/**
4952
* SCANOSS Scanner Class
5053
* <p>
@@ -68,26 +71,28 @@ public class Scanner {
6871
@Builder.Default
6972
private Boolean allFolders = Boolean.FALSE; // Enable Scanning of all folders (except hidden)
7073
@Builder.Default
71-
private Integer numThreads = 5; // Number of parallel threads to use when processing a folder
74+
private Integer numThreads = DEFAULT_WORKER_THREADS; // Number of parallel threads to use when processing a folder
7275
@Builder.Default
73-
private Integer timeout = 120; // API POST timeout
76+
private Duration timeout = Duration.ofSeconds(DEFAULT_TIMEOUT); // API POST timeout
7477
@Builder.Default
75-
private Integer retryLimit = 5; // Retry limit for posting scan requests
78+
private Integer retryLimit = DEFAULT_HTTP_RETRY_LIMIT; // Retry limit for posting scan requests
7679
private String url; // Alternative scanning URL
7780
private String apiKey; // API key
7881
private String scanFlags; // Scan flags to pass to the API
7982
private String sbomType; // SBOM type (identify/ignore)
8083
private String sbom; // SBOM to supply while scanning
8184
private int snippetLimit; // Size limit for a single line of generated snippet
85+
private String customCert; // Custom certificate
8286
private Winnowing winnowing;
8387
private ScanApi scanApi;
8488
private ScanFileProcessor scanFileProcessor;
8589
private WfpFileProcessor wfpFileProcessor;
8690

8791
@SuppressWarnings("unused")
8892
private Scanner(Boolean skipSnippets, Boolean allExtensions, Boolean obfuscate, Boolean hpsm,
89-
Boolean hiddenFilesFolders, Boolean allFolders, Integer numThreads, Integer timeout, Integer retryLimit,
90-
String url, String apiKey, String scanFlags, String sbomType, String sbom, Integer snippetLimit,
93+
Boolean hiddenFilesFolders, Boolean allFolders, Integer numThreads, Duration timeout,
94+
Integer retryLimit, String url, String apiKey, String scanFlags, String sbomType, String sbom,
95+
Integer snippetLimit, String customCert,
9196
Winnowing winnowing, ScanApi scanApi,
9297
ScanFileProcessor scanFileProcessor, WfpFileProcessor wfpFileProcessor
9398
) {
@@ -106,13 +111,20 @@ private Scanner(Boolean skipSnippets, Boolean allExtensions, Boolean obfuscate,
106111
this.sbomType = sbomType;
107112
this.sbom = sbom;
108113
this.snippetLimit = snippetLimit;
114+
this.customCert = customCert;
109115
this.winnowing = Objects.requireNonNullElseGet(winnowing, () ->
110-
Winnowing.builder().skipSnippets(skipSnippets).allExtensions(allExtensions).obfuscate(obfuscate).hpsm(hpsm).snippetLimit(snippetLimit).build());
116+
Winnowing.builder().skipSnippets(skipSnippets).allExtensions(allExtensions).obfuscate(obfuscate)
117+
.hpsm(hpsm).snippetLimit(snippetLimit)
118+
.build());
111119
this.scanApi = Objects.requireNonNullElseGet(scanApi, () ->
112-
ScanApi.builder().url(url).apiKey(apiKey).timeout(timeout).retryLimit(retryLimit).flags(scanFlags).scanType(sbomType).sbom(sbom).build());
120+
ScanApi.builder().url(url).apiKey(apiKey).timeout(timeout).retryLimit(retryLimit).flags(scanFlags)
121+
.scanType(sbomType).sbom(sbom).customCert(customCert)
122+
.build());
113123
this.scanFileProcessor = Objects.requireNonNullElseGet(scanFileProcessor, () ->
114124
ScanFileProcessor.builder().winnowing(this.winnowing).scanApi(this.scanApi).build());
115-
this.wfpFileProcessor = Objects.requireNonNullElseGet(wfpFileProcessor, () -> WfpFileProcessor.builder().winnowing(this.winnowing).build());
125+
this.wfpFileProcessor = Objects.requireNonNullElseGet(wfpFileProcessor, () -> WfpFileProcessor.builder()
126+
.winnowing(this.winnowing)
127+
.build());
116128
}
117129

118130
/**
@@ -288,9 +300,8 @@ public List<String> wfpFolder(@NonNull String folder) throws ScannerException, W
288300
* @return scan results string (in JSON format)
289301
* @throws ScannerException Something in Scanning failed
290302
* @throws WinnowingException Something in Winnowing failed
291-
* @throws InterruptedException Scan API was interrupted
292303
*/
293-
public String scanFile(@NonNull String filename) throws ScannerException, WinnowingException, InterruptedException {
304+
public String scanFile(@NonNull String filename) throws ScannerException, WinnowingException {
294305
String wfp = wfpFile(filename);
295306
if (wfp != null && !wfp.isEmpty()) {
296307
String response = this.scanApi.scan(wfp, "", 1);

src/main/java/com/scanoss/ScanossConstants.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@
1111
*/
1212
@SuppressWarnings("SpellCheckingInspection")
1313
public class ScanossConstants {
14+
/**
15+
* Default timeout for HTTP communication
16+
*/
17+
public static final int DEFAULT_TIMEOUT = 120;
18+
/**
19+
* Default number of worker threads to use then processing files
20+
*/
21+
public static final int DEFAULT_WORKER_THREADS = 5;
22+
/**
23+
* Default number of times to retry sending data to HTTP
24+
*/
25+
public static final int DEFAULT_HTTP_RETRY_LIMIT = 5;
1426

1527
static final int GRAM = 30; // Winnowing Gram size. Do NOT Modify
1628
static final int WINDOW = 64; // Winnowing Window size. Do NOT Modify

0 commit comments

Comments
 (0)