Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 97c71cc

Browse files
committed
Add the ability to talk to Kong when the admin endpoint sits behind a self signed cert
1 parent 23489d4 commit 97c71cc

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ docker run -it --rm \
118118
-e TEST_CERT=true \
119119
phpdockerio/kong-certbot-agent
120120

121+
# Get a certificate for a single domain, and submit to kong
122+
# Kong's admin API is behind a self-signed certificate
123+
docker run -it --rm \
124+
-e KONG_ENDPOINT=http://kong-admin:8001 \
125+
-e EMAIL=foo@bar.com \
126+
-e DOMAINS=bar.com \
127+
-e ALLOW_SELF_SIGNED_CERT_KONG=true \
128+
phpdockerio/kong-certbot-agent
121129
```
122130

123131
## FAQ

entrypoint.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ if [[ ! -z "${TEST_CERT}" ]]; then
55
EXTRA_PARAMS="--test-cert"
66
fi;
77

8+
if [[ ! -z "${ALLOW_SELF_SIGNED_CERT_KONG}" ]]; then
9+
EXTRA_PARAMS="--allow-self-signed-cert-kong"
10+
fi;
11+
812
exec /workdir/certbot-agent certs:update ${EXTRA_PARAMS} ${KONG_ENDPOINT} ${EMAIL} ${DOMAINS}

src/Certbot/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function acquireCertificate(array $domains, string $email, bool $testCert
8787
}
8888

8989
// Ensure certs are readable
90-
$fullChain = file_get_contents($fullChainPath);
90+
$fullChain = file_get_contents($fullChainPath);
9191
$privateKey = file_get_contents($privateKeyPath);
9292

9393
return new Certificate(

src/Command/UpdateCertificatesCommand.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,31 @@ protected function configure(): void
5656
$this
5757
->setDescription('Requests certificates from Let\'s Encrypt for the given domains and notifies Kong')
5858
->addArgument(
59-
'kong-endpoint',
60-
InputArgument::REQUIRED,
61-
'Base URL to Kong Admin API; eg: https://foo:8001'
59+
name: 'kong-endpoint',
60+
mode: InputArgument::REQUIRED,
61+
description: 'Base URL to Kong Admin API; eg: https://foo:8001'
6262
)
6363
->addArgument(
64-
'email',
65-
InputArgument::REQUIRED,
66-
'Email the set of domains is to be associated with at Let\'s Encrypt'
64+
name: 'email',
65+
mode: InputArgument::REQUIRED,
66+
description: 'Email the set of domains is to be associated with at Let\'s Encrypt'
6767
)
6868
->addArgument(
69-
'domains',
70-
InputArgument::REQUIRED,
71-
'Comma separated list of domains to request certs for; eg: bar.com,foo.bar.com'
69+
name: 'domains',
70+
mode: InputArgument::REQUIRED,
71+
description: 'Comma separated list of domains to request certs for; eg: bar.com,foo.bar.com'
7272
)
7373
->addOption(
74-
'test-cert',
75-
't',
76-
InputOption::VALUE_NONE,
77-
'Require test certificate from staging-letsencrypt'
74+
name: 'test-cert',
75+
shortcut: 't',
76+
mode: InputOption::VALUE_NONE,
77+
description: 'Require test certificate from staging-letsencrypt'
78+
)
79+
->addOption(
80+
name: 'allow-self-signed-cert-kong',
81+
shortcut: '-s',
82+
mode: InputOption::VALUE_NONE,
83+
description: "Allow self signed certs in Kong's admin endpoint",
7884
);
7985
}
8086

@@ -107,6 +113,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
107113
/** @var bool $testCert */
108114
$testCert = $input->getOption('test-cert');
109115

116+
/** @var bool $allowSelfSignedCert */
117+
$allowSelfSignedCert = $input->getOption('allow-self-signed-cert-kong');
118+
110119
$this->validateInput($email, $kongAdminUri, $domains);
111120

112121
// Acquire certificates from certbot. This is not all-or-nothing, whatever certs we acquire come out here
@@ -116,7 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
116125
$certificate = $this->certbot->acquireCertificate($domains, $email, $testCert);
117126

118127
// Store certs into kong via the admin UI. Again, not all-or-nothing
119-
if ($this->kong->store($certificate, $kongAdminUri) === true) {
128+
if ($this->kong->store($certificate, $kongAdminUri, $allowSelfSignedCert) === true) {
120129
$certOrCerts = count($certificate->getDomains()) > 1 ? 'Certificates' : 'Certificate';
121130

122131
$output->writeln(sprintf('%s for %s correctly sent to Kong', $certOrCerts, $outputDomains));

src/Kong/Handler.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(private ClientInterface $guzzle)
2828
/**
2929
* Stores the given certificate in Kong.
3030
*/
31-
public function store(Certificate $certificate, string $kongAdminUri): bool
31+
public function store(Certificate $certificate, string $kongAdminUri, bool $allowSelfSignedCert): bool
3232
{
3333
$payload = [
3434
'headers' => [
@@ -39,16 +39,17 @@ public function store(Certificate $certificate, string $kongAdminUri): bool
3939
'key' => $certificate->getKey(),
4040
'snis' => $certificate->getDomains(),
4141
],
42+
'verify' => !$allowSelfSignedCert,
4243
];
4344

4445
// From Kong 0.14, they finally fixed PUT as UPSERT
4546
// Any domain can be used on the endpoint, as they're aliased internally to the single
4647
// certificate object within Kong
4748
try {
4849
$this->guzzle->request(
49-
'put',
50-
sprintf('%s/certificates/%s', $kongAdminUri, $certificate->getDomains()[0]),
51-
$payload
50+
method: 'put',
51+
uri: sprintf('%s/certificates/%s', $kongAdminUri, $certificate->getDomains()[0]),
52+
options: $payload,
5253
);
5354
} catch (BadResponseException $ex) {
5455
$request = $ex->getRequest();

0 commit comments

Comments
 (0)