aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoraxtloss <axtlos@getcryst.al>2023-05-15 09:11:55 +0200
committeraxtloss <axtlos@getcryst.al>2023-05-15 09:11:55 +0200
commit51c1b2c80c8e0067b90b6515edf85476e5e3ba54 (patch)
tree1f2b59fa67e1a671be2ae297b26ae0078119ce98
downloadjshipit-51c1b2c80c8e0067b90b6515edf85476e5e3ba54.tar.gz
jshipit-51c1b2c80c8e0067b90b6515edf85476e5e3ba54.tar.bz2
Add files
-rwxr-xr-xpom.xml67
-rwxr-xr-xsrc/main/java/io/github/jshipit/DockerAPIHelper.java162
-rwxr-xr-xsrc/main/java/io/github/jshipit/Main.java41
3 files changed, 270 insertions, 0 deletions
diff --git a/pom.xml b/pom.xml
new file mode 100755
index 0000000..4146647
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>io.github.jshipit</groupId>
+ <artifactId>JavaShipit</artifactId>
+ <version>1.0-SNAPSHOT</version>
+
+ <properties>
+ <maven.compiler.source>20</maven.compiler.source>
+ <maven.compiler.target>20</maven.compiler.target>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>com.github.docker-java</groupId>
+ <artifactId>docker-java</artifactId>
+ <version>3.3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>com.github.docker-java</groupId>
+ <artifactId>docker-java-transport-httpclient5</artifactId>
+ <version>3.3.0</version>
+ </dependency>
+ <dependency>
+ <groupId>commons-cli</groupId>
+ <artifactId>commons-cli</artifactId>
+ <version>1.5.0</version>
+ </dependency>
+ <dependency>
+ <groupId>com.fasterxml.jackson.core</groupId>
+ <artifactId>jackson-databind</artifactId>
+ <version>2.14.2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>3.5.0</version>
+ <configuration>
+ <archive>
+ <manifest>
+ <mainClass>io.github.jshipit.Main</mainClass>
+ </manifest>
+ </archive>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ <executions>
+ <execution>
+ <id>assemble-all</id>
+ <phase>package</phase>
+ <goals>
+ <goal>single</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project> \ No newline at end of file
diff --git a/src/main/java/io/github/jshipit/DockerAPIHelper.java b/src/main/java/io/github/jshipit/DockerAPIHelper.java
new file mode 100755
index 0000000..dca582e
--- /dev/null
+++ b/src/main/java/io/github/jshipit/DockerAPIHelper.java
@@ -0,0 +1,162 @@
+package io.github.jshipit;
+
+
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+
+import java.io.*;
+import java.net.*;
+import java.util.Scanner;
+
+public class DockerAPIHelper {
+
+ private String apiToken;
+ private String apiRepo;
+ private String authURL;
+ private String repository;
+ private String image;
+
+ private String tag;
+
+ public DockerAPIHelper(String apiRepo, String authURL, String repository, String image, String tag) {
+ System.out.println("DockerAPIHelper constructor");
+ this.apiRepo = apiRepo;
+ this.repository = repository;
+ this.authURL = authURL;
+ this.image = image;
+ this.tag = tag;
+ try {
+ apiToken = generateAPIToken();
+ } catch (IOException | RuntimeException e) {
+ System.out.println("IOException | RuntimeException");
+ e.printStackTrace();
+ }
+ }
+
+ public String generateAPIToken() throws IOException, RuntimeException {
+ URL url_obj = null;
+ try {
+ url_obj = new URI(this.authURL + "?scope=repository:" + this.repository + "/" + this.image + ":pull" + "&service=" + this.apiRepo).toURL();
+ System.out.println(url_obj.toString());
+ } catch (URISyntaxException | MalformedURLException e) {
+ System.out.println("URISyntaxException | MalformedURLException");
+ e.printStackTrace();
+ }
+
+
+ assert url_obj != null;
+ HttpURLConnection con = (HttpURLConnection) url_obj.openConnection();
+ con.setRequestMethod("GET");
+ con.connect();
+ if (con.getResponseCode() != 200) {
+ System.out.println("Error: " + con.getResponseCode());
+ throw new RuntimeException("Failed : HTTP error code : "
+ + con.getResponseCode());
+ } else {
+ System.out.println("Success: " + con.getResponseCode());
+ String output = "";
+ Scanner scanner = new Scanner(url_obj.openStream());
+ while (scanner.hasNext()) {
+ output += scanner.nextLine();
+ }
+ scanner.close();
+ System.out.println(output);
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode token = mapper.readTree(output);
+ System.out.println(token.get("token").asText());
+ return token.get("token").asText();
+ }
+ }
+
+ public JsonNode fetchManifestJson() throws IOException, RuntimeException {
+ URL url_obj = null;
+ try {
+ String repo = this.apiRepo.replace("registry", "registry-1");
+ url_obj = new URI("https://" + repo + "/v2/" + this.repository + "/" + this.image + "/manifests/"+this.tag).toURL();
+ System.out.println(url_obj.toString());
+ } catch(URISyntaxException | MalformedURLException e) {
+ System.out.println("URISyntaxException | MalformedURLException");
+ e.printStackTrace();
+ }
+
+ assert url_obj != null;
+ HttpURLConnection con = (HttpURLConnection) url_obj.openConnection();
+ con.setRequestMethod("GET");
+ con.setRequestProperty("Authorization", "Bearer " + this.apiToken);
+ con.setRequestProperty("Accept", "application/vnd.docker.distribution.manifest.v2+json");
+ con.connect();
+
+ if (con.getResponseCode() != 200) {
+ System.out.println("Error: " + con.getResponseCode());
+ throw new RuntimeException("Failed : HTTP error code : "
+ + con.getResponseCode());
+ } else {
+ System.out.println("Success: " + con.getResponseCode());
+ BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
+ String inputLine;
+ StringBuffer content = new StringBuffer();
+ while ((inputLine = in.readLine()) != null) {
+ content.append(inputLine);
+ }
+ in.close();
+ con.disconnect();
+ ObjectMapper mapper = new ObjectMapper();
+ JsonNode manifest = mapper.readTree(content.toString());
+ return manifest;
+ }
+ }
+
+ public void fetchBlob(String digest, String tmpdir) throws IOException, RuntimeException {
+ URL url_obj = null;
+ try {
+ String repo = this.apiRepo.replace("registry", "registry-1");
+ url_obj = new URI("https://" + repo + "/v2/" + this.repository + "/" + this.image + "/blobs/"+digest).toURL();
+ System.out.println(url_obj.toString());
+ } catch(URISyntaxException | MalformedURLException e) {
+ System.out.println("URISyntaxException | MalformedURLException");
+ e.printStackTrace();
+ }
+
+ assert url_obj != null;
+ HttpURLConnection con = (HttpURLConnection) url_obj.openConnection();
+ con.setRequestMethod("GET");
+ con.setRequestProperty("Authorization", "Bearer " + this.apiToken);
+ con.setRequestProperty("Accept", "application/vnd.docker.distribution.manifest.v2+json");
+ con.connect();
+
+ if (con.getResponseCode() != 200) {
+ System.out.println("Error: " + con.getResponseCode());
+ throw new RuntimeException("Failed : HTTP error code : "
+ + con.getResponseCode());
+ } else {
+ System.out.println("Success: " + con.getResponseCode());
+ }
+ // Stream file download and output progress
+ InputStream inputStream = con.getInputStream();
+ FileOutputStream outputStream = new FileOutputStream(tmpdir+"/"+digest.replace("sha256:", "")+"layer.tar");
+
+ byte[] buffer = new byte[1024];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, bytesRead);
+ }
+
+ outputStream.close();
+ inputStream.close();
+ System.out.println("File downloaded successfully.");
+ }
+
+ public String getApiToken() {
+ return apiToken;
+ }
+
+ public String getImage() {
+ return image;
+ }
+
+ public String getTag() {
+ return tag;
+ }
+}
diff --git a/src/main/java/io/github/jshipit/Main.java b/src/main/java/io/github/jshipit/Main.java
new file mode 100755
index 0000000..df1cea7
--- /dev/null
+++ b/src/main/java/io/github/jshipit/Main.java
@@ -0,0 +1,41 @@
+package io.github.jshipit;
+
+import com.fasterxml.jackson.databind.JsonNode;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+public class Main {
+ public static void main(String[] args) {
+ DockerAPIHelper api = new DockerAPIHelper("registry.docker.io", "https://auth.docker.io/token", "library", "archlinux", "latest");
+ JsonNode manifest = null;
+
+ System.out.println("API Token: " + api.getApiToken());
+ try {
+ manifest = api.fetchManifestJson();
+ } catch (IOException e) {
+
+ }
+
+ System.out.println("Manifest: " + manifest);
+
+ Path path = Path.of("./tmp_"+api.getImage()+"_"+api.getTag());
+ try {
+ Files.createDirectory(path);
+ } catch (IOException e) {
+ System.out.println("Failed to create directory: " + path);
+ return;
+ }
+
+ JsonNode layers = manifest.get("layers");
+ for (JsonNode layer : layers) {
+ System.out.println("Layer: " + layer);
+ try {
+ api.fetchBlob(layer.get("digest").asText(), path.toString());
+ } catch (IOException e) {
+
+ }
+ }
+ }
+} \ No newline at end of file