Replace the service bearer tokens with [tls] ca_file and cert_dir. Caller identity comes from the verified certificate, not metadata. Without TLS, services listen and dial on loopback only. Task containers get only the CA and keep their grant.
88 lines
3.0 KiB
Go
88 lines
3.0 KiB
Go
// Package messagingrpc is the transport for messaging.v1.MessagingService:
|
|
// the server the Messaging Service registers and the client other services
|
|
// dial to deliver outbound chat messages.
|
|
package messagingrpc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/messaging/v1"
|
|
"github.com/samcharles93/archie-core/internal/domain/presence"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/rpctls"
|
|
"github.com/samcharles93/archie-core/internal/logging"
|
|
)
|
|
|
|
// ErrUnavailable marks a delivery the channel could not make now: the channel
|
|
// is unknown, stopped or cannot send. The caller may retry later.
|
|
var ErrUnavailable = errors.New("messaging: channel unavailable")
|
|
|
|
// Deliverer sends text to one chat on a named channel.
|
|
type Deliverer interface {
|
|
Deliver(ctx context.Context, channel, chatID, text string) error
|
|
}
|
|
|
|
type server struct {
|
|
pb.UnimplementedMessagingServiceServer
|
|
deliverer Deliverer
|
|
feed *logging.Feed
|
|
}
|
|
|
|
// RegisterServer registers MessagingService over d.
|
|
func RegisterServer(registrar grpc.ServiceRegistrar, d Deliverer, feed *logging.Feed) {
|
|
pb.RegisterMessagingServiceServer(registrar, &server{deliverer: d, feed: feed})
|
|
}
|
|
|
|
func (s *server) Deliver(ctx context.Context, req *pb.DeliverRequest) (*pb.DeliverResponse, error) {
|
|
if req.GetChannel() == "" || req.GetChatId() == "" || req.GetText() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "channel, chat_id and text are required")
|
|
}
|
|
err := s.deliverer.Deliver(ctx, req.GetChannel(), req.GetChatId(), req.GetText())
|
|
switch {
|
|
case err == nil:
|
|
return &pb.DeliverResponse{}, nil
|
|
case errors.Is(err, ErrUnavailable):
|
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
|
default:
|
|
return nil, status.Error(codes.Unavailable, err.Error())
|
|
}
|
|
}
|
|
|
|
// ServerOptions serves listen with id's credentials.
|
|
func ServerOptions(listen string, id *rpctls.Identity) ([]grpc.ServerOption, error) {
|
|
creds, err := id.ServerOption(listen, false)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("messaging: %w", err)
|
|
}
|
|
return []grpc.ServerOption{creds}, nil
|
|
}
|
|
|
|
// Client delivers through a dialled MessagingService.
|
|
type Client struct{ rpc pb.MessagingServiceClient }
|
|
|
|
// Dial returns a client for target, authenticated by id.
|
|
func Dial(target string, id *rpctls.Identity) (*Client, func(), error) {
|
|
target = strings.TrimSpace(target)
|
|
creds, err := id.DialOption(target, presence.Messaging)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("messaging: %w", err)
|
|
}
|
|
conn, err := grpc.NewClient(target, creds)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("create messaging client: %w", err)
|
|
}
|
|
return &Client{rpc: pb.NewMessagingServiceClient(conn)}, func() { _ = conn.Close() }, nil
|
|
}
|
|
|
|
// Deliver sends text to chatID on channel.
|
|
func (c *Client) Deliver(ctx context.Context, channel, chatID, text string) error {
|
|
_, err := c.rpc.Deliver(ctx, &pb.DeliverRequest{Channel: channel, ChatId: chatID, Text: text})
|
|
return err
|
|
}
|