feat(connector): the connector.v1 SDK and an Alertmanager connector (archie #1980)
publish / publish (push) Successful in 1m50s

This commit is contained in:
sam committed 2026-11-15 17:58:18 +11:00
1 parent 882a21596b
commit 7fd7a4104b
12 files changed
+2420 -28

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
// Package connector is what a Go connector extension builds on: the
// generated connector.v1 contract, the interfaces a connector implements and
// the Serve helper archie-core launches it with.
package connector
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"time"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// Contract is the connector contract version this SDK speaks.
const Contract = "v1"
// Config is the operator's settings and the host-resolved secrets.
type Config struct {
Settings map[string]string
Secrets map[string]string
}
// Description declares what a connector hears, does and answers.
type Description struct {
Events []*connectorv1.EventType
Actions []*connectorv1.ActionType
Lookups []*connectorv1.LookupType
}
// Event is one typed event parsed from a verified payload.
type Event struct {
Type string
SourceEventID string
Fields json.RawMessage
LinkKey string
OccurredAt time.Time
}
// Connector is the part every connector implements.
type Connector interface {
Describe() Description
}
// Parser turns a payload the host already verified into typed events.
type Parser interface {
Parse(ctx context.Context, body []byte, headers map[string]string) ([]Event, error)
}
// Actor resolves and runs actions. Invoke must honour the idempotency key for
// any action that isn't read-only, and returns unknown=true when it can't
// confirm what its effect did.
type Actor interface {
Resolve(ctx context.Context, action string, input json.RawMessage) (*connectorv1.ResolveResponse, error)
Invoke(ctx context.Context, action string, input json.RawMessage, idempotencyKey string) (output json.RawMessage, unknown bool, err error)
}
// Looker answers read-only lookups. found=false means nothing was found.
type Looker interface {
Lookup(ctx context.Context, name string, input json.RawMessage) (output json.RawMessage, found bool, err error)
}
// Digest identifies one concrete action, the same way archie-core does: its
// name and its exact input.
func Digest(action string, input []byte) string {
h := sha256.New()
h.Write([]byte(action))
h.Write([]byte{0})
h.Write(input)
return hex.EncodeToString(h.Sum(nil))
}
+41
View File
@@ -0,0 +1,41 @@
package connector
import (
"context"
goplugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
const surface = "connector"
type plugin struct {
goplugin.NetRPCUnsupportedPlugin
impl connectorv1.ConnectorServiceServer
}
func (p *plugin) GRPCServer(_ *goplugin.GRPCBroker, s *grpc.Server) error {
connectorv1.RegisterConnectorServiceServer(s, p.impl)
return nil
}
func (*plugin) GRPCClient(context.Context, *goplugin.GRPCBroker, *grpc.ClientConn) (any, error) {
return nil, nil
}
// Serve runs the connector build returns as the extension process. build is
// called on every Configure. Serve returns when archie-core stops the
// extension.
func Serve(build func(Config) (Connector, error)) {
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: goplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "ARCHIE_EXTENSION",
MagicCookieValue: "archie",
},
Plugins: goplugin.PluginSet{surface: &plugin{impl: &server{build: build}}},
GRPCServer: goplugin.DefaultGRPCServer,
})
}
+121
View File
@@ -0,0 +1,121 @@
package connector
import (
"context"
"sync/atomic"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
)
// server adapts a Connector to the gRPC surface. A capability the connector
// doesn't implement answers UNIMPLEMENTED.
type server struct {
connectorv1.UnimplementedConnectorServiceServer
build func(Config) (Connector, error)
c atomic.Pointer[Connector]
}
func (s *server) Configure(_ context.Context, req *connectorv1.ConfigureRequest) (*connectorv1.ConfigureResponse, error) {
c, err := s.build(Config{Settings: req.GetSettings(), Secrets: req.GetSecrets()})
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "configure: %v", err)
}
s.c.Store(&c)
return &connectorv1.ConfigureResponse{}, nil
}
func (s *server) connector() (Connector, error) {
c := s.c.Load()
if c == nil {
return nil, status.Error(codes.FailedPrecondition, "connector not configured")
}
return *c, nil
}
func (s *server) Describe(context.Context, *connectorv1.DescribeRequest) (*connectorv1.DescribeResponse, error) {
c, err := s.connector()
if err != nil {
return nil, err
}
d := c.Describe()
return &connectorv1.DescribeResponse{Contract: Contract, Events: d.Events, Actions: d.Actions, Lookups: d.Lookups}, nil
}
func (s *server) Parse(ctx context.Context, req *connectorv1.ParseRequest) (*connectorv1.ParseResponse, error) {
c, err := s.connector()
if err != nil {
return nil, err
}
p, ok := c.(Parser)
if !ok {
return nil, status.Error(codes.Unimplemented, "connector hears no payloads")
}
events, err := p.Parse(ctx, req.GetBody(), req.GetHeaders())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "parse: %v", err)
}
out := make([]*connectorv1.Event, 0, len(events))
for _, e := range events {
out = append(out, &connectorv1.Event{
Type: e.Type, SourceEventId: e.SourceEventID, Fields: e.Fields,
LinkKey: e.LinkKey, OccurredAt: timestamppb.New(e.OccurredAt),
})
}
return &connectorv1.ParseResponse{Events: out}, nil
}
func (s *server) actor() (Actor, error) {
c, err := s.connector()
if err != nil {
return nil, err
}
a, ok := c.(Actor)
if !ok {
return nil, status.Error(codes.Unimplemented, "connector has no actions")
}
return a, nil
}
func (s *server) Resolve(ctx context.Context, req *connectorv1.ResolveRequest) (*connectorv1.ResolveResponse, error) {
a, err := s.actor()
if err != nil {
return nil, err
}
return a.Resolve(ctx, req.GetAction(), req.GetInput())
}
// Invoke runs the action and reports the digest of the input it ran, so the
// host can check it ran the judged action.
func (s *server) Invoke(ctx context.Context, req *connectorv1.InvokeRequest) (*connectorv1.InvokeResponse, error) {
a, err := s.actor()
if err != nil {
return nil, err
}
out, unknown, err := a.Invoke(ctx, req.GetAction(), req.GetInput(), req.GetIdempotencyKey())
if err != nil {
return nil, status.Errorf(codes.Unknown, "invoke: %v", err)
}
return &connectorv1.InvokeResponse{
Output: out, ExecutedDigest: Digest(req.GetAction(), req.GetInput()), OutcomeUnknown: unknown,
}, nil
}
func (s *server) Lookup(ctx context.Context, req *connectorv1.LookupRequest) (*connectorv1.LookupResponse, error) {
c, err := s.connector()
if err != nil {
return nil, err
}
l, ok := c.(Looker)
if !ok {
return nil, status.Error(codes.Unimplemented, "connector has no lookups")
}
out, found, err := l.Lookup(ctx, req.GetLookup(), req.GetInput())
if err != nil {
return nil, status.Errorf(codes.Unknown, "lookup: %v", err)
}
return &connectorv1.LookupResponse{Output: out, Found: found}, nil
}
File diff suppressed because it is too large. Load diff
+343
View File
@@ -0,0 +1,343 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.6.2
// - protoc (unknown)
// source: connector/v1/connector.proto
package connectorv1
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
// Requires gRPC-Go v1.64.0 or later.
const _ = grpc.SupportPackageIsVersion9
const (
ConnectorService_Configure_FullMethodName = "/connector.v1.ConnectorService/Configure"
ConnectorService_Describe_FullMethodName = "/connector.v1.ConnectorService/Describe"
ConnectorService_Parse_FullMethodName = "/connector.v1.ConnectorService/Parse"
ConnectorService_Resolve_FullMethodName = "/connector.v1.ConnectorService/Resolve"
ConnectorService_Invoke_FullMethodName = "/connector.v1.ConnectorService/Invoke"
ConnectorService_Lookup_FullMethodName = "/connector.v1.ConnectorService/Lookup"
)
// ConnectorServiceClient is the client API for ConnectorService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
//
// ConnectorService is the surface every connector extension serves: it
// declares what it hears and does, turns verified payloads into typed events,
// resolves and runs actions, and answers lookups. The host invokes everything
// by name; payloads are JSON validated against the declared schemas.
type ConnectorServiceClient interface {
// Configure hands the connector its operator settings and resolved secrets.
// The host calls it once after launch and again when settings change.
Configure(ctx context.Context, in *ConfigureRequest, opts ...grpc.CallOption) (*ConfigureResponse, error)
// Describe declares the connector's event types, actions and lookups.
Describe(ctx context.Context, in *DescribeRequest, opts ...grpc.CallOption) (*DescribeResponse, error)
// Parse turns a payload the host has already verified into typed events.
Parse(ctx context.Context, in *ParseRequest, opts ...grpc.CallOption) (*ParseResponse, error)
// Resolve works out the structured target and the effect of one concrete
// action before it is judged. The same action can have different effects on
// different targets.
Resolve(ctx context.Context, in *ResolveRequest, opts ...grpc.CallOption) (*ResolveResponse, error)
// Invoke runs one judged action. The connector honours the idempotency key
// and returns the digest of what it actually executed.
Invoke(ctx context.Context, in *InvokeRequest, opts ...grpc.CallOption) (*InvokeResponse, error)
// Lookup answers a read-only question. Lookups change nothing and run no
// guards; a lookup that finds nothing says so.
Lookup(ctx context.Context, in *LookupRequest, opts ...grpc.CallOption) (*LookupResponse, error)
}
type connectorServiceClient struct {
cc grpc.ClientConnInterface
}
func NewConnectorServiceClient(cc grpc.ClientConnInterface) ConnectorServiceClient {
return &connectorServiceClient{cc}
}
func (c *connectorServiceClient) Configure(ctx context.Context, in *ConfigureRequest, opts ...grpc.CallOption) (*ConfigureResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ConfigureResponse)
err := c.cc.Invoke(ctx, ConnectorService_Configure_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *connectorServiceClient) Describe(ctx context.Context, in *DescribeRequest, opts ...grpc.CallOption) (*DescribeResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(DescribeResponse)
err := c.cc.Invoke(ctx, ConnectorService_Describe_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *connectorServiceClient) Parse(ctx context.Context, in *ParseRequest, opts ...grpc.CallOption) (*ParseResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ParseResponse)
err := c.cc.Invoke(ctx, ConnectorService_Parse_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *connectorServiceClient) Resolve(ctx context.Context, in *ResolveRequest, opts ...grpc.CallOption) (*ResolveResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(ResolveResponse)
err := c.cc.Invoke(ctx, ConnectorService_Resolve_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *connectorServiceClient) Invoke(ctx context.Context, in *InvokeRequest, opts ...grpc.CallOption) (*InvokeResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(InvokeResponse)
err := c.cc.Invoke(ctx, ConnectorService_Invoke_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *connectorServiceClient) Lookup(ctx context.Context, in *LookupRequest, opts ...grpc.CallOption) (*LookupResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(LookupResponse)
err := c.cc.Invoke(ctx, ConnectorService_Lookup_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// ConnectorServiceServer is the server API for ConnectorService service.
// All implementations must embed UnimplementedConnectorServiceServer
// for forward compatibility.
//
// ConnectorService is the surface every connector extension serves: it
// declares what it hears and does, turns verified payloads into typed events,
// resolves and runs actions, and answers lookups. The host invokes everything
// by name; payloads are JSON validated against the declared schemas.
type ConnectorServiceServer interface {
// Configure hands the connector its operator settings and resolved secrets.
// The host calls it once after launch and again when settings change.
Configure(context.Context, *ConfigureRequest) (*ConfigureResponse, error)
// Describe declares the connector's event types, actions and lookups.
Describe(context.Context, *DescribeRequest) (*DescribeResponse, error)
// Parse turns a payload the host has already verified into typed events.
Parse(context.Context, *ParseRequest) (*ParseResponse, error)
// Resolve works out the structured target and the effect of one concrete
// action before it is judged. The same action can have different effects on
// different targets.
Resolve(context.Context, *ResolveRequest) (*ResolveResponse, error)
// Invoke runs one judged action. The connector honours the idempotency key
// and returns the digest of what it actually executed.
Invoke(context.Context, *InvokeRequest) (*InvokeResponse, error)
// Lookup answers a read-only question. Lookups change nothing and run no
// guards; a lookup that finds nothing says so.
Lookup(context.Context, *LookupRequest) (*LookupResponse, error)
mustEmbedUnimplementedConnectorServiceServer()
}
// UnimplementedConnectorServiceServer must be embedded to have
// forward compatible implementations.
//
// NOTE: this should be embedded by value instead of pointer to avoid a nil
// pointer dereference when methods are called.
type UnimplementedConnectorServiceServer struct{}
func (UnimplementedConnectorServiceServer) Configure(context.Context, *ConfigureRequest) (*ConfigureResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Configure not implemented")
}
func (UnimplementedConnectorServiceServer) Describe(context.Context, *DescribeRequest) (*DescribeResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Describe not implemented")
}
func (UnimplementedConnectorServiceServer) Parse(context.Context, *ParseRequest) (*ParseResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Parse not implemented")
}
func (UnimplementedConnectorServiceServer) Resolve(context.Context, *ResolveRequest) (*ResolveResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Resolve not implemented")
}
func (UnimplementedConnectorServiceServer) Invoke(context.Context, *InvokeRequest) (*InvokeResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Invoke not implemented")
}
func (UnimplementedConnectorServiceServer) Lookup(context.Context, *LookupRequest) (*LookupResponse, error) {
return nil, status.Error(codes.Unimplemented, "method Lookup not implemented")
}
func (UnimplementedConnectorServiceServer) mustEmbedUnimplementedConnectorServiceServer() {}
func (UnimplementedConnectorServiceServer) testEmbeddedByValue() {}
// UnsafeConnectorServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to ConnectorServiceServer will
// result in compilation errors.
type UnsafeConnectorServiceServer interface {
mustEmbedUnimplementedConnectorServiceServer()
}
func RegisterConnectorServiceServer(s grpc.ServiceRegistrar, srv ConnectorServiceServer) {
// If the following call panics, it indicates UnimplementedConnectorServiceServer was
// embedded by pointer and is nil. This will cause panics if an
// unimplemented method is ever invoked, so we test this at initialization
// time to prevent it from happening at runtime later due to I/O.
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
t.testEmbeddedByValue()
}
s.RegisterService(&ConnectorService_ServiceDesc, srv)
}
func _ConnectorService_Configure_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ConfigureRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Configure(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Configure_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Configure(ctx, req.(*ConfigureRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ConnectorService_Describe_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DescribeRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Describe(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Describe_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Describe(ctx, req.(*DescribeRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ConnectorService_Parse_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ParseRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Parse(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Parse_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Parse(ctx, req.(*ParseRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ConnectorService_Resolve_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ResolveRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Resolve(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Resolve_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Resolve(ctx, req.(*ResolveRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ConnectorService_Invoke_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(InvokeRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Invoke(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Invoke_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Invoke(ctx, req.(*InvokeRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ConnectorService_Lookup_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(LookupRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ConnectorServiceServer).Lookup(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ConnectorService_Lookup_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ConnectorServiceServer).Lookup(ctx, req.(*LookupRequest))
}
return interceptor(ctx, in, info, handler)
}
// ConnectorService_ServiceDesc is the grpc.ServiceDesc for ConnectorService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var ConnectorService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "connector.v1.ConnectorService",
HandlerType: (*ConnectorServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Configure",
Handler: _ConnectorService_Configure_Handler,
},
{
MethodName: "Describe",
Handler: _ConnectorService_Describe_Handler,
},
{
MethodName: "Parse",
Handler: _ConnectorService_Parse_Handler,
},
{
MethodName: "Resolve",
Handler: _ConnectorService_Resolve_Handler,
},
{
MethodName: "Invoke",
Handler: _ConnectorService_Invoke_Handler,
},
{
MethodName: "Lookup",
Handler: _ConnectorService_Lookup_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "connector/v1/connector.proto",
}