From 76410d52b12b32ae366140c05283d6dc5778c10d Mon Sep 17 00:00:00 2001 From: Ben Evans Date: Tue, 6 Feb 2024 17:40:32 +0100 Subject: [PATCH] Fix 151 (#24) --- .../com/redhat/insights/agent/AgentMain.java | 25 +++++++++++-------- .../agent/InsightsAgentHttpClient.java | 10 ++++++++ 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/redhat/insights/agent/AgentMain.java b/src/main/java/com/redhat/insights/agent/AgentMain.java index 847d054..ca3ef91 100644 --- a/src/main/java/com/redhat/insights/agent/AgentMain.java +++ b/src/main/java/com/redhat/insights/agent/AgentMain.java @@ -169,21 +169,26 @@ private static boolean shouldLookForCerts(AgentConfiguration config) { private void start() { final InsightsReport report = AgentBasicReport.of(configuration); - Supplier httpClientSupplier; - if (configuration.isDebug() || configuration.isFileOnly()) { - httpClientSupplier = () -> new InsightsFileWritingClient(logger, configuration); - } else { - final PEMSupport pem = new PEMSupport(logger, configuration); - httpClientSupplier = - () -> new InsightsAgentHttpClient(configuration, () -> pem.createTLSContext()); - } + final Supplier clientSupplier = getInsightsClientSupplier(); try { final InsightsReportController controller = - InsightsReportController.of( - logger, configuration, report, httpClientSupplier, waitingJars); + InsightsReportController.of(logger, configuration, report, clientSupplier, waitingJars); controller.generate(); } catch (InsightsException e) { logger.info("Unable to start Red Hat Insights agent: " + e.getMessage()); } } + + private Supplier getInsightsClientSupplier() { + final Supplier out; + if (configuration.isDebug() || configuration.isFileOnly()) { + out = () -> new InsightsFileWritingClient(logger, configuration); + } else if (configuration.isOCP()) { + out = () -> new InsightsAgentHttpClient(configuration); + } else { + final PEMSupport pem = new PEMSupport(logger, configuration); + out = () -> new InsightsAgentHttpClient(configuration, pem::createTLSContext); + } + return out; + } } diff --git a/src/main/java/com/redhat/insights/agent/InsightsAgentHttpClient.java b/src/main/java/com/redhat/insights/agent/InsightsAgentHttpClient.java index fdf654a..c00c1a1 100644 --- a/src/main/java/com/redhat/insights/agent/InsightsAgentHttpClient.java +++ b/src/main/java/com/redhat/insights/agent/InsightsAgentHttpClient.java @@ -41,6 +41,16 @@ public InsightsAgentHttpClient( this.useMTLS = !configuration.getMaybeAuthToken().isPresent(); } + public InsightsAgentHttpClient(InsightsConfiguration configuration) { + this.configuration = configuration; + this.sslContextSupplier = + () -> { + throw new InsightsException( + ERROR_SSL_CREATING_CONTEXT, "Could not create SSL context in Token Auth mode"); + }; + this.useMTLS = false; + } + @Override public void decorate(InsightsReport report) { if (useMTLS) {