From e5867c560730702604729f609254a22a5d5b47ab Mon Sep 17 00:00:00 2001 From: Thiago Nunes Date: Thu, 26 Nov 2020 12:28:53 +1100 Subject: [PATCH] fix: uses wrapper encryption info for backups --- .../java/com/google/cloud/spanner/Backup.java | 3 +- .../com/google/cloud/spanner/BackupInfo.java | 4 +- .../cloud/spanner/EncryptionConfigInfo.java | 2 +- .../google/cloud/spanner/EncryptionInfo.java | 91 +++++++++++++++++++ 4 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionInfo.java diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Backup.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Backup.java index 0f04b492dcc..bcaaf3f4749 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Backup.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/Backup.java @@ -183,7 +183,8 @@ static Backup fromProto( .setSize(proto.getSizeBytes()) .setExpireTime(Timestamp.fromProto(proto.getExpireTime())) .setDatabase(DatabaseId.of(proto.getDatabase())) - .setEncryptionInfo(proto.getEncryptionInfo()) + .setEncryptionInfo( + EncryptionInfo.fromProtoOrNullIfDefaultInstance(proto.getEncryptionInfo())) .setProto(proto) .build(); } diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BackupInfo.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BackupInfo.java index 14781a45277..64d77341cc3 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BackupInfo.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/BackupInfo.java @@ -18,8 +18,6 @@ import com.google.api.client.util.Preconditions; import com.google.cloud.Timestamp; -import com.google.spanner.admin.database.v1.EncryptionConfig; -import com.google.spanner.admin.database.v1.EncryptionInfo; import java.util.Objects; import javax.annotation.Nullable; @@ -197,7 +195,7 @@ public EncryptionConfigInfo getEncryptionConfigInfo() { } /** - * Returns the {@link EncryptionConfig} of the backup if the backup is encrypted, or null + * Returns the {@link EncryptionInfo} of the backup if the backup is encrypted, or null * if this backup is not encrypted. * * @return diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionConfigInfo.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionConfigInfo.java index aade769754f..788cbbfa0fd 100644 --- a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionConfigInfo.java +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionConfigInfo.java @@ -41,7 +41,7 @@ public String getKmsKeyName() { /** * Returns a {@link EncryptionConfigInfo} instance from the given proto, or null if - * the given proto is the default proto instance (i.e. there is no encryption info). + * the given proto is the default proto instance (i.e. there is no encryption config info). */ static EncryptionConfigInfo fromProtoOrNullIfDefaultInstance( com.google.spanner.admin.database.v1.EncryptionConfig proto) { diff --git a/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionInfo.java b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionInfo.java new file mode 100644 index 00000000000..051d3f3d83f --- /dev/null +++ b/google-cloud-spanner/src/main/java/com/google/cloud/spanner/EncryptionInfo.java @@ -0,0 +1,91 @@ +/* + * Copyright 2020 Google LLC + * + * 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 com.google.cloud.spanner; + +import com.google.rpc.Status; +import java.util.Objects; + +/** Represents the encryption information for a Cloud Spanner backup. */ +public class EncryptionInfo { + + private final String kmsKeyVersion; + private final com.google.spanner.admin.database.v1.EncryptionInfo.Type encryptionType; + private final Status encryptionStatus; + + public EncryptionInfo(com.google.spanner.admin.database.v1.EncryptionInfo proto) { + this.kmsKeyVersion = proto.getKmsKeyVersion(); + this.encryptionType = proto.getEncryptionType(); + this.encryptionStatus = proto.getEncryptionStatus(); + } + + /** + * Returns a {@link EncryptionInfo} instance from the given proto, or null if the + * given proto is the default proto instance (i.e. there is no encryption info). + */ + static EncryptionInfo fromProtoOrNullIfDefaultInstance( + com.google.spanner.admin.database.v1.EncryptionInfo proto) { + return proto.equals(com.google.spanner.admin.database.v1.EncryptionInfo.getDefaultInstance()) + ? null + : new EncryptionInfo(proto); + } + + public com.google.spanner.admin.database.v1.EncryptionInfo toProto() { + return com.google.spanner.admin.database.v1.EncryptionInfo.newBuilder() + .setKmsKeyVersion(kmsKeyVersion) + .setEncryptionType(encryptionType) + .setEncryptionStatus(encryptionStatus) + .build(); + } + + public String getKmsKeyVersion() { + return kmsKeyVersion; + } + + public com.google.spanner.admin.database.v1.EncryptionInfo.Type getEncryptionType() { + return encryptionType; + } + + public Status getEncryptionStatus() { + return encryptionStatus; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + EncryptionInfo that = (EncryptionInfo) o; + return Objects.equals(kmsKeyVersion, that.kmsKeyVersion) + && encryptionType == that.encryptionType + && Objects.equals(encryptionStatus, that.encryptionStatus); + } + + @Override + public int hashCode() { + return Objects.hash(kmsKeyVersion, encryptionType, encryptionStatus); + } + + @Override + public String toString() { + return String.format( + "EncryptionInfo[kmsKeyVersion=%s,encryptionType=%s,encryptionStatus=%s]", + kmsKeyVersion, encryptionType, encryptionStatus); + } +}