tink-crypto / tink

Tink is a multi-language, cross-platform, open source library that provides cryptographic APIs that are secure, easy to use correctly, and hard(er) to misuse.
https://developers.google.com/tink
Apache License 2.0
13.5k stars 1.18k forks source link

No such method error: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; #557

Closed aditpshrimal closed 2 years ago

aditpshrimal commented 2 years ago

Hi team, PFB details of the task I plan to achieve.

  1. Read from JDBC table
  2. Encrypt data using Tink library
  3. Write encrypted data to BigQuery Note: I am planning to do this using Apache beam Dataflow (Java)

Steps I have completed:

  1. Created a basic template to read from JDBC and write to BigQuery using Google Provide Template https://github.com/GoogleCloudPlatform/DataflowTemplates/blob/HEAD/src/main/java/com/google/cloud/teleport/templates/JdbcToBigQuery.java
  2. Created Key ring and Keys in Google KMS.
  3. For encrypting data I have created Keyset in Java using following code String masterKeyUri = "gcp-kms://projects/<project>/locations/global/keyRings/my-app-keyring/cryptoKeys/testKey"; KeysetHandle handle = KeysetHandle.generateNew(KmsAeadKeyManager.createKeyTemplate(masterKeyUri)); kekAead = handle.getPrimitive(Aead.class); KeysetHandle handle = KeysetHandle.generateNew(KeyTemplates.get("AES256_GCM")); handle.write(JsonKeysetWriter.withFile(new File("./secure_keys.json")), kekAead);

Issues:

  1. When I read the Key "./secure_keys.json" using following code https://github.com/google/tink/blob/master/docs/JAVA-HOWTO.md#loading-existing-keysets File file = new File("./secure_keys.json"); String uri = "gcp-kms://projects/<project>/locations/global/keyRings/my-app-keyring/cryptoKeys/testKey"; KeysetHandle keysetHandle = KeysetHandle.read(JsonKeysetReader.withFile(file),new GcpKmsClient().getAead(uri)); I get the following error Exception in thread "main" java.lang.NoSuchMethodError: com.google.gson.JsonParser.parseString(Ljava/lang/String;)Lcom/google/gson/JsonElement; at com.google.crypto.tink.JsonKeysetReader.readEncrypted(JsonKeysetReader.java:163) at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:116) at org.example.analytics.KmsEncryption.initializeOnce(KmsEncryption.java:45) at org.example.analytics.JdbcToBigQuery.main(JdbcToBigQuery.java:18)

To Reproduce:

  1. Jdk 1.8
  2. Maven compiler source & target - 8
  3. Tink - 1.6.0
  4. Tink GCP Kms - 1.6.0
  5. GCP Kms - 1.40.0

Please let me know if I am doing something wrong.

aditpshrimal commented 2 years ago

I tried using this code as well https://developers.google.com/tink/generate-encrypted-keyset. Even after using the same code as mentioned in the official documentation, it gives the same error.

aditpshrimal commented 2 years ago

Looks like there were conflicts between dependencies. After updating KMS, beam and tink to latest versions I was able to resolve the issue.