Skip to content

Commit

Permalink
feat: implement GrpcStorageImpl#*HmacKey (#1576)
Browse files Browse the repository at this point in the history
* GrpcStorageImpl#createHmacKey
* GrpcStorageImpl#getHmacKey
* GrpcStorageImpl#updateHmacKey
* GrpcStorageImpl#deleteHmacKey

Add tests for each Hmac method to ITGrpcTest
  • Loading branch information
BenWhitehead authored Aug 23, 2022
1 parent 480a09d commit 84e4adf
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ private com.google.api.services.storage.model.HmacKeyMetadata hmacKeyMetadataEnc
to.setId(from.getId());
to.setProjectId(from.getProjectId());
ifNonNull(from.getServiceAccount(), ServiceAccount::getEmail, to::setServiceAccountEmail);
ifNonNull(from.getState(), Object::toString, to::setState);
ifNonNull(from.getState(), Enum::name, to::setState);
ifNonNull(from.getCreateTimeOffsetDateTime(), dateTimeCodec::encode, to::setTimeCreated);
ifNonNull(from.getUpdateTimeOffsetDateTime(), dateTimeCodec::encode, to::setUpdated);
return to;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.cloud.storage.Utils.durationMillisCodec;
import static com.google.cloud.storage.Utils.ifNonNull;
import static com.google.cloud.storage.Utils.lift;
import static com.google.cloud.storage.Utils.projectNameCodec;
import static com.google.cloud.storage.Utils.toImmutableListOf;
import static com.google.cloud.storage.Utils.todo;

Expand Down Expand Up @@ -678,12 +679,12 @@ private BucketInfo.LifecycleRule lifecycleRuleDecode(Bucket.Lifecycle.Rule from)

