-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jmx): custom entrypoint to support copying TLS certs into trusts…
…tore for JMX (#330)
- Loading branch information
1 parent
3331284
commit 2c22fac
Showing
11 changed files
with
215 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
DIR="$(dirname "$(realpath "$0")")" | ||
source "${DIR}/genpass.bash" | ||
|
||
function banner() { | ||
echo "+------------------------------------------+" | ||
printf "| %-40s |\n" "$(date)" | ||
echo "| |" | ||
printf "| %-40s |\n" "$@" | ||
echo "+------------------------------------------+" | ||
} | ||
|
||
PWFILE="/tmp/jmxremote.password" | ||
USRFILE="/tmp/jmxremote.access" | ||
function createJmxCredentials() { | ||
if [ -z "$CRYOSTAT_RJMX_USER" ]; then | ||
CRYOSTAT_RJMX_USER="cryostat" | ||
fi | ||
if [ -z "$CRYOSTAT_RJMX_PASS" ]; then | ||
CRYOSTAT_RJMX_PASS="$(genpass)" | ||
fi | ||
|
||
echo -n "$CRYOSTAT_RJMX_USER $CRYOSTAT_RJMX_PASS" > "$PWFILE" | ||
chmod 400 "$PWFILE" | ||
echo -n "$CRYOSTAT_RJMX_USER readwrite" > "$USRFILE" | ||
chmod 400 "$USRFILE" | ||
} | ||
|
||
function importTrustStores() { | ||
if [ -z "$CONF_DIR" ]; then | ||
CONF_DIR="/opt/cryostat.d" | ||
fi | ||
if [ -z "$SSL_TRUSTSTORE_DIR" ]; then | ||
SSL_TRUSTSTORE_DIR="/truststore" | ||
fi | ||
|
||
if [ ! -d "$SSL_TRUSTSTORE_DIR" ]; then | ||
banner "$SSL_TRUSTSTORE_DIR does not exist; no certificates to import" | ||
return 0 | ||
elif [ ! "$(ls -A "$SSL_TRUSTSTORE_DIR")" ]; then | ||
banner "$SSL_TRUSTSTORE_DIR is empty; no certificates to import" | ||
return 0 | ||
fi | ||
|
||
SSL_TRUSTSTORE_PASS="$(cat "${SSL_TRUSTSTORE_PASS_FILE:-$CONF_DIR/truststore.pass}")" | ||
|
||
find "$SSL_TRUSTSTORE_DIR" -type f | while IFS= read -r cert; do | ||
echo "Importing certificate $cert ..." | ||
|
||
keytool -importcert -v \ | ||
-noprompt \ | ||
-alias "imported-$(basename "$cert")" \ | ||
-trustcacerts \ | ||
-keystore "${SSL_TRUSTSTORE:-$CONF_DIR/truststore.p12}" \ | ||
-file "$cert"\ | ||
-storepass "$SSL_TRUSTSTORE_PASS" | ||
done | ||
|
||
FLAGS+=( | ||
"-Djavax.net.ssl.trustStore=$SSL_TRUSTSTORE" | ||
"-Djavax.net.ssl.trustStorePassword=$SSL_TRUSTSTORE_PASS" | ||
) | ||
} | ||
|
||
FLAGS=() | ||
importTrustStores | ||
|
||
if [ "$CRYOSTAT_DISABLE_JMX_AUTH" = "true" ]; then | ||
banner "JMX Auth Disabled" | ||
FLAGS+=("-Dcom.sun.management.jmxremote.authenticate=false") | ||
else | ||
createJmxCredentials | ||
FLAGS+=("-Dcom.sun.management.jmxremote.authenticate=true") | ||
FLAGS+=("-Dcom.sun.management.jmxremote.password.file=$PWFILE") | ||
FLAGS+=("-Dcom.sun.management.jmxremote.access.file=$USRFILE") | ||
fi | ||
|
||
export JAVA_OPTS_APPEND="${JAVA_OPTS_APPEND} ${FLAGS[*]}" | ||
exec $1 |
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/env bash | ||
|
||
genpass() { | ||
< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32 | ||
} |
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,24 @@ | ||
#!/usr/bin/env bash | ||
|
||
set -e | ||
|
||
DIR="$(dirname "$(realpath "$0")")" | ||
source "${DIR}/genpass.bash" | ||
|
||
SSL_TRUSTSTORE_PASS="$(genpass)" | ||
|
||
echo "$SSL_TRUSTSTORE_PASS" > "$SSL_TRUSTSTORE_PASS_FILE" | ||
|
||
trap "cd -" EXIT | ||
cd "$CONF_DIR" | ||
|
||
keytool -importkeystore \ | ||
-noprompt \ | ||
-storetype PKCS12 \ | ||
-srckeystore /usr/lib/jvm/jre-17-openjdk/lib/security/cacerts \ | ||
-srcstorepass changeit \ | ||
-destkeystore "$SSL_TRUSTSTORE" \ | ||
-deststorepass "$SSL_TRUSTSTORE_PASS" | ||
|
||
chmod 664 "${SSL_TRUSTSTORE}" | ||
chmod 640 "${SSL_TRUSTSTORE_PASS_FILE}" |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The Cryostat Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.cryostat.security; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
|
||
import io.cryostat.ConfigProperties; | ||
|
||
import io.smallrye.common.annotation.Blocking; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
import org.jboss.logging.Logger; | ||
|
||
@Path("") | ||
public class TrustStore { | ||
|
||
@ConfigProperty(name = ConfigProperties.SSL_TRUSTSTORE_DIR) | ||
java.nio.file.Path trustStoreDir; | ||
|
||
@Inject Logger logger; | ||
|
||
@Blocking | ||
@GET | ||
@Path("/api/v3/tls/certs") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public List<String> listCerts() throws IOException { | ||
return Files.walk(trustStoreDir) | ||
.map(java.nio.file.Path::toFile) | ||
.filter(File::isFile) | ||
.map(File::getPath) | ||
.toList(); | ||
} | ||
} |
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