This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
generated from homecentr/docker-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Certificate file permissions (#53)
- Loading branch information
Showing
12 changed files
with
180 additions
and
18 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
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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
#!/usr/bin/env ash | ||
|
||
certbot certonly --agree-tos --non-interactive --no-permissions-check $CERTBOT_ARGS | ||
# Execute certbot | ||
certbot certonly --agree-tos --non-interactive --logs-dir /logs $CERTBOT_ARGS | ||
|
||
# Fix file permissions | ||
chown "$PUID" -R /etc/letsencrypt | ||
chgrp "${CERTS_GID:-$PGID}" -R /etc/letsencrypt | ||
chmod "0750" -R /etc/letsencrypt | ||
|
||
# Copy files over to /data while preserving the file permissions | ||
cp -p /etc/letsencrypt/live/*/*.pem /data | ||
|
||
|
||
cat /logs/* |
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,29 @@ | ||
#!/usr/bin/with-contenv ash | ||
|
||
EXEC_USER=$(cat /var/run/s6/container_environment/EXEC_USER) | ||
|
||
if [ "$CERTS_GID" == "0" ] || [ "$CERTS_GID" == "" ] | ||
then | ||
# User doesn't want special group ownership, the group owner of the files will be PGID, skip creating the group | ||
return | ||
fi | ||
|
||
if [ "$CERTS_GID" == "$PGID" ] | ||
then | ||
CERTS_GID=$PGID | ||
fi | ||
|
||
# Check if the group already exists | ||
cat /etc/group | grep ^ssl-readers: > /dev/null | ||
|
||
if [ $? == 0 ] | ||
then | ||
# Group already exists, delete it | ||
delgroup ssl-readers | ||
fi | ||
|
||
# Make sure we really need to create a separate group | ||
if [ "$PGID" != "$CERTS_GID" ] | ||
then | ||
addgroup -g $CERTS_GID ssl-readers | ||
fi |
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,5 @@ | ||
#!/usr/bin/with-contenv ash | ||
|
||
chown "$PUID" -R /data | ||
chgrp "${CERTS_GID:-$PGID}" -R /data | ||
chmod "0750" -R /data |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
86 changes: 86 additions & 0 deletions
86
tests/src/test/java/CertbotContainerWithCertsGidEnvShould.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,86 @@ | ||
import helpers.CertbotDockerTagResolver; | ||
import io.homecentr.testcontainers.containers.GenericContainerEx; | ||
import io.homecentr.testcontainers.containers.wait.strategy.WaitEx; | ||
import io.homecentr.testcontainers.images.PullPolicyEx; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
|
||
import static io.homecentr.testcontainers.WaitLoop.waitFor; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class CertbotContainerWithCertsGidEnvShould { | ||
private static final Logger logger = LoggerFactory.getLogger(CertbotContainerShould.class); | ||
|
||
private static GenericContainerEx _certbotContainer; | ||
private static TestConfiguration _testConfig; | ||
|
||
@BeforeClass | ||
public static void before() throws Exception { | ||
_testConfig = TestConfiguration.create(); | ||
_testConfig.createCredentialsSecretFile(); | ||
|
||
_certbotContainer = new GenericContainerEx<>(new CertbotDockerTagResolver()) | ||
.withEnv("PUID", "9001") | ||
.withEnv("PGID", "9002") | ||
.withEnv("CRON_SCHEDULE", "* * * * *") | ||
.withEnv("CERTBOT_ARGS", _testConfig.getCertbotArgs()) | ||
.withEnv("CERTS_GID", "9003") | ||
.withFileSystemBind(TestConfiguration.cloudflareCredentialsHostPath, TestConfiguration.cloudflareCredentialsContainerPath) | ||
.withImagePullPolicy(PullPolicyEx.never()) | ||
.waitingFor(WaitEx.forS6OverlayStart()); | ||
|
||
_certbotContainer.start(); | ||
_certbotContainer.followOutput(new Slf4jLogConsumer(logger)); | ||
|
||
waitFor(Duration.ofSeconds(80), () -> _certbotContainer.getLogsAnalyzer().contains("Execution finished")); | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
_certbotContainer.close(); | ||
} | ||
|
||
@Test | ||
public void createCertificateFullChainFile() throws IOException, InterruptedException { | ||
assertTrue(fileExists("/data/fullchain.pem")); | ||
|
||
assertEquals((Integer)9001, _certbotContainer.getFileOwnerUid("/data/fullchain.pem")); | ||
assertEquals((Integer)9003, _certbotContainer.getFileOwningGid("/data/fullchain.pem")); | ||
} | ||
|
||
@Test | ||
public void createCertificateChainFile() throws IOException, InterruptedException { | ||
assertTrue(fileExists("/data/chain.pem")); | ||
|
||
assertEquals((Integer)9001, _certbotContainer.getFileOwnerUid("/data/chain.pem")); | ||
assertEquals((Integer)9003, _certbotContainer.getFileOwningGid("/data/chain.pem")); | ||
} | ||
|
||
@Test | ||
public void createPrivateKeyFile() throws IOException, InterruptedException { | ||
assertTrue(fileExists("/data/privkey.pem")); | ||
|
||
assertEquals((Integer)9001, _certbotContainer.getFileOwnerUid("/data/privkey.pem")); | ||
assertEquals((Integer)9003, _certbotContainer.getFileOwningGid("/data/privkey.pem")); | ||
} | ||
|
||
@Test | ||
public void createPublicKeyFile() throws IOException, InterruptedException { | ||
assertTrue(fileExists("/data/cert.pem")); | ||
|
||
assertEquals((Integer)9001, _certbotContainer.getFileOwnerUid("/data/cert.pem")); | ||
assertEquals((Integer)9003, _certbotContainer.getFileOwningGid("/data/cert.pem")); | ||
} | ||
|
||
private boolean fileExists(String fileNamePattern) throws IOException, InterruptedException { | ||
return _certbotContainer.execInContainer("ls", fileNamePattern).getExitCode() == 0; | ||
} | ||
} |
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