Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@

### Good to know

* You can override the default backend url by setting the `TRUSTIFY_DA_DEV_MODE` environment variable/system property to true:
* In case environment variable/System Property `TRUSTIFY_DA_DEV_MODE=true` - You can Override the default trustify-dependency-analytics backend by setting
`DEV_TRUSTIFY_DA_BACKEND_URL` env variable/system property to the desired trustify-dependency-analytics backend instance address ( useful for tests).
* 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.
* 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`.
* Environment variables takes precedence over System properties - for example, if System property `TRUSTIFY_DA_DEV_MODE=true`
but environment variable `TRUSTIFY_DA_DEV_MODE=false` , then default trustify-dependency-analytics prod will be used anyway.
* Backend URL Configuration:
* The client requires the backend URL to be configured through environment variable: `TRUSTIFY_DA_BACKEND_URL=https://backend.url` (required)
* The application will fail to start if this environment variable is not set

### OpenAPI Specifications

Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public class TrustifyExample {
// - (json) deserialized Stack Analysis report
// - (html) html Stack Analysis report
CompletableFuture<MixedReport> mixedStackReport = exhortApi.stackAnalysisMixed("/path/to/pom.xml");

// get a AnalysisReport future holding a deserialized Component Analysis report
var manifestContent = Files.readAllBytes(Path.of("/path/to/pom.xml"));
CompletableFuture<AnalysisReport> componentReport = exhortApi.componentAnalysis("/path/to/pom.xml", manifestContent);
Expand Down Expand Up @@ -609,9 +609,19 @@ Options:
- `--summary` - Output summary in JSON format
- (default) - Output full report in JSON format

#### Backend Configuration

The client requires the backend URL to be configured through the environment variable:

- **Environment variable**: `TRUSTIFY_DA_BACKEND_URL=https://backend.url` (required)

The application will fail to start if this environment variable is not set.

#### Examples

```shell
export TRUSTIFY_DA_BACKEND_URL=https://your-backend.url

# Stack analysis with JSON output (default)
java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml

Expand Down
62 changes: 11 additions & 51 deletions src/main/java/io/github/guacsec/trustifyda/impl/ExhortApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import jakarta.mail.internet.MimeMultipart;
import jakarta.mail.util.ByteArrayDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
Expand All @@ -51,7 +50,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -64,18 +62,13 @@
/** Concrete implementation of the Exhort {@link Api} Service. */
public final class ExhortApi implements Api {

private static final String DEV_TRUSTIFY_DA_BACKEND_URL = "DEV_TRUSTIFY_DA_BACKEND_URL";

private static final String TRUSTIFY_DA_DEV_MODE = "TRUSTIFY_DA_DEV_MODE";

private static final String HTTP_VERSION_TRUSTIFY_DA_CLIENT = "HTTP_VERSION_TRUSTIFY_DA_CLIENT";

private static final String TRUSTIFY_DA_PROXY_URL = "TRUSTIFY_DA_PROXY_URL";

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

public static final String DEFAULT_ENDPOINT = "https://rhda.rhcloud.com";
public static final String DEFAULT_ENDPOINT_DEV = "https://exhort.stage.devshift.net";
private static final String TRUSTIFY_DA_BACKEND_URL = "TRUSTIFY_DA_BACKEND_URL";
public static final String TRUST_DA_TOKEN_HEADER = "trust-da-token";
public static final String TRUST_DA_SOURCE_HEADER = "trust-da-source";
public static final String TRUST_DA_OPERATION_TYPE_HEADER = "trust-da-operation-type";
Expand Down Expand Up @@ -114,38 +107,9 @@ static HttpClient.Version getHttpVersion() {
}

ExhortApi(final HttpClient client) {
// // temp system property - as long as prod exhort url not implemented the multi-source v4
// endpoint, this
// property needs to be true
// System.setProperty("TRUSTIFY_DA_DEV_MODE","true");
commonHookBeginning(true);
this.client = client;
this.mapper = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
// Take default from config.properties in case client didn't override DEV MODE
if (Environment.get(TRUSTIFY_DA_DEV_MODE) == null) {
try {
InputStream exhortConfig =
this.getClass().getClassLoader().getResourceAsStream("config.properties");
if (exhortConfig == null) {
LOG.info(
"config.properties not found on the class path, fallback to default DEV MODE ="
+ " false");
System.setProperty(TRUSTIFY_DA_DEV_MODE, "false");
} else {
Properties properties = new Properties();
properties.load(exhortConfig);
System.setProperty(TRUSTIFY_DA_DEV_MODE, (String) properties.get(TRUSTIFY_DA_DEV_MODE));
}
} catch (IOException e) {
LOG.info(
String.format(
"Error loading config.properties , fallback to set default property DEV MODE ="
+ " false, Error message = %s",
e.getMessage()));
System.setProperty(TRUSTIFY_DA_DEV_MODE, "false");
}
}

this.endpoint = getExhortUrl();
}

Expand Down Expand Up @@ -192,24 +156,20 @@ private static String getClientRequestId() {
return RequestManager.getInstance().getTraceIdOfRequest();
}

public String getExhortUrl() {
String endpoint;
if (Environment.getBoolean(TRUSTIFY_DA_DEV_MODE, false)) {
endpoint = Environment.get(DEV_TRUSTIFY_DA_BACKEND_URL, DEFAULT_ENDPOINT_DEV);

} else {
endpoint = DEFAULT_ENDPOINT;
private String getExhortUrl() {
String endpoint = Environment.get(TRUSTIFY_DA_BACKEND_URL);
if (endpoint == null || endpoint.trim().isEmpty()) {
throw new IllegalStateException(
"Backend URL not configured. Please set the TRUSTIFY_DA_BACKEND_URL environment"
+ " variable.");
}
endpoint = endpoint.trim();

if (debugLoggingIsNeeded()) {
LOG.info(
String.format(
"TRUSTIFY_DA_DEV_MODE=%s,DEV_TRUSTIFY_DA_BACKEND_URL=%s, Chosen Backend URL=%s ,"
+ " DEFAULT_ENDPOINT_DEV=%s , DEFAULT_ENDPOINT=%s",
Environment.getBoolean(TRUSTIFY_DA_DEV_MODE, false),
Environment.get(DEV_TRUSTIFY_DA_BACKEND_URL, DEFAULT_ENDPOINT_DEV),
endpoint,
DEFAULT_ENDPOINT_DEV,
DEFAULT_ENDPOINT));
"Backend URL configured - TRUSTIFY_DA_BACKEND_URL=%s",
Environment.get(TRUSTIFY_DA_BACKEND_URL)));
}
return endpoint;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/cli_help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ COMMANDS:
OPTIONS:
-h, --help Show this help message

ENVIRONMENT VARIABLES:
TRUSTIFY_DA_BACKEND_URL Backend URL for the Trustify Dependency Analytics service (required)

EXAMPLES:
export TRUSTIFY_DA_BACKEND_URL=https://your-backend.url

java -jar trustify-da-java-client-cli.jar stack /path/to/pom.xml
java -jar trustify-da-java-client-cli.jar stack /path/to/package.json --summary
java -jar trustify-da-java-client-cli.jar stack /path/to/build.gradle --html
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/config.properties

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
@Tag("IntegrationTest")
@ExtendWith(HelperExtension.class)
@ExtendWith(MockitoExtension.class)
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://rhda.rhcloud.com")
@SetSystemProperty(key = "TRUST_DA_SOURCE", value = "trustify-da-java-client-it")
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
@RestoreSystemProperties
// TODO: Re-enable this integration test when https://issues.redhat.com/browse/TC-3192 is resolved
// The test is currently disabled due to backend service changes that prevent successful connections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,8 @@
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
@ClearSystemProperty(key = "TRUSTIFY_DA_DEV_MODE")
@ClearSystemProperty(key = "TRUSTIFY_DA_PROXY_URL")
@ClearSystemProperty(key = "DEV_TRUSTIFY_DA_BACKEND_URL")
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://test.backend.url")
@ClearSystemProperty(key = "TRUST_DA_TOKEN")
@ClearSystemProperty(key = "TRUST_DA_SOURCE")
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -412,41 +411,22 @@ void componentAnalysis_with_pom_xml_as_path_should_return_json_object_from_the_b
}

@Test
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "true")
@ClearSystemProperty(key = "DEV_TRUSTIFY_DA_BACKEND_URL")
@RestoreSystemProperties
void check_TRUSTIFY_DA_Url_When_DEV_Mode_true_And_DEV_TRUSTIFY_DA_Url_Set() {
String dummyUrl = "http://dummy-url";
System.setProperty("DEV_TRUSTIFY_DA_BACKEND_URL", dummyUrl);
ExhortApi exhortApi = new ExhortApi();
then(exhortApi.getEndpoint()).isEqualTo(dummyUrl);
}

@Test
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
void check_TRUSTIFY_DA_Url_When_DEV_Mode_false_And_DEV_TRUSTIFY_DA_Url_Set() {
ExhortApi exhortApi = new ExhortApi();
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
}

@Test
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "true")
void check_TRUSTIFY_DA_Url_When_DEV_Mode_true_Dev_TRUSTIFY_DA_URL_Selected() {
ExhortApi exhortApi = new ExhortApi();
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT_DEV);
}

@Test
@SetSystemProperty(key = "TRUSTIFY_DA_DEV_MODE", value = "false")
void check_TRUSTIFY_DA_Url_When_DEV_Mode_not_set_Then_Default_TRUSTIFY_DA_URL_Selected() {
ExhortApi exhortApi = new ExhortApi();
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
@ClearSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL")
void check_TRUSTIFY_DA_Url_Throws_Exception_When_Not_Set() {
IllegalStateException exception =
org.junit.jupiter.api.Assertions.assertThrows(
IllegalStateException.class, () -> new ExhortApi());
then(exception.getMessage())
.isEqualTo(
"Backend URL not configured. Please set the TRUSTIFY_DA_BACKEND_URL environment"
+ " variable.");
}

@Test
void check_TRUSTIFY_DA_Url_When_Nothing_Set_Then_Default_TRUSTIFY_DA_URL_Selected() {
@SetSystemProperty(key = "TRUSTIFY_DA_BACKEND_URL", value = "https://custom.test.url")
void check_TRUSTIFY_DA_Url_When_Environment_Variable_Set() {
ExhortApi exhortApi = new ExhortApi();
then(exhortApi.getEndpoint()).isEqualTo(ExhortApi.DEFAULT_ENDPOINT);
then(exhortApi.getEndpoint()).isEqualTo("https://custom.test.url");
}

@Test
Expand Down
Loading