Skip to content

Commit a7961da

Browse files
committed
fix: add details of providers to reponse body of ready health check
, add trusted content to healthcheck, and add more required adjustments Signed-off-by: Zvi Grinberg <[email protected]>
1 parent 223e072 commit a7961da

File tree

10 files changed

+98
-111
lines changed

10 files changed

+98
-111
lines changed

src/main/java/com/redhat/exhort/integration/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public final class Constants {
3131

3232
private Constants() {}
3333

34+
public static final String PROVIDER_NAME = "providerName";
3435
public static final String PROVIDERS_PARAM = "providers";
3536

3637
public static final String HEALTH_CHECKS_LIST_HEADER_NAME = "healthChecksRoutesList";

src/main/java/com/redhat/exhort/integration/backend/ExhortIntegration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.redhat.exhort.integration.Constants;
3737
import com.redhat.exhort.integration.backend.sbom.SbomParserFactory;
3838
import com.redhat.exhort.integration.providers.ProviderAggregationStrategy;
39+
import com.redhat.exhort.integration.providers.ProvidersBodyPlusResponseCodeAggregationStrategy;
3940
import com.redhat.exhort.integration.providers.VulnerabilityProvider;
4041
import com.redhat.exhort.integration.trustedcontent.TcResponseAggregation;
4142
import com.redhat.exhort.monitoring.MonitoringProcessor;
@@ -203,6 +204,12 @@ public void configure() {
203204
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(Status.INTERNAL_SERVER_ERROR.getStatusCode()))
204205
.setHeader(Exchange.CONTENT_TYPE, constant(MediaType.TEXT_PLAIN));
205206
//fmt:on
207+
208+
from(direct("exhortHealthCheck"))
209+
.routeId("exhortHealthCheck")
210+
.setProperty(PROVIDERS_PARAM, method(vulnerabilityProvider, "getEnabled"))
211+
.recipientList(header(Constants.HEALTH_CHECKS_LIST_HEADER_NAME))
212+
.aggregationStrategy(new ProvidersBodyPlusResponseCodeAggregationStrategy());
206213
}
207214

208215
private void processAnalysisRequest(Exchange exchange) {

src/main/java/com/redhat/exhort/integration/providers/ProviderHealthCheck.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.stream.Collectors;
2425

2526
import org.apache.camel.builder.ExchangeBuilder;
2627
import org.apache.camel.health.HealthCheckResultBuilder;
@@ -30,9 +31,14 @@
3031

3132
public class ProviderHealthCheck extends AbstractHealthCheck {
3233

34+
private static final List<String> allProvidersHealthChecks =
35+
List.of("direct:snykHealthCheck", "direct:osvNvdHealthCheck", "direct:ossIndexHealthCheck");
36+
public static final String PROVIDER_HTTP_STATUS_CODE_KEY = "StatusCode";
37+
public static final String PROVIDER_IS_ENABLED_KEY = "isEnabled";
38+
public static final String PROVIDER_RESPONSE_BODY_KEY = "responseBody";
39+
3340
public ProviderHealthCheck() {
3441
super("External Providers Readiness Check");
35-
setEnabled(true);
3642
}
3743

3844
@Override
@@ -41,32 +47,36 @@ protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> opti
4147
getCamelContext()
4248
.createProducerTemplate()
4349
.send(
44-
getHealthCheckRoute(),
50+
"direct:exhortHealthCheck",
4551
ExchangeBuilder.anExchange(getCamelContext())
4652
.withHeader(
47-
Constants.HEALTH_CHECKS_LIST_HEADER_NAME, getAllProvidersHealthChecks())
53+
Constants.HEALTH_CHECKS_LIST_HEADER_NAME, this.allProvidersHealthChecks)
4854
.build());
4955

50-
List<Map<Integer, String>> httpResponseBodiesAndStatuses =
51-
(List<Map<Integer, String>>) response.getMessage().getBody();
56+
List<Map<String, Map<String, String>>> httpResponseBodiesAndStatuses =
57+
(List<Map<String, Map<String, String>>>) response.getMessage().getBody();
58+
Map<String, Object> providers =
59+
httpResponseBodiesAndStatuses.stream()
60+
.map(Map::entrySet)
61+
.flatMap(Collection::stream)
62+
.collect(
63+
Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue(), (a, b) -> a));
64+
builder.details(providers);
65+
5266
if (httpResponseBodiesAndStatuses.stream()
53-
.map(entry -> entry.keySet())
67+
.map(Map::values)
5468
.flatMap(Collection::stream)
55-
.anyMatch(status -> status < 400)) {
69+
.anyMatch(
70+
providerDetails ->
71+
Integer.valueOf(providerDetails.get(PROVIDER_HTTP_STATUS_CODE_KEY)) < 400
72+
&& Boolean.parseBoolean(providerDetails.get(PROVIDER_IS_ENABLED_KEY)))) {
5673
builder.up();
74+
5775
} else {
5876
builder.down();
5977
}
6078
}
6179

