Skip to content

Commit

Permalink
Fix #21, Authentication not passed properly into wagon when using enc…
Browse files Browse the repository at this point in the history
…ryption in settings.xml
  • Loading branch information
chonton committed Jan 18, 2020
1 parent 60d0b77 commit c7a35f3
Show file tree
Hide file tree
Showing 16 changed files with 314 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Check if a maven artifact exists. Designed around the use case of skipping deployment if the stable version already exists.

## Goals
There are two goals: [local](https://chonton.github.io/exists-maven-plugin/0.1.0/local-mojo.html) checks
There are two goals: [local](https://chonton.github.io/exists-maven-plugin/0.2.0/local-mojo.html) checks
if the just built artifact is already in the local repository;
and [remote](https://chonton.github.io/exists-maven-plugin/0.1.0/remote-mojo.html) checks
and [remote](https://chonton.github.io/exists-maven-plugin/0.2.0/remote-mojo.html) checks
if the just built artifact is already in the remote repository.

Mojo details at [plugin info](https://chonton.github.io/exists-maven-plugin/0.1.0/plugin-info.html)
Mojo details at [plugin info](https://chonton.github.io/exists-maven-plugin/0.2.0/plugin-info.html)

## Parameters
Every parameter can be set with a maven property **exists.**_<parameter_name\>_. e.g. skip parameter can
Expand Down Expand Up @@ -44,7 +44,7 @@ be set from command line -Dexists.skip=true
<plugin>
<groupId>org.honton.chas</groupId>
<artifactId>exists-maven-plugin</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<executions>
<execution>
<goals>
Expand Down
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.honton.chas</groupId>
<artifactId>exists-maven-plugin</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<packaging>maven-plugin</packaging>

<name>Artifact Exists Maven Plugin</name>
Expand Down Expand Up @@ -186,6 +186,9 @@
<filterProperties>
<webserver.port>1080</webserver.port>
</filterProperties>
<properties>
<settings.security>${project.basedir}/src/it/settings-security.xml</settings.security>
</properties>
</configuration>
</plugin>

Expand Down Expand Up @@ -377,6 +380,7 @@
<profiles>
<!-- make sure to release for Java 8 -->
<profile>
<id>release for java 8</id>
<activation>
<jdk>[9,)</jdk>
</activation>
Expand Down
2 changes: 1 addition & 1 deletion src/it/deploy-with-auth/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.honton.chas</groupId>
<artifactId>exists-it-parent</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>exists-deploy-with-auth-it</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/it/deploy-with-encrypted-auth/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.goals = clean verify
invoker.failureBehavior = fail-at-end
145 changes: 145 additions & 0 deletions src/it/deploy-with-encrypted-auth/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?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>

<parent>
<groupId>org.honton.chas</groupId>
<artifactId>exists-it-parent</artifactId>
<version>0.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>exists-deploy-with-encrypted-auth-it</artifactId>

<distributionManagement>

<repository>
<id>test</id>
<url>http://localhost:${deploy.webserver.port}/repo</url>
</repository>

<snapshotRepository>
<id>test</id>
<url>http://localhost:${deploy.webserver.port}/repo</url>
</snapshotRepository>

</distributionManagement>

<properties>
<deploy.webserver.port>@deploy.webserver.port@</deploy.webserver.port>
</properties>

<dependencies>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
<version>2.3.1</version>
</dependency>

</dependencies>

<build>
<plugins>

<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>classpath</id>
<goals>
<goal>build-classpath</goal>
</goals>
<phase>validate</phase>
<configuration>
<includeScope>runtime</includeScope>
<outputProperty>runtime.classpath</outputProperty>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<!-- deploy in earlier than usual phase -->
<execution>
<goals>
<goal>deploy</goal>
</goals>
<phase>integration-test</phase>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.honton.chas</groupId>
<artifactId>exists-maven-plugin</artifactId>
<executions>
<execution>
<id>before-installation-with-encrypted-auth</id>
<goals>
<goal>remote</goal>
</goals>
<phase>test</phase>
<configuration>
<repository>http://localhost:${deploy.webserver.port}/auth/repo</repository>
<serverId>with-encrypted-auth</serverId>
</configuration>
</execution>

<execution>
<id>after-installation-with-encrypted-auth</id>
<goals>
<goal>remote</goal>
</goals>
<phase>integration-test</phase>
<configuration>
<repository>http://localhost:${deploy.webserver.port}/auth/repo</repository>
<serverId>with-encrypted-auth</serverId>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.honton.chas</groupId>
<artifactId>process-exec-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>start</goal>
</goals>
<phase>process-test-classes</phase>
<configuration>
<waitAfterLaunch>2</waitAfterLaunch>
<arguments>
<argument>${java.home}/bin/java</argument>
<argument>-classpath</argument>
<argument>${runtime.classpath}${path.separator}${project.build.outputDirectory}</argument>
<argument>org.honton.chas.exists.example.WebServer</argument>
<argument>${deploy.webserver.port}</argument>
</arguments>
</configuration>
</execution>

<execution>
<id>stop-all</id>
<goals>
<goal>stop-all</goal>
</goals>
<phase>post-integration-test</phase>
</execution>
</executions>
</plugin>

</plugins>

</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package org.honton.chas.exists.example;

import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoHTTPD.Response.Status;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class WebServer extends NanoHTTPD {
private static final Logger LOG = Logger.getLogger(WebServer.class.getName());

private static final Map<String, String> TYPES = new HashMap<>();
static {
TYPES.put("jar", "application/java-archive");
TYPES.put("asc", "text/plain");
TYPES.put("pom", "text/xml");
}

private Map<String, byte[]> storage = new HashMap<>();

public WebServer(int port) throws IOException {
super(port);
LOG.fine("Server running on port " + port);
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
}

public static void main(String[] args) throws IOException {
new WebServer(Integer.parseInt(args[0]));
}

byte[] getInput(IHTTPSession session) throws IOException {
int length = Integer.parseInt(getHeader(session, "Content-Length"));
byte[] content = new byte[length];
session.getInputStream().read(content);
return content;
}

private String getHeader(IHTTPSession session, String key) {
for(Map.Entry<String, String> header : session.getHeaders().entrySet()) {
if(header.getKey().equalsIgnoreCase(key)) {
return header.getValue();
}
} return null;
}

@Override
public Response serve(IHTTPSession session) {
LOG.fine(session.getMethod() + " " + session.getUri());
Response response = generateResponse(session);
LOG.fine("Response: " + response.getStatus());
return response;
}

private Response generateResponse(IHTTPSession session) {
String uri = session.getUri();
if (uri.startsWith("/auth")) {
String authorization = session.getHeaders().get("authorization");
if (authorization == null || !authorization.equals("Basic dXNlcjE6cGFzc3dvcmQxMjM=")) {
Response response = NanoHTTPD.newFixedLengthResponse(Status.UNAUTHORIZED, "", null);
response.addHeader("WWW-Authenticate", "Basic realm=\"Authentication needed\"");
return response;
}
uri = uri.substring("/auth".length());
}
String type = getType(uri);
switch (session.getMethod()) {
case HEAD: {
if (!storage.containsKey(uri)) {
return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, null);
}
return NanoHTTPD.newFixedLengthResponse(Status.OK, type, null);
}
case GET: {
byte[] file = storage.get(uri);
if (file == null) {
return NanoHTTPD.newFixedLengthResponse(Status.NOT_FOUND, type, uri);
}
return newFixedLengthResponse(Status.OK, type, new ByteArrayInputStream(file), (long)file.length);
}
case PUT: {
try {
storage.put(uri, getInput(session));
return NanoHTTPD.newFixedLengthResponse(Status.OK, "", uri);
}
catch (IOException e) {
return NanoHTTPD.newFixedLengthResponse(Status.INTERNAL_ERROR, "text/plain", e.getMessage());
}
}
default:
return NanoHTTPD.newFixedLengthResponse(Status.NOT_IMPLEMENTED, "text/plain", session.getMethod() + " not supported");
}
}

private String getType(String uri) {
int dot = uri.lastIndexOf('.');
if (dot == -1) {
return "text/plain";
} else {
String suffix = uri.substring(dot + 1);
String type = TYPES.get(suffix);
return type != null ? type : "text/" + suffix;
}
}
}
25 changes: 25 additions & 0 deletions src/it/deploy-with-encrypted-auth/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import java.io.BufferedReader;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

void findLine(BufferedReader reader, String expected) {
for(;;) {
String line = reader.readLine();
if(line == null) {
throw new IllegalStateException(expected + " not found");
}
if(line.matches(expected)) {
return;
}
}
}

File file = new File(basedir, "build.log" );
BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);

findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(before-installation-with-encrypted-auth\\) @ exists-deploy-with-encrypted-auth-it ---");
findLine(reader, "\\[INFO\\] org.honton.chas:exists-deploy-with-encrypted-auth-it:[0-9\\.]* does not exist");

findLine(reader, "\\[INFO\\] --- exists-maven-plugin:[0-9\\.]*:remote \\(after-installation-with-encrypted-auth\\) @ exists-deploy-with-encrypted-auth-it ---");
findLine(reader, "\\[INFO\\] setting maven.deploy.skip=true");
3 changes: 2 additions & 1 deletion src/it/deploy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.honton.chas</groupId>
<artifactId>exists-it-parent</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>exists-deploy-it</artifactId>
Expand Down Expand Up @@ -35,6 +35,7 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.nanohttpd</groupId>
<artifactId>nanohttpd</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/it/fail/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.honton.chas</groupId>
<artifactId>exists-it-parent</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>exists-fail-it</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/it/install/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.honton.chas</groupId>
<artifactId>exists-it-parent</artifactId>
<version>0.1.0</version>
<version>0.2.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>exists-install-it</artifactId>
Expand Down
Loading

0 comments on commit c7a35f3

Please sign in to comment.