Skip to content

Commit

Permalink
Validate creation of event group metadata descriptor (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanvuong2021 authored and ple13 committed Aug 16, 2024
1 parent 9f39c74 commit ede9e77
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.wfanet.measurement.kingdom.service.api.v2alpha

import com.google.protobuf.Descriptors.DescriptorValidationException
import io.grpc.Status
import io.grpc.StatusException
import kotlin.math.min
Expand Down Expand Up @@ -41,6 +42,7 @@ import org.wfanet.measurement.api.v2alpha.eventGroupMetadataDescriptor
import org.wfanet.measurement.api.v2alpha.listEventGroupMetadataDescriptorsPageToken
import org.wfanet.measurement.api.v2alpha.listEventGroupMetadataDescriptorsResponse
import org.wfanet.measurement.api.v2alpha.principalFromCurrentContext
import org.wfanet.measurement.common.ProtoReflection
import org.wfanet.measurement.common.api.ResourceKey
import org.wfanet.measurement.common.base64UrlDecode
import org.wfanet.measurement.common.base64UrlEncode
Expand Down Expand Up @@ -135,6 +137,15 @@ class EventGroupMetadataDescriptorsService(
}
}

// Check if the FileDescriptorSet is valid by trying to build a list of Descriptors from it.
try {
ProtoReflection.buildDescriptors(listOf(request.eventGroupMetadataDescriptor.descriptorSet))
} catch (e: DescriptorValidationException) {
throw Status.INVALID_ARGUMENT.withCause(e)
.withDescription("descriptor_set is invalid")
.asRuntimeException()
}

val createRequest =
request.eventGroupMetadataDescriptor.toInternal(
parentKey.dataProviderId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.wfanet.measurement.kingdom.service.api.v2alpha
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.extensions.proto.ProtoTruth.assertThat
import com.google.protobuf.DescriptorProtos.FileDescriptorSet
import com.google.protobuf.TextFormat
import io.grpc.Status
import io.grpc.StatusRuntimeException
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -234,6 +235,47 @@ class EventGroupMetadataDescriptorsServiceTest {
assertThat(exception.status.code).isEqualTo(Status.Code.PERMISSION_DENIED)
}

@Test
fun `createEventGroupMetadataDescriptor throws INVALID_ARGUMENT when descriptorSet invalid`() {
val badFileDescriptorSetText =
"""
file {
name: ""
package: ""
message_type {
name: ""
field {
name: "test"
number: 1
label: LABEL_OPTIONAL
type: TYPE_STRING
}
}
}
"""
.trimIndent()

val badFileDescriptorSetBuilder = FileDescriptorSet.newBuilder()
TextFormat.getParser().merge(badFileDescriptorSetText, badFileDescriptorSetBuilder)

val request = createEventGroupMetadataDescriptorRequest {
parent = DATA_PROVIDER_NAME
eventGroupMetadataDescriptor =
EVENT_GROUP_METADATA_DESCRIPTOR.copy {
clearDescriptorSet()
descriptorSet = badFileDescriptorSetBuilder.build()
}
}

val exception =
assertFailsWith<StatusRuntimeException> {
withDataProviderPrincipal(DATA_PROVIDER_NAME) {
runBlocking { service.createEventGroupMetadataDescriptor(request) }
}
}
assertThat(exception.status.code).isEqualTo(Status.Code.INVALID_ARGUMENT)
}

@Test
fun `createEventGroupMetadataDescriptor throws INVALID_ARGUMENT when parent is missing`() {
val exception =
Expand Down

0 comments on commit ede9e77

Please sign in to comment.