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

feat: implement access Policy public API #1998

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
135 changes: 127 additions & 8 deletions src/main/kotlin/org/wfanet/measurement/access/service/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,25 @@ class EtagMismatchException(requestEtag: String, etag: String, cause: Throwable?

class PrincipalNotFoundException(name: String, cause: Throwable? = null) :
ServiceException(
Errors.Reason.PRINCIPAL_NOT_FOUND,
reason,
"Principal $name not found",
mapOf(Errors.Metadata.PRINCIPAL to name),
cause,
)
) {
companion object : Factory<PrincipalNotFoundException>() {
override val reason: Errors.Reason
get() = Errors.Reason.PRINCIPAL_NOT_FOUND

override fun fromInternal(
internalMetadata: Map<InternalErrors.Metadata, String>,
cause: Throwable,
): PrincipalNotFoundException {
val principalKey =
PrincipalKey(internalMetadata.getValue(InternalErrors.Metadata.PRINCIPAL_RESOURCE_ID))
return PrincipalNotFoundException(principalKey.toName(), cause)
}
}
}

class PrincipalAlreadyExistsException(cause: Throwable? = null) :
ServiceException(
Expand Down Expand Up @@ -241,12 +255,20 @@ class PermissionNotFoundForRoleException(role: String, cause: Throwable? = null)
)

class RoleNotFoundException(name: String, cause: Throwable? = null) :
ServiceException(
Errors.Reason.ROLE_NOT_FOUND,
"Role $name not found",
mapOf(Errors.Metadata.ROLE to name),
cause,
)
ServiceException(reason, "Role $name not found", mapOf(Errors.Metadata.ROLE to name), cause) {
companion object : Factory<RoleNotFoundException>() {
override val reason: Errors.Reason
get() = Errors.Reason.ROLE_NOT_FOUND

override fun fromInternal(
internalMetadata: Map<InternalErrors.Metadata, String>,
cause: Throwable,
): RoleNotFoundException {
val roleKey = RoleKey(internalMetadata.getValue(InternalErrors.Metadata.ROLE_RESOURCE_ID))
return RoleNotFoundException(roleKey.toName(), cause)
}
}
}

class RoleAlreadyExistsException(name: String, cause: Throwable? = null) :
ServiceException(
Expand Down Expand Up @@ -289,3 +311,100 @@ class ResourceTypeNotFoundInPermissionException(
}
}
}

class PolicyNotFoundException(name: String, cause: Throwable? = null) :
ServiceException(
Errors.Reason.POLICY_NOT_FOUND,
"Policy $name not found",
mapOf(Errors.Metadata.POLICY to name),
cause,
)

class PolicyNotFoundForProtectedResourceException(name: String, cause: Throwable? = null) :
ServiceException(
Errors.Reason.POLICY_NOT_FOUND_FOR_PROTECTED_RESOURCE,
"Policy with protected resource $name not found",
mapOf(Errors.Metadata.PROTECTED_RESOURCE to name),
cause,
)

class PolicyAlreadyExistsException(cause: Throwable? = null) :
ServiceException(Errors.Reason.POLICY_ALREADY_EXISTS, "Policy already exists", emptyMap(), cause)

class PolicyBindingMembershipAlreadyExistsException(
policyName: String,
roleName: String,
principalName: String,
cause: Throwable? = null,
) :
ServiceException(
reason,
"Principal $principalName is already a member of role $roleName on policy $policyName",
mapOf(
Errors.Metadata.POLICY to policyName,
Errors.Metadata.ROLE to roleName,
Errors.Metadata.PRINCIPAL to principalName,
),
cause,
) {
companion object : Factory<PolicyBindingMembershipAlreadyExistsException>() {
override val reason: Errors.Reason
get() = Errors.Reason.POLICY_BINDING_MEMBERSHIP_ALREADY_EXISTS

override fun fromInternal(
internalMetadata: Map<InternalErrors.Metadata, String>,
cause: Throwable,
): PolicyBindingMembershipAlreadyExistsException {
val policyKey =
PolicyKey(internalMetadata.getValue(InternalErrors.Metadata.POLICY_RESOURCE_ID))
val roleKey = RoleKey(internalMetadata.getValue(InternalErrors.Metadata.ROLE_RESOURCE_ID))
val principalKey =
PrincipalKey(internalMetadata.getValue(InternalErrors.Metadata.PRINCIPAL_RESOURCE_ID))
return PolicyBindingMembershipAlreadyExistsException(
policyKey.toName(),
roleKey.toName(),
principalKey.toName(),
cause,
)
}
}
}

