Skip to content

Commit

Permalink
feat: test access to kubernetes rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolife999 committed Feb 1, 2024
1 parent d39e746 commit fa9a562
Show file tree
Hide file tree
Showing 9 changed files with 197 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ColumnRendering> columnMap= new HashMap<>();

Expand All @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public class InteractorMaintenanceOperations extends ArcWebGenericService<Mainte
@Override
protected void putAllVObjects(MaintenanceOperationsModel arcModel) {
views.setViewOperations(this.vObjectService.preInitialize(arcModel.getViewOperations()));
views.setViewKube(this.vObjectService.preInitialize(arcModel.getViewKube()));
views.setViewKubernetes(this.vObjectService.preInitialize(arcModel.getViewKubernetes()));

putVObject(views.getViewOperations(), t -> initializeOperations());
putVObject(views.getViewKube(), t -> initializeKube());
putVObject(views.getViewKubernetes(), t -> initializeKube());
}

public void initializeOperations() {
Expand All @@ -40,7 +40,7 @@ public void initializeOperations() {

public void initializeKube() {
Map<String, String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
2 changes: 1 addition & 1 deletion arc-web/src/main/webapp/WEB-INF/jsp/gererFile.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ accept-charset="UTF-8"
<div class="container-fluid">
<div class="row">
<div class="col-md-5 border-right">
<c:set var="view" value="${viewDirIn}" scope="request"/>
<c:set var="view" value="${viewDirIn}" scope="request"/>
<c:import url="tiles/templateVObject.jsp">
<c:param name="taille" value ="col-md4" />
<c:param name="ligneAdd" value="true" />
Expand Down
22 changes: 17 additions & 5 deletions arc-web/src/main/webapp/WEB-INF/jsp/maintenanceOperations.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,29 @@ Kubernetes
</h2>
<div class="row">
<div class="col-md">
<div id="viewKube">
<div id="viewKubernetes">

<p>Url</p>
<input type="text" m="" name="url" value="${url}" />
<p>Http Type</p>
<input type="text" m="" name="httpType" value="${httpType}" />
<p>Content</p>
<input type="text" m="" name="json" value="${json}" />
</br>
<input type="submit" class="btn btn-primary btn-sm"
id="viewOperations.createPods"
id="viewKubernetes.createPods"
value="<spring:message code="gui.button.createPods"/>"
scope="viewOperations;" doAction="createPods"
scope="viewKubernetes;" doAction="createPods"
onclick="return confirm('<spring:message code="gui.button.createPods.confirm" javaScriptEscape="true"/>');" />
<input type="submit" class="btn btn-primary btn-sm"
id="viewOperations.deletePods"
id="viewKubernetes.deletePods"
value="<spring:message code="gui.button.deletePods"/>"
scope="viewOperations;" doAction="deletePods"
scope="viewKubernetes;" doAction="deletePods"
onclick="return confirm('<spring:message code="gui.button.deletePods.confirm" javaScriptEscape="true"/>');" />
</br>
<p>output</p>
<input type="text" m="" name="httpOutput" value="${httpOutput}" />

</div>
</div>
</div>
Expand Down

0 comments on commit fa9a562

Please sign in to comment.