<commit_before>// Copyright 2020 The Cross-Media Measurement Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import io.grpc.Status
import org.wfanet.measurement.api.v2alpha.Certificate
import org.wfanet.measurement.api.v2alpha.Certificate.RevocationState
import org.wfanet.measurement.api.v2alpha.CertificateParentKey
import org.wfanet.measurement.api.v2alpha.CertificatesGrpcKt.CertificatesCoroutineImplBase
import org.wfanet.measurement.api.v2alpha.CreateCertificateRequest
import org.wfanet.measurement.api.v2alpha.DataProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.DataProviderKey
import org.wfanet.measurement.api.v2alpha.DuchyCertificateKey
import org.wfanet.measurement.api.v2alpha.DuchyKey
import org.wfanet.measurement.api.v2alpha.GetCertificateRequest
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerCertificateKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerKey
import org.wfanet.measurement.api.v2alpha.ModelProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.ModelProviderKey
import org.wfanet.measurement.api.v2alpha.ReleaseCertificateHoldRequest
import org.wfanet.measurement.api.v2alpha.ResourceKey
import org.wfanet.measurement.api.v2alpha.RevokeCertificateRequest
import org.wfanet.measurement.api.v2alpha.certificate
import org.wfanet.measurement.api.v2alpha.makeDataProviderCertificateName
import org.wfanet.measurement.api.v2alpha.makeDuchyCertificateName
import org.wfanet.measurement.api.v2alpha.makeMeasurementConsumerCertificateName
import org.wfanet.measurement.api.v2alpha.makeModelProviderCertificateName
import org.wfanet.measurement.api.v2alpha.principalFromCurrentContext
import org.wfanet.measurement.common.grpc.failGrpc
import org.wfanet.measurement.common.grpc.grpcRequire
import org.wfanet.measurement.common.grpc.grpcRequireNotNull
import org.wfanet.measurement.common.identity.apiIdToExternalId
import org.wfanet.measurement.common.identity.externalIdToApiId
import org.wfanet.measurement.internal.kingdom.Certificate as InternalCertificate
import org.wfanet.measurement.internal.kingdom.Certificate.RevocationState as InternalRevocationState
import org.wfanet.measurement.internal.kingdom.CertificatesGrpcKt.CertificatesCoroutineStub
import org.wfanet.measurement.internal.kingdom.certificate as internalCertificate
import org.wfanet.measurement.internal.kingdom.getCertificateRequest
import org.wfanet.measurement.internal.kingdom.releaseCertificateHoldRequest
import org.wfanet.measurement.internal.kingdom.revokeCertificateRequest

