-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from kekru/development
Update Nginx, Configure CA expiration and tests for cert generation
- Loading branch information
Showing
9 changed files
with
197 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
FROM nginx:1.15.12-alpine | ||
# Original Dockerfile: https://github.com/nginxinc/docker-nginx/tree/5488180ebdd45b12b45107694dfa92dc878a2795/stable/alpine | ||
FROM nginx:1.18.0-alpine | ||
LABEL MAINTAINER="Kevin Krummenauer <[email protected]>" | ||
RUN apk add --no-cache openssl | ||
|
||
COPY resources /script | ||
COPY resources/create-certs.sh /script/create-certs.sh | ||
COPY resources/nginx-cert.conf /etc/nginx/nginx.conf | ||
COPY resources/entrypoint.sh /docker-entrypoint.d/30_entrypoint.sh | ||
|
||
RUN cp /script/nginx-cert.conf /etc/nginx/nginx.conf \ | ||
&& chmod +x /script/create-certs.sh /script/entrypoint.sh | ||
RUN chmod +x /script/create-certs.sh /docker-entrypoint.d/30_entrypoint.sh | ||
|
||
ENV CREATE_CERTS_WITH_PW="" \ | ||
CERTS_DIR=/data/certs \ | ||
CERT_HOSTNAME="myserver.example.com" | ||
|
||
ENTRYPOINT ["/script/entrypoint.sh"] | ||
CMD ["nginx", "-g", "daemon off;"] | ||
CERT_HOSTNAME="abc.127.0.0.1.nip.io" \ | ||
CERT_EXPIRATION_DAYS="365" \ | ||
CA_EXPIRATION_DAYS="900" | ||
|
||
HEALTHCHECK --start-period=1s \ | ||
--interval=5s \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,20 @@ | |
import static org.junit.Assert.assertThrows; | ||
|
||
import de.kekru.dockerremoteapitls.test.utils.AbstractIntegrationTest; | ||
import de.kekru.dockerremoteapitls.test.utils.CertUtils; | ||
import java.io.File; | ||
import java.time.LocalDate; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class BasicConnectionTest extends AbstractIntegrationTest { | ||
|
||
@BeforeClass | ||
public static void init() { | ||
startRemoteApiContainer("CREATE_CERTS_WITH_PW=supersecret"); | ||
startRemoteApiContainer( | ||
"CERT_HOSTNAME=abc.127.0.0.1.nip.io", | ||
"CREATE_CERTS_WITH_PW=supersecret" | ||
); | ||
} | ||
|
||
@Test | ||
|
@@ -54,4 +60,40 @@ public void failsOnNoTls() { | |
.hasMessageContaining("error during connect: Get http://abc.127.0.0.1.nip.io:30129/v1.40/containers/json: EOF"); | ||
} | ||
|
||
@Test | ||
public void caCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDir + "/ca-cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("[email protected], CN=example.com, OU=IT, O=ExampleCompany, L=London, ST=London, C=GB"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(900)); | ||
} | ||
|
||
@Test | ||
public void serverCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDir + "/server-cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("CN=abc.127.0.0.1.nip.io"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(365)); | ||
} | ||
|
||
@Test | ||
public void clientCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDirClient + "/cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("CN=testClient"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(365)); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
test/src/test/java/de/kekru/dockerremoteapitls/test/CertGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package de.kekru.dockerremoteapitls.test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import de.kekru.dockerremoteapitls.test.utils.AbstractIntegrationTest; | ||
import de.kekru.dockerremoteapitls.test.utils.CertUtils; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.time.LocalDate; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class CertGenerationTest extends AbstractIntegrationTest { | ||
|
||
@BeforeClass | ||
public static void init() { | ||
startRemoteApiContainer( | ||
"CERT_HOSTNAME=something-else.127.0.0.1.nip.io", | ||
"CREATE_CERTS_WITH_PW=supersecret123", | ||
"CERT_EXPIRATION_DAYS=17", | ||
"CA_EXPIRATION_DAYS=1273" | ||
); | ||
} | ||
|
||
@Test | ||
public void caCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDir + "/ca-cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("[email protected], CN=example.com, OU=IT, O=ExampleCompany, L=London, ST=London, C=GB"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(1273)); | ||
} | ||
|
||
@Test | ||
public void serverCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDir + "/server-cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("CN=something-else.127.0.0.1.nip.io"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(17)); | ||
} | ||
|
||
@Test | ||
public void clientCertHasCorrectDefaultValues() { | ||
|
||
CertUtils caCert = new CertUtils(new File(certsDirClient + "/cert.pem")); | ||
|
||
assertThat(caCert.getCert().getSubjectDN().getName()) | ||
.isEqualTo("CN=testClient"); | ||
|
||
assertThat(caCert.getExpiresAt()) | ||
.isEqualTo(LocalDate.now().plusDays(17)); | ||
} | ||
|
||
@Test | ||
public void caCertInClientDirIsSameAsInServerDir() throws IOException { | ||
|
||
String ca = FileUtils.readFileToString(new File(certsDir + "/ca-cert.pem"), "UTF-8"); | ||
String caInClientDir = FileUtils.readFileToString(new File(certsDirClient + "/ca.pem"), "UTF-8"); | ||
|
||
assertThat(ca).isEqualTo(caInClientDir); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
test/src/test/java/de/kekru/dockerremoteapitls/test/utils/CertUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.kekru.dockerremoteapitls.test.utils; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
import java.time.LocalDate; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
import javax.security.cert.X509Certificate; | ||
|
||
public class CertUtils { | ||
|
||
private final X509Certificate cert; | ||
|
||
public CertUtils(File file) { | ||
cert = getCert(file); | ||
} | ||
|
||
private X509Certificate getCert(File file) { | ||
try (InputStream in = new FileInputStream(file)) { | ||
return X509Certificate.getInstance(in); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public LocalDate getExpiresAt() { | ||
return toLocalDate(cert.getNotAfter()); | ||
} | ||
|
||
public LocalDate toLocalDate(Date dateToConvert) { | ||
return dateToConvert.toInstant() | ||
.atZone(ZoneId.systemDefault()) | ||
.toLocalDate(); | ||
} | ||
|
||
public X509Certificate getCert() { | ||
return cert; | ||
} | ||
} |