Skip to content

Commit

Permalink
Checkstyle fixes for Cloud Storage CSEK sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast committed Mar 24, 2016
1 parent 47923d9 commit 99d0b0a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ class CustomerSuppliedEncryptionKeysSamples {
* @param storage A Storage object, ready for use
* @param bucketName The name of the destination bucket
* @param objectName The name of the destination object
* @param base64CSEKey An AES256 key, encoded as a base64 string.
* @param base64CSEKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
* @throws IOException if there was some error download from GCS.
* @param base64CseKey An AES256 key, encoded as a base64 string.
* @param base64CseKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
*
* @return An InputStream that contains the decrypted contents of the object.
*
* @throws IOException if there was some error download from GCS.
*/
public static InputStream downloadObject(
Storage storage,
String bucketName,
String objectName,
String base64CSEKey,
String base64CSEKeyHash)
String base64CseKey,
String base64CseKeyHash)
throws Exception {
Storage.Objects.Get getObject = storage.objects().get(bucketName, objectName);

Expand All @@ -66,8 +67,8 @@ public static InputStream downloadObject(
// Now set the CSEK headers
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("x-goog-encryption-algorithm", "AES256");
httpHeaders.set("x-goog-encryption-key", base64CSEKey);
httpHeaders.set("x-goog-encryption-key-sha256", base64CSEKeyHash);
httpHeaders.set("x-goog-encryption-key", base64CseKey);
httpHeaders.set("x-goog-encryption-key-sha256", base64CseKeyHash);

getObject.setRequestHeaders(httpHeaders);

Expand All @@ -89,17 +90,17 @@ public static InputStream downloadObject(
* @param bucketName The name of the destination bucket
* @param objectName The name of the destination object
* @param data An InputStream containing the contents of the object to upload
* @param base64CSEKey An AES256 key, encoded as a base64 string.
* @param base64CSEKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
* @param base64CseKey An AES256 key, encoded as a base64 string.
* @param base64CseKeyHash The SHA-256 hash of the above key, also encoded as a base64 string.
* @throws IOException if there was some error uploading to GCS.
*/
public static void uploadObject(
Storage storage,
String bucketName,
String objectName,
InputStream data,
String base64CSEKey,
String base64CSEKeyHash)
String base64CseKey,
String base64CseKeyHash)
throws IOException {
InputStreamContent mediaContent = new InputStreamContent("text/plain", data);
Storage.Objects.Insert insertObject =
Expand All @@ -112,8 +113,8 @@ public static void uploadObject(
// Now set the CSEK headers
final HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("x-goog-encryption-algorithm", "AES256");
httpHeaders.set("x-goog-encryption-key", base64CSEKey);
httpHeaders.set("x-goog-encryption-key-sha256", base64CSEKeyHash);
httpHeaders.set("x-goog-encryption-key", base64CseKey);
httpHeaders.set("x-goog-encryption-key-sha256", base64CseKeyHash);

insertObject.setRequestHeaders(httpHeaders);

Expand Down Expand Up @@ -192,18 +193,18 @@ public static void main(String[] args) throws Exception {
System.exit(1);
}
String bucketName = args[0];

Storage storage = StorageFactory.getService();
InputStream dataToUpload = new StorageUtils.ArbitrarilyLargeInputStream(10000000);

System.out.format("Uploading object gs://%s/%s using CSEK.\n", bucketName, OBJECT_NAME);
uploadObject(storage, bucketName, OBJECT_NAME, dataToUpload, CSEK_KEY, CSEK_KEY_HASH);

System.out.format("Downloading object gs://%s/%s using CSEK.\n", bucketName, OBJECT_NAME);
InputStream objectData =
downloadObject(storage, bucketName, OBJECT_NAME, CSEK_KEY, CSEK_KEY_HASH);
StorageUtils.readStream(objectData);

System.out.println("Rotating object to use a different CSEK.");
rotateKey(storage, bucketName, OBJECT_NAME, CSEK_KEY, CSEK_KEY_HASH,
ANOTHER_CESK_KEY, ANOTHER_CSEK_KEY_HASH);
Expand Down
4 changes: 2 additions & 2 deletions storage/json-api/src/main/java/StorageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import java.io.InputStream;

public class StorageUtils {

/**
* Reads the contents of an InputStream and does nothing with it.
*/
public static void readStream(InputStream is) throws IOException {
byte inputBuffer[] = new byte[256];
byte[] inputBuffer = new byte[256];
while (is.read(inputBuffer) != -1) {}
// The caller is responsible for closing this InputStream.
is.close();
Expand Down

0 comments on commit 99d0b0a

Please sign in to comment.