From f1debd2750c69587ec9c29215633d755c0c0f5e6 Mon Sep 17 00:00:00 2001
From: Ben Evans <benjamin.john.evans@gmail.com>
Date: Wed, 31 Jul 2024 09:58:11 +0200
Subject: [PATCH] fix: MWTELE 266 upload directory creation  (#248)

* EOD

* Prepare to fix conflicting branches

* Add more belt-and-braces
---
 .../com/redhat/insights/InsightsReportController.java     | 8 +++++---
 .../config/EnvAndSysPropsInsightsConfiguration.java       | 6 +++++-
 .../com/redhat/insights/config/InsightsConfiguration.java | 6 +++++-
 .../redhat/insights/http/InsightsFileWritingClient.java   | 7 +++++--
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/api/src/main/java/com/redhat/insights/InsightsReportController.java b/api/src/main/java/com/redhat/insights/InsightsReportController.java
index ba3a3865..381b9c1f 100644
--- a/api/src/main/java/com/redhat/insights/InsightsReportController.java
+++ b/api/src/main/java/com/redhat/insights/InsightsReportController.java
@@ -1,4 +1,4 @@
-/* Copyright (C) Red Hat 2022-2023 */
+/* Copyright (C) Red Hat 2022-2024 */
 package com.redhat.insights;
 
 import static com.redhat.insights.InsightsErrorCode.ERROR_GENERATING_HASH;
@@ -103,11 +103,12 @@ public static InsightsReportController of(
   /** Generates the report (including subreports), computes identifying hash and schedules sends */
   public void generate() {
     try {
-
       if (configuration.isOptingOut()) {
         throw new InsightsException(OPT_OUT, "Opting out of the Red Hat Insights client");
       }
-      final InsightsReport updateReport = new UpdateReportImpl(jarsToSend, logger);
+      if (System.getProperty("os.name").toLowerCase().contains("windows")) {
+        throw new InsightsException(OPT_OUT, "Red Hat Insights is not supported on Windows.");
+      }
 
       // Schedule initial event
       Runnable sendConnect =
@@ -131,6 +132,7 @@ public void generate() {
       scheduler.scheduleConnect(sendConnect);
 
       // Schedule a possible Jar send (every few mins? Defaults to 5 min)
+      final InsightsReport updateReport = new UpdateReportImpl(jarsToSend, logger);
       Runnable sendNewJarsIfAny =
           () -> {
             InsightsHttpClient httpClient = httpClientSupplier.get();
diff --git a/api/src/main/java/com/redhat/insights/config/EnvAndSysPropsInsightsConfiguration.java b/api/src/main/java/com/redhat/insights/config/EnvAndSysPropsInsightsConfiguration.java
index 4e828a85..5689d243 100644
--- a/api/src/main/java/com/redhat/insights/config/EnvAndSysPropsInsightsConfiguration.java
+++ b/api/src/main/java/com/redhat/insights/config/EnvAndSysPropsInsightsConfiguration.java
@@ -1,4 +1,4 @@
-/* Copyright (C) Red Hat 2023 */
+/* Copyright (C) Red Hat 2023-2024 */
 package com.redhat.insights.config;
 
 import static com.redhat.insights.InsightsErrorCode.ERROR_IDENTIFICATION_NOT_DEFINED;
@@ -123,6 +123,10 @@ public Optional<ProxyConfiguration> getProxyConfiguration() {
 
   @Override
   public boolean isOptingOut() {
+    String osName = System.getProperty("os.name");
+    if (osName != null && osName.trim().toLowerCase().contains("windows")) {
+      return true;
+    }
     String value = lookup(ENV_OPT_OUT);
     if (value != null) {
       return "true".equalsIgnoreCase(value.trim());
diff --git a/api/src/main/java/com/redhat/insights/config/InsightsConfiguration.java b/api/src/main/java/com/redhat/insights/config/InsightsConfiguration.java
index ab2c2d50..61f611e9 100644
--- a/api/src/main/java/com/redhat/insights/config/InsightsConfiguration.java
+++ b/api/src/main/java/com/redhat/insights/config/InsightsConfiguration.java
@@ -1,4 +1,4 @@
-/* Copyright (C) Red Hat 2023 */
+/* Copyright (C) Red Hat 2023-2024 */
 package com.redhat.insights.config;
 
 import java.time.Duration;
@@ -69,6 +69,10 @@ default Optional<ProxyConfiguration> getProxyConfiguration() {
   }
 
   default boolean isOptingOut() {
+    String osName = System.getProperty("os.name");
+    if (osName != null) {
+      return osName.trim().toLowerCase().contains("windows");
+    }
     return false;
   }
 
diff --git a/api/src/main/java/com/redhat/insights/http/InsightsFileWritingClient.java b/api/src/main/java/com/redhat/insights/http/InsightsFileWritingClient.java
index df852f03..0b095bfd 100644
--- a/api/src/main/java/com/redhat/insights/http/InsightsFileWritingClient.java
+++ b/api/src/main/java/com/redhat/insights/http/InsightsFileWritingClient.java
@@ -1,4 +1,4 @@
-/* Copyright (C) Red Hat 2023 */
+/* Copyright (C) Red Hat 2023-2024 */
 package com.redhat.insights.http;
 
 import static com.redhat.insights.InsightsErrorCode.ERROR_UPLOAD_DIR_CREATION;
@@ -27,7 +27,7 @@ public InsightsFileWritingClient(InsightsLogger logger, InsightsConfiguration co
 
   private void ensureArchiveUploadDirExists() {
     Path dir = Paths.get(config.getArchiveUploadDir());
-    if (Files.notExists(dir)) {
+    if (Files.notExists(dir) && !config.isOptingOut()) {
       try {
         Files.createDirectories(dir);
       } catch (IOException e) {
@@ -44,6 +44,9 @@ public void decorate(InsightsReport report) {
 
   @Override
   public void sendInsightsReport(String filename, InsightsReport report) {
+    if (config.isOptingOut()) {
+      return;
+    }
     decorate(report);
 
     // Can't reuse upload path - as this may be called as part of fallback