Skip to content

Commit 0b52c37

Browse files
committed
Add initial Python plugin
1 parent 9bcd572 commit 0b52c37

File tree

13 files changed

+189
-26
lines changed

13 files changed

+189
-26
lines changed

buf.gen.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
version: v1
22
plugins:
3+
# Go (Protobuf)
34
- plugin: buf.build/protocolbuffers/go
45
out: .
56
opt:
67
- paths=source_relative
8+
9+
# Go (gRPC)
710
- plugin: buf.build/grpc/go:v1.3.0
811
out: .
912
opt:
1013
- paths=source_relative
1114
- require_unimplemented_servers=false
15+
16+
# Python (Protobuf)
17+
- plugin: buf.build/protocolbuffers/python:v24.4
18+
out: plugin-python
19+
20+
# Python (gRPC)
21+
- plugin: buf.build/grpc/python:v1.58.1
22+
out: plugin-python
23+
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Copyright (c) HashiCorp, Inc.
2-
// SPDX-License-Identifier: MPL-2.0
3-
41
package main
52

63
import (
@@ -16,23 +13,24 @@ import (
1613
type Authorize struct{}
1714

1815
func (Authorize) Get(user string, host string) ([]byte, error) {
19-
shared.Logger.Info("Get", "user", user, "host", host)
20-
2116
if user == "" {
2217
return nil, fmt.Errorf("user is required (e.g. ./authorize <USER> <HOST>)")
2318
}
2419

20+
shared.Logger.Info("Get", "user", user, "host", host)
2521
resp, err := http.Get(host + user)
2622
if err != nil {
2723
return nil, fmt.Errorf("error making request: %w", err)
2824
}
2925
defer resp.Body.Close()
3026

27+
shared.Logger.Info("Response", "status", resp.Status)
3128
body, err := io.ReadAll(resp.Body)
3229
if err != nil {
3330
return nil, fmt.Errorf("error reading response body: %w", err)
3431
}
3532

33+
shared.Logger.Info("Response", "body", string(body))
3634
return body, nil
3735
}
3836

plugin-python/README.md

Whitespace-only changes.

plugin-python/main.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from concurrent import futures
2+
import sys
3+
import time
4+
5+
import grpc
6+
7+
from proto import auth_pb2
8+
from proto import auth_pb2_grpc
9+
10+
from grpc_health.v1.health import HealthServicer
11+
from grpc_health.v1 import health_pb2, health_pb2_grpc
12+
13+
class AuthorizeServicer(auth_pb2_grpc.AuthorizeServicer):
14+
"""Implementation of Authorize service."""
15+
16+
def Get(self, request, context):
17+
filename = "auth_"+request.key
18+
with open(filename, 'r+b') as f:
19+
result = auth_pb2.GetResponse()
20+
result.value = f.read()
21+
return result
22+
23+
def serve():
24+
# We need to build a health service to work with go-plugin
25+
health = HealthServicer()
26+
health.set("plugin", health_pb2.HealthCheckResponse.ServingStatus.Value('SERVING'))
27+
28+
# Start the server.
29+
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
30+
auth_pb2_grpc.add_AuthorizeServicer_to_server(AuthorizeServicer(), server)
31+
health_pb2_grpc.add_HealthServicer_to_server(health, server)
32+
server.add_insecure_port('127.0.0.1:1234')
33+
server.start()
34+
35+
# Output information
36+
print("1|1|tcp|127.0.0.1:1234|grpc")
37+
sys.stdout.flush()
38+
39+
try:
40+
while True:
41+
time.sleep(60 * 60 * 24)
42+
except KeyboardInterrupt:
43+
server.stop(0)
44+
45+
if __name__ == '__main__':
46+
serve()

plugin-python/proto/auth_pb2.py

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
2+
"""Client and server classes corresponding to protobuf-defined services."""
3+
import grpc
4+
5+
from proto import auth_pb2 as proto_dot_auth__pb2
6+
7+
8+
class AuthorizeStub(object):
9+
"""Missing associated documentation comment in .proto file."""
10+
11+
def __init__(self, channel):
12+
"""Constructor.
13+
14+
Args:
15+
channel: A grpc.Channel.
16+
"""
17+
self.Get = channel.unary_unary(
18+
'/proto.Authorize/Get',
19+
request_serializer=proto_dot_auth__pb2.GetRequest.SerializeToString,
20+
response_deserializer=proto_dot_auth__pb2.GetResponse.FromString,
21+
)
22+
23+
24+
class AuthorizeServicer(object):
25+
"""Missing associated documentation comment in .proto file."""
26+
27+
def Get(self, request, context):
28+
"""Missing associated documentation comment in .proto file."""
29+
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
30+
context.set_details('Method not implemented!')
31+
raise NotImplementedError('Method not implemented!')
32+
33+
34+
def add_AuthorizeServicer_to_server(servicer, server):
35+
rpc_method_handlers = {
36+
'Get': grpc.unary_unary_rpc_method_handler(
37+
servicer.Get,
38+
request_deserializer=proto_dot_auth__pb2.GetRequest.FromString,
39+
response_serializer=proto_dot_auth__pb2.GetResponse.SerializeToString,
40+
),
41+
}
42+
generic_handler = grpc.method_handlers_generic_handler(
43+
'proto.Authorize', rpc_method_handlers)
44+
server.add_generic_rpc_handlers((generic_handler,))
45+
46+
47+
# This class is part of an EXPERIMENTAL API.
48+
class Authorize(object):
49+
"""Missing associated documentation comment in .proto file."""
50+
51+
@staticmethod
52+
def Get(request,
53+
target,
54+
options=(),
55+
channel_credentials=None,
56+
call_credentials=None,
57+
insecure=False,
58+
compression=None,
59+
wait_for_ready=None,
60+
timeout=None,
61+
metadata=None):
62+
return grpc.experimental.unary_unary(request, target, '/proto.Authorize/Get',
63+
proto_dot_auth__pb2.GetRequest.SerializeToString,
64+
proto_dot_auth__pb2.GetResponse.FromString,
65+
options, channel_credentials,
66+
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)

plugin-python/pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "funnel-auth-plugin"
3+
version = "0.1.0"
4+
description = "Python version of the Funnel Authoritzation Plugin"
5+
readme = "README.md"
6+
requires-python = ">=3.13"
7+
dependencies = []

plugin-python/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
grpcio
2+
grpcio-health-checking
3+
protobuf

plugin-python/tests.py

Whitespace-only changes.

0 commit comments

Comments
 (0)