-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: test access to kubernetes rest api
- Loading branch information
Showing
9 changed files
with
197 additions
and
74 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...n/java/fr/insee/arc/web/gui/maintenanceoperation/controller/ControllerViewKubernetes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
...rc/main/java/fr/insee/arc/web/gui/maintenanceoperation/service/ServiceViewKubernetes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters