Skip to content

Commit 1867682

Browse files
committed
chore: remove exhort_dev_mode and default endpoints and use value from environment variables or arguments
1 parent d42371f commit 1867682

10 files changed

Lines changed: 124 additions & 111 deletions

File tree

CONTRIBUTING.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737

3838
### Good to know
3939

40-
* You can override the default backend url by setting the `TRUSTIFY_DA_DEV_MODE` environment variable/system property to true:
41-
* In case environment variable/System Property `TRUSTIFY_DA_DEV_MODE=true` - You can Override the default trustify-dependency-analytics backend by setting
42-
`DEV_TRUSTIFY_DA_BACKEND_URL` env variable/system property to the desired trustify-dependency-analytics backend instance address ( useful for tests).
43-
* In case `DEV_TRUSTIFY_DA_BACKEND_URL` is not set via environment variable/system property, then the default DEV trustify-dependency-analytics backend is picked.
44-
* In case `TRUSTIFY_DA_DEV_MODE=false` or not set at all levels, then default backend url ( trustify-dependency-analytics prod) is picked, regardless of the value of `DEV_TRUSTIFY_DA_BACKEND_URL`.
45-
* Environment variables takes precedence over System properties - for example, if System property `TRUSTIFY_DA_DEV_MODE=true`
46-
but environment variable `TRUSTIFY_DA_DEV_MODE=false` , then default trustify-dependency-analytics prod will be used anyway.
40+
* Backend URL Configuration:
41+
* The client can be configured to use custom backend URLs through:
42+
* Command line argument: `--backend-url https://custom.backend.url`
43+
* Environment variable: `TRUSTIFY_DA_BACKEND_URL=https://custom.backend.url`
44+
* Priority order: Command line argument > Environment variable > Default production endpoint
45+
* Default production endpoint: `https://rhda.rhcloud.com`
46+
* For development scenarios, you may use: `https://exhort.stage.devshift.net`
4747

4848
### OpenAPI Specifications
4949

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,12 @@ import java.util.concurrent.CompletableFuture;
142142

