fix(archiegateway): confine non-operator chat identities to their own tasks
This commit is contained in:
1 parent
5675284b08
commit
b395eec893
2 files changed
+108
-6
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
@@ -35,10 +36,14 @@ func (a chatTaskControllerAdapter) ApproveChatTask(ctx context.Context, taskID i
|
||||
if a.approve == nil {
|
||||
return fmt.Errorf("task approval is unavailable")
|
||||
}
|
||||
// Chat acts for the operator, as the dashboard does, so it is not
|
||||
// narrowed to one identity's tasks. /approve carries no selection syntax,
|
||||
// so the review gate answer posts every offered finding.
|
||||
return a.approve(ctx, nil, actor, taskID, taskactions.ActionPayload{})
|
||||
// /approve carries no selection syntax, so the review gate answer posts
|
||||
// every offered finding.
|
||||
var scope *string
|
||||
if !seesEveryTask(string(actor.Identity)) {
|
||||
id := string(actor.Identity)
|
||||
scope = &id
|
||||
}
|
||||
return a.approve(ctx, scope, actor, taskID, taskactions.ActionPayload{})
|
||||
}
|
||||
|
||||
func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID int64, reason string) error {
|
||||
@@ -68,7 +73,13 @@ func (a chatTaskControllerAdapter) CancelChatTask(ctx context.Context, taskID in
|
||||
return err
|
||||
}
|
||||
|
||||
// chatTaskListerAdapter gives the gateway a read view of the operator's tasks.
|
||||
// seesEveryTask reports whether a chat profile acts for the operator, as the
|
||||
// dashboard does. Any other identity is confined to its own tasks.
|
||||
func seesEveryTask(chatIdentity string) bool {
|
||||
return chatIdentity == string(identity.OperatorID())
|
||||
}
|
||||
|
||||
// chatTaskListerAdapter gives the gateway a read view of an identity's tasks.
|
||||
// gateway deliberately does not import the task store, so the projection
|
||||
// happens here, as it does for the writer and controller adapters below.
|
||||
type chatTaskListerAdapter struct {
|
||||
@@ -76,12 +87,25 @@ type chatTaskListerAdapter struct {
|
||||
}
|
||||
|
||||
func (a chatTaskListerAdapter) ListChatTasks(ctx context.Context, identity string, limit int) ([]gateway.ChatTaskSummary, error) {
|
||||
rows, err := a.tasks(ctx, limit)
|
||||
all := seesEveryTask(identity)
|
||||
read := limit
|
||||
if !all {
|
||||
// Tasks applies its limit across the whole table, so over-read before
|
||||
// filtering to this identity.
|
||||
read *= taskListOverRead
|
||||
}
|
||||
rows, err := a.tasks(ctx, read)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]gateway.ChatTaskSummary, 0, limit)
|
||||
for _, task := range rows {
|
||||
if !all && task.Identity != identity {
|
||||
continue
|
||||
}
|
||||
if len(out) >= limit {
|
||||
break
|
||||
}
|
||||
out = append(out, gateway.ChatTaskSummary{
|
||||
ID: task.ID,
|
||||
Repo: task.Owner + "/" + task.Repo,
|
||||
@@ -115,6 +139,9 @@ func (a chatTaskLogReaderAdapter) ReadChatTaskLogs(
|
||||
if task == nil {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d not found", taskID)
|
||||
}
|
||||
if !seesEveryTask(identity) && task.Identity != identity {
|
||||
return gateway.ChatTaskLogResult{}, fmt.Errorf("task %d belongs to another identity", taskID)
|
||||
}
|
||||
if attempt <= 0 {
|
||||
attempt = task.Attempt
|
||||
}
|
||||
@@ -155,3 +182,7 @@ func configureTaskCommands(
|
||||
router.TaskLister = lister
|
||||
router.Identity = identity
|
||||
}
|
||||
|
||||
// taskListOverRead multiplies the requested limit when rows are filtered by
|
||||
// identity afterwards.
|
||||
const taskListOverRead = 5
|
||||
@@ -0,0 +1,71 @@
|
||||
package archiegateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/identity"
|
||||
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
||||
"github.com/samcharles93/archie-core/internal/domain/taskactions"
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow"
|
||||
"github.com/samcharles93/archie-core/internal/gateway"
|
||||
"github.com/samcharles93/archie-core/internal/logging"
|
||||
)
|
||||
|
||||
type emptyTaskLogs struct{ storecontract.TaskLogStore }
|
||||
|
||||
func (emptyTaskLogs) TaskLog(context.Context, int64, int, logging.Query) (logging.TaskLogPage, error) {
|
||||
return logging.TaskLogPage{}, nil
|
||||
}
|
||||
|
||||
// TestChatTaskScope pins the chat grant boundary: the operator profile sees
|
||||
// and approves every task, and any other identity only its own.
|
||||
func TestChatTaskScope(t *testing.T) {
|
||||
operator := string(identity.OperatorID())
|
||||
tasks := []workflow.Task{{ID: 1, Identity: "alice"}, {ID: 2, Identity: "bob"}}
|
||||
byID := func(_ context.Context, id int64) (*workflow.Task, error) {
|
||||
for i := range tasks {
|
||||
if tasks[i].ID == id {
|
||||
return &tasks[i], nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
chat string
|
||||
wantList int
|
||||
wantScope *string
|
||||
otherLogs bool
|
||||
}{
|
||||
{name: "operator sees every task", chat: operator, wantList: 2, otherLogs: true},
|
||||
{name: "an identity sees only its own", chat: "alice", wantList: 1, wantScope: new("alice")},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
lister := chatTaskListerAdapter{tasks: func(context.Context, int) ([]workflow.Task, error) { return tasks, nil }}
|
||||
got, err := lister.ListChatTasks(t.Context(), tt.chat, 10)
|
||||
if err != nil || len(got) != tt.wantList {
|
||||
t.Fatalf("list = %d tasks, %v; want %d", len(got), err, tt.wantList)
|
||||
}
|
||||
|
||||
var scope *string
|
||||
controller := chatTaskControllerAdapter{approve: func(_ context.Context, s *string, _ taskactions.Actor, _ int64, _ taskactions.ActionPayload) error {
|
||||
scope = s
|
||||
return nil
|
||||
}}
|
||||
if err := controller.ApproveChatTask(t.Context(), 2, taskactions.Actor{Identity: identity.IdentityID(tt.chat)}); err != nil {
|
||||
t.Fatalf("approve: %v", err)
|
||||
}
|
||||
if (scope == nil) != (tt.wantScope == nil) || (scope != nil && *scope != *tt.wantScope) {
|
||||
t.Fatalf("approve scope = %v, want %v", scope, tt.wantScope)
|
||||
}
|
||||
|
||||
reader := chatTaskLogReaderAdapter{tasks: byID, taskLogs: emptyTaskLogs{}}
|
||||
_, err = reader.ReadChatTaskLogs(t.Context(), tt.chat, 2, 0, gateway.ChatTaskLogQuery{})
|
||||
if refused := err != nil && err.Error() == "task 2 belongs to another identity"; refused == tt.otherLogs {
|
||||
t.Fatalf("reading another identity's logs: err = %v, want allowed=%v", err, tt.otherLogs)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user