Skip to content

Commit

Permalink
fix: uses wrapper encryption info for backups
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagotnunes committed Jan 21, 2021
1 parent 4f5e296 commit e5867c5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
Expand Up @@ -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();
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -197,7 +195,7 @@ public EncryptionConfigInfo getEncryptionConfigInfo() {
}

/**
* Returns the {@link EncryptionConfig} of the backup if the backup is encrypted, or <code>null
* Returns the {@link EncryptionInfo} of the backup if the backup is encrypted, or <code>null
* </code> if this backup is not encrypted.
*
* @return
Expand Down
Expand Up @@ -41,7 +41,7 @@ public String getKmsKeyName() {

/**
* Returns a {@link EncryptionConfigInfo} instance from the given proto, or <code>null</code> 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) {
Expand Down
@@ -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 <code>null</code> 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);
}
}

0 comments on commit e5867c5

Please sign in to comment.