Skip to content

Commit

Permalink
chore: remove Notification, HMAC, SA grpc support (#693)
Browse files Browse the repository at this point in the history
* chore: remove Notification, HMAC, SA grpc support

* fix lint
  • Loading branch information
cojenco authored Oct 24, 2024
1 parent e07483d commit 7ba2902
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 649 deletions.
6 changes: 6 additions & 0 deletions testbench/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ def __validate_grpc_method_implemented_retry(self, method):
"storage.default_object_acl.insert",
"storage.default_object_acl.patch",
"storage.default_object_acl.update",
"storage.hmacKey.create",
"storage.hmacKey.delete",
"storage.hmacKey.get",
"storage.hmacKey.list",
"storage.hmacKey.update",
"storage.object_acl.get",
"storage.object_acl.list",
"storage.object_acl.delete",
Expand All @@ -616,6 +621,7 @@ def __validate_grpc_method_implemented_retry(self, method):
"storage.notifications.get",
"storage.notifications.insert",
"storage.notifications.list",
"storage.serviceaccount.get",
}
if method in not_supported_grpc_w_retry:
testbench.error.unimplemented(
Expand Down
183 changes: 0 additions & 183 deletions testbench/grpc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,72 +352,6 @@ def UpdateBucket(self, request, context):
bucket.metadata.update_time.FromDatetime(now)
return bucket.metadata

def _notification_from_rest(self, rest, bucket_name):
# We need to make a copy before changing any values
rest = rest.copy()
rest.pop("kind")
rest["name"] = bucket_name + "/notificationConfigs/" + rest.pop("id")
rest["topic"] = "//pubsub.googleapis.com/" + rest["topic"]
return json_format.ParseDict(rest, storage_pb2.NotificationConfig())

def _decompose_notification_name(self, notification_name, context):
loc = notification_name.find("/notificationConfigs/")
if loc == -1:
testbench.error.invalid(
"GetNotificationConfig() malformed notification name [%s]"
% notification_name,
context,
)
return (None, None)
bucket_name = notification_name[:loc]
notification_id = notification_name[loc + len("/notificationConfigs/") :]
return (bucket_name, notification_id)

def DeleteNotificationConfig(self, request, context):
bucket_name, notification_id = self._decompose_notification_name(
request.name, context
)
if bucket_name is None:
return None
bucket = self.db.get_bucket(bucket_name, context)
bucket.delete_notification(notification_id, context)
return empty_pb2.Empty()

def GetNotificationConfig(self, request, context):
bucket_name, notification_id = self._decompose_notification_name(
request.name, context
)
if bucket_name is None:
return None
bucket = self.db.get_bucket(bucket_name, context)
rest = bucket.get_notification(notification_id, context)
return self._notification_from_rest(rest, bucket_name)

def CreateNotificationConfig(self, request, context):
pattern = "^//pubsub.googleapis.com/projects/[^/]+/topics/[^/]+$"
if re.match(pattern, request.notification_config.topic) is None:
return testbench.error.invalid(
"topic names must be in"
+ " //pubsub.googleapis.com/projects/{project-identifier}/topics/{my-topic}"
+ " format, got=%s" % request.notification_config.topic,
context,
)
bucket = self.db.get_bucket(request.parent, context)
notification = json_format.MessageToDict(request.notification_config)
# Convert topic names to REST format
notification["topic"] = notification["topic"][len("//pubsub.googleapis.com/") :]
rest = bucket.insert_notification(json.dumps(notification), context)
return self._notification_from_rest(rest, request.parent)

def ListNotificationConfigs(self, request, context):
bucket = self.db.get_bucket(request.parent, context)
items = bucket.list_notifications(context).get("items", [])
return storage_pb2.ListNotificationConfigsResponse(
notification_configs=[
self._notification_from_rest(r, request.parent) for r in items
]
)

@retry_test(method="storage.objects.compose")
def ComposeObject(self, request, context):
if len(request.source_objects) == 0:
Expand Down Expand Up @@ -815,123 +749,6 @@ def QueryWriteStatus(self, request, context):
return storage_pb2.QueryWriteStatusResponse(resource=upload.blob.metadata)
return storage_pb2.QueryWriteStatusResponse(persisted_size=len(upload.media))

@retry_test("storage.serviceaccount.get")
def GetServiceAccount(self, request, context):
if not request.project.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % request.project,
context,
)
project_id = request.project[len("projects/") :]
project = self.db.get_project(project_id)
return storage_pb2.ServiceAccount(email_address=project.service_account_email())

