1
0
mirror of synced 2025-12-25 02:09:19 -05:00

destination-postgres: convert stric-encrypt prod code to kotlin (#40563)

## What
<!--
* Describe what the change is solving. Link all GitHub issues related to this change.
-->

## How
<!--
* Describe how code changes achieve the solution.
-->

## Review guide
<!--
1. `x.py`
2. `y.py`
-->

## User Impact
<!--
* What is the end result perceived by the user?
* If there are negative side effects, please list them. 
-->

## Can this PR be safely reverted and rolled back?
<!--
* If unsure, leave it blank.
-->
- [ ] YES 💚
- [ ] NO 
This commit is contained in:
Stephane Geneix
2024-06-28 10:09:46 -07:00
committed by GitHub
parent e9e5c1e6cf
commit 7df8a9430f
5 changed files with 82 additions and 81 deletions

View File

@@ -2,7 +2,7 @@ data:
connectorSubtype: database
connectorType: destination
definitionId: 25c5221d-dce2-4163-ade9-739ef790f503
dockerImageTag: 2.0.14
dockerImageTag: 2.0.15
dockerRepository: airbyte/destination-postgres-strict-encrypt
documentationUrl: https://docs.airbyte.com/integrations/destinations/postgres
githubIssueLabel: destination-postgres

View File

@@ -1,78 +0,0 @@
/*
* Copyright (c) 2023 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.integrations.destination.postgres;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.airbyte.cdk.db.jdbc.JdbcUtils;
import io.airbyte.cdk.integrations.base.AirbyteExceptionHandler;
import io.airbyte.cdk.integrations.base.Destination;
import io.airbyte.cdk.integrations.base.IntegrationRunner;
import io.airbyte.cdk.integrations.base.spec_modification.SpecModifyingDestination;
import io.airbyte.commons.json.Jsons;
import io.airbyte.protocol.models.v0.AirbyteConnectionStatus;
import io.airbyte.protocol.models.v0.AirbyteConnectionStatus.Status;
import io.airbyte.protocol.models.v0.ConnectorSpecification;
import java.util.Set;
import org.postgresql.util.PSQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PostgresDestinationStrictEncrypt extends SpecModifyingDestination implements Destination {
private static final Logger LOGGER = LoggerFactory.getLogger(PostgresDestinationStrictEncrypt.class);
private static final String PROPERTIES = "properties";
public static final String TUNNEL_METHOD = "tunnel_method";
public static final String NO_TUNNEL = "NO_TUNNEL";
public static final String SSL_MODE = "ssl_mode";
public static final String MODE = "mode";
public static final String SSL_MODE_ALLOW = "allow";
public static final String SSL_MODE_PREFER = "prefer";
public static final String SSL_MODE_DISABLE = "disable";
public PostgresDestinationStrictEncrypt() {
super(PostgresDestination.sshWrappedDestination());
}
@Override
public ConnectorSpecification modifySpec(final ConnectorSpecification originalSpec) {
final ConnectorSpecification spec = Jsons.clone(originalSpec);
((ObjectNode) spec.getConnectionSpecification().get(PROPERTIES)).remove(JdbcUtils.SSL_KEY);
return spec;
}
@Override
public AirbyteConnectionStatus check(final JsonNode config) throws Exception {
if (config.has(TUNNEL_METHOD)
&& config.get(TUNNEL_METHOD).has(TUNNEL_METHOD)
&& config.get(TUNNEL_METHOD).get(TUNNEL_METHOD).asText().equals(NO_TUNNEL)) {
// If no SSH tunnel
if (config.has(SSL_MODE) && config.get(SSL_MODE).has(MODE)) {
if (Set.of(SSL_MODE_DISABLE, SSL_MODE_ALLOW, SSL_MODE_PREFER).contains(config.get(SSL_MODE).get(MODE).asText())) {
// Fail in case SSL mode is disable, allow or prefer
return new AirbyteConnectionStatus()
.withStatus(Status.FAILED)
.withMessage(
"Unsecured connection not allowed. If no SSH Tunnel set up, please use one of the following SSL modes: require, verify-ca, verify-full");
}
}
}
return super.check(config);
}
@Override
public boolean isV2Destination() {
return true;
}
public static void main(final String[] args) throws Exception {
AirbyteExceptionHandler.addThrowableForDeinterpolation(PSQLException.class);
final Destination destination = new PostgresDestinationStrictEncrypt();
LOGGER.info("starting destination: {}", PostgresDestinationStrictEncrypt.class);
new IntegrationRunner(destination).run(args);
LOGGER.info("completed destination: {}", PostgresDestinationStrictEncrypt.class);
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright (c) 2023 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.integrations.destination.postgres
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ObjectNode
import io.airbyte.cdk.db.jdbc.JdbcUtils
import io.airbyte.cdk.integrations.base.AirbyteExceptionHandler
import io.airbyte.cdk.integrations.base.Destination
import io.airbyte.cdk.integrations.base.IntegrationRunner
import io.airbyte.cdk.integrations.base.spec_modification.SpecModifyingDestination
import io.airbyte.commons.json.Jsons
import io.airbyte.integrations.destination.postgres.PostgresDestination.Companion.sshWrappedDestination
import io.airbyte.protocol.models.v0.AirbyteConnectionStatus
import io.airbyte.protocol.models.v0.ConnectorSpecification
import org.postgresql.util.PSQLException
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class PostgresDestinationStrictEncrypt :
SpecModifyingDestination(sshWrappedDestination()), Destination {
override fun modifySpec(originalSpec: ConnectorSpecification): ConnectorSpecification {
val spec: ConnectorSpecification = Jsons.clone<ConnectorSpecification>(originalSpec)
(spec.connectionSpecification[PROPERTIES] as ObjectNode).remove(JdbcUtils.SSL_KEY)
return spec
}
@Throws(Exception::class)
override fun check(config: JsonNode): AirbyteConnectionStatus? {
if (
(config.has(TUNNEL_METHOD) && config[TUNNEL_METHOD].has(TUNNEL_METHOD)) &&
config[TUNNEL_METHOD][TUNNEL_METHOD].asText() == NO_TUNNEL
) {
// If no SSH tunnel
if (config.has(SSL_MODE) && config[SSL_MODE].has(MODE)) {
if (
setOf(SSL_MODE_DISABLE, SSL_MODE_ALLOW, SSL_MODE_PREFER)
.contains(config[SSL_MODE][MODE].asText())
) {
// Fail in case SSL mode is disable, allow or prefer
return AirbyteConnectionStatus()
.withStatus(AirbyteConnectionStatus.Status.FAILED)
.withMessage(
"Unsecured connection not allowed. If no SSH Tunnel set up, please use one of the following SSL modes: require, verify-ca, verify-full"
)
}
}
}
return super.check(config)
}
override val isV2Destination: Boolean
get() = true
companion object {
private val LOGGER: Logger =
LoggerFactory.getLogger(PostgresDestinationStrictEncrypt::class.java)
private const val PROPERTIES = "properties"
const val TUNNEL_METHOD: String = "tunnel_method"
const val NO_TUNNEL: String = "NO_TUNNEL"
const val SSL_MODE: String = "ssl_mode"
const val MODE: String = "mode"
const val SSL_MODE_ALLOW: String = "allow"
const val SSL_MODE_PREFER: String = "prefer"
const val SSL_MODE_DISABLE: String = "disable"
@Throws(Exception::class)
@JvmStatic
fun main(args: Array<String>) {
AirbyteExceptionHandler.addThrowableForDeinterpolation(PSQLException::class.java)
val destination: Destination = PostgresDestinationStrictEncrypt()
LOGGER.info("starting destination: {}", PostgresDestinationStrictEncrypt::class.java)
IntegrationRunner(destination).run(args)
LOGGER.info("completed destination: {}", PostgresDestinationStrictEncrypt::class.java)
}
}
}