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

Add detailed logging for public api keys service #1541

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ class ApiKeysService(private val internalApiKeysStub: ApiKeysCoroutineStub) :
val result =
try {
internalApiKeysStub.createApiKey(internalCreateApiKeyRequest)
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.NOT_FOUND ->
failGrpc(Status.NOT_FOUND, ex) { "MeasurementConsumer not found." }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}

return result.toApiKey()
Expand All @@ -94,11 +93,11 @@ class ApiKeysService(private val internalApiKeysStub: ApiKeysCoroutineStub) :
val result =
try {
internalApiKeysStub.deleteApiKey(deleteApiKeyRequest)
} catch (ex: StatusException) {
when (ex.status.code) {
Status.Code.NOT_FOUND -> failGrpc(Status.NOT_FOUND, ex) { ex.message ?: "Not found." }
else -> failGrpc(Status.UNKNOWN, ex) { "Unknown exception." }
}
} catch (e: StatusException) {
throw when (e.status.code) {
Status.Code.NOT_FOUND -> Status.NOT_FOUND
else -> Status.UNKNOWN
}.toExternalStatusRuntimeException(e)
}

return result.toApiKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ kt_jvm_library(
deps = [
":account_authentication_server_interceptor",
"//src/main/kotlin/org/wfanet/measurement/api/v2alpha:resource_key",
"//src/main/kotlin/org/wfanet/measurement/kingdom/service/api/v2alpha:internal_status_conversion",
"//src/main/proto/wfa/measurement/api/v2alpha:api_key_kt_jvm_proto",
"//src/main/proto/wfa/measurement/api/v2alpha:api_keys_service_kt_jvm_grpc_proto",
"//src/main/proto/wfa/measurement/internal/kingdom:api_key_kt_jvm_proto",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ fun Status.toExternalStatusRuntimeException(
errorMessage = "Permission Denied."
}
ErrorCode.API_KEY_NOT_FOUND -> {
val apiKeyApiId =
externalIdToApiId(checkNotNull(errorInfo.metadataMap["external_api_key_id"]).toLong())

put("external_api_key_id", apiKeyApiId)
errorMessage = "ApiKey not found."
}
ErrorCode.EVENT_GROUP_NOT_FOUND -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.mockito.kotlin.any
import org.mockito.kotlin.stub
import org.wfanet.measurement.api.v2alpha.AccountKey
import org.wfanet.measurement.api.v2alpha.ApiKey
import org.wfanet.measurement.api.v2alpha.ApiKeyKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerKey
import org.wfanet.measurement.api.v2alpha.apiKey
import org.wfanet.measurement.api.v2alpha.createApiKeyRequest
import org.wfanet.measurement.api.v2alpha.deleteApiKeyRequest
import org.wfanet.measurement.common.grpc.errorInfo
import org.wfanet.measurement.common.grpc.testing.GrpcTestServerRule
import org.wfanet.measurement.common.grpc.testing.mockService
import org.wfanet.measurement.common.identity.ExternalId
import org.wfanet.measurement.common.identity.apiIdToExternalId
import org.wfanet.measurement.common.identity.externalIdToApiId
import org.wfanet.measurement.common.testing.verifyProtoArgument
Expand All @@ -45,13 +49,20 @@ import org.wfanet.measurement.internal.kingdom.account as internalAccount
import org.wfanet.measurement.internal.kingdom.apiKey as internalApiKey
import org.wfanet.measurement.internal.kingdom.copy
import org.wfanet.measurement.internal.kingdom.deleteApiKeyRequest as internalDeleteApiKeyRequest
import org.wfanet.measurement.kingdom.deploy.gcloud.spanner.common.ApiKeyNotFoundException
import org.wfanet.measurement.kingdom.deploy.gcloud.spanner.common.MeasurementConsumerNotFoundException

private const val ACCOUNT_NAME = "accounts/AAAAAAC8YU4"
private const val MEASUREMENT_CONSUMER_NAME = "measurementConsumers/AAAAAAAAAHs"
private const val MEASUREMENT_CONSUMER_NAME_2 = "measurementConsumers/AAAAAAAAAJs"
private const val API_KEY_NAME = "$MEASUREMENT_CONSUMER_NAME/apiKeys/AAAAAAAAAMs"
private const val API_KEY_NAME_2 = "$MEASUREMENT_CONSUMER_NAME_2/apiKeys/AAAAAAAAANs"
private const val EXTERNAL_MEASUREMENT_CONSUMER_ID = 123L
private const val EXTERNAL_MEASUREMENT_CONSUMER_ID_2 = 155L
private const val AUTHENTICATION_KEY = 12345672L
private val MEASUREMENT_CONSUMER_NAME =
MeasurementConsumerKey(externalIdToApiId(EXTERNAL_MEASUREMENT_CONSUMER_ID)).toName()
private val MEASUREMENT_CONSUMER_NAME_2 =
MeasurementConsumerKey(externalIdToApiId(EXTERNAL_MEASUREMENT_CONSUMER_ID_2)).toName()
private val API_KEY_NAME = "$MEASUREMENT_CONSUMER_NAME/apiKeys/AAAAAAAAAMs"
private val API_KEY_NAME_2 = "$MEASUREMENT_CONSUMER_NAME_2/apiKeys/AAAAAAAAANs"
private val EXTERNAL_API_KEY_ID = apiIdToExternalId(ApiKeyKey.fromName(API_KEY_NAME)!!.apiKeyId)

@RunWith(JUnit4::class)
class ApiKeysServiceTest {
Expand Down Expand Up @@ -175,6 +186,49 @@ class ApiKeysServiceTest {
}
assertThat(exception.status.code).isEqualTo(Status.Code.PERMISSION_DENIED)
}

@Test
fun `createApiKey throws NOT_FOUND with measurment consumer name when measurement consumer is not found`() {
internalApiKeysMock.stub {
onBlocking { createApiKey(any()) }
.thenThrow(
MeasurementConsumerNotFoundException(ExternalId(EXTERNAL_MEASUREMENT_CONSUMER_ID))
.asStatusRuntimeException(Status.Code.NOT_FOUND, "MeasurementConsumer not found.")
)
}
val request = createApiKeyRequest {
parent = MEASUREMENT_CONSUMER_NAME
apiKey = PUBLIC_API_KEY
}
val exception =
assertFailsWith<StatusRuntimeException> {
withAccount(INTERNAL_ACCOUNT) { runBlocking { service.createApiKey(request) } }
}

assertThat(exception.status.code).isEqualTo(Status.Code.NOT_FOUND)
assertThat(exception.errorInfo?.metadataMap)
.containsEntry("measurement_consumer", MEASUREMENT_CONSUMER_NAME)
}

@Test
fun `deleteApiKey throws NOT_FOUND with api key id when api key is not found`() {
internalApiKeysMock.stub {
onBlocking { deleteApiKey(any()) }
.thenThrow(
ApiKeyNotFoundException(ExternalId(EXTERNAL_API_KEY_ID))
.asStatusRuntimeException(Status.Code.NOT_FOUND, "Api Key not found.")
)
}
val request = deleteApiKeyRequest { name = API_KEY_NAME }
val exception =
assertFailsWith<StatusRuntimeException> {
withAccount(INTERNAL_ACCOUNT) { runBlocking { service.deleteApiKey(request) } }
}

assertThat(exception.status.code).isEqualTo(Status.Code.NOT_FOUND)
assertThat(exception.errorInfo?.metadataMap)
.containsEntry("external_api_key_id", ExternalId(EXTERNAL_API_KEY_ID).apiId.value)
}
}

private val PUBLIC_API_KEY: ApiKey = apiKey {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ kt_jvm_test(
],
test_class = "org.wfanet.measurement.kingdom.service.api.v2alpha.ApiKeysServiceTest",
deps = [
"//src/main/kotlin/org/wfanet/measurement/kingdom/deploy/gcloud/spanner/common",
"@wfa_common_jvm//imports/java/com/google/common/truth",
"@wfa_common_jvm//imports/java/com/google/common/truth/extensions/proto",
"@wfa_common_jvm//imports/java/org/junit",
Expand Down
Loading