Skip to content

Commit 52cf5a4

Browse files
Fix JKS secrets startup check (#550)
* Fix JKS secrets startup check * Provide a method to check for config files in config manager / loader * Use config manager to decide how to init the JKS secret service * Init sequence for JKS secret service
1 parent 6f5828e commit 52cf5a4

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed

tracdap-libs/tracdap-lib-common/src/main/java/org/finos/tracdap/common/config/ConfigManager.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ public URI resolveConfigFile(URI relativePath) {
171171
// -----------------------------------------------------------------------------------------------------------------
172172

173173

174+
/**
175+
* Check whether a config file with the given URL exists
176+
*
177+
* <p>Config URLs may be relative or absolute. Relative URLs are resolved relative to the
178+
* root config directory, absolute URLs may either be a local file path or a full URL
179+
* including a protocol. Absolute URLs can use a different protocol from the root config
180+
* file, so long as a config loader is available that can handle that protocol and the required
181+
* access has been set up.</p>
182+
*
183+
* @param configUrl URL of the config file to check for
184+
* @return True if the file is available from the underlying config store, false otherwise
185+
* @throws EStartup The requested config file could not be checked for any reason
186+
*/
187+
public boolean hasConfig(String configUrl) {
188+
189+
var parsed = parseUrl(configUrl);
190+
var resolved = resolveUrl(parsed);
191+
return checkUrl(resolved);
192+
}
193+
174194
/**
175195
* Load a config file as an array of bytes
176196
*
@@ -469,6 +489,20 @@ private URI resolveRootUrl(URI url, Path workingDir) {
469489
}
470490
}
471491

492+
private boolean checkUrl(URI absoluteUrl) {
493+
494+
// Display relative URLs in the log if possible
495+
var relativeUrl = rootConfigDir.relativize(absoluteUrl);
496+
497+
var message = String.format("Checking for config file: [%s]", relativeUrl);
498+
StartupLog.log(this, Level.INFO, message);
499+
500+
var protocol = absoluteUrl.getScheme();
501+
var loader = configLoaderForProtocol(protocol);
502+
503+
return loader.hasFile(absoluteUrl);
504+
}
505+
472506
private byte[] loadUrl(URI absoluteUrl) {
473507

474508
// Display relative URLs in the log if possible

tracdap-libs/tracdap-lib-common/src/main/java/org/finos/tracdap/common/config/IConfigLoader.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343
*/
4444
public interface IConfigLoader {
4545

46+
/**
47+
* Check whether a given config file exists.
48+
*
49+
* @param configUrl The config file to check
50+
* @return True if the file exists, false otherwise
51+
*/
52+
boolean hasFile(URI configUrl);
53+
4654
/**
4755
* Use the loader to load a text file.
4856
*

tracdap-libs/tracdap-lib-common/src/main/java/org/finos/tracdap/common/config/local/JksSecretService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import java.io.IOException;
2828
import java.net.URI;
29-
import java.nio.file.Files;
30-
import java.nio.file.Path;
3129
import java.security.KeyStoreException;
3230
import java.security.NoSuchAlgorithmException;
3331
import java.security.cert.CertificateException;
@@ -47,17 +45,24 @@ public JksSecretService(Properties properties) {
4745
@Override
4846
public void init(ConfigManager configManager, boolean createIfMissing) {
4947

50-
if (Files.exists(Path.of(keystoreUrl)) || !createIfMissing) {
48+
if (ready) {
49+
StartupLog.log(this, Level.ERROR, "JKS secret service initialized twice");
50+
throw new EStartup("JKS secret service initialized twice");
51+
}
52+
53+
if (configManager.hasConfig(keystoreUrl) || !createIfMissing) {
5154

5255
init(configManager);
5356
return;
5457
}
5558

5659
try {
5760

61+
this.configManager = configManager;
62+
5863
this.keystore.load(null, keystoreKey.toCharArray());
64+
this.commit();
5965

60-
this.configManager = configManager;
6166
this.ready = true;
6267
}
6368
catch (IOException e) {

tracdap-libs/tracdap-lib-common/src/main/java/org/finos/tracdap/common/config/local/LocalConfigLoader.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
*/
3434
public class LocalConfigLoader implements IConfigLoader {
3535

36+
@Override
37+
public boolean hasFile(URI configUrl) {
38+
39+
return Files.exists(Paths.get(configUrl));
40+
}
41+
3642
@Override
3743
public String loadTextFile(URI url) {
3844

tracdap-libs/tracdap-lib-common/src/test/java/org/finos/tracdap/common/config/test/TestConfigLoader.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public TestConfigLoader(Path tempDir) {
3535
this.tempDir = tempDir;
3636
}
3737

38+
@Override
39+
public boolean hasFile(URI uri) {
40+
41+
// Match the logic of loadBinaryFile()
42+
var relativePath = uri.getPath().substring(1);
43+
var absolutePath = tempDir.resolve(relativePath);
44+
45+
return Files.exists(absolutePath);
46+
}
47+
3848
@Override
3949
public byte[] loadBinaryFile(URI uri) {
4050

0 commit comments

Comments
 (0)