This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-27747-pg-replication-slot-tls in repository https://gitbox.apache.org/repos/asf/camel.git
commit 55f8f3cd88c68fc498f766382802a1950d8b9b31 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Nov 27 21:16:47 2026 +0100 CAMEL-27747: camel-pg-replication-slot - add TLS options for the connection The endpoint built the JDBC connection with only the user, the password and the replication properties, so the default sslmode of the PostgreSQL JDBC driver (prefer) always applied. New options sslMode, sslRootCert, sslCert, sslKey and sslPassword (secret) are passed to the driver as connection properties. They are not set by default, so nothing changes for existing routes. An unknown sslMode fails when the endpoint is initialized. The doc page has a TLS section. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../catalog/components/pg-replication-slot.json | 7 +- .../docs/pg-replication-slot-component.adoc | 16 +++ .../slot/PgReplicationSlotEndpointConfigurer.java | 30 ++++++ .../slot/PgReplicationSlotEndpointUriFactory.java | 10 +- .../pg/replication/slot/pg-replication-slot.json | 7 +- .../main/docs/pg-replication-slot-component.adoc | 16 +++ .../slot/PgReplicationSlotEndpoint.java | 109 ++++++++++++++++++++- .../replication/slot/PgReplicationSlotTlsTest.java | 79 +++++++++++++++ .../PgReplicationSlotEndpointBuilderFactory.java | 85 ++++++++++++++++ 9 files changed, 352 insertions(+), 7 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pg-replication-slot.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pg-replication-slot.json index 94bdb41bba17..72214f1fca1f 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pg-replication-slot.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/pg-replication-slot.json @@ -65,6 +65,11 @@ "schedulerProperties": { "index": 26, "kind": "parameter", "displayName": "Scheduler Properties", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "prefix": "scheduler.", "multiValue": true, "deprecated": false, "autowired": false, "secret": false, "description": "To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. This i [...] "startScheduler": { "index": 27, "kind": "parameter", "displayName": "Start Scheduler", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether the scheduler should be auto started." }, "timeUnit": { "index": 28, "kind": "parameter", "displayName": "Time Unit", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "enum", "javaType": "java.util.concurrent.TimeUnit", "enum": [ "NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "MILLISECONDS", "description": "Time unit for initialDelay and delay options." }, - "useFixedDelay": { "index": 29, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." } + "useFixedDelay": { "index": 29, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." }, + "sslCert": { "index": 30, "kind": "parameter", "displayName": "Ssl Cert", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the client certificate (PEM) for a server that checks client certificates. When not set, the PostgreSQL JDBC driver reads postgresql.crt in the .postgresql folder of the home directory of the user, if it exists." }, + "sslKey": { "index": 31, "kind": "parameter", "displayName": "Ssl Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the private key of the client certificate: PKCS-8 in DER format, or a PKCS-12 file (.p12 or .pfx) that holds the certificate and the key, as the PostgreSQL JDBC driver expects. When not set, the driver reads postgresql. [...] + "sslMode": { "index": 32, "kind": "parameter", "displayName": "Ssl Mode", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "disable", "allow", "prefer", "require", "verify-ca", "verify-full" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SSL mode of the connection to the PostgreSQL server: disable, allow, prefer, require, verify-ca or verify-full. When not set, the default of the P [...] + "sslPassword": { "index": 33, "kind": "parameter", "displayName": "Ssl Password", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The password of the private key in sslKey." }, + "sslRootCert": { "index": 34, "kind": "parameter", "displayName": "Ssl Root Cert", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the root certificates (PEM) that the server certificate is checked against with sslMode verify-ca or verify-full. When not set, the PostgreSQL JDBC driver reads root.crt in the .postgresql folder of the home [...] } } diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pg-replication-slot-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pg-replication-slot-component.adoc index 2ce266d365a4..79bcdb08e31b 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pg-replication-slot-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/pg-replication-slot-component.adoc @@ -58,6 +58,22 @@ instead of `INSERT`, etc). This will make sure repeated messages won't affect yo The log sequence number (LSN) of a message is in the `CamelPgReplicationSlotLsn` header. It is not unique: several messages can have the same LSN, for example the `BEGIN` and the first change of a transaction with `test_decoding`. +=== TLS + +The `sslMode` option sets how the connection uses TLS. When it is not set, the default of the PostgreSQL JDBC driver +applies, which is `prefer`: TLS when the server offers it, without checking the server certificate, and a plain +connection otherwise. To require TLS and check the server, use `verify-full` (certificate and host name) or +`verify-ca` (certificate only), with the root certificates of the server in `sslRootCert`: + +[source,text] +---- +pg-replication-slot:db.example.com:5432/mydb/myslot:test_decoding?user=replicator&password=RAW(secret)&sslMode=verify-full&sslRootCert=/etc/certs/root.crt +---- + +For a server that checks client certificates, set `sslCert` and `sslKey` (and `sslPassword` when the key has a +password). The PostgreSQL JDBC driver expects the key in PKCS-8 DER format, or a PKCS-12 file (`.p12` or `.pfx`) that +holds the certificate and the key. + == Examples .Example route diff --git a/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointConfigurer.java b/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointConfigurer.java index c35f85b80fad..0fa38e0b577c 100644 --- a/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointConfigurer.java +++ b/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointConfigurer.java @@ -60,6 +60,16 @@ public class PgReplicationSlotEndpointConfigurer extends PropertyConfigurerSuppo case "sendEmptyMessageWhenIdle": target.setSendEmptyMessageWhenIdle(property(camelContext, boolean.class, value)); return true; case "slotoptions": case "slotOptions": target.setSlotOptions(property(camelContext, java.util.Map.class, value)); return true; + case "sslcert": + case "sslCert": target.setSslCert(property(camelContext, java.lang.String.class, value)); return true; + case "sslkey": + case "sslKey": target.setSslKey(property(camelContext, java.lang.String.class, value)); return true; + case "sslmode": + case "sslMode": target.setSslMode(property(camelContext, java.lang.String.class, value)); return true; + case "sslpassword": + case "sslPassword": target.setSslPassword(property(camelContext, java.lang.String.class, value)); return true; + case "sslrootcert": + case "sslRootCert": target.setSslRootCert(property(camelContext, java.lang.String.class, value)); return true; case "startscheduler": case "startScheduler": target.setStartScheduler(property(camelContext, boolean.class, value)); return true; case "statusinterval": @@ -112,6 +122,16 @@ public class PgReplicationSlotEndpointConfigurer extends PropertyConfigurerSuppo case "sendEmptyMessageWhenIdle": return boolean.class; case "slotoptions": case "slotOptions": return java.util.Map.class; + case "sslcert": + case "sslCert": return java.lang.String.class; + case "sslkey": + case "sslKey": return java.lang.String.class; + case "sslmode": + case "sslMode": return java.lang.String.class; + case "sslpassword": + case "sslPassword": return java.lang.String.class; + case "sslrootcert": + case "sslRootCert": return java.lang.String.class; case "startscheduler": case "startScheduler": return boolean.class; case "statusinterval": @@ -165,6 +185,16 @@ public class PgReplicationSlotEndpointConfigurer extends PropertyConfigurerSuppo case "sendEmptyMessageWhenIdle": return target.isSendEmptyMessageWhenIdle(); case "slotoptions": case "slotOptions": return target.getSlotOptions(); + case "sslcert": + case "sslCert": return target.getSslCert(); + case "sslkey": + case "sslKey": return target.getSslKey(); + case "sslmode": + case "sslMode": return target.getSslMode(); + case "sslpassword": + case "sslPassword": return target.getSslPassword(); + case "sslrootcert": + case "sslRootCert": return target.getSslRootCert(); case "startscheduler": case "startScheduler": return target.isStartScheduler(); case "statusinterval": diff --git a/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointUriFactory.java b/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointUriFactory.java index e7fe198754d0..f8984fc2d292 100644 --- a/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointUriFactory.java +++ b/components/camel-pg-replication-slot/src/generated/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpointUriFactory.java @@ -24,7 +24,7 @@ public class PgReplicationSlotEndpointUriFactory extends org.apache.camel.suppor private static final Set<String> ENDPOINT_IDENTITY_PROPERTY_NAMES; private static final Map<String, String> MULTI_VALUE_PREFIXES; static { - Set<String> props = new HashSet<>(30); + Set<String> props = new HashSet<>(35); props.add("autoCreateSlot"); props.add("backoffErrorThreshold"); props.add("backoffIdleThreshold"); @@ -50,14 +50,20 @@ public class PgReplicationSlotEndpointUriFactory extends org.apache.camel.suppor props.add("sendEmptyMessageWhenIdle"); props.add("slot"); props.add("slotOptions"); + props.add("sslCert"); + props.add("sslKey"); + props.add("sslMode"); + props.add("sslPassword"); + props.add("sslRootCert"); props.add("startScheduler"); props.add("statusInterval"); props.add("timeUnit"); props.add("useFixedDelay"); props.add("user"); PROPERTY_NAMES = Collections.unmodifiableSet(props); - Set<String> secretProps = new HashSet<>(1); + Set<String> secretProps = new HashSet<>(2); secretProps.add("password"); + secretProps.add("sslPassword"); SECRET_PROPERTY_NAMES = Collections.unmodifiableSet(secretProps); ENDPOINT_IDENTITY_PROPERTY_NAMES = Collections.emptySet(); Map<String, String> prefixes = new HashMap<>(2); diff --git a/components/camel-pg-replication-slot/src/generated/resources/META-INF/org/apache/camel/component/pg/replication/slot/pg-replication-slot.json b/components/camel-pg-replication-slot/src/generated/resources/META-INF/org/apache/camel/component/pg/replication/slot/pg-replication-slot.json index 94bdb41bba17..72214f1fca1f 100644 --- a/components/camel-pg-replication-slot/src/generated/resources/META-INF/org/apache/camel/component/pg/replication/slot/pg-replication-slot.json +++ b/components/camel-pg-replication-slot/src/generated/resources/META-INF/org/apache/camel/component/pg/replication/slot/pg-replication-slot.json @@ -65,6 +65,11 @@ "schedulerProperties": { "index": 26, "kind": "parameter", "displayName": "Scheduler Properties", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "object", "javaType": "java.util.Map<java.lang.String, java.lang.Object>", "prefix": "scheduler.", "multiValue": true, "deprecated": false, "autowired": false, "secret": false, "description": "To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. This i [...] "startScheduler": { "index": 27, "kind": "parameter", "displayName": "Start Scheduler", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether the scheduler should be auto started." }, "timeUnit": { "index": 28, "kind": "parameter", "displayName": "Time Unit", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "enum", "javaType": "java.util.concurrent.TimeUnit", "enum": [ "NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "MILLISECONDS", "description": "Time unit for initialDelay and delay options." }, - "useFixedDelay": { "index": 29, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." } + "useFixedDelay": { "index": 29, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." }, + "sslCert": { "index": 30, "kind": "parameter", "displayName": "Ssl Cert", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the client certificate (PEM) for a server that checks client certificates. When not set, the PostgreSQL JDBC driver reads postgresql.crt in the .postgresql folder of the home directory of the user, if it exists." }, + "sslKey": { "index": 31, "kind": "parameter", "displayName": "Ssl Key", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the private key of the client certificate: PKCS-8 in DER format, or a PKCS-12 file (.p12 or .pfx) that holds the certificate and the key, as the PostgreSQL JDBC driver expects. When not set, the driver reads postgresql. [...] + "sslMode": { "index": 32, "kind": "parameter", "displayName": "Ssl Mode", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "disable", "allow", "prefer", "require", "verify-ca", "verify-full" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SSL mode of the connection to the PostgreSQL server: disable, allow, prefer, require, verify-ca or verify-full. When not set, the default of the P [...] + "sslPassword": { "index": 33, "kind": "parameter", "displayName": "Ssl Password", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The password of the private key in sslKey." }, + "sslRootCert": { "index": 34, "kind": "parameter", "displayName": "Ssl Root Cert", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": false, "description": "The file of the root certificates (PEM) that the server certificate is checked against with sslMode verify-ca or verify-full. When not set, the PostgreSQL JDBC driver reads root.crt in the .postgresql folder of the home [...] } } diff --git a/components/camel-pg-replication-slot/src/main/docs/pg-replication-slot-component.adoc b/components/camel-pg-replication-slot/src/main/docs/pg-replication-slot-component.adoc index 2ce266d365a4..79bcdb08e31b 100644 --- a/components/camel-pg-replication-slot/src/main/docs/pg-replication-slot-component.adoc +++ b/components/camel-pg-replication-slot/src/main/docs/pg-replication-slot-component.adoc @@ -58,6 +58,22 @@ instead of `INSERT`, etc). This will make sure repeated messages won't affect yo The log sequence number (LSN) of a message is in the `CamelPgReplicationSlotLsn` header. It is not unique: several messages can have the same LSN, for example the `BEGIN` and the first change of a transaction with `test_decoding`. +=== TLS + +The `sslMode` option sets how the connection uses TLS. When it is not set, the default of the PostgreSQL JDBC driver +applies, which is `prefer`: TLS when the server offers it, without checking the server certificate, and a plain +connection otherwise. To require TLS and check the server, use `verify-full` (certificate and host name) or +`verify-ca` (certificate only), with the root certificates of the server in `sslRootCert`: + +[source,text] +---- +pg-replication-slot:db.example.com:5432/mydb/myslot:test_decoding?user=replicator&password=RAW(secret)&sslMode=verify-full&sslRootCert=/etc/certs/root.crt +---- + +For a server that checks client certificates, set `sslCert` and `sslKey` (and `sslPassword` when the key has a +password). The PostgreSQL JDBC driver expects the key in PKCS-8 DER format, or a PKCS-12 file (`.p12` or `.pfx`) that +holds the certificate and the key. + == Examples .Example route diff --git a/components/camel-pg-replication-slot/src/main/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpoint.java b/components/camel-pg-replication-slot/src/main/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpoint.java index 8099dd1dcaf0..6a764b445bc8 100644 --- a/components/camel-pg-replication-slot/src/main/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpoint.java +++ b/components/camel-pg-replication-slot/src/main/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotEndpoint.java @@ -20,6 +20,7 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.regex.Matcher; @@ -57,6 +58,9 @@ public class PgReplicationSlotEndpoint extends ScheduledPollEndpoint { private static final Logger LOG = LoggerFactory.getLogger(PgReplicationSlotEndpoint.class); + private static final List<String> SSL_MODES + = List.of("disable", "allow", "prefer", "require", "verify-ca", "verify-full"); + private static final Pattern URI_PATTERN = Pattern.compile( "^pg-replication-slot:(//)?(?<host>[^:]*):?(?<port>\\d*)?/(?<database>\\w+)/(?<slot>\\w+):(?<plugin>\\w+).*$"); @@ -105,6 +109,31 @@ public class PgReplicationSlotEndpoint extends ScheduledPollEndpoint { + " while the route is stopped.", label = "advanced", defaultValue = "true") private Boolean autoCreateSlot = true; + @UriParam(description = "The SSL mode of the connection to the PostgreSQL server: disable, allow, prefer, require," + + " verify-ca or verify-full. When not set, the default of the PostgreSQL JDBC driver applies," + + " which is prefer: TLS when the server offers it, without checking the server certificate, and" + + " a plain connection otherwise. Use verify-full to require TLS and check both the certificate" + + " and the host name of the server.", + label = "security", enums = "disable,allow,prefer,require,verify-ca,verify-full") + private String sslMode; + @UriParam(description = "The file of the root certificates (PEM) that the server certificate is checked against with" + + " sslMode verify-ca or verify-full. When not set, the PostgreSQL JDBC driver reads" + + " root.crt in the .postgresql folder of the home directory of the user.", + label = "security") + private String sslRootCert; + @UriParam(description = "The file of the client certificate (PEM) for a server that checks client certificates. When" + + " not set, the PostgreSQL JDBC driver reads postgresql.crt in the .postgresql folder of the home" + + " directory of the user, if it exists.", + label = "security") + private String sslCert; + @UriParam(description = "The file of the private key of the client certificate: PKCS-8 in DER format, or a PKCS-12 file" + + " (.p12 or .pfx) that holds the certificate and the key, as the PostgreSQL JDBC driver expects." + + " When not set, the driver reads postgresql.pk8 in the .postgresql folder of the home directory" + + " of the user, if it exists.", + label = "security") + private String sslKey; + @UriParam(description = "The password of the private key in sslKey.", label = "security", security = "secret") + private String sslPassword; public PgReplicationSlotEndpoint(String uri, Component component) { super(uri, component); @@ -127,6 +156,15 @@ public class PgReplicationSlotEndpoint extends ScheduledPollEndpoint { * Creates a new PostgreSQL JDBC connection that's setup for replication. */ Connection newDbConnection() throws SQLException { + return DriverManager.getConnection( + String.format("jdbc:postgresql://%s:%d/%s", this.getHost(), this.getPort(), this.getDatabase()), + createConnectionProperties()); + } + + /** + * The properties of the JDBC connection. + */ + Properties createConnectionProperties() { Properties props = new Properties(); PGProperty.USER.set(props, this.getUser()); @@ -136,9 +174,34 @@ public class PgReplicationSlotEndpoint extends ScheduledPollEndpoint { PGProperty.PREFER_QUERY_MODE.set(props, "simple"); PGProperty.TCP_KEEP_ALIVE.set(props, true); - return DriverManager.getConnection( - String.format("jdbc:postgresql://%s:%d/%s", this.getHost(), this.getPort(), this.getDatabase()), - props); + // TLS: only the options that are set, so the defaults of the driver apply otherwise + if (sslMode != null) { + PGProperty.SSL_MODE.set(props, sslMode); + } + if (sslRootCert != null) { + PGProperty.SSL_ROOT_CERT.set(props, sslRootCert); + } + if (sslCert != null) { + PGProperty.SSL_CERT.set(props, sslCert); + } + if (sslKey != null) { + PGProperty.SSL_KEY.set(props, sslKey); + } + if (sslPassword != null) { + PGProperty.SSL_PASSWORD.set(props, sslPassword); + } + + return props; + } + + @Override + protected void doInit() throws Exception { + super.doInit(); + + if (sslMode != null && !SSL_MODES.contains(sslMode)) { + throw new IllegalArgumentException( + "Invalid sslMode: " + sslMode + ". Use one of: " + String.join(", ", SSL_MODES)); + } } /** @@ -247,4 +310,44 @@ public class PgReplicationSlotEndpoint extends ScheduledPollEndpoint { public void setAutoCreateSlot(Boolean autoCreateSlot) { this.autoCreateSlot = autoCreateSlot; } + + public String getSslMode() { + return sslMode; + } + + public void setSslMode(String sslMode) { + this.sslMode = sslMode; + } + + public String getSslRootCert() { + return sslRootCert; + } + + public void setSslRootCert(String sslRootCert) { + this.sslRootCert = sslRootCert; + } + + public String getSslCert() { + return sslCert; + } + + public void setSslCert(String sslCert) { + this.sslCert = sslCert; + } + + public String getSslKey() { + return sslKey; + } + + public void setSslKey(String sslKey) { + this.sslKey = sslKey; + } + + public String getSslPassword() { + return sslPassword; + } + + public void setSslPassword(String sslPassword) { + this.sslPassword = sslPassword; + } } diff --git a/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotTlsTest.java b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotTlsTest.java new file mode 100644 index 000000000000..5cb419e08d88 --- /dev/null +++ b/components/camel-pg-replication-slot/src/test/java/org/apache/camel/component/pg/replication/slot/PgReplicationSlotTlsTest.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.camel.component.pg.replication.slot; + +import java.util.Properties; + +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.camel.util.ObjectHelper; +import org.junit.jupiter.api.Test; +import org.postgresql.PGProperty; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The TLS options become properties of the JDBC connection; no database is needed. + */ +public class PgReplicationSlotTlsTest extends CamelTestSupport { + + private static final String BASE = "pg-replication-slot:localhost:5432/mydb/myslot:test_decoding"; + + @Test + public void tlsOptions() { + PgReplicationSlotEndpoint endpoint = context.getEndpoint(BASE + + "?sslMode=verify-full&sslRootCert=/certs/root.crt" + + "&sslCert=/certs/client.crt&sslKey=/certs/client.pk8" + + "&sslPassword=secret", + PgReplicationSlotEndpoint.class); + + Properties props = endpoint.createConnectionProperties(); + + assertEquals("verify-full", PGProperty.SSL_MODE.getOrDefault(props)); + assertEquals("/certs/root.crt", PGProperty.SSL_ROOT_CERT.getOrDefault(props)); + assertEquals("/certs/client.crt", PGProperty.SSL_CERT.getOrDefault(props)); + assertEquals("/certs/client.pk8", PGProperty.SSL_KEY.getOrDefault(props)); + assertEquals("secret", PGProperty.SSL_PASSWORD.getOrDefault(props)); + // the replication settings are still there + assertEquals("database", PGProperty.REPLICATION.getOrDefault(props)); + } + + @Test + public void noTlsOptionsKeepTheDriverDefaults() { + PgReplicationSlotEndpoint endpoint = context.getEndpoint(BASE, PgReplicationSlotEndpoint.class); + + Properties props = endpoint.createConnectionProperties(); + + for (PGProperty property : new PGProperty[] { + PGProperty.SSL_MODE, PGProperty.SSL_ROOT_CERT, PGProperty.SSL_CERT, PGProperty.SSL_KEY, + PGProperty.SSL_PASSWORD }) { + assertFalse(props.containsKey(property.getName()), property.getName() + " must not be set"); + } + assertEquals("postgres", PGProperty.USER.getOrDefault(props)); + } + + @Test + public void invalidSslMode() { + Exception e = assertThrows(Exception.class, () -> context.getEndpoint(BASE + "?sslMode=verify")); + IllegalArgumentException cause = ObjectHelper.getException(IllegalArgumentException.class, e); + assertNotNull(cause, "IllegalArgumentException expected as cause of " + e); + assertTrue(cause.getMessage().contains("Invalid sslMode: verify"), cause.getMessage()); + } +} diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PgReplicationSlotEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PgReplicationSlotEndpointBuilderFactory.java index b4ccc959ccac..b9a003a5116c 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PgReplicationSlotEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/PgReplicationSlotEndpointBuilderFactory.java @@ -605,6 +605,91 @@ public interface PgReplicationSlotEndpointBuilderFactory { doSetProperty("useFixedDelay", useFixedDelay); return this; } + /** + * The file of the client certificate (PEM) for a server that checks + * client certificates. When not set, the PostgreSQL JDBC driver reads + * postgresql.crt in the .postgresql folder of the home directory of the + * user, if it exists. + * + * The option is a: <code>java.lang.String</code> type. + * + * Group: security + * + * @param sslCert the value to set + * @return the dsl builder + */ + default PgReplicationSlotEndpointBuilder sslCert(String sslCert) { + doSetProperty("sslCert", sslCert); + return this; + } + /** + * The file of the private key of the client certificate: PKCS-8 in DER + * format, or a PKCS-12 file (.p12 or .pfx) that holds the certificate + * and the key, as the PostgreSQL JDBC driver expects. When not set, the + * driver reads postgresql.pk8 in the .postgresql folder of the home + * directory of the user, if it exists. + * + * The option is a: <code>java.lang.String</code> type. + * + * Group: security + * + * @param sslKey the value to set + * @return the dsl builder + */ + default PgReplicationSlotEndpointBuilder sslKey(String sslKey) { + doSetProperty("sslKey", sslKey); + return this; + } + /** + * The SSL mode of the connection to the PostgreSQL server: disable, + * allow, prefer, require, verify-ca or verify-full. When not set, the + * default of the PostgreSQL JDBC driver applies, which is prefer: TLS + * when the server offers it, without checking the server certificate, + * and a plain connection otherwise. Use verify-full to require TLS and + * check both the certificate and the host name of the server. + * + * The option is a: <code>java.lang.String</code> type. + * + * Group: security + * + * @param sslMode the value to set + * @return the dsl builder + */ + default PgReplicationSlotEndpointBuilder sslMode(String sslMode) { + doSetProperty("sslMode", sslMode); + return this; + } + /** + * The password of the private key in sslKey. + * + * The option is a: <code>java.lang.String</code> type. + * + * Group: security + * + * @param sslPassword the value to set + * @return the dsl builder + */ + default PgReplicationSlotEndpointBuilder sslPassword(String sslPassword) { + doSetProperty("sslPassword", sslPassword); + return this; + } + /** + * The file of the root certificates (PEM) that the server certificate + * is checked against with sslMode verify-ca or verify-full. When not + * set, the PostgreSQL JDBC driver reads root.crt in the .postgresql + * folder of the home directory of the user. + * + * The option is a: <code>java.lang.String</code> type. + * + * Group: security + * + * @param sslRootCert the value to set + * @return the dsl builder + */ + default PgReplicationSlotEndpointBuilder sslRootCert(String sslRootCert) { + doSetProperty("sslRootCert", sslRootCert); + return this; + } } /**
