-
Notifications
You must be signed in to change notification settings - Fork 0
feat: collect permission manifests #4
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
velocity/src/main/kotlin/gg/grounds/permissions/velocity/PermissionCatalogClient.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package gg.grounds.permissions.velocity | ||
|
|
||
| import gg.grounds.grpc.permissions.PermissionCatalogServiceGrpc | ||
| import gg.grounds.grpc.permissions.PermissionManifestEntry as GrpcPermissionManifestEntry | ||
| import gg.grounds.grpc.permissions.PermissionScopeKind | ||
| import gg.grounds.grpc.permissions.RegisterPermissionManifestRequest | ||
| import io.grpc.ManagedChannel | ||
| import io.grpc.ManagedChannelBuilder | ||
| import io.grpc.Status | ||
| import io.grpc.StatusRuntimeException | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| sealed interface PermissionManifestRegistrationResult { | ||
| data object Accepted : PermissionManifestRegistrationResult | ||
|
|
||
| data class Unavailable(val reason: String) : PermissionManifestRegistrationResult | ||
| } | ||
|
|
||
| interface PermissionCatalogClient : AutoCloseable { | ||
| fun register( | ||
| manifest: PermissionManifest, | ||
| source: String, | ||
| sourceVersion: String, | ||
| context: PermissionSnapshotContext, | ||
| ): PermissionManifestRegistrationResult | ||
|
|
||
| override fun close() {} | ||
| } | ||
|
|
||
| class GrpcPermissionCatalogClient private constructor(private val channel: ManagedChannel) : | ||
| PermissionCatalogClient { | ||
| private val stub = PermissionCatalogServiceGrpc.newBlockingStub(channel) | ||
|
|
||
| override fun register( | ||
| manifest: PermissionManifest, | ||
| source: String, | ||
| sourceVersion: String, | ||
| context: PermissionSnapshotContext, | ||
| ): PermissionManifestRegistrationResult = | ||
| try { | ||
| val reply = | ||
| stub | ||
| .withDeadlineAfter(DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS) | ||
| .registerPermissionManifest( | ||
| RegisterPermissionManifestRequest.newBuilder() | ||
| .setSource(source) | ||
| .setSourceVersion(sourceVersion) | ||
| .setServerType(context.serverType.orEmpty()) | ||
| .setServerId(context.serverId.orEmpty()) | ||
| .addAllPermissions( | ||
| manifest.permissions.map(PermissionManifestEntry::toGrpc) | ||
| ) | ||
| .build() | ||
| ) | ||
| if (reply.accepted) { | ||
| PermissionManifestRegistrationResult.Accepted | ||
| } else { | ||
| PermissionManifestRegistrationResult.Unavailable( | ||
| reply.message.ifBlank { "rejected" } | ||
| ) | ||
| } | ||
| } catch (e: StatusRuntimeException) { | ||
| PermissionManifestRegistrationResult.Unavailable(e.status.toSafeReason()) | ||
| } catch (e: RuntimeException) { | ||
| PermissionManifestRegistrationResult.Unavailable(e.message ?: e::class.java.name) | ||
| } | ||
|
|
||
| override fun close() { | ||
| channel.shutdown() | ||
| try { | ||
| if (!channel.awaitTermination(3, TimeUnit.SECONDS)) { | ||
| channel.shutdownNow() | ||
| channel.awaitTermination(3, TimeUnit.SECONDS) | ||
| } | ||
| } catch (e: InterruptedException) { | ||
| Thread.currentThread().interrupt() | ||
| channel.shutdownNow() | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| fun create(target: String): GrpcPermissionCatalogClient = | ||
| GrpcPermissionCatalogClient( | ||
| ManagedChannelBuilder.forTarget(target).usePlaintext().build() | ||
| ) | ||
|
|
||
| private const val DEFAULT_TIMEOUT_MS = 2000L | ||
| } | ||
| } | ||
|
|
||
| private fun PermissionManifestEntry.toGrpc(): GrpcPermissionManifestEntry = | ||
| GrpcPermissionManifestEntry.newBuilder() | ||
| .setKey(key) | ||
| .setLabel(label) | ||
| .setDescription(description) | ||
| .addAllSupportedScopes(supportedScopes.map(PermissionManifestScope::toGrpc)) | ||
| .build() | ||
|
|
||
| private fun PermissionManifestScope.toGrpc(): PermissionScopeKind = | ||
| when (this) { | ||
| PermissionManifestScope.GLOBAL -> PermissionScopeKind.PERMISSION_SCOPE_KIND_GLOBAL | ||
| PermissionManifestScope.SERVER_TYPE -> PermissionScopeKind.PERMISSION_SCOPE_KIND_SERVER_TYPE | ||
| PermissionManifestScope.SERVER -> PermissionScopeKind.PERMISSION_SCOPE_KIND_SERVER | ||
| } | ||
|
|
||
| private fun Status.toSafeReason(): String = code.toString() |
45 changes: 45 additions & 0 deletions
45
velocity/src/main/kotlin/gg/grounds/permissions/velocity/PermissionCommandPermissions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package gg.grounds.permissions.velocity | ||
|
|
||
| class PermissionCommandPermissions private constructor(private val nodes: Map<String, String>) { | ||
| fun forArguments(arguments: Array<String>): String = | ||
| when { | ||
| arguments.firstOrNull().equals("status", ignoreCase = true) -> node("status") | ||
| arguments.firstOrNull().equals("refresh", ignoreCase = true) -> node("refresh") | ||
| arguments.firstOrNull().equals("user", ignoreCase = true) && | ||
| arguments.getOrNull(2).equals("info", ignoreCase = true) -> node("user.info") | ||
| arguments.firstOrNull().equals("user", ignoreCase = true) && | ||
| arguments.getOrNull(2).equals("refresh", ignoreCase = true) -> node("user.refresh") | ||
| arguments.firstOrNull().equals("user", ignoreCase = true) && | ||
| arguments.getOrNull(2).equals("permission", ignoreCase = true) && | ||
| arguments.getOrNull(3).equals("check", ignoreCase = true) -> node("user.check") | ||
| else -> node("root") | ||
| } | ||
|
|
||
| private fun node(key: String): String = | ||
| requireNotNull(nodes[key]) { "Missing command permission: $key" } | ||
|
|
||
| companion object { | ||
| private val REQUIRED_KEYS = | ||
| setOf("root", "status", "user.info", "user.check", "user.refresh", "refresh") | ||
|
|
||
| fun fromManifest(manifest: PermissionManifest): PermissionCommandPermissions { | ||
| val nodes = | ||
| manifest.permissions.map(PermissionManifestEntry::key).associateBy { key -> | ||
| when (key) { | ||
| "grounds.permissions.command" -> "root" | ||
| "grounds.permissions.command.status" -> "status" | ||
| "grounds.permissions.command.user.info" -> "user.info" | ||
| "grounds.permissions.command.user.check" -> "user.check" | ||
| "grounds.permissions.command.user.refresh" -> "user.refresh" | ||
| "grounds.permissions.command.refresh" -> "refresh" | ||
| else -> key | ||
| } | ||
| } | ||
| val missing = REQUIRED_KEYS - nodes.keys | ||
| require(missing.isEmpty()) { | ||
| "Missing required command permissions: ${missing.sorted().joinToString(",")}" | ||
| } | ||
| return PermissionCommandPermissions(nodes) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the backend fetch fails for an online player with a valid disk cache,
PermissionLoginListener.loadSnapshotreturnsallowed(cached)so logins can continue. Wiring the refresh command to that same method means/perm refresh <player>andrefresh allreport success/policyVersion while only reloading cached data, so operators can think a permission change was refreshed even though the backend was unavailable.Useful? React with 👍 / 👎.