From 0c590daf2b0a3c4fd6aebcdff18128974b83b213 Mon Sep 17 00:00:00 2001
From: NThakur4 <nthakur4@slb.com>
Date: Wed, 12 Jan 2022 15:19:13 -0600
Subject: [PATCH] add endpoint to disable auto index creation

---
 .../indexer/api/DataPartitionSettingsApi.java | 52 +++++++++++++++++++
 .../osdu/indexer/logging/AuditEvents.java     | 14 +++++
 .../osdu/indexer/logging/AuditLogger.java     |  4 ++
 .../ClusterConfigurationServiceImpl.java      | 48 +++++++++++++++++
 .../service/IClusterConfigurationService.java | 22 ++++++++
 5 files changed, 140 insertions(+)
 create mode 100644 indexer-core/src/main/java/org/opengroup/osdu/indexer/api/DataPartitionSettingsApi.java
 create mode 100644 indexer-core/src/main/java/org/opengroup/osdu/indexer/service/ClusterConfigurationServiceImpl.java
 create mode 100644 indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IClusterConfigurationService.java

diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/DataPartitionSettingsApi.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/DataPartitionSettingsApi.java
new file mode 100644
index 000000000..cd3ddb3e9
--- /dev/null
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/api/DataPartitionSettingsApi.java
@@ -0,0 +1,52 @@
+// Copyright © Schlumberger
+//
+// 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 org.opengroup.osdu.indexer.api;
+
+import lombok.extern.java.Log;
+import org.opengroup.osdu.indexer.logging.AuditLogger;
+import org.opengroup.osdu.indexer.service.IClusterConfigurationService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.context.annotation.RequestScope;
+
+import java.io.IOException;
+import java.util.ArrayList;
+
+
+@Log
+@RestController
+@RequestMapping("/partitions/settings")
+@RequestScope
+public class DataPartitionSettingsApi {
+
+    private static final String OPS = "users.datalake.ops";
+
+    @Autowired
+    private IClusterConfigurationService clusterConfigurationService;
+    @Autowired
+    private AuditLogger auditLogger;
+
+    @PreAuthorize("@authorizationFilter.hasPermission('" + OPS + "')")
+    @PostMapping
+    public ResponseEntity<?> partitionInit() throws IOException {
+        this.clusterConfigurationService.updateClusterConfiguration();
+        this.auditLogger.getConfigurePartition(new ArrayList<>());
+        return new ResponseEntity<>(org.springframework.http.HttpStatus.OK);
+    }
+}
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
index 1f0970f1d..eb8a638cb 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditEvents.java
@@ -57,6 +57,9 @@ public class AuditEvents {
     private static final String INDEX_MAPPING_UPDATE_SUCCESS = "Successfully updated index mapping";
     private static final String INDEX_MAPPING_UPDATE_FAILURE = "Failed updating index mapping";
 
+    private static final String CONFIGURE_PARTITION_ACTION_ID = "IN0012";
+    private static final String CONFIGURE_PARTITION_OPERATION = "Data partition cluster configuration update";
+
     private final String user;
 
     public AuditEvents(String user) {
@@ -209,6 +212,17 @@ public class AuditEvents {
                 .build();
     }
 
+    public AuditPayload getConfigurePartitionEvent(List<String> resources) {
+        return AuditPayload.builder()
+                .action(AuditAction.UPDATE)
+                .status(AuditStatus.FAILURE)
+                .actionId(CONFIGURE_PARTITION_ACTION_ID)
+                .message(CONFIGURE_PARTITION_OPERATION)
+                .resources(resources)
+                .user(this.user)
+                .build();
+    }
+
     public AuditPayload getIndexMappingUpdateEvent(List<String> resources, boolean isSuccess) {
         if (isSuccess) {
             return AuditPayload.builder()
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java
index 431e99afc..1a37d7688 100644
--- a/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/logging/AuditLogger.java
@@ -100,6 +100,10 @@ public class AuditLogger {
         this.writeLog(this.getAuditEvents().getIndexMappingUpdateEvent(resources,false));
     }
 
+    public void getConfigurePartition(List<String> resources) {
+        this.writeLog(this.getAuditEvents().getConfigurePartitionEvent(resources));
+    }
+
     private void writeLog(AuditPayload log) {
         this.logger.audit(log);
     }
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/ClusterConfigurationServiceImpl.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/ClusterConfigurationServiceImpl.java
new file mode 100644
index 000000000..e828cad8e
--- /dev/null
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/ClusterConfigurationServiceImpl.java
@@ -0,0 +1,48 @@
+// Copyright © Schlumberger
+//
+// 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 org.opengroup.osdu.indexer.service;
+
+import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest;
+import org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.common.settings.Settings;
+import org.elasticsearch.common.unit.TimeValue;
+import org.opengroup.osdu.indexer.util.ElasticClientHandler;
+import org.springframework.beans.factory.annotation.Autowired;
+
+import java.io.IOException;
+
+public class ClusterConfigurationServiceImpl implements IClusterConfigurationService {
+
+    @Autowired
+    private ElasticClientHandler elasticClientHandler;
+
+    @Override
+    public boolean updateClusterConfiguration() throws IOException {
+        ClusterUpdateSettingsRequest request = new ClusterUpdateSettingsRequest();
+
+        Settings persistentSettings =
+                Settings.builder()
+                        .put("action.auto_create_index", "false")
+                        .build();
+        request.persistentSettings(persistentSettings);
+        request.timeout(TimeValue.timeValueMinutes(1));
+        try (RestHighLevelClient client = this.elasticClientHandler.createRestClient()) {
+            ClusterUpdateSettingsResponse response = client.cluster().putSettings(request, RequestOptions.DEFAULT);
+            return response.isAcknowledged();
+        }
+    }
+}
diff --git a/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IClusterConfigurationService.java b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IClusterConfigurationService.java
new file mode 100644
index 000000000..25255be14
--- /dev/null
+++ b/indexer-core/src/main/java/org/opengroup/osdu/indexer/service/IClusterConfigurationService.java
@@ -0,0 +1,22 @@
+// Copyright © Schlumberger
+//
+// 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 org.opengroup.osdu.indexer.service;
+
+import java.io.IOException;
+
+public interface IClusterConfigurationService {
+
+    boolean updateClusterConfiguration() throws IOException;
+}
-- 
GitLab