feat(eda): name and delete capture sources; an armed binding blocks the delete
This commit is contained in:
22 files changed
+1103
-592
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -111,6 +111,8 @@ const (
|
||||
StateStoreService_ListSources_FullMethodName = "/state.v1.StateStoreService/ListSources"
|
||||
StateStoreService_SetSourceSigning_FullMethodName = "/state.v1.StateStoreService/SetSourceSigning"
|
||||
StateStoreService_SetSourceSecret_FullMethodName = "/state.v1.StateStoreService/SetSourceSecret"
|
||||
StateStoreService_SetSourceName_FullMethodName = "/state.v1.StateStoreService/SetSourceName"
|
||||
StateStoreService_DeleteSource_FullMethodName = "/state.v1.StateStoreService/DeleteSource"
|
||||
StateStoreService_ArmedBindingsForSource_FullMethodName = "/state.v1.StateStoreService/ArmedBindingsForSource"
|
||||
StateStoreService_RecordDispatch_FullMethodName = "/state.v1.StateStoreService/RecordDispatch"
|
||||
StateStoreService_SetDispatchTask_FullMethodName = "/state.v1.StateStoreService/SetDispatchTask"
|
||||
@@ -315,6 +317,8 @@ type StateStoreServiceClient interface {
|
||||
ListSources(ctx context.Context, in *ListSourcesRequest, opts ...grpc.CallOption) (*ListSourcesResponse, error)
|
||||
SetSourceSigning(ctx context.Context, in *SetSourceSigningRequest, opts ...grpc.CallOption) (*SetSourceSigningResponse, error)
|
||||
SetSourceSecret(ctx context.Context, in *SetSourceSecretRequest, opts ...grpc.CallOption) (*SetSourceSecretResponse, error)
|
||||
SetSourceName(ctx context.Context, in *SetSourceNameRequest, opts ...grpc.CallOption) (*SetSourceNameResponse, error)
|
||||
DeleteSource(ctx context.Context, in *DeleteSourceRequest, opts ...grpc.CallOption) (*DeleteSourceResponse, error)
|
||||
// Dispatch
|
||||
ArmedBindingsForSource(ctx context.Context, in *ArmedBindingsForSourceRequest, opts ...grpc.CallOption) (*ArmedBindingsForSourceResponse, error)
|
||||
RecordDispatch(ctx context.Context, in *RecordDispatchRequest, opts ...grpc.CallOption) (*RecordDispatchResponse, error)
|
||||
@@ -1310,6 +1314,26 @@ func (c *stateStoreServiceClient) SetSourceSecret(ctx context.Context, in *SetSo
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) SetSourceName(ctx context.Context, in *SetSourceNameRequest, opts ...grpc.CallOption) (*SetSourceNameResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(SetSourceNameResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_SetSourceName_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) DeleteSource(ctx context.Context, in *DeleteSourceRequest, opts ...grpc.CallOption) (*DeleteSourceResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(DeleteSourceResponse)
|
||||
err := c.cc.Invoke(ctx, StateStoreService_DeleteSource_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *stateStoreServiceClient) ArmedBindingsForSource(ctx context.Context, in *ArmedBindingsForSourceRequest, opts ...grpc.CallOption) (*ArmedBindingsForSourceResponse, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(ArmedBindingsForSourceResponse)
|
||||
@@ -1746,6 +1770,8 @@ type StateStoreServiceServer interface {
|
||||
ListSources(context.Context, *ListSourcesRequest) (*ListSourcesResponse, error)
|
||||
SetSourceSigning(context.Context, *SetSourceSigningRequest) (*SetSourceSigningResponse, error)
|
||||
SetSourceSecret(context.Context, *SetSourceSecretRequest) (*SetSourceSecretResponse, error)
|
||||
SetSourceName(context.Context, *SetSourceNameRequest) (*SetSourceNameResponse, error)
|
||||
DeleteSource(context.Context, *DeleteSourceRequest) (*DeleteSourceResponse, error)
|
||||
// Dispatch
|
||||
ArmedBindingsForSource(context.Context, *ArmedBindingsForSourceRequest) (*ArmedBindingsForSourceResponse, error)
|
||||
RecordDispatch(context.Context, *RecordDispatchRequest) (*RecordDispatchResponse, error)
|
||||
@@ -2079,6 +2105,12 @@ func (UnimplementedStateStoreServiceServer) SetSourceSigning(context.Context, *S
|
||||
func (UnimplementedStateStoreServiceServer) SetSourceSecret(context.Context, *SetSourceSecretRequest) (*SetSourceSecretResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method SetSourceSecret not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) SetSourceName(context.Context, *SetSourceNameRequest) (*SetSourceNameResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method SetSourceName not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) DeleteSource(context.Context, *DeleteSourceRequest) (*DeleteSourceResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method DeleteSource not implemented")
|
||||
}
|
||||
func (UnimplementedStateStoreServiceServer) ArmedBindingsForSource(context.Context, *ArmedBindingsForSourceRequest) (*ArmedBindingsForSourceResponse, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method ArmedBindingsForSource not implemented")
|
||||
}
|
||||
@@ -3817,6 +3849,42 @@ func _StateStoreService_SetSourceSecret_Handler(srv interface{}, ctx context.Con
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_SetSourceName_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(SetSourceNameRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).SetSourceName(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_SetSourceName_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).SetSourceName(ctx, req.(*SetSourceNameRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_DeleteSource_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(DeleteSourceRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(StateStoreServiceServer).DeleteSource(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: StateStoreService_DeleteSource_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(StateStoreServiceServer).DeleteSource(ctx, req.(*DeleteSourceRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _StateStoreService_ArmedBindingsForSource_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(ArmedBindingsForSourceRequest)
|
||||
if err := dec(in); err != nil {
|
||||
@@ -4627,6 +4695,14 @@ var StateStoreService_ServiceDesc = grpc.ServiceDesc{
|
||||
MethodName: "SetSourceSecret",
|
||||
Handler: _StateStoreService_SetSourceSecret_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "SetSourceName",
|
||||
Handler: _StateStoreService_SetSourceName_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "DeleteSource",
|
||||
Handler: _StateStoreService_DeleteSource_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "ArmedBindingsForSource",
|
||||
Handler: _StateStoreService_ArmedBindingsForSource_Handler,
|
||||
|
||||
@@ -35,7 +35,9 @@ var (
|
||||
// captures and binding matchers reference it, so it does not change after
|
||||
// creation. Secret is the HMAC key a signed sender signs with.
|
||||
type Source struct {
|
||||
Path string `json:"path"`
|
||||
Path string `json:"path"`
|
||||
// Name is the operator's label; Path stays the source's identity.
|
||||
Name string `json:"name,omitempty"`
|
||||
Signing Signing `json:"signing"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
|
||||
@@ -248,6 +248,10 @@ type SourceStore interface {
|
||||
ListSources(ctx context.Context) ([]source.Source, error)
|
||||
SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error
|
||||
SetSourceSecret(ctx context.Context, path, secret string) error
|
||||
SetSourceName(ctx context.Context, path, name string) error
|
||||
// DeleteSource refuses with ErrSourceInUse while an armed binding fires
|
||||
// on the source.
|
||||
DeleteSource(ctx context.Context, path string) error
|
||||
}
|
||||
|
||||
// DispatchLedger reads the binding dispatch ledger.
|
||||
@@ -488,6 +492,9 @@ var (
|
||||
ErrEventTypeNotFound = errors.New("store: event type not found")
|
||||
// ErrSourceNotFound is returned when a source path does not exist.
|
||||
ErrSourceNotFound = errors.New("store: source not found")
|
||||
// ErrSourceInUse is returned when deleting a source an armed binding
|
||||
// fires on.
|
||||
ErrSourceInUse = errors.New("store: source has an armed binding")
|
||||
// ErrSourcePathTaken is returned when a new source's path is in use.
|
||||
ErrSourcePathTaken = errors.New("store: source path already taken")
|
||||
// ErrSourceSigningStale is returned when a signing write's expected
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/domain/binding"
|
||||
"github.com/samcharles93/archie-core/internal/domain/eventtype"
|
||||
"github.com/samcharles93/archie-core/internal/domain/mapping"
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
|
||||
@@ -148,4 +149,24 @@ func TestBindingLifecycle(t *testing.T) {
|
||||
if err := eda.PauseBinding(ctx, "missing"); !errors.Is(err, storecontract.ErrBindingNotFound) {
|
||||
t.Fatalf("pause of a missing binding = %v, want ErrBindingNotFound", err)
|
||||
}
|
||||
|
||||
// A source is not deleted while an armed binding fires on it.
|
||||
if err := eda.InsertSource(ctx, source.Source{Path: "forge", Signing: source.SigningSigned}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.ApproveBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceInUse) {
|
||||
t.Fatalf("delete of a source an armed binding fires on = %v, want ErrSourceInUse", err)
|
||||
}
|
||||
if err := eda.PauseBinding(ctx, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); err != nil {
|
||||
t.Fatalf("delete of a source with no armed binding = %v", err)
|
||||
}
|
||||
if err := eda.DeleteSource(ctx, "forge"); !errors.Is(err, storecontract.ErrSourceNotFound) {
|
||||
t.Fatalf("second delete = %v, want ErrSourceNotFound", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
-- sources.name is the operator's label for a source; its path stays the
|
||||
-- identity captures and bindings reference.
|
||||
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE sources ADD COLUMN name text NOT NULL DEFAULT '';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE sources DROP COLUMN name;
|
||||
@@ -189,6 +189,21 @@ func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID strin
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteUnboundSource = `-- name: DeleteUnboundSource :execrows
|
||||
DELETE FROM sources s WHERE s.path = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed')
|
||||
`
|
||||
|
||||
// A source an armed binding fires on is not deleted: the binding would
|
||||
// silently stop receiving events.
|
||||
func (q *Queries) DeleteUnboundSource(ctx context.Context, path string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteUnboundSource, path)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deriveSources = `-- name: DeriveSources :exec
|
||||
SELECT derive_sources()
|
||||
`
|
||||
@@ -342,7 +357,7 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
|
||||
}
|
||||
|
||||
const getSource = `-- name: GetSource :one
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
|
||||
@@ -356,6 +371,7 @@ func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
|
||||
&i.UpdatedAt,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.Name,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -837,7 +853,7 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
|
||||
}
|
||||
|
||||
const listSources = `-- name: ListSources :many
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path
|
||||
`
|
||||
|
||||
func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
|
||||
@@ -857,6 +873,7 @@ func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
|
||||
&i.UpdatedAt,
|
||||
&i.OrgID,
|
||||
&i.WorkspaceID,
|
||||
&i.Name,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -958,6 +975,23 @@ func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDisp
|
||||
return err
|
||||
}
|
||||
|
||||
const setSourceName = `-- name: SetSourceName :execrows
|
||||
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1
|
||||
`
|
||||
|
||||
type SetSourceNameParams struct {
|
||||
Path string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, setSourceName, arg.Path, arg.Name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const setSourceSecret = `-- name: SetSourceSecret :execrows
|
||||
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1
|
||||
`
|
||||
|
||||
@@ -344,6 +344,7 @@ type Source struct {
|
||||
UpdatedAt time.Time
|
||||
OrgID string
|
||||
WorkspaceID string
|
||||
Name string
|
||||
}
|
||||
|
||||
type StepExecution struct {
|
||||
|
||||
@@ -152,10 +152,10 @@ SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text))
|
||||
INSERT INTO sources (path, signing, secret) VALUES ($1, $2, $3);
|
||||
|
||||
-- name: GetSource :one
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources WHERE path = $1;
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources WHERE path = $1;
|
||||
|
||||
-- name: ListSources :many
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id FROM sources ORDER BY created_at DESC, path;
|
||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name FROM sources ORDER BY created_at DESC, path;
|
||||
|
||||
-- name: SetSourceSigning :execrows
|
||||
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
|
||||
@@ -164,6 +164,15 @@ WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing);
|
||||
-- name: SetSourceSecret :execrows
|
||||
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1;
|
||||
|
||||
-- name: SetSourceName :execrows
|
||||
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1;
|
||||
|
||||
-- name: DeleteUnboundSource :execrows
|
||||
-- A source an armed binding fires on is not deleted: the binding would
|
||||
-- silently stop receiving events.
|
||||
DELETE FROM sources s WHERE s.path = $1
|
||||
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
|
||||
|
||||
-- name: SourceExists :one
|
||||
SELECT EXISTS (SELECT 1 FROM sources WHERE path = $1);
|
||||
|
||||
|
||||
@@ -102,6 +102,37 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetSourceName relabels a source.
|
||||
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
|
||||
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name})
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: set source name: %w", err)
|
||||
}
|
||||
if n == 0 {
|
||||
return storecontract.ErrSourceNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteSource deletes a source no armed binding fires on.
|
||||
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
|
||||
n, err := s.q.DeleteUnboundSource(ctx, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: delete source: %w", err)
|
||||
}
|
||||
if n > 0 {
|
||||
return nil
|
||||
}
|
||||
exists, err := s.q.SourceExists(ctx, path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("edastore: delete source: %w", err)
|
||||
}
|
||||
if exists {
|
||||
return storecontract.ErrSourceInUse
|
||||
}
|
||||
return storecontract.ErrSourceNotFound
|
||||
}
|
||||
|
||||
// sealSecret encrypts a non-empty secret when a cipher is configured.
|
||||
func (s *EDA) sealSecret(secret string) (string, error) {
|
||||
if s.cipher == nil || secret == "" {
|
||||
@@ -124,7 +155,7 @@ func (s *EDA) sourceValue(r postgresdb.Source) (source.Source, error) {
|
||||
secret = plain
|
||||
}
|
||||
return source.Source{
|
||||
Path: r.Path, Signing: source.Signing(r.Signing), Secret: secret,
|
||||
Path: r.Path, Name: r.Name, Signing: source.Signing(r.Signing), Secret: secret,
|
||||
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -659,6 +659,16 @@ func (c *Client) SetSourceSigning(ctx context.Context, path string, from, to sou
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceName(ctx context.Context, path, name string) error {
|
||||
_, err := c.client.SetSourceName(ctx, &pb.SetSourceNameRequest{Path: path, Name: name})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) DeleteSource(ctx context.Context, path string) error {
|
||||
_, err := c.client.DeleteSource(ctx, &pb.DeleteSourceRequest{Path: path})
|
||||
return unmapError(err)
|
||||
}
|
||||
|
||||
func (c *Client) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||
_, err := c.client.SetSourceSecret(ctx, &pb.SetSourceSecretRequest{Path: path, Secret: secret})
|
||||
return unmapError(err)
|
||||
|
||||
@@ -954,6 +954,28 @@ func (s *server) SetSourceSecret(ctx context.Context, r *pb.SetSourceSecretReque
|
||||
return &pb.SetSourceSecretResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) SetSourceName(ctx context.Context, r *pb.SetSourceNameRequest) (*pb.SetSourceNameResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.SetSourceName(ctx, r.Path, r.Name); err != nil {
|
||||
return nil, s.logErr("SetSourceName", err)
|
||||
}
|
||||
return &pb.SetSourceNameResponse{}, nil
|
||||
}
|
||||
|
||||
func (s *server) DeleteSource(ctx context.Context, r *pb.DeleteSourceRequest) (*pb.DeleteSourceResponse, error) {
|
||||
ss, err := s.sources()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := ss.DeleteSource(ctx, r.Path); err != nil {
|
||||
return nil, s.logErr("DeleteSource", err)
|
||||
}
|
||||
return &pb.DeleteSourceResponse{}, nil
|
||||
}
|
||||
|
||||
// Dispatch
|
||||
|
||||
func (s *server) ArmedBindingsForSource(ctx context.Context, r *pb.ArmedBindingsForSourceRequest) (*pb.ArmedBindingsForSourceResponse, error) {
|
||||
|
||||
@@ -211,7 +211,7 @@ func capturedEventValue(c *pb.CapturedEvent) storecontract.CapturedEvent {
|
||||
|
||||
func sourceProto(s source.Source) *pb.Source {
|
||||
return &pb.Source{
|
||||
Path: s.Path, Signing: string(s.Signing), Secret: s.Secret,
|
||||
Path: s.Path, Name: s.Name, Signing: string(s.Signing), Secret: s.Secret,
|
||||
CreatedAt: timestamp(s.CreatedAt), UpdatedAt: timestamp(s.UpdatedAt),
|
||||
}
|
||||
}
|
||||
@@ -221,7 +221,7 @@ func sourceValue(s *pb.Source) source.Source {
|
||||
return source.Source{}
|
||||
}
|
||||
return source.Source{
|
||||
Path: s.Path, Signing: source.Signing(s.Signing), Secret: s.Secret,
|
||||
Path: s.Path, Name: s.Name, Signing: source.Signing(s.Signing), Secret: s.Secret,
|
||||
CreatedAt: timeValue(s.CreatedAt), UpdatedAt: timeValue(s.UpdatedAt),
|
||||
}
|
||||
}
|
||||
@@ -496,6 +496,7 @@ var wireErrors = []struct {
|
||||
{storecontract.ErrMappingNotFound, codes.NotFound, msgMappingNotFound},
|
||||
{storecontract.ErrEventTypeNotFound, codes.NotFound, msgEventTypeNotFound},
|
||||
{storecontract.ErrSourceNotFound, codes.NotFound, msgSourceNotFound},
|
||||
{storecontract.ErrSourceInUse, codes.FailedPrecondition, storecontract.ErrSourceInUse.Error()},
|
||||
{storecontract.ErrAlreadyDispatched, codes.AlreadyExists, msgAlreadyDispatched},
|
||||
{storecontract.ErrCallNotYours, codes.PermissionDenied, storecontract.ErrCallNotYours.Error()},
|
||||
{storecontract.ErrCallCallerNotRunning, codes.FailedPrecondition, storecontract.ErrCallCallerNotRunning.Error()},
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/source"
|
||||
@@ -131,6 +132,47 @@ func (s *Server) handleSourceSecret(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, src)
|
||||
}
|
||||
|
||||
// handleSourceName relabels a source; its path does not change.
|
||||
func (s *Server) handleSourceName(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
}
|
||||
src, ok := s.loadSource(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var request struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&request); err != nil {
|
||||
http.Error(w, "invalid source name", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
src.Name = strings.TrimSpace(request.Name)
|
||||
if err := s.Sources.SetSourceName(r.Context(), src.Path, src.Name); err != nil {
|
||||
s.sourceError(w, "set source name", err)
|
||||
return
|
||||
}
|
||||
src.Secret = ""
|
||||
writeJSON(w, src)
|
||||
}
|
||||
|
||||
// handleSourceDelete deletes a source no armed binding fires on.
|
||||
func (s *Server) handleSourceDelete(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.authorizeTaskMutation(w, r) {
|
||||
return
|
||||
}
|
||||
if s.Sources == nil {
|
||||
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if err := s.Sources.DeleteSource(r.Context(), r.PathValue("path")); err != nil {
|
||||
s.sourceError(w, "delete source", err)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (s *Server) loadSource(w http.ResponseWriter, r *http.Request) (*source.Source, bool) {
|
||||
if s.Sources == nil {
|
||||
http.Error(w, "sources not configured", http.StatusServiceUnavailable)
|
||||
@@ -156,6 +198,8 @@ func (s *Server) sourceError(w http.ResponseWriter, what string, err error) {
|
||||
http.Error(w, "source signing changed; reload and retry", http.StatusConflict)
|
||||
case errors.Is(err, storecontract.ErrSourceNotFound):
|
||||
http.Error(w, "source not found", http.StatusNotFound)
|
||||
case errors.Is(err, storecontract.ErrSourceInUse):
|
||||
http.Error(w, "an armed binding fires on this source; pause or delete it first", http.StatusConflict)
|
||||
default:
|
||||
s.Log.Error(what, "err", err)
|
||||
http.Error(w, what+" failed", http.StatusInternalServerError)
|
||||
|
||||
@@ -304,6 +304,8 @@ func (s *Server) registerMappingAndBindingRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("POST /api/sources/{path}/signing", s.handleSourceSigning)
|
||||
mux.HandleFunc("POST /api/sources/{path}/approve-unsigned", s.handleSourceApproveUnsigned)
|
||||
mux.HandleFunc("POST /api/sources/{path}/secret", s.handleSourceSecret)
|
||||
mux.HandleFunc("PUT /api/sources/{path}/name", s.handleSourceName)
|
||||
mux.HandleFunc("DELETE /api/sources/{path}", s.handleSourceDelete)
|
||||
}
|
||||
|
||||
func (s *Server) registerHarnessRoutes(mux *http.ServeMux) {
|
||||
|
||||
Reference in new issue
Block a user