class PolicyBindingMembershipNotFoundException(
policyName: String,
roleName: String,
principalName: String,
cause: Throwable? = null,
) :
ServiceException(
reason,
"Principal $principalName is not a member of role $roleName on policy $policyName",
mapOf(
Errors.Metadata.POLICY to policyName,
Errors.Metadata.ROLE to roleName,
Errors.Metadata.PRINCIPAL to principalName,
),
cause,
) {
companion object : Factory<PolicyBindingMembershipNotFoundException>() {
override val reason: Errors.Reason
get() = Errors.Reason.POLICY_BINDING_MEMBERSHIP_NOT_FOUND

override fun fromInternal(
internalMetadata: Map<InternalErrors.Metadata, String>,
cause: Throwable,
): PolicyBindingMembershipNotFoundException {
val policyKey =
PolicyKey(internalMetadata.getValue(InternalErrors.Metadata.POLICY_RESOURCE_ID))
val roleKey = RoleKey(internalMetadata.getValue(InternalErrors.Metadata.ROLE_RESOURCE_ID))
val principalKey =
PrincipalKey(internalMetadata.getValue(InternalErrors.Metadata.PRINCIPAL_RESOURCE_ID))
return PolicyBindingMembershipNotFoundException(
policyKey.toName(),
roleKey.toName(),
principalKey.toName(),
cause,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import org.wfanet.measurement.common.ResourceNameParser
internal enum class IdVariable {
PRINCIPAL,
PERMISSION,
ROLE
ROLE,
POLICY,
}

internal fun ResourceNameParser.assembleName(idMap: Map<IdVariable, String>): String {
Expand Down
33 changes: 33 additions & 0 deletions src/main/kotlin/org/wfanet/measurement/access/service/PolicyKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2025 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.access.service

import org.wfanet.measurement.common.ResourceNameParser
import org.wfanet.measurement.common.api.ResourceKey

class PolicyKey(val policyId: String) : ResourceKey {
override fun toName(): String {
return parser.assembleName(mapOf(IdVariable.POLICY to policyId))
}

companion object : ResourceKey.Factory<PolicyKey> {
private val parser = ResourceNameParser("policies/{policy}")

override fun fromName(resourceName: String): PolicyKey? {
val idVars: Map<IdVariable, String> = parser.parseIdVars(resourceName) ?: return null
return PolicyKey(idVars.getValue(IdVariable.POLICY))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,29 @@ kt_jvm_library(
],
)

kt_jvm_library(
name = "policies_service",
srcs = ["PoliciesService.kt"],
deps = [
":resource_conversion",
"//src/main/kotlin/org/wfanet/measurement/access/service:errors",
"//src/main/kotlin/org/wfanet/measurement/access/service:resource_key",
"//src/main/kotlin/org/wfanet/measurement/access/service/internal:errors",
"//src/main/kotlin/org/wfanet/measurement/common/api:resource_ids",
"//src/main/proto/wfa/measurement/access/v1alpha:policies_service_kt_jvm_grpc_proto",
"//src/main/proto/wfa/measurement/internal/access:policies_service_kt_jvm_grpc_proto",
],
)

kt_jvm_library(
name = "resource_conversion",
srcs = ["ResourceConversion.kt"],
deps = [
"//src/main/kotlin/org/wfanet/measurement/access/service:resource_key",
"//src/main/proto/wfa/measurement/access/v1alpha:policy_kt_jvm_proto",
"//src/main/proto/wfa/measurement/access/v1alpha:principal_kt_jvm_proto",
"//src/main/proto/wfa/measurement/access/v1alpha:role_kt_jvm_proto",
"//src/main/proto/wfa/measurement/internal/access:policy_kt_jvm_proto",
"//src/main/proto/wfa/measurement/internal/access:principal_kt_jvm_proto",
"//src/main/proto/wfa/measurement/internal/access:role_kt_jvm_proto",
],
Expand All @@ -51,6 +67,7 @@ kt_jvm_library(
name = "services",
srcs = ["Services.kt"],
deps = [
":policies_service",
":principals_service",
":roles_service",
"//src/main/proto/wfa/measurement/access/v1alpha:permissions_service_kt_jvm_grpc_proto",
Expand Down
Loading