-
-
Notifications
You must be signed in to change notification settings - Fork 2
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 #25 from 3KeyCompany/release/1.2.0
Release/1.2.0
- Loading branch information
Showing
9 changed files
with
335 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/bin/sh | ||
|
||
czertainlyHome="/opt/czertainly" | ||
source ${czertainlyHome}/static-functions | ||
|
||
if [ -f ${czertainlyHome}/trusted-certificates.pem ] | ||
then | ||
log "INFO" "Adding additional trusted certificates to cacerts" | ||
./update-cacerts.sh /opt/czertainly/trusted-certificates.pem | ||
else | ||
log "INFO" "No trusted certificates were provided, continue!" | ||
fi | ||
|
||
log "INFO" "Launching the Core" | ||
java -jar ./app.jar | ||
|
||
#exec "$@" |
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,17 @@ | ||
#!/bin/bash | ||
|
||
log() { | ||
# 2022-02-08 15:49:15 | ||
dateString="$(date +%F' '%T)" | ||
logLevel=$(printf '%-5s' "${1:-INFO}") | ||
className="$0" | ||
processId="$$" | ||
#threadId="$(ps H -o 'tid' $processId | tail -n 1| tr -d ' ')" | ||
if [ -z "$2" ] ; then | ||
while read line ; do | ||
echo "[$dateString] $logLevel [$className] (process:$processId) ${line}" | ||
done | ||
else | ||
echo "[$dateString] $logLevel [$className] (process:$processId) ${2}" | ||
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,15 @@ | ||
#!/bin/sh | ||
|
||
PEM_FILE=$1 | ||
PASSWORD=changeit | ||
CERTS=$(grep 'END CERTIFICATE' $PEM_FILE| wc -l) | ||
|
||
# For every cert in the PEM file, extract it and import into the JKS keystore | ||
# awk command: step 1, if line is in the desired cert, print the line | ||
# step 2, increment counter when last line of cert is found | ||
for N in $(seq 0 $(($CERTS - 1))); do | ||
ALIAS="czertainly-trusted-$N" | ||
cat $PEM_FILE | | ||
awk "n==$N { print }; /END CERTIFICATE/ { n++ }" | | ||
keytool -noprompt -import -trustcacerts -cacerts -alias $ALIAS -storepass $PASSWORD | ||
done |
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
35 changes: 35 additions & 0 deletions
35
src/main/java/com/czertainly/ca/connector/ejbca/config/proxy/MultiServerAuthenticator.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,35 @@ | ||
package com.czertainly.ca.connector.ejbca.config.proxy; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.Authenticator; | ||
import java.net.PasswordAuthentication; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* This is an {@link Authenticator} implementation able to manage several servers | ||
* Inspired by <a href="https://github.com/Orange-OpenSource/spring-boot-autoconfigure-proxy">spring-boot-autoconfigure-proxy</a> | ||
*/ | ||
public class MultiServerAuthenticator extends Authenticator { | ||
private static final Logger logger = LoggerFactory.getLogger(MultiServerAuthenticator.class); | ||
|
||
private final Map<String, PasswordAuthentication> host2Authent = new HashMap<>(); | ||
|
||
public void add(String host, String user, String password) { | ||
host2Authent.put(host, new PasswordAuthentication(user, password.toCharArray())); | ||
} | ||
|
||
@Override | ||
protected PasswordAuthentication getPasswordAuthentication() { | ||
String host = "" + getRequestingHost() + ":" + getRequestingPort(); | ||
PasswordAuthentication passwordAuthentication = host2Authent.get(host); | ||
logger.trace("using proxy authentication for <{}>: {}", host, passwordAuthentication == null ? "none" : passwordAuthentication.getUserName() + "/***"); | ||
return passwordAuthentication; | ||
} | ||
|
||
public int size() { | ||
return host2Authent.size(); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/com/czertainly/ca/connector/ejbca/config/proxy/ProxyConfiguration.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,54 @@ | ||
package com.czertainly.ca.connector.ejbca.config.proxy; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.AutoConfigureOrder; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.Ordered; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.net.Authenticator; | ||
|
||
/** | ||
* Inspired by <a href="https://github.com/Orange-OpenSource/spring-boot-autoconfigure-proxy">spring-boot-autoconfigure-proxy</a> | ||
*/ | ||
@Configuration | ||
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) | ||
public class ProxyConfiguration { | ||
private static final Logger logger = LoggerFactory.getLogger(ProxyConfiguration.class); | ||
|
||
private static final String[] PROTOCOLS = {"http", "https", "ftp"}; | ||
|
||
@PostConstruct | ||
public void setupProxyConfiguration() { | ||
MultiServerAuthenticator msa = new MultiServerAuthenticator(); | ||
|
||
for (String protocol : PROTOCOLS) { | ||
ProxySettings proxySettings = ProxySettings.read(protocol); | ||
if (proxySettings != null) { | ||
// CASE 2: auto-conf from ENV | ||
logger.info("Configuring proxy for {} from env '{}': {}", protocol, proxySettings.getEnvName(), proxySettings); | ||
|
||
// set password authentication if specified | ||
if (proxySettings.getUsername() != null && proxySettings.getPassword() != null) { | ||
msa.add(proxySettings.getHost() + ":" + proxySettings.getPort(), proxySettings.getUsername(), proxySettings.getPassword()); | ||
} | ||
|
||
// set proxy properties | ||
System.setProperty(protocol + ".proxyHost", proxySettings.getHost()); | ||
System.setProperty(protocol + ".proxyPort", String.valueOf(proxySettings.getPort())); | ||
if (proxySettings.getNoProxyHosts() != null && proxySettings.getNoProxyHosts().length > 0) { | ||
System.setProperty(protocol + ".nonProxyHosts", String.join("|", proxySettings.getNoProxyHosts())); | ||
} | ||
} | ||
} | ||
|
||
// install default authenticator (if not empty) | ||
if (msa.size() > 0) { | ||
// see: https://www.oracle.com/technetwork/java/javase/8u111-relnotes-3124969.html | ||
System.setProperty("jdk.http.auth.tunneling.disabledSchemes", ""); | ||
Authenticator.setDefault(msa); | ||
} | ||
} | ||
|
||
} |
165 changes: 165 additions & 0 deletions
165
src/main/java/com/czertainly/ca/connector/ejbca/config/proxy/ProxySettings.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,165 @@ | ||
package com.czertainly.ca.connector.ejbca.config.proxy; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* Inspired by <a href="https://github.com/Orange-OpenSource/spring-boot-autoconfigure-proxy">spring-boot-autoconfigure-proxy</a> | ||
*/ | ||
public class ProxySettings { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ProxySettings.class); | ||
|
||
private final String forProtocol; | ||
private final String protocol; | ||
private final String host; | ||
private final int port; | ||
private final String[] noProxyHosts; | ||
private final String username; | ||
private final String password; | ||
|
||
public ProxySettings(String forProtocol, String protocol, String host, int port, String[] noProxyHosts, String username, String password) { | ||
this.forProtocol = forProtocol; | ||
this.protocol = protocol; | ||
this.username = username; | ||
this.password = password; | ||
this.host = host; | ||
this.noProxyHosts = noProxyHosts; | ||
this.port = port; | ||
} | ||
|
||
/** | ||
* Returns the proxy protocol (one of {@code http}, {@code socks} or {@code socks5}) | ||
*/ | ||
public String getProtocol() { | ||
return protocol; | ||
} | ||
|
||
/** | ||
* Returns the protocol (scheme) this proxy setting applies to | ||
*/ | ||
public String getForProtocol() { | ||
return forProtocol; | ||
} | ||
|
||
/** | ||
* Returns the proxy server host | ||
*/ | ||
public String getHost() { | ||
return host; | ||
} | ||
|
||
/** | ||
* Returns the proxy server port | ||
*/ | ||
public int getPort() { | ||
return port; | ||
} | ||
|
||
/** | ||
* Returns the list of no-proxy server hosts (matchers) | ||
*/ | ||
public String[] getNoProxyHosts() { | ||
return noProxyHosts; | ||
} | ||
|
||
/** | ||
* Returns the proxy username (if requires authentication) | ||
*/ | ||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
/** | ||
* Returns the proxy password (if requires authentication) | ||
*/ | ||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
/** | ||
* Returns the proxy setting environment variable name | ||
*/ | ||
public String getEnvName() { | ||
return forProtocol + "_proxy"; | ||
} | ||
|
||
/** | ||
* Returns the proxy setting environment variable value (hides the password) | ||
*/ | ||
public String getEnvVal() { | ||
return protocol + "://" + (username == null ? "" : username + ":***@") + host + ":" + port; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ProxySettingsFromEnv{" + | ||
"protocol='" + protocol + '\'' + | ||
", host='" + host + '\'' + | ||
", port=" + port + | ||
", forProtocol=" + forProtocol + | ||
", noProxyHosts=" + Arrays.toString(noProxyHosts) + | ||
", username='" + (username == null ? "(none)" : username) + '\'' + | ||
", password='" + (password == null ? "(none)" : "***") + '\'' + | ||
'}'; | ||
} | ||
|
||
/** | ||
* Reads and parses the proxy settings from system environment | ||
* | ||
* @param protocol determines for which forProtocol the proxy settings shall be read | ||
* @return parsed setting, or {@code null} if not set | ||
*/ | ||
public static ProxySettings read(String protocol) { | ||
return parse(protocol, getEnvIgnoreCase(protocol + "_proxy"), getEnvIgnoreCase("no_proxy")); | ||
} | ||
|
||
static ProxySettings parse(String protocol, String proxyUrl, String noProxy) { | ||
if (proxyUrl == null) { | ||
return null; | ||
} | ||
try { | ||
URI url = new URI(proxyUrl); | ||
if (url.getHost() == null) { | ||
logger.error("Invalid proxy configuration URL for {}: {} - host not specified", protocol, proxyUrl); | ||
return null; | ||
} | ||
if (url.getPort() == -1) { | ||
logger.error("Invalid proxy configuration URL for {}: {} - port not specified", protocol, proxyUrl); | ||
return null; | ||
} | ||
// scheme is optional (defaults to http) | ||
String scheme = url.getScheme() == null ? "http" : url.getScheme(); | ||
|
||
// read login/password | ||
String username = null; | ||
String password = null; | ||
String userInfo = url.getUserInfo(); | ||
if (userInfo != null) { | ||
int idx = userInfo.indexOf(':'); | ||
username = userInfo.substring(0, idx); | ||
password = userInfo.substring(idx + 1); | ||
} | ||
// add no proxy hosts | ||
String[] noProxyHosts = null; | ||
if (noProxy != null) { | ||
noProxyHosts = noProxy.split("\\s*,\\s*"); | ||
} | ||
return new ProxySettings(protocol, scheme, url.getHost(), url.getPort(), noProxyHosts, username, password); | ||
} catch (URISyntaxException e) { | ||
logger.error("Could not decode proxy configuration for {}: {}", protocol, proxyUrl, e); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
static String getEnvIgnoreCase(String name) { | ||
String val = System.getenv(name.toLowerCase()); | ||
return val != null ? val : System.getenv(name.toUpperCase()); | ||
} | ||
|
||
} |