62-
private List<String> getAllProvidersHealthChecks() {
63-
return List.of("direct:snykHealthCheck", "direct:osvNvdHealthCheck");
64-
}
65-
66-
private String getHealthCheckRoute() {
67-
return "direct:exhortHealthCheck";
68-
}
69-
7080
@Override
7181
public boolean isLiveness() {
7282
return false;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 Red Hat, Inc. and/or its affiliates
3+
* and other contributors as indicated by the @author tags.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.redhat.exhort.integration.providers;
20+
21+
import static com.redhat.exhort.integration.providers.ProviderHealthCheck.*;
22+
23+
import java.util.HashMap;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import org.apache.camel.Exchange;
28+
import org.apache.camel.processor.aggregate.AbstractListAggregationStrategy;
29+
30+
import com.redhat.exhort.integration.Constants;
31+
32+
public class ProvidersBodyPlusResponseCodeAggregationStrategy
33+
extends AbstractListAggregationStrategy<Map<String, Map<String, String>>> {
34+
@Override
35+
public Map<String, Map<String, String>> getValue(Exchange exchange) {
36+
Map<String, Map<String, String>> result = new HashMap<>();
37+
Map<String, String> providerValues = new HashMap<>();
38+
providerValues.put(PROVIDER_RESPONSE_BODY_KEY, exchange.getMessage().getBody(String.class));
39+
providerValues.put(
40+
PROVIDER_HTTP_STATUS_CODE_KEY,
41+
exchange.getMessage().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class).toString());
42+
List<String> enabledProviders = exchange.getProperty(Constants.PROVIDERS_PARAM, List.class);
43+
if (enabledProviders.contains(exchange.getProperty(Constants.PROVIDER_NAME, String.class))) {
44+
providerValues.put(PROVIDER_IS_ENABLED_KEY, "true");
45+
} else {
46+
providerValues.put(PROVIDER_IS_ENABLED_KEY, "false");
47+
}
48+
result.put(exchange.getProperty(Constants.PROVIDER_NAME, String.class), providerValues);
49+
return result;
50+
}
51+
}

src/main/java/com/redhat/exhort/integration/providers/ProvidersBodyPlusResponseCodeStrategy.java

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

src/main/java/com/redhat/exhort/integration/providers/ProvidersIntegration.java

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

src/main/java/com/redhat/exhort/integration/providers/ProvidersResponseCodeStrategy.java

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

src/main/java/com/redhat/exhort/integration/providers/ossindex/OssIndexIntegration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ public void configure() {
8282
.onFallback()
8383
.process(responseHandler::processResponseError);
8484

85+
from(direct("ossIndexHealthCheck"))
86+
.routeId("ossIndexHealthCheck")
87+
.setProperty(Constants.PROVIDER_NAME, constant(Constants.OSS_INDEX_PROVIDER))
88+
.setHeader(Constants.OSS_INDEX_TOKEN_HEADER,constant(" "))
89+
.to(direct("ossValidateCredentials"));
90+
8591
from(direct("ossValidateCredentials"))
8692
.routeId("ossValidateCredentials")
8793
.circuitBreaker()

src/main/java/com/redhat/exhort/integration/providers/osvnvd/OsvNvdIntegration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package com.redhat.exhort.integration.providers.osvnvd;
2020

2121
import org.apache.camel.Exchange;
22+
import org.apache.camel.builder.ExpressionBuilder;
2223
import org.apache.camel.builder.endpoint.EndpointRouteBuilder;
2324
import org.eclipse.microprofile.config.inject.ConfigProperty;
2425

@@ -62,6 +63,7 @@ public void configure() throws Exception {
6263

6364
from(direct("osvNvdHealthCheck"))
6465
.routeId("osvNvdHealthCheck")
66+
.setProperty(Constants.PROVIDER_NAME, constant(Constants.OSV_NVD_PROVIDER))
6567
.process(this::processHealthRequest)
6668
.circuitBreaker()
6769
.faultToleranceConfiguration()
@@ -70,7 +72,9 @@ public void configure() throws Exception {
7072
.end()
7173
.to(vertxHttp("{{api.osvnvd.management.host}}"))
7274
.onFallback()
73-
.process(responseHandler::processTokenFallBack);
75+
.setBody(ExpressionBuilder.bodyExpression("osvNvd Service is down"))
76+
.setHeader(Exchange.HTTP_RESPONSE_CODE,constant("503"));
77+
7478
// fmt:on
7579
}
7680

src/main/java/com/redhat/exhort/integration/providers/snyk/SnykIntegration.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ public void configure() {
9797
.routeId("snykValidateToken")
9898
.process(this::processTokenValidation)
9999
.to(direct("snykTokenRequest"));
100+
100101
from(direct("snykHealthCheck"))
101102
.routeId("snykHealthCheck")
103+
.setProperty(Constants.PROVIDER_NAME, constant(Constants.SNYK_PROVIDER))
102104
.process(this::setAuthToken)
103105
.to(direct("snykTokenRequest"));
106+
104107
from(direct("snykTokenRequest"))
105108
.routeId("snykTokenRequest")
106109
.process(this::processTokenRequest)

0 commit comments

Comments
 (0)