aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/jshipit/DockerAPIHelper.java
blob: b7c472c7d4fe16c9e2ed5ba1108810cddda24719 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package io.github.jshipit;


import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class DockerAPIHelper {

    private String apiToken;
    private final String apiRepo;
    private String authURL;
    private String authService;
    private final String repository;
    private final String image;
    private final String tag;

    public DockerAPIHelper(String apiRepo, String repository, String image, String tag) {
        if (apiRepo.contains("registry.docker.io")) {
            apiRepo = apiRepo.replace("registry", "registry-1"); // Docker is just funny like that
        }
        this.apiRepo = apiRepo;
        this.repository = repository;
        this.image = image;
        this.tag = tag;
        this.authURL = "https://auth.docker.io/token";
        this.authService = "registry.docker.io";
        try {
            getAuthenticationUrl();
            apiToken = generateAPIToken();
        } catch (IOException | RuntimeException e) {
            e.printStackTrace();
        }
    }

    /*
     * Get the Authentication URL from the registry
     */
    public void getAuthenticationUrl() throws IOException {
        URL url_obj = null;
        try {
            url_obj = new URI("https://"+this.apiRepo+"/v2/").toURL();
        } catch (URISyntaxException | MalformedURLException e) {
            e.printStackTrace();
        }

        assert url_obj != null;
        HttpURLConnection con = (HttpURLConnection) url_obj.openConnection();
        con.setRequestMethod("GET");
        con.connect();
        if (con.getResponseCode() == 401 ) {
            Map<String, List<String>> headers = con.getHeaderFields();
            List<String> authenticate = headers.get("www-authenticate");
            if (authenticate == null) {
                authenticate = headers.get("Www-Authenticate"); // Some registries (registry.getcryst.al) do this for some reason
            }
            assert authenticate != null;
            this.authURL = authenticate.get(0).replace("Bearer realm=", "").replace("\"", "").split(",")[0];
            this.authService = authenticate.get(0).replace("service=", "").replace("\"", "").split(",")[1];
        } else {
            this.authURL = "https://auth.docker.io/token"; // Just default to docker if the registry does not support or need authentication
            this.authService = "registry.docker.io";
        }
    }

    /*
     * Generate an API token from the authentication URL
     *
     * @return String API token
     */
    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.authService).toURL();
        } 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 {
            StringBuilder output = new StringBuilder();
            Scanner scanner = new Scanner(url_obj.openStream());
            while (scanner.hasNext()) {
                output.append(scanner.nextLine());
            }
            scanner.close();
            ObjectMapper mapper = new ObjectMapper();
            JsonNode token = mapper.readTree(output.toString());
            return token.get("token").asText();
        }
    }

    /*
     * Fetch the manifest JSON from the registry
     *
     * @return JsonNode Manifest JSON
     */
    public JsonNode fetchManifestJson() throws IOException, RuntimeException {
        URL url_obj = null;
        try {
            url_obj = new URI("https://" + this.apiRepo + "/v2/" + this.repository + "/" + this.image + "/manifests/"+this.tag).toURL();
        } 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 {
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder content = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
            in.close();
            con.disconnect();
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readTree(content.toString());
        }
    }

    /*
     * Fetch a blob from a specific image
     *
     * @param digest Digest of the blob to fetch
     * @param tmpdir Directory to store the blob
     * @param extract Whether or not to extract the blob
     * @param renameTo Rename the blob to this name
     */
    public void fetchBlob(String digest, String tmpdir, boolean extract, String renameTo) throws IOException, RuntimeException {
        String url = "https://" + this.apiRepo + "/v2/" + this.repository + "/" + this.image + "/blobs/"+digest;
        String[][] headers = {{"Authorization", "Bearer "+this.apiToken}, {"Accept", "application/vnd/docker.distribution.manifest.v2+json"}};
        BlobDownloader downloader = new BlobDownloader(url, digest, headers, tmpdir, extract, renameTo);
        downloader.start();
    }

    public String getImage() {
        return image;
    }

    public String getTag() {
        return tag;
    }
}