Commit 61b6d01

mo khan <mo@mokhan.ca>
2025-03-06 00:12:02
feat: define the protobuf service definition
1 parent 4100066
Changed files (4)
bin/rpc
@@ -0,0 +1,34 @@
+#!/usr/bin/env ruby
+
+require "bundler/inline"
+
+gemfile do
+  source "https://rubygems.org"
+
+  gem "grpc", "~> 1.0"
+  gem "grpc-tools", "~> 1.0"
+  gem "logger", "~> 1.0"
+end
+
+lib_path = Pathname.new(__FILE__).parent.parent.join('lib').realpath.to_s
+$LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
+
+require 'ability_services_pb'
+
+class AbilityHandler < ::Ability::Service
+  def allowed(request, _call)
+    puts [request, _call].inspect
+    # TODO:: entrypoint to declarative policies
+    AllowReply.new(result: true)
+  end
+end
+
+host = ENV.fetch("HOST", "localhost")
+port = ENV.fetch("PORT", "50051")
+bind_addr = "#{host}:#{port}"
+server = GRPC::RpcServer.new
+server.add_http2_port(bind_addr, :this_port_is_insecure)
+GRPC.logger = Logger.new($stderr, level: :debug)
+GRPC.logger.info("... running insecurely on #{bind_addr}")
+server.handle(AbilityHandler.new)
+server.run_till_terminated_or_interrupted([1, 'int', 'SIGQUIT'])
lib/.keep
protos/ability.proto
@@ -0,0 +1,13 @@
+service Ability {
+  rpc Allowed (AllowRequest) returns (AllowReply) {}
+}
+
+message AllowRequest {
+  required string subject = 1;
+  required string permission = 2;
+  required string resource = 3;
+}
+
+message AllowReply {
+  required bool result = 1;
+}
magefile.go
@@ -54,6 +54,16 @@ func Api() error {
 	return sh.RunWithV(env, "ruby", "./bin/api")
 }
 
+// Run the gRPC Server
+func Rpc(ctx context.Context) error {
+	mg.CtxDeps(ctx, Protos)
+	env := map[string]string{
+		"PORT": "50051",
+		"HOST": "localhost",
+	}
+	return sh.RunWithV(env, "ruby", "./bin/rpc")
+}
+
 // Open a web browser to the login page
 func Browser() error {
 	url := "http://localhost:8080/ui/sessions/new"
@@ -64,7 +74,18 @@ func Browser() error {
 	}
 }
 
+// Generate gRPC from protocal buffers
+func Protos() error {
+	return sh.RunV(
+		"grpc_tools_ruby_protoc",
+		"--proto_path=./protos",
+		"--ruby_out=lib",
+		"--grpc_out=lib",
+		"protos/ability.proto",
+	)
+}
+
 // Run All the servers
 func Run(ctx context.Context) {
-	mg.CtxDeps(ctx, Idp, UI, Api, Gateway)
+	mg.CtxDeps(ctx, Idp, UI, Api, Rpc, Gateway)
 }