vault implemented

This commit is contained in:
Radek Davidek 2026-03-17 20:38:42 +01:00
parent 4629a2fae7
commit 4923d498ed

View File

@ -1,210 +1,199 @@
package cz.moneta.test.harness.endpoints.imq; package cz.moneta.test.harness.endpoints.imq;
import cz.moneta.test.harness.connectors.messaging.IbmMqConnector;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.Endpoint;
import cz.moneta.test.harness.messaging.MqMessageFormat;
import cz.moneta.test.harness.messaging.ReceivedMessage;
import cz.moneta.test.harness.connectors.VaultConnector;
import cz.moneta.test.harness.support.auth.Credentials;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.time.Duration; import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cz.moneta.test.harness.connectors.VaultConnector;
import cz.moneta.test.harness.connectors.messaging.IbmMqConnector;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.Endpoint;
import cz.moneta.test.harness.messaging.MqMessageFormat;
import cz.moneta.test.harness.messaging.ReceivedMessage;
import cz.moneta.test.harness.support.auth.Credentials;
/** /**
* IBM MQ First Vision endpoint. * IBM MQ First Vision endpoint. Provides high-level access to IBM MQ queues
* Provides high-level access to IBM MQ queues with configuration from StoreAccessor. * with configuration from StoreAccessor.
* <p> * <p>
* Credentials are loaded from HashiCorp Vault. * Credentials are loaded from HashiCorp Vault.
*/ */
public class ImqFirstVisionEndpoint implements Endpoint { public class ImqFirstVisionEndpoint implements Endpoint {
private static final Logger LOG = LogManager.getLogger(ImqFirstVisionEndpoint.class); private static final Logger LOG = LogManager.getLogger(ImqFirstVisionEndpoint.class);
private final IbmMqConnector connector; private final IbmMqConnector connector;
private final StoreAccessor store; private final StoreAccessor store;
// Configuration keys private String username, password, keystorePassword;
private static final String CONNECTION_NAME_LIST_KEY = "endpoints.imq-first-vision.connection-name-list";
private static final String CHANNEL_KEY = "endpoints.imq-first-vision.channel";
private static final String QUEUE_MANAGER_KEY = "endpoints.imq-first-vision.queue-manager";
private static final String SSL_CIPHER_SUITE_KEY = "endpoints.imq-first-vision.ssl-cipher-suite";
private static final String VAULT_PATH_KEY = "vault.imq-first-vision.secrets.path";
/** // Configuration keys
* Constructor that reads configuration from StoreAccessor. private static final String CONNECTION_NAME_LIST_KEY = "endpoints.imq-first-vision.connection-name-list";
*/ private static final String CHANNEL_KEY = "endpoints.imq-first-vision.channel";
public ImqFirstVisionEndpoint(StoreAccessor store) { private static final String QUEUE_MANAGER_KEY = "endpoints.imq-first-vision.queue-manager";
this.store = store; private static final String SSL_CIPHER_SUITE_KEY = "endpoints.imq-first-vision.ssl-cipher-suite";
private static final String VAULT_PATH_KEY = "vault.imq-first-vision.secrets.path";
private static final String VAULT_KEYSTORE_PASSWORD_KEY = "keystorePassword";
// Read configuration /**
String connectionNameList = getConfig(CONNECTION_NAME_LIST_KEY); * Constructor that reads configuration from StoreAccessor.
String channel = getConfig(CHANNEL_KEY); */
String queueManager = getConfig(QUEUE_MANAGER_KEY); public ImqFirstVisionEndpoint(StoreAccessor store) {
String sslCipherSuite = getConfig(SSL_CIPHER_SUITE_KEY); this.store = store;
// Load credentials from Vault // Read configuration
String vaultPath = getVaultPath(); String connectionNameList = getConfig(CONNECTION_NAME_LIST_KEY);
Credentials credentials = loadCredentialsFromVault(vaultPath); String channel = getConfig(CHANNEL_KEY);
String queueManager = getConfig(QUEUE_MANAGER_KEY);
String sslCipherSuite = getConfig(SSL_CIPHER_SUITE_KEY);
// SSL configuration (optional) loadCredentialsFromVault();
String keystorePath = "/home/kamma/aa/mq-docker/truststore.jks";
String keystorePassword = "changeit";
try { // SSL configuration (optional)
this.connector = new IbmMqConnector( String keystorePath = "/home/kamma/aa/mq-docker/truststore.jks";
connectionNameList,
channel,
queueManager,
credentials.getUsername(),
credentials.getPassword(),
keystorePath,
keystorePassword,
sslCipherSuite
);
LOG.info("Initialized IBM MQ First Vision endpoint for queue manager: {}", queueManager); try {
this.connector = new IbmMqConnector(connectionNameList, channel, queueManager, username, password,
keystorePath, keystorePassword, sslCipherSuite);
} catch (Exception e) { LOG.info("Initialized IBM MQ First Vision endpoint for queue manager: {}", queueManager);
throw new IllegalStateException("Failed to initialize IBM MQ endpoint", e);
}
}
/** } catch (Exception e) {
* Get a configuration value from StoreAccessor. throw new IllegalStateException("Failed to initialize IBM MQ endpoint", e);
*/ }
private String getConfig(String key) { }
return Optional.ofNullable(store.getConfig(key))
.orElseThrow(() -> new IllegalStateException(
"You need to configure " + key));
}
/** /**
* Get vault path from configuration. * Get a configuration value from StoreAccessor.
*/ */
private String getVaultPath() { private String getConfig(String key) {
return Optional.ofNullable(store.getConfig(VAULT_PATH_KEY)) return Optional.ofNullable(store.getConfig(key))
.orElseThrow(() -> new IllegalStateException( .orElseThrow(() -> new IllegalStateException("You need to configure " + key));
"You need to configure " + VAULT_PATH_KEY)); }
}
/** /**
* Load credentials from HashiCorp Vault. * Load credentials from HashiCorp Vault.
*/ */
private Credentials loadCredentialsFromVault(String vaultPath) { private void loadCredentialsFromVault() {
try { try {
// Get vault URL from configuration // Get vault URL from configuration
String vaultUrl = getConfig("vault.url"); String vaultPath = getConfig(VAULT_PATH_KEY);
String vaultUser = getConfig("vault.user"); String vaultUrl = getConfig("vault.url");
String vaultPassword = getConfig("vault.password"); String vaultUser = getConfig("vault.user");
String vaultPassword = getConfig("vault.password");
VaultConnector vaultConnector = new VaultConnector(vaultUrl, vaultUser, vaultPassword); VaultConnector vaultConnector = new VaultConnector(vaultUrl, vaultUser, vaultPassword);
Optional<Credentials> credentials = vaultConnector.getUsernameAndPassword(vaultPath); Optional<Credentials> credentials = vaultConnector.getUsernameAndPassword(vaultPath);
return credentials.orElseThrow(() -> new IllegalStateException( if (credentials.isPresent()) {
"Credentials not found in Vault at path: " + vaultPath)); this.username = credentials.get().getUsername();
this.password = credentials.get().getPassword();
this.keystorePassword = vaultConnector.getValue(vaultPath, VAULT_KEYSTORE_PASSWORD_KEY)
.map(Object::toString).orElse(null);
LOG.info("Successfully loaded credentials from Vault for path: {}", vaultPath);
} else {
throw new IllegalStateException("Credentials not found in Vault at path: " + vaultPath);
}
} catch (Exception e) {
throw new IllegalStateException("Failed to load credentials from Vault", e);
}
}
} catch (Exception e) { /**
throw new IllegalStateException("Failed to load credentials from Vault", e); * Send a message to a queue.
} *
} * @param queueName Physical queue name or logical name (from
* ImqFirstVisionQueue)
* @param payload Message payload
* @param format Message format
* @param properties JMS properties
*/
public void send(String queueName, String payload, MqMessageFormat format,
java.util.Map<String, String> properties) {
connector.send(queueName, payload, format, properties);
}
/** /**
* Send a message to a queue. * Send a message to a queue using logical queue name.
* */
* @param queueName Physical queue name or logical name (from ImqFirstVisionQueue) public void send(ImqFirstVisionQueue queue, String payload, MqMessageFormat format,
* @param payload Message payload java.util.Map<String, String> properties) {
* @param format Message format String physicalQueueName = resolveQueue(queue);
* @param properties JMS properties connector.send(physicalQueueName, payload, format, properties);
*/ }
public void send(String queueName, String payload, MqMessageFormat format,
java.util.Map<String, String> properties) {
connector.send(queueName, payload, format, properties);
}
/** /**
* Send a message to a queue using logical queue name. * Receive a message from a queue.
*/ *
public void send(ImqFirstVisionQueue queue, String payload, MqMessageFormat format, * @param queueName Physical queue name or logical name
java.util.Map<String, String> properties) { * @param messageSelector JMS message selector (optional)
String physicalQueueName = resolveQueue(queue); * @param format Expected message format
connector.send(physicalQueueName, payload, format, properties); * @param timeout Timeout duration
} * @return Received message
*/
public ReceivedMessage receive(String queueName, String messageSelector, MqMessageFormat format, Duration timeout) {
return connector.receive(queueName, messageSelector, format, timeout);
}
/** /**
* Receive a message from a queue. * Receive a message from a queue using logical queue name.
* */
* @param queueName Physical queue name or logical name public ReceivedMessage receive(ImqFirstVisionQueue queue, String messageSelector, MqMessageFormat format,
* @param messageSelector JMS message selector (optional) Duration timeout) {
* @param format Expected message format String physicalQueueName = resolveQueue(queue);
* @param timeout Timeout duration return connector.receive(physicalQueueName, messageSelector, format, timeout);
* @return Received message }
*/
public ReceivedMessage receive(String queueName, String messageSelector,
MqMessageFormat format, Duration timeout) {
return connector.receive(queueName, messageSelector, format, timeout);
}
/** /**
* Receive a message from a queue using logical queue name. * Browse a queue (non-destructive read).
*/ *
public ReceivedMessage receive(ImqFirstVisionQueue queue, String messageSelector, * @param queueName Physical queue name or logical name
MqMessageFormat format, Duration timeout) { * @param messageSelector JMS message selector (optional)
String physicalQueueName = resolveQueue(queue); * @param format Expected message format
return connector.receive(physicalQueueName, messageSelector, format, timeout); * @param maxMessages Maximum number of messages
} * @return List of received messages
*/
public List<ReceivedMessage> browse(String queueName, String messageSelector, MqMessageFormat format,
int maxMessages) {
return connector.browse(queueName, messageSelector, format, maxMessages);
}
/** /**
* Browse a queue (non-destructive read). * Browse a queue using logical queue name.
* */
* @param queueName Physical queue name or logical name public List<ReceivedMessage> browse(ImqFirstVisionQueue queue, String messageSelector, MqMessageFormat format,
* @param messageSelector JMS message selector (optional) int maxMessages) {
* @param format Expected message format String physicalQueueName = resolveQueue(queue);
* @param maxMessages Maximum number of messages return connector.browse(physicalQueueName, messageSelector, format, maxMessages);
* @return List of received messages }
*/
public List<ReceivedMessage> browse(String queueName, String messageSelector,
MqMessageFormat format, int maxMessages) {
return connector.browse(queueName, messageSelector, format, maxMessages);
}
/** /**
* Browse a queue using logical queue name. * Resolve logical queue name to physical queue name.
*/ *
public List<ReceivedMessage> browse(ImqFirstVisionQueue queue, String messageSelector, * @param logicalName Logical queue name or ImqFirstVisionQueue enum
MqMessageFormat format, int maxMessages) { * @return Physical queue name
String physicalQueueName = resolveQueue(queue); */
return connector.browse(physicalQueueName, messageSelector, format, maxMessages); public String resolveQueue(String logicalName) {
} String configKey = "endpoints.imq-first-vision." + logicalName + ".queue";
return Optional.ofNullable(store.getConfig(configKey)).orElseThrow(
() -> new IllegalStateException("Queue '" + logicalName + "' is not configured in " + configKey));
}
/** /**
* Resolve logical queue name to physical queue name. * Resolve ImqFirstVisionQueue enum to physical queue name.
* */
* @param logicalName Logical queue name or ImqFirstVisionQueue enum public String resolveQueue(ImqFirstVisionQueue queue) {
* @return Physical queue name return resolveQueue(queue.getConfigKey());
*/ }
public String resolveQueue(String logicalName) {
String configKey = "endpoints.imq-first-vision." + logicalName + ".queue";
return Optional.ofNullable(store.getConfig(configKey))
.orElseThrow(() -> new IllegalStateException(
"Queue '" + logicalName + "' is not configured in " + configKey));
}
/** @Override
* Resolve ImqFirstVisionQueue enum to physical queue name. public void close() {
*/ if (connector != null) {
public String resolveQueue(ImqFirstVisionQueue queue) { connector.close();
return resolveQueue(queue.getConfigKey()); }
} }
@Override
public void close() {
if (connector != null) {
connector.close();
}
}
} }