File: StageArtifacts.java

package info (click to toggle)
lucene9 9.10.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 114,036 kB
  • sloc: java: 775,336; python: 6,157; xml: 1,464; perl: 448; sh: 353; makefile: 11
file content (430 lines) | stat: -rw-r--r-- 16,892 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.Console;
import java.io.IOException;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Sonatype nexus artifact staging/deployment script. This could be made
 * nicer, but this keeps it to JDK classes only.
 *
 * <p>The implementation is based on the REST API documentation of
 * <a href="https://oss.sonatype.org/nexus-staging-plugin/default/docs/index.html">nexus-staging-plugin</a>
 * and on anecdotal evidence and reverse-engineered information from around
 * the web... Weird that such a crucial piece of infrastructure has such obscure
 * documentation.
 */
public class StageArtifacts {
  private static final String DEFAULT_NEXUS_URI = "https://repository.apache.org";

  private static class Params {
    URI nexusUri = URI.create(DEFAULT_NEXUS_URI);
    String userName;
    char[] userPass;
    Path mavenDir;
    String description;

    private static char[] envVar(String envVar) {
      var value = System.getenv(envVar);
      return value == null ? null : value.toCharArray();
    }

    static void requiresArgument(String[] args, int at) {
      if (at + 1 >= args.length) {
        throw new RuntimeException("Option '" + args[at]
            + "' requires an argument, pass --help for help.");
      }
    }

    static Params parse(String[] args) {
      try {
        var params = new Params();
        for (int i = 0; i < args.length; i++) {
          switch (args[i]) {
            case "-n":
            case "--nexus":
              requiresArgument(args, i);
              params.nexusUri = URI.create(args[++i]);
              break;
            case "-u":
            case "--user":
              requiresArgument(args, i);
              params.userName = args[++i];
              break;
            case "-p":
            case "--password":
              requiresArgument(args, i);
              params.userPass = args[++i].toCharArray();
              break;
            case "--description":
              requiresArgument(args, i);
              params.description = args[++i];
              break;

            case "-h":
            case "--help":
              System.out.println("java " + StageArtifacts.class.getName() + " [options] path-to-maven-artifacts");
              System.out.println("  -u, --user  User name for authentication.");
              System.out.println("              better: ASF_USERNAME env. var.");
              System.out.println("  -p, --password  Password for authentication.");
              System.out.println("              better: ASF_PASSWORD env. var.");
              System.out.println("  -n, --nexus URL to Apache Nexus (optional).");
              System.out.println("  --description  Staging repo description (optional).");
              System.out.println("");
              System.out.println("  path        Path to maven artifact directory.");
              System.out.println("");
              System.out.println(" Password can be omitted for console prompt-input.");
              System.exit(0);

            default:
              if (params.mavenDir != null) {
                throw new RuntimeException("Exactly one maven artifact directory should be provided.");
              }
              params.mavenDir = Paths.get(args[i]);
              break;
          }
        }

        if (params.userName == null) {
          var v = envVar("ASF_USERNAME");
          if (v != null) {
            params.userName = new String(v);
          }
        }
        Objects.requireNonNull(params.userName, "User name is required for authentication.");

        if (params.userPass == null) {
          params.userPass = envVar("ASF_PASSWORD");
          if (params.userPass == null) {
            Console console = System.console();
            if (console != null) {
              System.out.println("Enter password for " + params.userName + ":");
              params.userPass = console.readPassword();
            } else {
              throw new RuntimeException("No console, can't prompt for password.");
            }
          }
        }
        Objects.requireNonNull(params.userPass, "User password is required for authentication.");

        if (params.mavenDir == null || !Files.isDirectory(params.mavenDir)) {
          throw new RuntimeException("Maven artifact directory is required and must exist.");
        }
        return params;
      } catch (IndexOutOfBoundsException e) {
        throw new RuntimeException("Required argument missing (pass --help for help)?");
      }
    }
  }

  private static class NexusApi {
    private final HttpClient client;
    private final URI nexusUri;

    public NexusApi(Params params) {
      Authenticator authenticator = new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(params.userName, params.userPass);
        }
      };

      this.client = HttpClient.newBuilder()
          .authenticator(authenticator)
          .build();