143143
public class TrustifyExample {
144144
public static void main(String... args) throws Exception {
145-
// instantiate the Trustify DA Java Client implementation
145+
// instantiate the Trustify DA Java Client implementation (uses default backend URL)
146146
var exhortApi = new ExhortApi();
147147

148+
// alternatively, specify custom backend URL
149+
var customExhortApi = new ExhortApi("https://custom.backend.url");
150+
148151
// get a byte array future holding a html Stack Analysis report
149152
CompletableFuture<byte[]> htmlStackReport = exhortApi.stackAnalysisHtml("/path/to/pom.xml");
150153

@@ -155,7 +158,7 @@ public class TrustifyExample {
155158
// - (json) deserialized Stack Analysis report
156159
// - (html) html Stack Analysis report
157160
CompletableFuture<MixedReport> mixedStackReport = exhortApi.stackAnalysisMixed("/path/to/pom.xml");
158-
161+
159162
// get a AnalysisReport future holding a deserialized Component Analysis report
160163
var manifestContent = Files.readAllBytes(Path.of("/path/to/pom.xml"));
161164
CompletableFuture<AnalysisReport> componentReport = exhortApi.componentAnalysis("/path/to/pom.xml", manifestContent);
@@ -590,25 +593,36 @@ java -jar target/trustify-da-java-client-cli.jar <COMMAND> <FILE_PATH> [OPTIONS]
590593

591594
**Stack Analysis**
592595
```shell
593-
java -jar trustify-da-java-client-cli.jar stack <file_path> [--summary|--html]
596+
java -jar trustify-da-java-client-cli.jar stack <file_path> [--summary|--html] [--backend-url <url>]
594597
```
595598
Perform stack analysis on the specified manifest file.
596599

597600
Options:
598601
- `--summary` - Output summary in JSON format
599602
- `--html` - Output full report in HTML format
603+
- `--backend-url <url>` - Custom backend URL for Trustify Dependency Analytics service
600604
- (default) - Output full report in JSON format
601605

602606
**Component Analysis**
603607
```shell
604-
java -jar trustify-da-java-client-cli.jar component <file_path> [--summary]
608+
java -jar trustify-da-java-client-cli.jar component <file_path> [--summary] [--backend-url <url>]
605609
```
606610
Perform component analysis on the specified manifest file.
607611

608612
Options:
609613
- `--summary` - Output summary in JSON format
614+
- `--backend-url <url>` - Custom backend URL for Trustify Dependency Analytics service
610615
- (default) - Output full report in JSON format
611616

617+
#### Backend Configuration
618+
619+
The client can be configured to use custom backend URLs through:
620+
621+
- **Command line argument**: `--backend-url https://custom.backend.url`
622+
- **Environment variable**: `TRUSTIFY_DA_BACKEND_URL=https://custom.backend.url`
623+
624+
Priority order: Command line argument > Environment variable > Default production endpoint (`https://rhda.rhcloud.com`)
625+
612626
#### Examples
613627

614628
```shell
@@ -621,12 +635,21 @@ java -jar trustify-da-java-client-cli.jar stack /path/to/package.json --summary
621635
# Stack analysis with HTML output
622636
java -jar trustify-da-java-client-cli.jar stack /path/to/build.gradle --html
623637

638+
# Stack analysis with custom backend URL
639+
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml --backend-url https://custom.backend.url
640+
624641
# Component analysis with JSON output (default)
625642
java -jar trustify-da-java-client-cli.jar component /path/to/requirements.txt
626643

627644
# Component analysis with summary
628645
java -jar trustify-da-java-client-cli.jar component /path/to/go.mod --summary
629646

647+
# Component analysis with custom backend URL
648+
java -jar trustify-da-java-client-cli.jar component /path/to/requirements.txt --backend-url https://custom.backend.url
649+
650+
# Using environment variable for backend URL
651+
TRUSTIFY_DA_BACKEND_URL=https://custom.backend.url java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml
652+
630653
# Show help
631654
java -jar trustify-da-java-client-cli.jar --help
632655
```

src/main/java/io/github/guacsec/trustifyda/cli/App.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,27 @@ private static CliArgs parseArgs(String[] args) {
8181
}
8282

8383
Command command = parseCommand(args[0]);
84-
8584
Path path = validateFile(args[1]);
86-
8785
OutputFormat outputFormat = OutputFormat.JSON;
88-
if (args.length == 3) {
89-
outputFormat = parseOutputFormat(command, args[2]);
86+
String backendUrl = null;
87+
88+
for (int i = 2; i < args.length; i++) {
89+
String arg = args[i];
90+
if (arg.startsWith("--backend-url=")) {
91+
backendUrl = arg.substring("--backend-url=".length());
92+
if (backendUrl.isEmpty()) {
93+
throw new IllegalArgumentException("--backend-url requires a value");
94+
}
95+
} else if ("--backend-url".equals(arg) && i + 1 < args.length) {
96+
backendUrl = args[++i];
97+
} else if ("--backend-url".equals(arg)) {
98+
throw new IllegalArgumentException("--backend-url requires a URL argument");
99+
} else {
100+
outputFormat = parseOutputFormat(command, arg);
101+
}
90102
}
91103

92-
return new CliArgs(command, path, outputFormat);
104+
return new CliArgs(command, path, outputFormat, backendUrl);
93105
}
94106

95107
private static Command parseCommand(String commandStr) {
@@ -133,18 +145,19 @@ private static Path validateFile(String filePath) {
133145
private static CompletableFuture<String> executeCommand(CliArgs args) throws IOException {
134146
switch (args.command) {
135147
case STACK:
136-
return executeStackAnalysis(args.filePath.toAbsolutePath().toString(), args.outputFormat);
148+
return executeStackAnalysis(
149+
args.filePath.toAbsolutePath().toString(), args.outputFormat, args.backendUrl);
137150
case COMPONENT:
138151
return executeComponentAnalysis(
139-
args.filePath.toAbsolutePath().toString(), args.outputFormat);
152+
args.filePath.toAbsolutePath().toString(), args.outputFormat, args.backendUrl);
140153
default:
141154
throw new AssertionError();
142155
}
143156
}
144157

145158
private static CompletableFuture<String> executeStackAnalysis(
146-
String filePath, OutputFormat outputFormat) throws IOException {
147-
Api api = new ExhortApi();
159+
String filePath, OutputFormat outputFormat, String backendUrl) throws IOException {
160+
Api api = (backendUrl != null) ? new ExhortApi(backendUrl) : new ExhortApi();
148161
switch (outputFormat) {
149162
case JSON:
150163
return api.stackAnalysis(filePath).thenApply(App::toJsonString);
@@ -160,8 +173,8 @@ private static CompletableFuture<String> executeStackAnalysis(
160173
}
161174

162175
private static CompletableFuture<String> executeComponentAnalysis(
163-
String filePath, OutputFormat outputFormat) throws IOException {
164-
Api api = new ExhortApi();
176+
String filePath, OutputFormat outputFormat, String backendUrl) throws IOException {
177+
Api api = (backendUrl != null) ? new ExhortApi(backendUrl) : new ExhortApi();
165178
CompletableFuture<AnalysisReport> analysis = api.componentAnalysis(filePath);
166179
if (outputFormat.equals(OutputFormat.SUMMARY)) {
167180
var summary = analysis.thenApply(App::extractSummary);

src/main/java/io/github/guacsec/trustifyda/cli/CliArgs.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public class CliArgs {
2222
public final Command command;
2323
public final Path filePath;
2424
public final OutputFormat outputFormat;
25+
public final String backendUrl;
2526

26-
public CliArgs(Command command, Path filePath, OutputFormat outputFormat) {
27+
public CliArgs(Command command, Path filePath, OutputFormat outputFormat, String backendUrl) {
2728
this.command = command;
2829
this.filePath = filePath;
2930
this.outputFormat = outputFormat;
31+
this.backendUrl = backendUrl;
3032
}
3133
}

src/main/java/io/github/guacsec/trustifyda/impl/ExhortApi.java

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import jakarta.mail.internet.MimeMultipart;
3535
import jakarta.mail.util.ByteArrayDataSource;
3636
import java.io.IOException;
37-
import java.io.InputStream;
3837
import java.net.InetSocketAddress;
3938
import java.net.ProxySelector;
4039
import java.net.URI;
@@ -51,7 +50,6 @@
5150
import java.util.Map;
5251
import java.util.Objects;
5352
import java.util.Optional;
54-
import java.util.Properties;
5553
import java.util.Set;
5654
import java.util.UUID;
5755
import java.util.concurrent.CompletableFuture;
@@ -64,18 +62,14 @@
6462
/** Concrete implementation of the Exhort {@link Api} Service. */
6563
public final class ExhortApi implements Api {
6664

67-
private static final String DEV_TRUSTIFY_DA_BACKEND_URL = "DEV_TRUSTIFY_DA_BACKEND_URL";
68-
69-
private static final String TRUSTIFY_DA_DEV_MODE = "TRUSTIFY_DA_DEV_MODE";
70-
7165
private static final String HTTP_VERSION_TRUSTIFY_DA_CLIENT = "HTTP_VERSION_TRUSTIFY_DA_CLIENT";
7266

7367
private static final String TRUSTIFY_DA_PROXY_URL = "TRUSTIFY_DA_PROXY_URL";
7468

7569
private static final Logger LOG = LoggersFactory.getLogger(ExhortApi.class.getName());
7670

77-
public static final String DEFAULT_ENDPOINT = "https://rhda.rhcloud.com";
78-
public static final String DEFAULT_ENDPOINT_DEV = "https://exhort.stage.devshift.net";
71+
private static final String TRUSTIFY_DA_BACKEND_URL = "TRUSTIFY_DA_BACKEND_URL";
72+
private static final String DEFAULT_PROD_ENDPOINT = "https://rhda.rhcloud.com";
7973
public static final String TRUST_DA_TOKEN_HEADER = "trust-da-token";
8074
public static final String TRUST_DA_SOURCE_HEADER = "trust-da-source";
8175
public static final String TRUST_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
@@ -97,7 +91,11 @@ public String getEndpoint() {
9791
private LocalDateTime endTime;
9892

9993
public ExhortApi() {
100-
this(createHttpClient());
94+
this(createHttpClient(), null);
95+
}
96+
97+
public ExhortApi(String backendUrl) {
98+
this(createHttpClient(), backendUrl);
10199
}
102100

103101
/**
@@ -113,40 +111,11 @@ static HttpClient.Version getHttpVersion() {
113111
: HttpClient.Version.HTTP_1_1;
114112
}
115113

116-
ExhortApi(final HttpClient client) {
117-
// // temp system property - as long as prod exhort url not implemented the multi-source v4
118-
// endpoint, this
119-
// property needs to be true
120-
// System.setProperty("TRUSTIFY_DA_DEV_MODE","true");
114+
ExhortApi(final HttpClient client, final String backendUrl) {
121115
commonHookBeginning(true);
122116
this.client = client;
123117
this.mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
124-
// Take default from config.properties in case client didn't override DEV MODE
125-
if (Environment.get(TRUSTIFY_DA_DEV_MODE) == null) {
126-
try {
127-
InputStream exhortConfig =
128-
this.getClass().getClassLoader().getResourceAsStream("config.properties");
129-
if (exhortConfig == null) {
130-
LOG.info(
131-
"config.properties not found on the class path, fallback to default DEV MODE ="
132-
+ " false");
133-
System.setProperty(TRUSTIFY_DA_DEV_MODE, "false");
134-
} else {
135-
Properties properties = new Properties();
136-
properties.load(exhortConfig);
137-
System.setProperty(TRUSTIFY_DA_DEV_MODE, (String) properties.get(TRUSTIFY_DA_DEV_MODE));
138-
}
139-
} catch (IOException e) {
140-
LOG.info(
141-
String.format(
142-
"Error loading config.properties , fallback to set default property DEV MODE ="
143-
+ " false, Error message = %s",
144-
e.getMessage()));
145-
System.setProperty(TRUSTIFY_DA_DEV_MODE, "false");
146-
}
147-
}
148-
149-
this.endpoint = getExhortUrl();
118+
this.endpoint = getExhortUrl(backendUrl);
150119
}
151120

152121
public static HttpClient createHttpClient() {
@@ -192,24 +161,35 @@ private static String getClientRequestId() {
192161
return RequestManager.getInstance().getTraceIdOfRequest();
193162
}
194163

195-
public String getExhortUrl() {
164+
private String getExhortUrl(String backendUrl) {
196165
String endpoint;
197-
if (Environment.getBoolean(TRUSTIFY_DA_DEV_MODE, false)) {
198-
endpoint = Environment.get(DEV_TRUSTIFY_DA_BACKEND_URL, DEFAULT_ENDPOINT_DEV);
199166

167+
if (backendUrl != null && !backendUrl.trim().isEmpty()) {
168+
endpoint = backendUrl.trim();
200169
} else {
201-
endpoint = DEFAULT_ENDPOINT;
170+
endpoint = Environment.get(TRUSTIFY_DA_BACKEND_URL, DEFAULT_PROD_ENDPOINT);
202171
}
172+
203173
if (debugLoggingIsNeeded()) {
174+
String source;
175+
String sourceDetail;
176+
177+
if (backendUrl != null && !backendUrl.trim().isEmpty()) {
178+
source = "CLI parameter";
179+
sourceDetail = String.format("--backend-url=%s", backendUrl.trim());
180+
} else if (Environment.get(TRUSTIFY_DA_BACKEND_URL) != null) {
181+
source = "environment variable";
182+
sourceDetail =
183+
String.format("TRUSTIFY_DA_BACKEND_URL=%s", Environment.get(TRUSTIFY_DA_BACKEND_URL));
184+
} else {
185+
source = "default configuration";
186+
sourceDetail = String.format("DEFAULT_PROD_ENDPOINT=%s", DEFAULT_PROD_ENDPOINT);
187+
}
188+
204189
LOG.info(
205190
String.format(
206-
"TRUSTIFY_DA_DEV_MODE=%s,DEV_TRUSTIFY_DA_BACKEND_URL=%s, Chosen Backend URL=%s ,"
207-
+ " DEFAULT_ENDPOINT_DEV=%s , DEFAULT_ENDPOINT=%s",
208-
Environment.getBoolean(TRUSTIFY_DA_DEV_MODE, false),
209-
Environment.get(DEV_TRUSTIFY_DA_BACKEND_URL, DEFAULT_ENDPOINT_DEV),
210-
endpoint,
211-
DEFAULT_ENDPOINT_DEV,
212-
DEFAULT_ENDPOINT));
191+
"Backend URL configured - Source: %s, Value: %s, Final endpoint: %s",
192+
source, sourceDetail, endpoint));
213193
}
214194
return endpoint;
215195
}

src/main/resources/cli_help.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@ USAGE:
44
java -jar trustify-da-java-client-cli.jar <COMMAND> <FILE_PATH> [OPTIONS]
55

66
COMMANDS:
7-
stack <file_path> [--summary|--html]
7+
stack <file_path> [--summary|--html] [--backend-url <url>]
88
Perform stack analysis on the specified manifest file
99
Options:
1010
--summary Output summary in JSON format
1111
--html Output full report in HTML format
1212
(default) Output full report in JSON format
1313

14-
component <file_path> [--summary]
14+
component <file_path> [--summary] [--backend-url <url>]
1515
Perform component analysis on the specified manifest file
1616
Options:
1717
--summary Output summary in JSON format
1818
(default) Output full report in JSON format
1919

2020
OPTIONS:
21-
-h, --help Show this help message
21+
-h, --help Show this help message
22+
--backend-url <url> Specify custom backend URL
23+
Can also be set via TRUSTIFY_DA_BACKEND_URL environment variable
24+
Defaults to: https://rhda.rhcloud.com
25+
26+
ENVIRONMENT VARIABLES:
27+
TRUSTIFY_DA_BACKEND_URL Backend URL for the Trustify Dependency Analytics service
2228

2329
EXAMPLES:
2430
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml
2531
java -jar trustify-da-java-client-cli.jar stack /path/to/package.json --summary
2632
java -jar trustify-da-java-client-cli.jar stack /path/to/build.gradle --html
2733
java -jar trustify-da-java-client-cli.jar component /path/to/requirements.txt
34+
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml --backend-url https://custom.backend.url
35+
TRUSTIFY_DA_BACKEND_URL=https://custom.backend.url java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml

src/main/resources/config.properties

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)