fix(issuerpc): list only issues opened by users with write access (#1830)
Adds Issue.Author to the forge contract; the daemon drops issues from authors without write access and fails the listing closed when the forge cannot answer.
This commit is contained in:
6 files changed
+108
-3
No files matched your search
@@ -228,6 +228,7 @@ type Issue struct {
|
|||||||
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
|
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
|
||||||
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
Body string `protobuf:"bytes,3,opt,name=body,proto3" json:"body,omitempty"`
|
||||||
Labels []string `protobuf:"bytes,4,rep,name=labels,proto3" json:"labels,omitempty"`
|
Labels []string `protobuf:"bytes,4,rep,name=labels,proto3" json:"labels,omitempty"`
|
||||||
|
Author string `protobuf:"bytes,5,opt,name=author,proto3" json:"author,omitempty"`
|
||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
sizeCache protoimpl.SizeCache
|
sizeCache protoimpl.SizeCache
|
||||||
}
|
}
|
||||||
@@ -290,6 +291,13 @@ func (x *Issue) GetLabels() []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *Issue) GetAuthor() string {
|
||||||
|
if x != nil {
|
||||||
|
return x.Author
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
type AssignedIssuesResponse struct {
|
type AssignedIssuesResponse struct {
|
||||||
state protoimpl.MessageState `protogen:"open.v1"`
|
state protoimpl.MessageState `protogen:"open.v1"`
|
||||||
Issues []*Issue `protobuf:"bytes,1,rep,name=issues,proto3" json:"issues,omitempty"`
|
Issues []*Issue `protobuf:"bytes,1,rep,name=issues,proto3" json:"issues,omitempty"`
|
||||||
@@ -2918,12 +2926,13 @@ const file_forge_v1_forge_proto_rawDesc = "" +
|
|||||||
"\x04repo\x18\x02 \x01(\tR\x04repo\"O\n" +
|
"\x04repo\x18\x02 \x01(\tR\x04repo\"O\n" +
|
||||||
"\x0ePullRequestRef\x12%\n" +
|
"\x0ePullRequestRef\x12%\n" +
|
||||||
"\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\x12\x16\n" +
|
"\x04repo\x18\x01 \x01(\v2\x11.forge.v1.RepoRefR\x04repo\x12\x16\n" +
|
||||||
"\x06number\x18\x02 \x01(\x05R\x06number\"a\n" +
|
"\x06number\x18\x02 \x01(\x05R\x06number\"y\n" +
|
||||||
"\x05Issue\x12\x16\n" +
|
"\x05Issue\x12\x16\n" +
|
||||||
"\x06number\x18\x01 \x01(\x05R\x06number\x12\x14\n" +
|
"\x06number\x18\x01 \x01(\x05R\x06number\x12\x14\n" +
|
||||||
"\x05title\x18\x02 \x01(\tR\x05title\x12\x12\n" +
|
"\x05title\x18\x02 \x01(\tR\x05title\x12\x12\n" +
|
||||||
"\x04body\x18\x03 \x01(\tR\x04body\x12\x16\n" +
|
"\x04body\x18\x03 \x01(\tR\x04body\x12\x16\n" +
|
||||||
"\x06labels\x18\x04 \x03(\tR\x06labels\"A\n" +
|
"\x06labels\x18\x04 \x03(\tR\x06labels\x12\x16\n" +
|
||||||
|
"\x06author\x18\x05 \x01(\tR\x06author\"A\n" +
|
||||||
"\x16AssignedIssuesResponse\x12'\n" +
|
"\x16AssignedIssuesResponse\x12'\n" +
|
||||||
"\x06issues\x18\x01 \x03(\v2\x0f.forge.v1.IssueR\x06issues\"B\n" +
|
"\x06issues\x18\x01 \x03(\v2\x0f.forge.v1.IssueR\x06issues\"B\n" +
|
||||||
"\x17IssuesWithLabelResponse\x12'\n" +
|
"\x17IssuesWithLabelResponse\x12'\n" +
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ type Issue struct {
|
|||||||
Title string
|
Title string
|
||||||
Body string
|
Body string
|
||||||
Labels []string
|
Labels []string
|
||||||
|
// Author is the login that opened the issue.
|
||||||
|
Author string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Forge is the interface for interacting with a git host.
|
// Forge is the interface for interacting with a git host.
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ func prRef(owner, repo string, number int) *forgev1.PullRequestRef {
|
|||||||
func issues(in []*forgev1.Issue) []forge.Issue {
|
func issues(in []*forgev1.Issue) []forge.Issue {
|
||||||
out := make([]forge.Issue, 0, len(in))
|
out := make([]forge.Issue, 0, len(in))
|
||||||
for _, i := range in {
|
for _, i := range in {
|
||||||
out = append(out, forge.Issue{Number: int(i.GetNumber()), Title: i.GetTitle(), Body: i.GetBody(), Labels: i.GetLabels()})
|
out = append(out, forge.Issue{Number: int(i.GetNumber()), Title: i.GetTitle(), Body: i.GetBody(), Labels: i.GetLabels(), Author: i.GetAuthor()})
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ func (s *Server) Register(nc *nats.Conn) (func(), error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
issues, err = f.IssuesWithLabel(ctx, req.Owner, req.Repo, "")
|
issues, err = f.IssuesWithLabel(ctx, req.Owner, req.Repo, "")
|
||||||
}
|
}
|
||||||
|
if err == nil {
|
||||||
|
issues, err = writerIssues(ctx, s.Log, f, req.Owner, req.Repo, issues)
|
||||||
|
}
|
||||||
natsrpc.Respond(msg, s.Log, "issuerpc", listResponse{Envelope: natsrpc.NewEnvelope(err), Issues: issues})
|
natsrpc.Respond(msg, s.Log, "issuerpc", listResponse{Envelope: natsrpc.NewEnvelope(err), Issues: issues})
|
||||||
}},
|
}},
|
||||||
{Subject: createSubject, Handler: func(ctx context.Context, msg *nats.Msg) {
|
{Subject: createSubject, Handler: func(ctx context.Context, msg *nats.Msg) {
|
||||||
@@ -121,3 +124,32 @@ func (c Client) CreateIssue(ctx context.Context, owner, repo, title, body string
|
|||||||
}
|
}
|
||||||
return resp.Number, nil
|
return resp.Number, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// writerIssues keeps the issues opened by users with write access: anyone can
|
||||||
|
// open an issue on a public repository, and agents act on what they read. A
|
||||||
|
// forge that cannot answer fails the listing closed.
|
||||||
|
func writerIssues(ctx context.Context, log *slog.Logger, f forge.IssueForge, owner, repo string, issues []forge.Issue) ([]forge.Issue, error) {
|
||||||
|
checker, ok := f.(forge.WriteAccessChecker)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("forge cannot check write access")
|
||||||
|
}
|
||||||
|
known := map[string]bool{}
|
||||||
|
out := issues[:0:0]
|
||||||
|
for _, is := range issues {
|
||||||
|
allowed, seen := known[is.Author]
|
||||||
|
if !seen {
|
||||||
|
var err error
|
||||||
|
if allowed, err = checker.HasWriteAccess(ctx, owner, repo, is.Author); err != nil {
|
||||||
|
return nil, fmt.Errorf("check write access for %s: %w", is.Author, err)
|
||||||
|
}
|
||||||
|
known[is.Author] = allowed
|
||||||
|
}
|
||||||
|
if allowed {
|
||||||
|
out = append(out, is)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.WarnContext(ctx, "dropped issue from author without write access",
|
||||||
|
"repo", owner+"/"+repo, "issue", is.Number, "author", is.Author)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package issuerpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log/slog"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/forge"
|
||||||
|
)
|
||||||
|
|
||||||
|
type writerForge struct {
|
||||||
|
forge.IssueForge
|
||||||
|
writers map[string]bool
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f writerForge) HasWriteAccess(_ context.Context, _, _, user string) (bool, error) {
|
||||||
|
return f.writers[user], f.err
|
||||||
|
}
|
||||||
|
|
||||||
|
type plainForge struct{ forge.IssueForge }
|
||||||
|
|
||||||
|
func TestWriterIssues(t *testing.T) {
|
||||||
|
issues := []forge.Issue{
|
||||||
|
{Number: 1, Author: "maint"},
|
||||||
|
{Number: 2, Author: "stranger"},
|
||||||
|
{Number: 3, Author: "maint"},
|
||||||
|
{Number: 4},
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
forge forge.IssueForge
|
||||||
|
want []int
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"keeps collaborators only", writerForge{writers: map[string]bool{"maint": true}}, []int{1, 3}, false},
|
||||||
|
{"fails closed on forge error", writerForge{err: errors.New("down")}, nil, true},
|
||||||
|
{"fails closed without a checker", plainForge{}, nil, true},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := writerIssues(t.Context(), slog.New(slog.DiscardHandler), tt.forge, "o", "r", issues)
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
var nums []int
|
||||||
|
for _, is := range got {
|
||||||
|
nums = append(nums, is.Number)
|
||||||
|
}
|
||||||
|
if len(nums) != len(tt.want) {
|
||||||
|
t.Fatalf("got %v, want %v", nums, tt.want)
|
||||||
|
}
|
||||||
|
for i := range nums {
|
||||||
|
if nums[i] != tt.want[i] {
|
||||||
|
t.Fatalf("got %v, want %v", nums, tt.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -70,6 +70,7 @@ message Issue {
|
|||||||
string title = 2;
|
string title = 2;
|
||||||
string body = 3;
|
string body = 3;
|
||||||
repeated string labels = 4;
|
repeated string labels = 4;
|
||||||
|
string author = 5;
|
||||||
}
|
}
|
||||||
message AssignedIssuesResponse { repeated Issue issues = 1; }
|
message AssignedIssuesResponse { repeated Issue issues = 1; }
|
||||||
message IssuesWithLabelResponse { repeated Issue issues = 1; }
|
message IssuesWithLabelResponse { repeated Issue issues = 1; }
|
||||||
|
|||||||
Reference in new issue
Block a user