Skip to content

Commit

Permalink
Add fallback plan if InetAddress.getLocalHost() fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrez committed Oct 3, 2024
1 parent f99f581 commit dc7bbbd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>io.github.theprez</groupId>
<artifactId>mapepire-server</artifactId>
<version>2.1.5</version>
<version>2.1.6</version>
<name>Mapepire (IBM i) Server Component</name>
<description>Server-side support for Mapepire cloud-native database access layer.</description>
<url>https://github.com/Mapepire-IBMi/mapepire-server/</url>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.ibm.mapepire.certstuff;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;

import com.github.ibm.mapepire.Tracer;

class LocalHostResolver {
static String getFQDN() throws IOException {
try {
return InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
} catch (IOException e) {
Tracer.warn(e);
Process p = Runtime.getRuntime().exec("/QOpenSys/usr/bin/hostname");
try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
return br.readLine().toLowerCase().trim();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public ServerCertInfo generate(final String _storePassword, final String _keyPas
final File javaHome = new File(System.getProperty("java.home"));
final File keytoolDir = new File(javaHome, "bin");

final InetAddress localHost = InetAddress.getLocalHost();
final String fqdn = localHost.getHostName();
final String fqdn = LocalHostResolver.getFQDN();
final String dname = String.format("cn=%s, ou=Web Socket Server, o=Db2 for IBM i, c=Unknown, st=Unknown", fqdn);
final File keytoolPath = new File(keytoolDir, getKeytoolBinaryName());
final String[] cmdArray = new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private File findLetsEncryptCertDir() {
}

try {
String myHostName = InetAddress.getLocalHost().getCanonicalHostName().toLowerCase();
String myHostName = LocalHostResolver.getFQDN();
Tracer.info("------we think our hostname is "+myHostName);
for (File candidate : candidates) {
if (candidate.getName().equalsIgnoreCase(myHostName)) {
Expand Down

0 comments on commit dc7bbbd

Please sign in to comment.