def _hmac_key_metadata_from_rest(self, rest):
rest = rest.copy()
rest.pop("kind", None)
rest["project"] = "projects/" + rest.pop("projectId")
rest["create_time"] = rest.pop("timeCreated")
rest["update_time"] = rest.pop("updated")
return json_format.ParseDict(rest, storage_pb2.HmacKeyMetadata())

@retry_test(method="storage.hmacKey.create")
def CreateHmacKey(self, request, context):
if not request.project.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % request.project,
context,
)
if request.service_account_email == "":
return testbench.error.invalid(
"service account email must be non-empty", context
)
project_id = request.project[len("projects/") :]
project = self.db.get_project(project_id)
rest = project.insert_hmac_key(request.service_account_email)
return storage_pb2.CreateHmacKeyResponse(
secret_key_bytes=base64.b64decode(rest.get("secret").encode("utf-8")),
metadata=self._hmac_key_metadata_from_rest(rest.get("metadata")),
)

@retry_test(method="storage.hmacKey.delete")
def DeleteHmacKey(self, request, context):
if not request.project.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % request.project,
context,
)
project_id = request.project[len("projects/") :]
project = self.db.get_project(project_id)
project.delete_hmac_key(request.access_id, context)
return empty_pb2.Empty()

@retry_test("storage.hmacKey.get")
def GetHmacKey(self, request, context):
if not request.project.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % request.project,
context,
)
project_id = request.project[len("projects/") :]
project = self.db.get_project(project_id)
rest = project.get_hmac_key(request.access_id, context)
return self._hmac_key_metadata_from_rest(rest)

@retry_test("storage.hmacKey.list")
def ListHmacKeys(self, request, context):
if not request.project.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % request.project,
context,
)
project_id = request.project[len("projects/") :]
project = self.db.get_project(project_id)

items = []
sa_email = request.service_account_email
if len(sa_email) != 0:
service_account = project.service_account(sa_email)
if service_account:
items = service_account.key_items()
else:
for sa in project.service_accounts.values():
items.extend(sa.key_items())

state_filter = lambda x: x.get("state") != "DELETED"
if request.show_deleted_keys:
state_filter = lambda x: True

return storage_pb2.ListHmacKeysResponse(
hmac_keys=[
self._hmac_key_metadata_from_rest(i) for i in items if state_filter(i)
]
)

@retry_test(method="storage.hmacKey.update")
def UpdateHmacKey(self, request, context):
if request.update_mask.paths == []:
return testbench.error.invalid(
"UpdateHmacKey() with an empty update mask", context
)
if request.update_mask.paths != ["state"]:
return testbench.error.invalid(
"UpdateHmacKey() only the `state` field can be modified [%s]"
% ",".join(request.update_mask.paths),
context,
)
project_id = request.hmac_key.project
if not project_id.startswith("projects/"):
return testbench.error.invalid(
"project name must start with projects/, got=%s" % project_id, context
)
project_id = project_id[len("projects/") :]
project = self.db.get_project(project_id)
payload = {"state": request.hmac_key.state}
if request.hmac_key.etag != "":
payload["etag"] = request.hmac_key.etag
rest = project.update_hmac_key(request.hmac_key.access_id, payload, context)
return self._hmac_key_metadata_from_rest(rest)


def run(port, database, echo_metadata=False):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
Expand Down
Loading

0 comments on commit 7ba2902

Please sign in to comment.