private HmacKeyMetadata hmacKeyMetadataEncode(HmacKey.HmacKeyMetadata from) {
HmacKeyMetadata.Builder to = HmacKeyMetadata.newBuilder();
to.setAccessId(from.getAccessId());
// TODO etag
to.setId(from.getId());
to.setProject(from.getProjectId());
ifNonNull(from.getEtag(), to::setEtag);
ifNonNull(from.getId(), to::setId);
ifNonNull(from.getAccessId(), to::setAccessId);
ifNonNull(from.getProjectId(), projectNameCodec::encode, to::setProject);
ifNonNull(from.getServiceAccount(), ServiceAccount::getEmail, to::setServiceAccountEmail);
ifNonNull(from.getState(), java.lang.Object::toString, to::setState);
ifNonNull(from.getState(), Enum::name, to::setState);
ifNonNull(from.getCreateTimeOffsetDateTime(), timestampCodec::encode, to::setCreateTime);
ifNonNull(from.getUpdateTimeOffsetDateTime(), timestampCodec::encode, to::setUpdateTime);
return to.build();
Expand All @@ -694,7 +695,7 @@ private HmacKey.HmacKeyMetadata hmacKeyMetadataDecode(HmacKeyMetadata from) {
.setAccessId(from.getAccessId())
.setCreateTimeOffsetDateTime(timestampCodec.decode(from.getCreateTime()))
.setId(from.getId())
.setProjectId(from.getProject())
.setProjectId(projectNameCodec.decode(from.getProject()))
.setState(HmacKey.HmacKeyState.valueOf(from.getState()))
.setUpdateTimeOffsetDateTime(timestampCodec.decode(from.getUpdateTime()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,28 @@
import com.google.cloud.storage.UnifiedOpts.BucketSourceOpt;
import com.google.cloud.storage.UnifiedOpts.BucketTargetOpt;
import com.google.cloud.storage.UnifiedOpts.HmacKeyListOpt;
import com.google.cloud.storage.UnifiedOpts.HmacKeySourceOpt;
import com.google.cloud.storage.UnifiedOpts.HmacKeyTargetOpt;
import com.google.cloud.storage.UnifiedOpts.ObjectListOpt;
import com.google.cloud.storage.UnifiedOpts.ObjectSourceOpt;
import com.google.cloud.storage.UnifiedOpts.ObjectTargetOpt;
import com.google.cloud.storage.UnifiedOpts.Opts;
import com.google.cloud.storage.UnifiedOpts.ProjectId;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;
import com.google.protobuf.ByteString;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Message;
import com.google.storage.v2.ComposeObjectRequest;
import com.google.storage.v2.ComposeObjectRequest.SourceObject;
import com.google.storage.v2.CreateBucketRequest;
import com.google.storage.v2.CreateHmacKeyRequest;
import com.google.storage.v2.DeleteBucketRequest;
import com.google.storage.v2.DeleteHmacKeyRequest;
import com.google.storage.v2.DeleteObjectRequest;
import com.google.storage.v2.GetBucketRequest;
import com.google.storage.v2.GetHmacKeyRequest;
import com.google.storage.v2.GetObjectRequest;
import com.google.storage.v2.GetServiceAccountRequest;
import com.google.storage.v2.ListBucketsRequest;
Expand All @@ -83,6 +88,7 @@
import com.google.storage.v2.StorageClient.ListObjectsPage;
import com.google.storage.v2.StorageClient.ListObjectsPagedResponse;
import com.google.storage.v2.UpdateBucketRequest;
import com.google.storage.v2.UpdateHmacKeyRequest;
import com.google.storage.v2.UpdateObjectRequest;
import com.google.storage.v2.WriteObjectRequest;
import com.google.storage.v2.WriteObjectResponse;
Expand Down Expand Up @@ -141,7 +147,7 @@ final class GrpcStorageImpl extends BaseService<StorageOptions> implements Stora
private final GrpcRetryAlgorithmManager retryAlgorithmManager;
private final SyntaxDecoders syntaxDecoders;

private final transient ProjectId defaultProjectId;
@Deprecated private final transient ProjectId defaultProjectId;

GrpcStorageImpl(GrpcStorageOptions options, StorageClient storageClient) {
super(options);
Expand Down Expand Up @@ -389,9 +395,12 @@ public Page<Bucket> list(BucketListOption... options) {
Opts<BucketListOpt> opts = Opts.unwrap(options);
GrpcCallContext grpcCallContext =
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());
ListBucketsRequest.Builder builder =
ListBucketsRequest.newBuilder().setParent(projectNameCodec.encode(defaultProjectId.val));
ListBucketsRequest request = opts.listBucketsRequest().apply(builder).build();
ListBucketsRequest request =
defaultProjectId
.listBuckets()
.andThen(opts.listBucketsRequest())
.apply(ListBucketsRequest.newBuilder())
.build();
ListBucketsPagedResponse call = listBucketsCallable.call(request, grpcCallContext);
ListBucketsPage page = call.getPage();
return new TransformingPageDecorator<>(page, syntaxDecoders.bucket);
Expand Down Expand Up @@ -873,7 +882,27 @@ public List<Acl> listAcls(BlobId blob) {

@Override
public HmacKey createHmacKey(ServiceAccount serviceAccount, CreateHmacKeyOption... options) {
return todo();
Opts<HmacKeyTargetOpt> opts = Opts.unwrap(options);
GrpcCallContext grpcCallContext =
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());
CreateHmacKeyRequest request =
defaultProjectId
.createHmacKey()
.andThen(opts.createHmacKeysRequest())
.apply(CreateHmacKeyRequest.newBuilder())
.setServiceAccountEmail(serviceAccount.getEmail())
.build();
return Retrying.run(
getOptions(),
retryAlgorithmManager.getFor(request),
() -> grpcStorageStub.createHmacKeyCallable().call(request, grpcCallContext),
resp -> {
ByteString secretKeyBytes = resp.getSecretKeyBytes();
String b64SecretKey = BaseEncoding.base64().encode(secretKeyBytes.toByteArray());
return HmacKey.newBuilder(b64SecretKey)
.setMetadata(codecs.hmacKeyMetadata().decode(resp.getMetadata()))
.build();
});
}

@Override
Expand All @@ -884,19 +913,34 @@ public Page<HmacKeyMetadata> listHmacKeys(ListHmacKeysOption... options) {
GrpcCallContext grpcCallContext =
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());

ProjectId projectId = opts.projectId().orElse(defaultProjectId);

ListHmacKeysRequest.Builder builder =
projectId.listHmacKeys().apply(ListHmacKeysRequest.newBuilder());
ListHmacKeysRequest request = opts.listHmacKeysRequest().apply(builder).build();
ListHmacKeysRequest request =
defaultProjectId
.listHmacKeys()
.andThen(opts.listHmacKeysRequest())
.apply(ListHmacKeysRequest.newBuilder())
.build();
ListHmacKeysPagedResponse call = listHmacKeysCallable.call(request, grpcCallContext);
ListHmacKeysPage page = call.getPage();
return new TransformingPageDecorator<>(page, codecs.hmacKeyMetadata());
}

@Override
public HmacKeyMetadata getHmacKey(String accessId, GetHmacKeyOption... options) {
return todo();
Opts<HmacKeySourceOpt> opts = Opts.unwrap(options);
GrpcCallContext grpcCallContext =
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());
GetHmacKeyRequest request =
defaultProjectId
.getHmacKey()
.andThen(opts.getHmacKeysRequest())
.apply(GetHmacKeyRequest.newBuilder())
.setAccessId(accessId)
.build();
return Retrying.run(
getOptions(),
retryAlgorithmManager.getFor(request),
() -> grpcStorageStub.getHmacKeyCallable().call(request, grpcCallContext),
codecs.hmacKeyMetadata());
}

@Override
Expand All @@ -907,7 +951,7 @@ public void deleteHmacKey(HmacKeyMetadata hmacKeyMetadata, DeleteHmacKeyOption..
DeleteHmacKeyRequest req =
DeleteHmacKeyRequest.newBuilder()
.setAccessId(hmacKeyMetadata.getAccessId())
.setProject(hmacKeyMetadata.getProjectId())
.setProject(projectNameCodec.encode(hmacKeyMetadata.getProjectId()))
.build();
Retrying.run(
getOptions(),
Expand All @@ -922,7 +966,21 @@ public void deleteHmacKey(HmacKeyMetadata hmacKeyMetadata, DeleteHmacKeyOption..
@Override
public HmacKeyMetadata updateHmacKeyState(
HmacKeyMetadata hmacKeyMetadata, HmacKeyState state, UpdateHmacKeyOption... options) {
return todo();
Opts<HmacKeyTargetOpt> opts = Opts.unwrap(options);
GrpcCallContext grpcCallContext =
opts.grpcMetadataMapper().apply(GrpcCallContext.createDefault());
com.google.storage.v2.HmacKeyMetadata encode =
codecs.hmacKeyMetadata().encode(hmacKeyMetadata).toBuilder().setState(state.name()).build();

UpdateHmacKeyRequest.Builder builder =
opts.updateHmacKeysRequest().apply(UpdateHmacKeyRequest.newBuilder()).setHmacKey(encode);
UpdateHmacKeyRequest request =
builder.setUpdateMask(FieldMask.newBuilder().addPaths("state").build()).build();
return Retrying.run(
getOptions(),
retryAlgorithmManager.getFor(request),
() -> grpcStorageStub.updateHmacKeyCallable().call(request, grpcCallContext),
codecs.hmacKeyMetadata());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.util.Collection;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand Down Expand Up @@ -1104,7 +1103,7 @@ public Mapper<ListObjectsRequest.Builder> listObjects() {
*/
@Deprecated
static final class ProjectId extends RpcOptVal<String>
implements HmacKeySourceOpt, HmacKeyTargetOpt, HmacKeyListOpt {
implements HmacKeySourceOpt, HmacKeyTargetOpt, HmacKeyListOpt, BucketListOpt {
private static final long serialVersionUID = 1471462503030451598L;

private ProjectId(String val) {
Expand All @@ -1122,9 +1121,14 @@ public Mapper<GetHmacKeyRequest.Builder> getHmacKey() {
}

@Override
public Mapper<DeleteHmacKeyRequest.Builder> deleteHmacKey() {
public Mapper<CreateHmacKeyRequest.Builder> createHmacKey() {
return b -> b.setProject(projectNameCodec.encode(val));
}

@Override
public Mapper<ListBucketsRequest.Builder> listBuckets() {
return b -> b.setParent(projectNameCodec.encode(val));
}
}

static final class Projection extends RpcOptVal<String> implements BucketTargetOpt {
Expand Down Expand Up @@ -1958,11 +1962,6 @@ Mapper<BlobInfo.Builder> blobInfoMapper() {
return fuseMappers(ObjectTargetOpt.class, ObjectTargetOpt::blobInfo);
}

@Deprecated
Optional<ProjectId> projectId() {
return filterTo(ProjectId.class).findFirst();
}

private Mapper<ImmutableMap.Builder<StorageRpc.Option, Object>> rpcOptionMapper() {
return fuseMappers(RpcOptVal.class, RpcOptVal::mapper);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import com.google.storage.v2.CommonObjectRequestParams;
import com.google.storage.v2.ComposeObjectRequest;
import com.google.storage.v2.CreateBucketRequest;
import com.google.storage.v2.CreateHmacKeyRequest;
import com.google.storage.v2.DeleteBucketRequest;
import com.google.storage.v2.DeleteHmacKeyRequest;
import com.google.storage.v2.DeleteObjectRequest;
import com.google.storage.v2.GetBucketRequest;
import com.google.storage.v2.GetHmacKeyRequest;
Expand Down Expand Up @@ -828,14 +828,14 @@ public void getHmacKey_nonResource() {
}

@Test
public void deleteHmacKey_nonResource() {
DeleteHmacKeyRequest expected =
DeleteHmacKeyRequest.newBuilder().setProject("projects/proj").build();
public void createHmacKey_nonResource() {
CreateHmacKeyRequest expected =
CreateHmacKeyRequest.newBuilder().setProject("projects/proj").build();

DeleteHmacKeyRequest actual =
CreateHmacKeyRequest actual =
UnifiedOpts.projectId("proj")
.deleteHmacKey()
.apply(DeleteHmacKeyRequest.newBuilder())
.createHmacKey()
.apply(CreateHmacKeyRequest.newBuilder())
.build();
assertThat(actual).isEqualTo(expected);
}
Expand Down Expand Up @@ -867,14 +867,40 @@ public void getHmacKey_resource() {
}

@Test
public void deleteHmacKey_resource() {
DeleteHmacKeyRequest expected =
DeleteHmacKeyRequest.newBuilder().setProject("projects/proj").build();
public void createHmacKey_resource() {
CreateHmacKeyRequest expected =
CreateHmacKeyRequest.newBuilder().setProject("projects/proj").build();

DeleteHmacKeyRequest actual =
CreateHmacKeyRequest actual =
UnifiedOpts.projectId("projects/proj")
.deleteHmacKey()
.apply(DeleteHmacKeyRequest.newBuilder())
.createHmacKey()
.apply(CreateHmacKeyRequest.newBuilder())
.build();
assertThat(actual).isEqualTo(expected);
}

@Test
public void listBuckets_nonResource() {
ListBucketsRequest expected =
ListBucketsRequest.newBuilder().setParent("projects/proj").build();

ListBucketsRequest actual =
UnifiedOpts.projectId("proj")
.listBuckets()
.apply(ListBucketsRequest.newBuilder())
.build();
assertThat(actual).isEqualTo(expected);
}

@Test
public void listBuckets_resource() {
ListBucketsRequest expected =
ListBucketsRequest.newBuilder().setParent("projects/proj").build();

ListBucketsRequest actual =
UnifiedOpts.projectId("projects/proj")
.listBuckets()
.apply(ListBucketsRequest.newBuilder())
.build();
assertThat(actual).isEqualTo(expected);
}
Expand Down
Loading

0 comments on commit 84e4adf

Please sign in to comment.