class CertificatesService(private val internalCertificatesStub: CertificatesCoroutineStub) :
  CertificatesCoroutineImplBase() {

  override suspend fun getCertificate(request: GetCertificateRequest): Certificate {
    val key =
      grpcRequireNotNull(createResourceKey(request.name)) { "Resource name unspecified or invalid" }

    val principal = principalFromCurrentContext

    val internalGetCertificateRequest = getCertificateRequest {
      externalCertificateId = apiIdToExternalId(key.certificateId)
      when (key) {
        is DataProviderCertificateKey -> {
          externalDataProviderId = apiIdToExternalId(key.dataProviderId)

          when (val resourceKey = principal.resourceKey) {
            is DataProviderKey -> {
              if (apiIdToExternalId(resourceKey.dataProviderId) != externalDataProviderId) {
                failGrpc(Status.PERMISSION_DENIED) {
                  "Cannot get another DataProvider's Certificate"
                }
              }
            }
            is MeasurementConsumerKey -> {}
            else -> {
              failGrpc(Status.PERMISSION_DENIED) {
                "Caller does not have permission to get a DataProvider's Certificate"
              }
            }
          }
        }
        is DuchyCertificateKey -> externalDuchyId = key.duchyId
        is MeasurementConsumerCertificateKey -> {
          externalMeasurementConsumerId = apiIdToExternalId(key.measurementConsumerId)

          when (val resourceKey = principal.resourceKey) {
            is DataProviderKey -> {}
            is MeasurementConsumerKey -> {
              if (apiIdToExternalId(resourceKey.measurementConsumerId) !=
                  externalMeasurementConsumerId
              ) {
                failGrpc(Status.PERMISSION_DENIED) {
                  "Cannot get another MeasurementConsumer's Certificate"
                }
              }
            }
            else -> {
              failGrpc(Status.PERMISSION_DENIED) {
                "Caller does not have permission to get a MeasurementConsumer's Certificate"
              }
            }<commit_msg>Allow ModelProvider to get DataProviders certificate (#452)<commit_after>// Copyright 2020 The Cross-Media Measurement Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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

import io.grpc.Status
import org.wfanet.measurement.api.v2alpha.Certificate
import org.wfanet.measurement.api.v2alpha.Certificate.RevocationState
import org.wfanet.measurement.api.v2alpha.CertificateParentKey
import org.wfanet.measurement.api.v2alpha.CertificatesGrpcKt.CertificatesCoroutineImplBase
import org.wfanet.measurement.api.v2alpha.CreateCertificateRequest
import org.wfanet.measurement.api.v2alpha.DataProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.DataProviderKey
import org.wfanet.measurement.api.v2alpha.DuchyCertificateKey
import org.wfanet.measurement.api.v2alpha.DuchyKey
import org.wfanet.measurement.api.v2alpha.GetCertificateRequest
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerCertificateKey
import org.wfanet.measurement.api.v2alpha.MeasurementConsumerKey
import org.wfanet.measurement.api.v2alpha.ModelProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.ModelProviderKey
import org.wfanet.measurement.api.v2alpha.ReleaseCertificateHoldRequest
import org.wfanet.measurement.api.v2alpha.ResourceKey
import org.wfanet.measurement.api.v2alpha.RevokeCertificateRequest
import org.wfanet.measurement.api.v2alpha.certificate
import org.wfanet.measurement.api.v2alpha.makeDataProviderCertificateName
import org.wfanet.measurement.api.v2alpha.makeDuchyCertificateName
import org.wfanet.measurement.api.v2alpha.makeMeasurementConsumerCertificateName
import org.wfanet.measurement.api.v2alpha.makeModelProviderCertificateName
import org.wfanet.measurement.api.v2alpha.principalFromCurrentContext
import org.wfanet.measurement.common.grpc.failGrpc
import org.wfanet.measurement.common.grpc.grpcRequire
import org.wfanet.measurement.common.grpc.grpcRequireNotNull
import org.wfanet.measurement.common.identity.apiIdToExternalId
import org.wfanet.measurement.common.identity.externalIdToApiId
import org.wfanet.measurement.internal.kingdom.Certificate as InternalCertificate
import org.wfanet.measurement.internal.kingdom.Certificate.RevocationState as InternalRevocationState
import org.wfanet.measurement.internal.kingdom.CertificatesGrpcKt.CertificatesCoroutineStub
import org.wfanet.measurement.internal.kingdom.certificate as internalCertificate
import org.wfanet.measurement.internal.kingdom.getCertificateRequest
import org.wfanet.measurement.internal.kingdom.releaseCertificateHoldRequest
import org.wfanet.measurement.internal.kingdom.revokeCertificateRequest

class CertificatesService(private val internalCertificatesStub: CertificatesCoroutineStub) :
  CertificatesCoroutineImplBase() {

  override suspend fun getCertificate(request: GetCertificateRequest): Certificate {
    val key =
      grpcRequireNotNull(createResourceKey(request.name)) { "Resource name unspecified or invalid" }

    val principal = principalFromCurrentContext

    val internalGetCertificateRequest = getCertificateRequest {
      externalCertificateId = apiIdToExternalId(key.certificateId)
      when (key) {
        is DataProviderCertificateKey -> {
          externalDataProviderId = apiIdToExternalId(key.dataProviderId)

          when (val resourceKey = principal.resourceKey) {
            is DataProviderKey -> {
              if (apiIdToExternalId(resourceKey.dataProviderId) != externalDataProviderId) {
                failGrpc(Status.PERMISSION_DENIED) {
                  "Cannot get another DataProvider's Certificate"
                }
              }
            }
            is MeasurementConsumerKey -> {}
            is ModelProviderKey -> {}
            else -> {
              failGrpc(Status.PERMISSION_DENIED) {
                "Caller does not have permission to get a DataProvider's Certificate"
              }
            }
          }
        }
        is DuchyCertificateKey -> externalDuchyId = key.duchyId
        is MeasurementConsumerCertificateKey -> {
          externalMeasurementConsumerId = apiIdToExternalId(key.measurementConsumerId)

          when (val resourceKey = principal.resourceKey) {
            is DataProviderKey -> {}
            is MeasurementConsumerKey -> {
              if (apiIdToExternalId(resourceKey.measurementConsumerId) !=
                  externalMeasurementConsumerId
              ) {
                failGrpc(Status.PERMISSION_DENIED) {
                  "Cannot get another MeasurementConsumer's Certificate"
                }
              }
            }
            else -> {
              failGrpc(Status.PERMISSION_DENIED) {
                "Caller does not have permission to get a MeasurementConsumer's Certificate"
              }
            }
{'fraction_non_alphanumeric': 0.07211902956785443, 'fraction_numerical': 0.00634950720242608, 'mean_word_length': 3.723813786929275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492151', 'n_tokens_mistral': 2919, 'n_tokens_neox': 2823, 'n_words': 542}