Skip to content

Commit

Permalink
Add test for changing admin password.
Browse files Browse the repository at this point in the history
Make /password.txt writable so that it can be changed when admin password changes.
  • Loading branch information
OndroMih committed Jan 19, 2025
1 parent c8bbe0b commit 21920c8
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 63 deletions.
9 changes: 5 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.glassfish.main.distributions</groupId>
Expand Down Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.11.3</version>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -42,13 +42,14 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.20.3</version>
<version>1.20.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.20.3</version>
<version>1.20.4</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ RUN true \
&& echo "Generating password file at ${AS_PASSWORD_FILE} ..." \
&& set +x \
&& echo "AS_ADMIN_PASSWORD=${AS_ADMIN_PASSWORD}" > "${AS_PASSWORD_FILE}" \
&& chown glassfish:glassfish "${AS_PASSWORD_FILE}" \
&& echo "AS_ADMIN_PASSWORD=" > "${PATH_GF_PASSWORD_FILE_FOR_CHANGE}" \
&& echo "AS_ADMIN_NEWPASSWORD=${AS_ADMIN_PASSWORD}" >> "${PATH_GF_PASSWORD_FILE_FOR_CHANGE}" \
&& echo "" >> "${PATH_GF_PASSWORD_FILE_FOR_CHANGE}" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@

package org.glassfish.main.distributions.docker;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.glassfish.main.distributions.docker.CommonIntegrationTests.assertDefaultServerRoot;


/**
*
Expand All @@ -45,18 +39,6 @@ public class AsadminIT {

@Test
void getRoot() throws Exception {
URL url = URI.create("http://localhost:" + server.getMappedPort(8080) + "/").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content;
try {
connection.setRequestMethod("GET");
assertEquals(200, connection.getResponseCode(), "Response code");
try (InputStream in = connection.getInputStream()) {
content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
} finally {
connection.disconnect();
}
assertThat(content, stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality"));
assertDefaultServerRoot(server);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.distributions.docker;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.glassfish.main.distributions.docker.CommonIntegrationTests.assertDefaultServerRoot;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
*
* @author Ondro Mihalyi
*/
@Testcontainers
public class ChangePasswordsIT {

@SuppressWarnings({"rawtypes", "resource"})
@Container
private final GenericContainer server = new GenericContainer<>(System.getProperty("docker.glassfish.image"))
.withExposedPorts(8080, 4848)
.withEnv("AS_ADMIN_MASTERPASSWORD", "mymasterpassword")
.withEnv("AS_ADMIN_PASSWORD", "myadminpassword")
.withLogConsumer(o -> System.err.print("GF: " + o.getUtf8String()));

@Test
void getRoot() throws Exception {
assertDefaultServerRoot(server);
}

@Test
void customAdminPassword() throws Exception {
URI uri = URI.create("https://localhost:" + server.getMappedPort(4848) + "/management/domain.json");

try (HttpClient client = newInsecureHttpClient()) {
String basicAuthValue = basicAuthValue("admin", "myadminpassword");
final HttpResponse<Void> response = client.send(HttpRequest.newBuilder(uri)
.setHeader("Authorization", basicAuthValue)
.build(), HttpResponse.BodyHandlers.discarding());
assertEquals(200, response.statusCode(), "Response code");
}
}

private static HttpClient newInsecureHttpClient() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new SecureRandom());
HttpClient client = HttpClient.newBuilder()
.sslContext(sslContext)
.build();
return client;
}

private static String basicAuthValue(String user, String password) {
String userCredentials = user + ":" + password;
String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
return basicAuth;
}

private static final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}

@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
}
}
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.main.distributions.docker;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.testcontainers.containers.GenericContainer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
*
* @author Ondro Mihalyi
*/
public final class CommonIntegrationTests {

private CommonIntegrationTests() {
}

static void assertDefaultServerRoot(GenericContainer server) throws Exception {
URL url = URI.create("http://localhost:" + server.getMappedPort(8080) + "/").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content;
try {
connection.setRequestMethod("GET");
assertEquals(200, connection.getResponseCode(), "Response code");
try (InputStream in = connection.getInputStream()) {
content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
} finally {
connection.disconnect();
}
assertThat(content, stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality"));
}

static void assertEmbeddedDefaultRoot(GenericContainer server) throws Exception {
URL url = URI.create("http://localhost:" + server.getMappedPort(8080) + "/").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("GET");
assertEquals(404, connection.getResponseCode(), "Response code");
} finally {
connection.disconnect();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

package org.glassfish.main.distributions.docker;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame.OutputType;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.glassfish.main.distributions.docker.CommonIntegrationTests.assertEmbeddedDefaultRoot;

/**
*
Expand All @@ -46,13 +43,6 @@ public class RunembeddedIT {

@Test
void getRoot() throws Exception {
URL url = URI.create("http://localhost:" + server.getMappedPort(8080) + "/").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
connection.setRequestMethod("GET");
assertEquals(404, connection.getResponseCode(), "Response code");
} finally {
connection.disconnect();
}
assertEmbeddedDefaultRoot(server);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,24 @@

package org.glassfish.main.distributions.docker;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.glassfish.main.distributions.docker.CommonIntegrationTests.assertDefaultServerRoot;

/**
*
*/
@Testcontainers
public class StartServIT {

@SuppressWarnings({"rawtypes", "resource"})
@Container
private final GenericContainer server = new GenericContainer<>(System.getProperty("docker.glassfish.image"))
.withCommand("startserv").withExposedPorts(8080)
.withExposedPorts(8080)
.withLogConsumer(o -> System.err.print("GF: " + o.getUtf8String()));

@Test
void getRoot() throws Exception {
URL url = URI.create("http://localhost:" + server.getMappedPort(8080) + "/").toURL();
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String content;
try {
connection.setRequestMethod("GET");
assertEquals(200, connection.getResponseCode(), "Response code");
try (InputStream in = connection.getInputStream()) {
content = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
} finally {
connection.disconnect();
}
assertThat(content, stringContainsInOrder("Eclipse GlassFish", "index.html", "production-quality"));
assertDefaultServerRoot(server);
}
}

0 comments on commit 21920c8

Please sign in to comment.