      this.nexusUri = params.nexusUri;
    }

    public String requestProfileId(PomInfo pomInfo) throws IOException {
      String result = sendGet("/service/local/staging/profile_evaluate", Map.of(
          "g", pomInfo.groupId,
          "a", pomInfo.artifactId,
          "v", pomInfo.version,
          "t", "maven2"
      ));

      return XmlElement.parse(result)
          .onlychild("stagingProfiles")
          .onlychild("data")
          .onlychild("stagingProfile")
          .onlychild("id")
          .text();
    }

    public String createStagingRepository(String profileId, String description) throws IOException {
      String result = sendPost("/service/local/staging/profiles/" + URLEncoder.encode(profileId, StandardCharsets.UTF_8) + "/start",
          "application/xml",
          HttpURLConnection.HTTP_CREATED,
          ("<promoteRequest>\n" +
              "  <data>\n" +
              "    <description><![CDATA[" + description + "]]></description>\n" +
              "  </data>\n" +
              "</promoteRequest>").getBytes(StandardCharsets.UTF_8));

      return XmlElement.parse(result)
          .onlychild("promoteResponse")
          .onlychild("data")
          .onlychild("stagedRepositoryId")
          .text();
    }

    public void uploadArtifact(String stagingRepoId, Path path, String relativePath) throws IOException {
      sendPost("/service/local/staging/deployByRepositoryId/"
              + URLEncoder.encode(stagingRepoId, StandardCharsets.UTF_8)
              + "/"
              + relativePath,
          "application/octet-stream",
          HttpURLConnection.HTTP_CREATED,
          Files.readAllBytes(path));
    }

    public void closeStagingRepository(String profileId, String stagingRepoId) throws IOException {
      sendPost("/service/local/staging/profiles/" + URLEncoder.encode(profileId, StandardCharsets.UTF_8) + "/finish",
          "application/xml",
          HttpURLConnection.HTTP_CREATED,
          ("<promoteRequest>\n" +
              "  <data>\n" +
              "    <stagedRepositoryId><![CDATA[" + stagingRepoId + "]]></stagedRepositoryId>\n" +
              "  </data>\n" +
              "</promoteRequest>").getBytes(StandardCharsets.UTF_8));
    }

    private String sendPost(String serviceEndpoint, String contentType, int expectedStatus, byte[] bytes) throws IOException {
      URI target = nexusUri.resolve(serviceEndpoint);

      try {
        HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);
        HttpRequest req = HttpRequest.newBuilder()
            .POST(HttpRequest.BodyPublishers.ofByteArray(bytes))
            .header("Content-Type", contentType)
            .uri(target)
            // we could use json if XML is too difficult to work with.
            // .header("Accept", "application/json")
            .build();
        HttpResponse<String> response = client.send(req, bodyHandler);
        if (response.statusCode() != expectedStatus) {
          throw new IOException("Unexpected HTTP error returned: " + response.statusCode() + ", response body: "
              + response.body());
        }
        return response.body();
      } catch (InterruptedException e) {
        throw new IOException("HTTP timeout", e);
      }
    }

    private String sendGet(String serviceEndpoint, Map<String, String> getArgs) throws IOException {
      // JDK: jeez... why make a http client and not provide uri-manipulation utilities?
      URI target;
      try {
        target = nexusUri.resolve(serviceEndpoint);
        target = new URI(
            target.getScheme(), target.getUserInfo(), target.getHost(), target.getPort(),
            target.getPath(),
            getArgs.entrySet().stream()
                .map(e -> entityEncode(e.getKey()) + "=" + entityEncode(e.getValue()))
                .collect(Collectors.joining("&")),
            null);

        HttpResponse.BodyHandler<String> bodyHandler = HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8);
        HttpRequest req = HttpRequest.newBuilder()
            .GET()
            .uri(target)
            // we could use json if XML is too difficult to work with.
            // .header("Accept", "application/json")
            .build();
        HttpResponse<String> response = client.send(req, bodyHandler);
        if (response.statusCode() != HttpURLConnection.HTTP_OK) {
          throw new IOException("Unexpected HTTP error returned: " + response.statusCode());
        }
        return response.body();
      } catch (URISyntaxException e) {
        throw new RuntimeException(e);
      } catch (InterruptedException e) {
        throw new IOException("HTTP timeout", e);
      }
    }

    private String entityEncode(String value) {
      return URLEncoder.encode(value, StandardCharsets.UTF_8);
    }
  }

  private static class XmlElement {
    private final Node element;

    static XmlElement parse(String xml) throws IOException {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      try (var is = new StringReader(xml)) {
        Document parse = dbf.newDocumentBuilder().parse(new InputSource(is));
        return new XmlElement(parse);
      } catch (ParserConfigurationException | SAXException e) {
        throw new RuntimeException(e);
      }
    }

    public XmlElement(Node child) {
      this.element = child;
    }

    public XmlElement onlychild(String tagName) throws IOException {
      ArrayList<XmlElement> children = new ArrayList<>();
      NodeList childNodes = element.getChildNodes();
      for (int i = 0, max = childNodes.getLength(); i < max; i++) {
        var child = childNodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE &&
            Objects.equals(child.getNodeName(), tagName)
        ) {
          children.add(new XmlElement(child));
        }
      }
      if (children.isEmpty()) {
        throw new IOException("No child node found for: " + tagName);
      }
      return children.get(0);
    }

    public String text() {
      return element.getTextContent();
    }
  }

  private static class PomInfo {
    String groupId;
    String artifactId;
    String version;

    public static PomInfo extractPomInfo(Path path) throws IOException {
      PomInfo pomInfo = new PomInfo();
      XmlElement project =
          XmlElement.parse(Files.readString(path, StandardCharsets.UTF_8))
              .onlychild("project");
      pomInfo.groupId = project.onlychild("groupId").text();
      pomInfo.artifactId = project.onlychild("artifactId").text();
      pomInfo.version = project.onlychild("version").text();
      return pomInfo;
    }
  }

  public static void main(String[] args) throws Exception {
    try {
      var params = Params.parse(args);
      var nexus = new NexusApi(params);

      // Collect all files to be uploaded.
      List<Path> artifacts;
      try (Stream<Path> pathStream = Files.walk(params.mavenDir)) {
        artifacts = pathStream
            .filter(Files::isRegularFile)
            // Ignore locally generated maven metadata files.
            .filter(path -> !path.getFileName().toString().startsWith("maven-metadata."))
            .sorted(Comparator.comparing(Path::toString))
            .collect(Collectors.toList());
      }

      // Figure out nexus profile ID based on POMs. It is assumed that all artifacts
      // fall under the same profile.
      PomInfo pomInfo = PomInfo.extractPomInfo(
          artifacts.stream()
              .filter(path -> path.getFileName().toString().endsWith(".pom"))
              .findFirst()
              .orElseThrow());

      // Sanity check for directory structure - all files should have the groupId folder prefix.
      {
        String expectedPrefix = pomInfo.groupId.replace('.', '/');
        for (Path path : artifacts) {
          Path relative = params.mavenDir.relativize(path);
          List<String> urlSegments = new ArrayList<>();
          for (Path segment : relative) {
            urlSegments.add(URLEncoder.encode(segment.toString(), StandardCharsets.UTF_8));
          }
          String relativeUrl = String.join("/", urlSegments);

          if (!relativeUrl.startsWith(expectedPrefix)) {
            throw new RuntimeException("Maven folder structure does not match the expected groupId, "
              + "expected prefix: " + expectedPrefix + ", artifact: " + relativeUrl);
          }
        }
      }

      System.out.println("Requesting profile ID for artifact: "
          + pomInfo.groupId + ":" + pomInfo.artifactId + ":" + pomInfo.version);
      String profileId = nexus.requestProfileId(pomInfo);
      System.out.println("  => Profile ID: " + profileId);

      System.out.println("Creating staging repository.");
      String description = Objects.requireNonNullElse(params.description,
          "Staging repository: " + pomInfo.groupId + "."
              + pomInfo.artifactId + ":" + pomInfo.version);
      String stagingRepoId = nexus.createStagingRepository(profileId, description);
      System.out.println("  => Staging repository ID: " + stagingRepoId);

      System.out.printf("Uploading %s artifact(s).%n", artifacts.size());
      for (Path path : artifacts) {
        Path relative = params.mavenDir.relativize(path);
        List<String> urlSegments = new ArrayList<>();
        for (Path segment : relative) {
          urlSegments.add(URLEncoder.encode(segment.toString(), StandardCharsets.UTF_8));
        }
        String relativeUrl = String.join("/", urlSegments);
        System.out.println("  => " + relative + ": " + relativeUrl);
        nexus.uploadArtifact(stagingRepoId, path, relativeUrl);
      }

      System.out.println("Closing the staging repository.");
      nexus.closeStagingRepository(profileId, stagingRepoId);
      System.out.println("  => Staging repository is available at: ");
      System.out.println("     https://repository.apache.org/content/repositories/" + stagingRepoId);

      System.out.println();
      System.out.println("You must review and release the staging repository manually from Nexus GUI!");
    } catch (Exception e) {
      System.err.println("Something went wrong: " + e.getMessage());
      System.exit(1);
    }
  }
}