|
| 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 java.io.FileOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.file.Files; |
| 32 | +import java.nio.file.Paths; |
| 33 | + |
| 34 | +public class CryptFile { |
| 35 | + |
| 36 | + String projectId; |
| 37 | + |
| 38 | + public CryptFile(String projectId) { |
| 39 | + this.projectId = projectId; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates an authorized CloudKMS client service using Application Default Credentials. |
| 44 | + * |
| 45 | + * @return an authorized CloudKMS client |
| 46 | + * @throws IOException if there's an error getting the default credentials. |
| 47 | + */ |
| 48 | + public static CloudKMS createAuthorizedClient() throws IOException { |
| 49 | + // Create the credential |
| 50 | + HttpTransport transport = new NetHttpTransport(); |
| 51 | + JsonFactory jsonFactory = new JacksonFactory(); |
| 52 | + // Authorize the client using Application Default Credentials |
| 53 | + // @see https://g.co/dv/identity/protocols/application-default-credentials |
| 54 | + GoogleCredential credential = GoogleCredential.getApplicationDefault(transport, jsonFactory); |
| 55 | + |
| 56 | + // Depending on the environment that provides the default credentials (e.g. Compute Engine, App |
| 57 | + // Engine), the credentials may require us to specify the scopes we need explicitly. |
| 58 | + // Check for this case, and inject the scope if required. |
| 59 | + if (credential.createScopedRequired()) { |
| 60 | + credential = credential.createScoped(CloudKMSScopes.all()); |
| 61 | + } |
| 62 | + |
| 63 | + return new CloudKMS.Builder(transport, jsonFactory, credential) |
| 64 | + .setApplicationName("CloudKMS CryptFile") |
| 65 | + .build(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Encrypts the given bytes, using the specified crypto key. |
| 70 | + */ |
| 71 | + public byte[] encrypt(String ringId, String keyId, byte[] plaintext) |
| 72 | + throws IOException { |
| 73 | + String location = "global"; |
| 74 | + // The resource name of the cryptoKey |
| 75 | + String cryptoKeyName = String.format( |
| 76 | + "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", |
| 77 | + projectId, location, ringId, keyId); |
| 78 | + // Create the Cloud KMS client. |
| 79 | + CloudKMS kms = createAuthorizedClient(); |
| 80 | + |
| 81 | + EncryptRequest request = new EncryptRequest().encodePlaintext(plaintext); |
| 82 | + EncryptResponse response = kms.projects().locations().keyRings().cryptoKeys() |
| 83 | + .encrypt(cryptoKeyName, request) |
| 84 | + .execute(); |
| 85 | + |
| 86 | + return response.decodeCiphertext(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Decrypts the given encrypted bytes, using the specified crypto key. |
| 91 | + */ |
| 92 | + public byte[] decrypt(String ringId, String keyId, byte[] encrypted) |
| 93 | + throws IOException { |
| 94 | + String location = "global"; |
| 95 | + // Create the Cloud KMS client. |
| 96 | + CloudKMS kms = createAuthorizedClient(); |
| 97 | + |
| 98 | + // The resource name of the cryptoKey |
| 99 | + String cryptoKeyName = String.format( |
| 100 | + "projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", |
| 101 | + projectId, location, ringId, keyId); |
| 102 | + |
| 103 | + DecryptRequest request = new DecryptRequest().encodeCiphertext(encrypted); |
| 104 | + DecryptResponse response = kms.projects().locations().keyRings().cryptoKeys() |
| 105 | + .decrypt(cryptoKeyName, request) |
| 106 | + .execute(); |
| 107 | + |
| 108 | + return response.decodePlaintext(); |
| 109 | + } |
| 110 | + |
| 111 | + public static void main(String[] args) throws IOException { |
| 112 | + // Your Google Cloud Platform project ID |
| 113 | + String projectId = args[0]; |
| 114 | + String command = args[1]; |
| 115 | + |
| 116 | + CryptFile cryptFile = new CryptFile(projectId); |
| 117 | + |
| 118 | + if ("encrypt".equals(command)) { |
| 119 | + String ringId = args[2]; |
| 120 | + String keyId = args[3]; |
| 121 | + String inFile = args[4]; |
| 122 | + String outFile = args[5]; |
| 123 | + |
| 124 | + byte[] encrypted = cryptFile.encrypt( |
| 125 | + ringId, keyId, |
| 126 | + Files.readAllBytes(Paths.get(inFile))); |
| 127 | + |
| 128 | + FileOutputStream stream = new FileOutputStream(outFile); |
| 129 | + try { |
| 130 | + stream.write(encrypted); |
| 131 | + } finally { |
| 132 | + stream.close(); |
| 133 | + } |
| 134 | + |
| 135 | + } else if ("decrypt".equals(command)) { |
| 136 | + String ringId = args[2]; |
| 137 | + String keyId = args[3]; |
| 138 | + String inFile = args[4]; |
| 139 | + String outFile = args[5]; |
| 140 | + |
| 141 | + byte[] decrypted = cryptFile.decrypt( |
| 142 | + ringId, keyId, |
| 143 | + Files.readAllBytes(Paths.get(inFile))); |
| 144 | + |
| 145 | + FileOutputStream stream = new FileOutputStream(outFile); |
| 146 | + try { |
| 147 | + stream.write(decrypted); |
| 148 | + } finally { |
| 149 | + stream.close(); |
| 150 | + } |
| 151 | + |
| 152 | + } else { |
| 153 | + throw new RuntimeException("Unrecognized command: " + command); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments