Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
<cdap.version>6.11.0</cdap.version>
<cdap.plugin.version>2.13.0</cdap.plugin.version>
<dropwizard.metrics-core.version>3.2.6</dropwizard.metrics-core.version>
<google.http.client.version>1.41.8</google.http.client.version>
<google.oauth.client.version>1.33.3</google.oauth.client.version>
<flogger.system.backend.version>0.7.1</flogger.system.backend.version>
<gcs.connector.version>hadoop3-2.2.21</gcs.connector.version>
<google.cloud.bigtable.version>2.18.3</google.cloud.bigtable.version>
Expand All @@ -74,13 +76,15 @@
<google.cloud.speech.version>1.24.7</google.cloud.speech.version>
<google.cloud.storage.version>2.3.0</google.cloud.storage.version>
<google.cloud.datastore.version>1.105.1</google.cloud.datastore.version>
<google.protobuf.java.version>3.19.4</google.protobuf.java.version>
<google.protobuf.java.version>3.25.5</google.protobuf.java.version>
<google.tink.version>1.3.0-rc3</google.tink.version>
<org.json.version>20260719</org.json.version>
<guava.version>27.0.1-jre</guava.version>
<hadoop.version>3.3.6</hadoop.version>
<hbase-shaded-mapreduce.version>2.6.6-hadoop3</hbase-shaded-mapreduce.version>
<httpclient.version>4.5.13</httpclient.version>
<jackson.core.version>2.13.4.2</jackson.core.version>
<httpclient.version>4.5.14</httpclient.version>
<httpcore.version>4.4.16</httpcore.version>
<jackson.core.version>2.18.12</jackson.core.version>
<junit.version>4.13.1</junit.version>
<powermock.version>2.0.2</powermock.version>
<slf4j.version>1.7.5</slf4j.version>
Expand All @@ -92,6 +96,41 @@

<dependencyManagement>
<dependencies>
<!-- Aligns every Jackson module (jackson-core, -databind, -dataformat-cbor, ...) on one version. -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>${jackson.core.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Aligns protobuf-java and protobuf-java-util. -->
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-bom</artifactId>
<version>${google.protobuf.java.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- google-oauth-client 1.33.3 is built against google-http-client 1.41.x. -->
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-bom</artifactId>
<version>${google.http.client.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>${google.oauth.client.version}</version>
</dependency>
<!-- org.json comes in through tink and is used by GCSTinkTool / TinkDecryptor. -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${org.json.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-bigquery</artifactId>
Expand All @@ -102,6 +141,16 @@
<artifactId>google-api-services-storage</artifactId>
<version>v1-rev20211201-1.32.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -613,6 +662,16 @@
<artifactId>avro-mapred</artifactId>
<version>${avro.version}</version>
<exclusions>
<!-- avro-ipc is only needed for Avro RPC / tether jobs, which the plugins do not use.
It brings in velocity-engine-core and, through avro-ipc-jetty, Jetty. -->
<exclusion>
<groupId>org.apache.avro</groupId>
<artifactId>avro-ipc</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.avro</groupId>
<artifactId>avro-ipc-jetty</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down Expand Up @@ -766,7 +825,7 @@
<dependency>
<groupId>org.xerial.snappy</groupId>
<artifactId>snappy-java</artifactId>
<version>1.1.10.5</version>
<version>1.1.10.12</version>
</dependency>
<!-- Start: dependency for Google PubSub Streaming Source -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright © 2026 Cask Data, Inc.
*
* Licensed 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 io.cdap.plugin.gcp.gcs.source;

import com.google.common.io.ByteStreams;
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.JsonKeysetReader;
import com.google.crypto.tink.JsonKeysetWriter;
import com.google.crypto.tink.KeysetHandle;
import com.google.crypto.tink.StreamingAead;
import com.google.crypto.tink.aead.AeadConfig;
import com.google.crypto.tink.aead.AeadKeyTemplates;
import com.google.crypto.tink.streamingaead.StreamingAeadConfig;
import com.google.crypto.tink.streamingaead.StreamingAeadKeyTemplates;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* Checks that the encryption metadata written by {@link GCSTinkTool} can be parsed with org.json and read back by
* Tink the same way {@link TinkDecryptor} does. A local AEAD key stands in for the Cloud KMS key.
*/
public class TinkKeysetJsonTest {

private static final int ITERATIONS = 10;

@BeforeClass
public static void setupClass() throws Exception {
AeadConfig.register();
StreamingAeadConfig.register();
}

@Test
public void testMetadataRoundTrip() throws Exception {
Aead keysetAead = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM).getPrimitive(Aead.class);
byte[] plaintext = "some plaintext to encrypt".getBytes(UTF_8);
byte[] associatedData = "gs://bucket/path/file.csv".getBytes(UTF_8);

// Each iteration uses a new keyset, so different random key ids go through the JSON round trip.
for (int i = 0; i < ITERATIONS; i++) {
KeysetHandle handle = KeysetHandle.generateNew(StreamingAeadKeyTemplates.AES256_GCM_HKDF_4KB);

// Encrypt and write the metadata the same way as GCSTinkTool.
ByteArrayOutputStream ciphertext = new ByteArrayOutputStream();
try (OutputStream os = handle.getPrimitive(StreamingAead.class).newEncryptingStream(ciphertext,
associatedData)) {
os.write(plaintext);
}
ByteArrayOutputStream keyset = new ByteArrayOutputStream();
handle.write(JsonKeysetWriter.withOutputStream(keyset), keysetAead);
JSONObject metadataObj = new JSONObject();
metadataObj.put("kms", "gcp-kms://projects/p/locations/global/keyRings/r/cryptoKeys/k");
metadataObj.put("aad", Base64.getEncoder().encodeToString(associatedData));
metadataObj.put("keyset", new JSONObject(new String(keyset.toByteArray(), UTF_8)));
String metadataJson = metadataObj.toString(4);

// Parse and read it back the same way as TinkDecryptor.
JSONObject metadata = new JSONObject(metadataJson);
KeysetHandle readHandle = KeysetHandle.read(JsonKeysetReader.withJsonObject(metadata.getJSONObject("keyset")),
keysetAead);
byte[] aad = Base64.getDecoder().decode(metadata.getString("aad"));
Assert.assertEquals(handle.getKeysetInfo().getPrimaryKeyId(), readHandle.getKeysetInfo().getPrimaryKeyId());

try (InputStream is = readHandle.getPrimitive(StreamingAead.class)
.newDecryptingStream(new ByteArrayInputStream(ciphertext.toByteArray()), aad)) {
Assert.assertArrayEquals(plaintext, ByteStreams.toByteArray(is));
}
}
}
}
Loading