Skip to content

Commit 2890a64

Browse files
authored
chore: remove exhort_dev_mode and default endpoints and use value from environment variables or arguments (#211)
## Description chore: remove exhort_dev_mode and default endpoints and use value from environment variables or arguments **Related issue (if any):** fixes #210 ## Checklist - [x] I have followed this repository's contributing guidelines. - [x] I will adhere to the project's code of conduct.
1 parent 984fd27 commit 2890a64

7 files changed

Lines changed: 44 additions & 94 deletions

File tree

CONTRIBUTING.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@
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 requires the backend URL to be configured through environment variable: `TRUSTIFY_DA_BACKEND_URL=https://backend.url` (required)
42+
* The application will fail to start if this environment variable is not set
4743

4844
### OpenAPI Specifications
4945

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class TrustifyExample {
155155
// - (json) deserialized Stack Analysis report
156156
// - (html) html Stack Analysis report
157157
CompletableFuture<MixedReport> mixedStackReport = exhortApi.stackAnalysisMixed("/path/to/pom.xml");
158-
158+
159159
// get a AnalysisReport future holding a deserialized Component Analysis report
160160
var manifestContent = Files.readAllBytes(Path.of("/path/to/pom.xml"));
161161
CompletableFuture<AnalysisReport> componentReport = exhortApi.componentAnalysis("/path/to/pom.xml", manifestContent);
@@ -609,9 +609,19 @@ Options:
609609
- `--summary` - Output summary in JSON format
610610
- (default) - Output full report in JSON format
611611

612+
#### Backend Configuration
613+
614+
The client requires the backend URL to be configured through the environment variable:
615+
616+
- **Environment variable**: `TRUSTIFY_DA_BACKEND_URL=https://backend.url` (required)
617+
618+
The application will fail to start if this environment variable is not set.
619+
612620
#### Examples
613621

614622
```shell
623+
export TRUSTIFY_DA_BACKEND_URL=https://your-backend.url
624+
615625
# Stack analysis with JSON output (default)
616626
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml
617627

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

Lines changed: 11 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,13 @@
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";
7972
public static final String TRUST_DA_TOKEN_HEADER = "trust-da-token";
8073
public static final String TRUST_DA_SOURCE_HEADER = "trust-da-source";
8174
public static final String TRUST_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
@@ -114,38 +107,9 @@ static HttpClient.Version getHttpVersion() {
114107
}
115108

116109
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");
121110
commonHookBeginning(true);
122111
this.client = client;
123112
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-
149113
this.endpoint = getExhortUrl();
150114
}
151115

@@ -192,24 +156,20 @@ private static String getClientRequestId() {
192156
return RequestManager.getInstance().getTraceIdOfRequest();
193157
}
194158

195-
public String getExhortUrl() {
196-
String endpoint;
197-
if (Environment.getBoolean(TRUSTIFY_DA_DEV_MODE, false)) {
198-
endpoint = Environment.get(DEV_TRUSTIFY_DA_BACKEND_URL, DEFAULT_ENDPOINT_DEV);
199-
200-
} else {
201-
endpoint = DEFAULT_ENDPOINT;
159+
private String getExhortUrl() {
160+
String endpoint = Environment.get(TRUSTIFY_DA_BACKEND_URL);
161+
if (endpoint == null || endpoint.trim().isEmpty()) {
162+
throw new IllegalStateException(
163+
"Backend URL not configured. Please set the TRUSTIFY_DA_BACKEND_URL environment"
164+
+ " variable.");
202165
}
166+
endpoint = endpoint.trim();
167+
203168
if (debugLoggingIsNeeded()) {
204169
LOG.info(
205170
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));
171+
"Backend URL configured - TRUSTIFY_DA_BACKEND_URL=%s",
172+
Environment.get(TRUSTIFY_DA_BACKEND_URL)));
213173
}
214174
return endpoint;
215175
}

src/main/resources/cli_help.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ COMMANDS:
2020
OPTIONS:
2121
-h, --help Show this help message
2222

23+
ENVIRONMENT VARIABLES:
24+
TRUSTIFY_DA_BACKEND_URL Backend URL for the Trustify Dependency Analytics service (required)
25+
2326
EXAMPLES:
27+
export TRUSTIFY_DA_BACKEND_URL=https://your-backend.url
28+
2429
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml
2530
java -jar trustify-da-java-client-cli.jar stack /path/to/package.json --summary
2631
java -jar trustify-da-java-client-cli.jar stack /path/to/build.gradle --html

src/main/resources/config.properties

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

src/test/java/io/github/guacsec/trustifyda/impl/ExhortApiIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
@Tag("IntegrationTest")
8080
@ExtendWith(HelperExtension.class)
8181
@ExtendWith(MockitoExtension.class)
82+
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://rhda.rhcloud.com")
8283
@SetSystemProperty(key = "TRUST_DA_SOURCE", value = "trustify-da-java-client-it")
83-
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
8484
@RestoreSystemProperties
8585
// TODO: Re-enable this integration test when https://issues.redhat.com/browse/TC-3192 is resolved
8686
// The test is currently disabled due to backend service changes that prevent successful connections

src/test/java/io/github/guacsec/trustifyda/impl/Exhort_Api_Test.java

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@
7979
import org.mockito.junit.jupiter.MockitoExtension;
8080

8181
@ExtendWith(MockitoExtension.class)
82-
@ClearSystemProperty(key = "TRUSTIFY_DA_DEV_MODE")
8382
@ClearSystemProperty(key = "TRUSTIFY_DA_PROXY_URL")
84-
@ClearSystemProperty(key = "DEV_TRUSTIFY_DA_BACKEND_URL")
83+
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://test.backend.url")
8584
@ClearSystemProperty(key = "TRUST_DA_TOKEN")
8685
@ClearSystemProperty(key = "TRUST_DA_SOURCE")
8786
@SuppressWarnings("unchecked")
@@ -412,41 +411,22 @@ void componentAnalysis_with_pom_xml_as_path_should_return_json_object_from_the_b
412411
}
413412

414413
@Test
415-
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "true")
416-
@ClearSystemProperty(key = "DEV_TRUSTIFY_DA_BACKEND_URL")
417-
@RestoreSystemProperties
418-
void check_TRUSTIFY_DA_Url_When_DEV_Mode_true_And_DEV_TRUSTIFY_DA_Url_Set() {
419-
String dummyUrl = "http://dummy-url";
420-
System.setProperty("DEV_TRUSTIFY_DA_BACKEND_URL", dummyUrl);
421-
ExhortApi exhortApi = new ExhortApi();
422-
then(exhortApi.getEndpoint()).isEqualTo(dummyUrl);
423-
}
424-
425-
@Test
426-
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
427-
void check_TRUSTIFY_DA_Url_When_DEV_Mode_false_And_DEV_TRUSTIFY_DA_Url_Set() {
428-
ExhortApi exhortApi = new ExhortApi();
429-
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
430-
}
431-
432-
@Test
433-
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "true")
434-
void check_TRUSTIFY_DA_Url_When_DEV_Mode_true_Dev_TRUSTIFY_DA_URL_Selected() {
435-
ExhortApi exhortApi = new ExhortApi();
436-
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT_DEV);
437-
}
438-
439-
@Test
440-
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
441-
void check_TRUSTIFY_DA_Url_When_DEV_Mode_not_set_Then_Default_TRUSTIFY_DA_URL_Selected() {
442-
ExhortApi exhortApi = new ExhortApi();
443-
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
414+
@ClearSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL")
415+
void check_TRUSTIFY_DA_Url_Throws_Exception_When_Not_Set() {
416+
IllegalStateException exception =
417+
org.junit.jupiter.api.Assertions.assertThrows(
418+
IllegalStateException.class, () -> new ExhortApi());
419+
then(exception.getMessage())
420+
.isEqualTo(
421+
"Backend URL not configured. Please set the TRUSTIFY_DA_BACKEND_URL environment"
422+
+ " variable.");
444423
}
445424

446425
@Test
447-
void check_TRUSTIFY_DA_Url_When_Nothing_Set_Then_Default_TRUSTIFY_DA_URL_Selected() {
426+
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://custom.test.url")
427+
void check_TRUSTIFY_DA_Url_When_Environment_Variable_Set() {
448428
ExhortApi exhortApi = new ExhortApi();
449-
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
429+
then(exhortApi.getEndpoint()).isEqualTo("https://custom.test.url");
450430
}
451431

452432
@Test

0 commit comments

Comments
 (0)