From fa9a562abb236e10c92c15b2559a7e2013541a11 Mon Sep 17 00:00:00 2001 From: Manuel Soulier Date: Thu, 1 Feb 2024 09:41:27 +0100 Subject: [PATCH] feat: test access to kubernetes rest api --- .../controller/ControllerViewKubernetes.java | 27 ++++++ .../controller/ControllerViewOperation.java | 34 +------ .../model/MaintenanceOperationsModel.java | 55 +++++++++-- .../{ViewKube.java => ViewKubernetes.java} | 8 +- .../InteractorMaintenanceOperations.java | 6 +- .../service/ServiceViewKubernetes.java | 92 +++++++++++++++++++ .../service/ServiceViewOperation.java | 25 ----- .../src/main/webapp/WEB-INF/jsp/gererFile.jsp | 2 +- .../WEB-INF/jsp/maintenanceOperations.jsp | 22 ++++- 9 files changed, 197 insertions(+), 74 deletions(-) create mode 100644 arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewKubernetes.java rename arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/{ViewKube.java => ViewKubernetes.java} (84%) create mode 100644 arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewKubernetes.java diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewKubernetes.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewKubernetes.java new file mode 100644 index 000000000..b9a53e983 --- /dev/null +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewKubernetes.java @@ -0,0 +1,27 @@ +package fr.insee.arc.web.gui.maintenanceoperation.controller; + +import java.io.IOException; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import fr.insee.arc.web.gui.maintenanceoperation.service.ServiceViewKubernetes; + +@Controller +public class ControllerViewKubernetes extends ServiceViewKubernetes { + + @RequestMapping("/secure/createPods") + public String createPodsAction(Model model) throws NoSuchAlgorithmException, IOException, KeyManagementException { + return createPods(model); + } + + @RequestMapping("/secure/deletePods") + public String deletePodsAction(Model model) { + return deletePods(model); + } + + +} \ No newline at end of file diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewOperation.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewOperation.java index 4e4d01b52..dbf113b02 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewOperation.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewOperation.java @@ -9,40 +9,14 @@ @Controller public class ControllerViewOperation extends ServiceViewOperation { - @RequestMapping("/secure/generateErrorMessageInLogsOperations") - public String generateErrorMessageInLogsOperationsAction(Model model) { - return generateErrorMessageInLogsOperations(model); - } - @RequestMapping("/secure/selectOperations") public String selectOperationsAction(Model model) { return selectOperations(model); } - - @RequestMapping("/secure/addOperations") - public String addOperationsAction(Model model) { - return addOperations(model); - } - - @RequestMapping("/secure/deleteOperations") - public String deleteOperationsAction(Model model) { - return deleteOperations(model); - } - - @RequestMapping("/secure/updateOperations") - public String updateOperationsAction(Model model) { - return updateOperations(model); - } - - @RequestMapping("/secure/sortOperations") - public String sortOperationsAction(Model model) { - return sortOperations(model); - } - - @RequestMapping("/secure/startOperations") - public String startOperationsAction(Model model) { - return startOperations(model); + + @RequestMapping("/secure/generateErrorMessageInLogsOperations") + public String generateErrorMessageInLogsOperationsAction(Model model) { + return generateErrorMessageInLogsOperations(model); } - } \ No newline at end of file diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/MaintenanceOperationsModel.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/MaintenanceOperationsModel.java index 9b6686732..747bfb802 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/MaintenanceOperationsModel.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/MaintenanceOperationsModel.java @@ -8,13 +8,21 @@ @Component public class MaintenanceOperationsModel implements ArcModel { + + private String url; + private String httpType; + private String json; + private String httpOutput; + + + private VObject viewOperations; - private VObject viewKube; + private VObject viewKubernetes; public MaintenanceOperationsModel() { this.viewOperations = new ViewOperations(); - this.viewKube = new ViewKube(); + this.viewKubernetes = new ViewKubernetes(); } public VObject getViewOperations() { @@ -25,12 +33,47 @@ public void setViewOperations(VObject viewOperations) { this.viewOperations = viewOperations; } - public VObject getViewKube() { - return viewKube; + public VObject getViewKubernetes() { + return viewKubernetes; } - public void setViewKube(VObject viewKube) { - this.viewKube = viewKube; + public void setViewKubernetes(VObject viewKubernetes) { + this.viewKubernetes = viewKubernetes; } + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getHttpType() { + return httpType; + } + + public void setHttpType(String httpType) { + this.httpType = httpType; + } + + public String getJson() { + return json; + } + + public void setJson(String json) { + this.json = json; + } + + public String getHttpOutput() { + return httpOutput; + } + + public void setHttpOutput(String httpOutput) { + this.httpOutput = httpOutput; + } + + + + } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKube.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKubernetes.java similarity index 84% rename from arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKube.java rename to arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKubernetes.java index ea44359d6..8aef57a74 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKube.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/model/ViewKubernetes.java @@ -8,7 +8,7 @@ import fr.insee.arc.web.gui.all.util.VObject; -public class ViewKube extends VObject { +public class ViewKubernetes extends VObject { private static final Map columnMap= new HashMap<>(); @@ -20,10 +20,10 @@ public class ViewKube extends VObject { } - public ViewKube() { + public ViewKubernetes() { super(); - this.setTitle("view.kube"); - this.setSessionName("viewKube"); + this.setTitle("view.viewKubernetes"); + this.setSessionName("viewKubernetes"); this.setDefaultPaginationSize(20); this.setConstantVObject(new ConstantVObject(columnMap)); } diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/InteractorMaintenanceOperations.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/InteractorMaintenanceOperations.java index b09ed3fbd..40050941e 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/InteractorMaintenanceOperations.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/InteractorMaintenanceOperations.java @@ -27,10 +27,10 @@ public class InteractorMaintenanceOperations extends ArcWebGenericService initializeOperations()); - putVObject(views.getViewKube(), t -> initializeKube()); + putVObject(views.getViewKubernetes(), t -> initializeKube()); } public void initializeOperations() { @@ -40,7 +40,7 @@ public void initializeOperations() { public void initializeKube() { Map defaultInputFields = new HashMap<>(); - this.vObjectService.initialize(views.getViewKube(), new ArcPreparedStatementBuilder("SELECT true"), "arc.kube", defaultInputFields); + this.vObjectService.initialize(views.getViewKubernetes(), new ArcPreparedStatementBuilder("SELECT true"), "arc.kube", defaultInputFields); } @Override diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewKubernetes.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewKubernetes.java new file mode 100644 index 000000000..e8931802b --- /dev/null +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewKubernetes.java @@ -0,0 +1,92 @@ +package fr.insee.arc.web.gui.maintenanceoperation.service; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; + +import javax.net.ssl.HttpsURLConnection; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.springframework.stereotype.Service; +import org.springframework.ui.Model; + + +@Service +public class ServiceViewKubernetes extends InteractorMaintenanceOperations { + + public String createPods(Model model) throws IOException, NoSuchAlgorithmException, KeyManagementException { + + System.out.println(views.getHttpType()); + System.out.println(views.getUrl()); + + // récupération du token + String tokenBearer = "Bearer "+new String(Files.readAllBytes(Paths.get("/var/run/secrets/kubernetes.io/serviceaccount/token")), StandardCharsets.UTF_8); + + X509TrustManager x= new X509TrustManager() { + public java.security.cert.X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + public void checkClientTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + // selfsigned cetificate + } + public void checkServerTrusted( + java.security.cert.X509Certificate[] certs, String authType) { + // selfsigned cetificate + } + } ; + + + TrustManager[] trustAllCerts = new TrustManager[] { x }; + + // Install the all-trusting trust manager + SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, trustAllCerts, new java.security.SecureRandom()); + HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); + + URL url = new URL(views.getUrl()); + HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); + con.setRequestMethod(views.getHttpType()); + con.setRequestProperty("Authorization", tokenBearer); + con.setRequestProperty("Accept", "application/json"); + con.setRequestProperty("Content-Type", "application/json"); + + if (views.getJson()!=null || !views.getJson().isBlank()) { + OutputStream os = con.getOutputStream(); + OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8"); + osw.write(views.getJson()); + osw.flush(); + osw.close(); + os.close(); + } + + String result; + BufferedInputStream bis = new BufferedInputStream(con.getInputStream()); + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + int result2 = bis.read(); + while(result2 != -1) { + buf.write((byte) result2); + result2 = bis.read(); + } + result = buf.toString(); + System.out.println(result); + views.setHttpOutput(result); + + return generateDisplay(model, RESULT_SUCCESS); + } + + public String deletePods(Model model) { + return generateDisplay(model, RESULT_SUCCESS); + } +} \ No newline at end of file diff --git a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewOperation.java b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewOperation.java index 0d4627ec6..455ea7d48 100644 --- a/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewOperation.java +++ b/arc-web/src/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewOperation.java @@ -19,30 +19,5 @@ public String generateErrorMessageInLogsOperations(Model model) { public String selectOperations(Model model) { return generateDisplay(model, RESULT_SUCCESS); } - - public String addOperations(Model model) { - this.vObjectService.insert(views.getViewOperations()); - return generateDisplay(model, RESULT_SUCCESS); - } - - public String deleteOperations(Model model) { - this.vObjectService.delete(views.getViewOperations()); - return generateDisplay(model, RESULT_SUCCESS); - } - - public String updateOperations(Model model) { - this.vObjectService.update(views.getViewOperations()); - return generateDisplay(model, RESULT_SUCCESS); - } - - public String sortOperations(Model model) { - this.vObjectService.sort(views.getViewOperations()); - return generateDisplay(model, RESULT_SUCCESS); - } - - public String startOperations(Model model) { - return generateDisplay(model, RESULT_SUCCESS); - } - } \ No newline at end of file diff --git a/arc-web/src/main/webapp/WEB-INF/jsp/gererFile.jsp b/arc-web/src/main/webapp/WEB-INF/jsp/gererFile.jsp index b18cb683a..413b30622 100644 --- a/arc-web/src/main/webapp/WEB-INF/jsp/gererFile.jsp +++ b/arc-web/src/main/webapp/WEB-INF/jsp/gererFile.jsp @@ -22,7 +22,7 @@ accept-charset="UTF-8"
- + diff --git a/arc-web/src/main/webapp/WEB-INF/jsp/maintenanceOperations.jsp b/arc-web/src/main/webapp/WEB-INF/jsp/maintenanceOperations.jsp index 8753ba10e..1f1383a72 100644 --- a/arc-web/src/main/webapp/WEB-INF/jsp/maintenanceOperations.jsp +++ b/arc-web/src/main/webapp/WEB-INF/jsp/maintenanceOperations.jsp @@ -57,17 +57,29 @@ Kubernetes
-
+
+ +

Url

+ +

Http Type

+ +

Content

+ +
" -scope="viewOperations;" doAction="createPods" +scope="viewKubernetes;" doAction="createPods" onclick="return confirm('');" /> " -scope="viewOperations;" doAction="deletePods" +scope="viewKubernetes;" doAction="deletePods" onclick="return confirm('');" /> +
+

output

+ +