|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.example; |
| 16 | + |
| 17 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 18 | +import com.google.api.client.http.HttpTransport; |
| 19 | +import com.google.api.client.http.javanet.NetHttpTransport; |
| 20 | +import com.google.api.client.json.JsonFactory; |
| 21 | +import com.google.api.client.json.jackson2.JacksonFactory; |
| 22 | +import com.google.api.services.cloudkms.v1beta1.CloudKMS; |
| 23 | +import com.google.api.services.cloudkms.v1beta1.CloudKMSScopes; |
| 24 | +import com.google.api.services.cloudkms.v1beta1.model.DecryptRequest; |
| 25 | +import com.google.api.services.cloudkms.v1beta1.model.DecryptResponse; |
| 26 | +import com.google.api.services.cloudkms.v1beta1.model.EncryptRequest; |
| 27 | +import com.google.api.services.cloudkms.v1beta1.model.EncryptResponse; |
| 28 | + |
| 29 | +import org.kohsuke.args4j.CmdLineException; |
| 30 | +import org.kohsuke.args4j.CmdLineParser; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +public class CryptFile { |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates an authorized CloudKMS client service using Application Default Credentials. |
| 38 | + * |
| 39 | + * @return an authorized CloudKMS client |
| 40 | + * @throws IOException if there's an error getting the default credentials. |
| 41 | + */ |
| 42 | + public static CloudKMS createAuthorizedClient() throws IOException { |
| 43 | + // Create the credential |
| 44 | + HttpTransport transport = new NetHttpTransport(); |
| 45 | + JsonFactory jsonFactory = new JacksonFactory(); |
| 46 | + // Authorize the client using Application Default Credentials |
| 47 | + // @see https://g.co/dv/identity/protocols/application-default-credentials |
| 48 | + GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); |
| 49 | + |
| 50 | + // Depending on the environment that provides the default credentials (e.g. Compute Engine, App |
| 51 | + // Engine), the credentials may require us to specify the scopes we need explicitly. |
| 52 | + // Check for this case, and inject the scope if required. |
| 53 | + if (credential.createScopedRequired()) { |
| 54 | + credential = credential.createScoped(CloudKMSScopes.all()); |
| 55 | + } |
| 56 | + |
| 57 | + return new CloudKMS.Builder(transport, jsonFactory, credential) |
| 58 | + .setApplicationName("CloudKMS CryptFile") |
| 59 | + .build(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Encrypts the given bytes, using the specified crypto key. |
| 64 | + */ |
| 65 | + public static byte[] encrypt(String projectId, String ringId, String keyId, byte[] plaintext) |
| 66 | + throws IOException { |
| 67 | + String location = "global"; |
| 68 | + // The resource name of the cryptoKey |
| 69 | + String cryptoKeyName = String.format( |
| 70 | + "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", |
| 71 | + projectId, location, ringId, keyId); |
| 72 | + // Create the Cloud KMS client. |
| 73 | + CloudKMS kms = createAuthorizedClient(); |
| 74 | + |
| 75 | + EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext); |
| 76 | + EncryptResponse response = kms.projects().locations().keyRings().cryptoKeys() |
| 77 | + .encrypt(cryptoKeyName, request) |
| 78 | + .execute(); |
| 79 | + |
| 80 | + return response.decodeCiphertext(); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Decrypts the given encrypted bytes, using the specified crypto key. |
| 85 | + */ |
| 86 | + public static byte[] decrypt(String projectId, String ringId, String keyId, byte[] encrypted) |
| 87 | + throws IOException { |
| 88 | + String location = "global"; |
| 89 | + // Create the Cloud KMS client. |
| 90 | + CloudKMS kms = createAuthorizedClient(); |
| 91 | + |
| 92 | + // The resource name of the cryptoKey |
| 93 | + String cryptoKeyName = String.format( |
| 94 | + "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", |
| 95 | + projectId, location, ringId, keyId); |
| 96 | + |
| 97 | + DecryptRequest request = new DecryptRequest().encodeCiphertext(encrypted); |
| 98 | + DecryptResponse response = kms.projects().locations().keyRings().cryptoKeys() |
| 99 | + .decrypt(cryptoKeyName, request) |
| 100 | + .execute(); |
| 101 | + |
| 102 | + return response.decodePlaintext(); |
| 103 | + } |
| 104 | + |
| 105 | + public static void main(String[] args) throws IOException { |
| 106 | + CryptFileCommands commands = new CryptFileCommands(); |
| 107 | + CmdLineParser parser = new CmdLineParser(commands); |
| 108 | + |
| 109 | + try { |
| 110 | + parser.parseArgument(args); |
| 111 | + } catch (CmdLineException e) { |
| 112 | + System.out.println(e); |
| 113 | + System.out.println(); |
| 114 | + e.getParser().printUsage(System.out); |
| 115 | + System.exit(1); |
| 116 | + } |
| 117 | + commands.command.run(); |
| 118 | + } |
| 119 | +} |
0 commit comments