-
Notifications
You must be signed in to change notification settings - Fork 20
feat: add readiness health check for external services #293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zvigrinberg
merged 17 commits into
guacsec:main
from
zvigrinberg:feature/rediness-probe-external-services
Mar 6, 2024
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
11ce559
feat: add readiness health check probes external services
zvigrinberg 341b7bc
fix: improve and fix couple of issues
zvigrinberg fe1b2bb
fix: switch to health checks in snyk and osvNvd providers
zvigrinberg 5cfcc66
fix: make the restClient of all services a singleton
zvigrinberg 54060c6
fix: transform RestClient to singleton provided by CDI context
zvigrinberg 674787e
docs: add code documentation
zvigrinberg 4190cf7
fix: adjust readiness parameters
zvigrinberg a7bf56c
fix: change readiness health check to use camel routes
zvigrinberg 5982a5b
fix: add details of providers to reponse body of ready health check
zvigrinberg e75168f
fix: use POJO instead of map for providers details
zvigrinberg cccf2a4
fix: prevent null pointer exception
zvigrinberg c8099f6
fix: use /version endpoint for oss-index service
zvigrinberg 4bf0a28
refactor: remove 3 unused constants
zvigrinberg e5d294f
fix: apply suggestions from code review
zvigrinberg 8ea71fc
fix: final improvements and refinements for exhort readiness health c…
zvigrinberg 2473511
refactor: fix styling using spotless
zvigrinberg ce64267
fix: import Constants correctly
zvigrinberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/main/java/com/redhat/exhort/integration/providers/ProviderHealthCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.redhat.exhort.integration.providers; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import org.apache.camel.builder.ExchangeBuilder; | ||
| import org.apache.camel.health.HealthCheckResultBuilder; | ||
| import org.apache.camel.impl.health.AbstractHealthCheck; | ||
|
|
||
| import com.redhat.exhort.api.v4.ProviderStatus; | ||
| import com.redhat.exhort.integration.Constants; | ||
|
|
||
| public class ProviderHealthCheck extends AbstractHealthCheck { | ||
|
|
||
| private static final List<String> ALL_PROVIDERS_HEALTH_CHECKS = | ||
| List.of("direct:snykHealthCheck", "direct:osvNvdHealthCheck", "direct:ossIndexHealthCheck"); | ||
|
|
||
| public ProviderHealthCheck() { | ||
| super("External Providers Readiness Check"); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doCall(HealthCheckResultBuilder builder, Map<String, Object> options) { | ||
| var response = | ||
| getCamelContext() | ||
| .createProducerTemplate() | ||
| .send( | ||
| "direct:exhortHealthCheck", | ||
| ExchangeBuilder.anExchange(getCamelContext()) | ||
| .withHeader( | ||
| Constants.HEALTH_CHECKS_LIST_HEADER_NAME, this.ALL_PROVIDERS_HEALTH_CHECKS) | ||
| .build()); | ||
|
|
||
| List<ProviderStatus> httpResponseBodiesAndStatuses = | ||
| (List<ProviderStatus>) response.getMessage().getBody(); | ||
| Map<String, Object> providers = | ||
| httpResponseBodiesAndStatuses.stream() | ||
| .collect( | ||
| Collectors.toMap( | ||
| provider -> provider.getName(), | ||
| provider -> formatProviderStatus(provider), | ||
| (a, b) -> a)); | ||
| builder.details(providers); | ||
|
|
||
| if (httpResponseBodiesAndStatuses.stream() | ||
| .filter(providerStatus -> Objects.nonNull(providerStatus.getCode())) | ||
| .anyMatch(providerDetails -> providerDetails.getCode() < 400 && providerDetails.getOk())) { | ||
| builder.up(); | ||
|
|
||
| } else { | ||
| builder.down(); | ||
| } | ||
| } | ||
|
|
||
| private static String formatProviderStatus(ProviderStatus provider) { | ||
| if (Objects.nonNull(provider.getCode())) { | ||
| return String.format( | ||
| "providerName=%s, isEnabled=%s, statusCode=%s, message=%s", | ||
| provider.getName(), provider.getOk(), provider.getCode(), provider.getMessage()); | ||
| } else { | ||
| return String.format( | ||
| "providerName=%s, isEnabled=%s, message=%s", | ||
| provider.getName(), provider.getOk(), provider.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLiveness() { | ||
| return false; | ||
| } | ||
| } | ||
77 changes: 77 additions & 0 deletions
77
...redhat/exhort/integration/providers/ProvidersBodyPlusResponseCodeAggregationStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2024 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.redhat.exhort.integration.providers; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| import org.apache.camel.Exchange; | ||
| import org.apache.camel.Message; | ||
| import org.apache.camel.processor.aggregate.AbstractListAggregationStrategy; | ||
|
|
||
| import com.redhat.exhort.api.v4.ProviderStatus; | ||
| import com.redhat.exhort.integration.Constants; | ||
|
|
||
| import jakarta.ws.rs.core.Response; | ||
|
|
||
| public class ProvidersBodyPlusResponseCodeAggregationStrategy | ||
| extends AbstractListAggregationStrategy<ProviderStatus> { | ||
| @Override | ||
| public ProviderStatus getValue(Exchange exchange) { | ||
| ProviderStatus providerValues = new ProviderStatus(); | ||
| providerValues.setMessage(getHttpResponseBodyFromMessage(exchange.getMessage())); | ||
| Integer statusCode = Integer.valueOf(getHttpResponseStatusFromMessage(exchange.getMessage())); | ||
| if (!serviceExcludedFromReadinessCheck(exchange)) { | ||
| providerValues.setCode(statusCode); | ||
| } | ||
| providerValues.setOk(!serviceExcludedFromReadinessCheck(exchange)); | ||
| String providerName = exchange.getProperty(Constants.PROVIDER_NAME, String.class); | ||
| providerValues.setName(providerName); | ||
|
|
||
| return providerValues; | ||
| } | ||
|
|
||
| private static Boolean serviceExcludedFromReadinessCheck(Exchange exchange) { | ||
| return Objects.requireNonNullElse( | ||
| exchange.getProperty(Constants.EXCLUDE_FROM_READINESS_CHECK, Boolean.class), false); | ||
| } | ||
|
|
||
| private static String getHttpResponseStatusFromMessage(Message message) { | ||
| if (message.getHeader(Exchange.HTTP_RESPONSE_CODE) instanceof Integer) { | ||
| return message.getHeader(Exchange.HTTP_RESPONSE_CODE).toString(); | ||
| } else { | ||
| return String.valueOf( | ||
| message.getHeader(Exchange.HTTP_RESPONSE_CODE, Response.Status.class).getStatusCode()); | ||
| } | ||
| } | ||
|
|
||
| private static String getHttpResponseBodyFromMessage(Message message) { | ||
| if (Objects.nonNull(message.getHeader(Exchange.HTTP_RESPONSE_TEXT, String.class))) { | ||
| return message.getHeader(Exchange.HTTP_RESPONSE_TEXT, String.class); | ||
| } else { | ||
| if (Objects.nonNull(message.getBody(String.class)) | ||
| && message.getBody(String.class) instanceof String) { | ||
| return message.getBody(String.class); | ||
| } else { | ||
| return Objects.requireNonNull( | ||
| message.getHeader(Exchange.HTTP_RESPONSE_CODE, String.class), | ||
| message.getBody(String.class)); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.