Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate creation of event group metadata descriptor #1057

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