feat(repos): access check reports the operations the agent can perform
This commit is contained in:
1 parent
1410fbc565
commit
b25263308b
5 files changed
+146
-102
No files matched your search
@@ -59,10 +59,10 @@ func (s *repositoryServer) CheckRepository(ctx context.Context, r *pb.CheckRepos
|
||||
}
|
||||
return response, nil
|
||||
}
|
||||
return checkRepositoryAccess(ctx, resolved), nil
|
||||
return checkRepositoryAccess(ctx, resolved, r.IdentityId), nil
|
||||
}
|
||||
|
||||
func checkRepositoryAccess(ctx context.Context, resolved daemon.RepositoryAccess) *pb.CheckRepositoryResponse {
|
||||
func checkRepositoryAccess(ctx context.Context, resolved daemon.RepositoryAccess, identityID string) *pb.CheckRepositoryResponse {
|
||||
response := &pb.CheckRepositoryResponse{Connection: resolved.Repository.Connection, BaseBranch: resolved.Repository.BaseBranch(), Operation: config.OperationClone}
|
||||
response.Connection = resolved.Repository.Connection
|
||||
response.BaseBranch = resolved.Repository.BaseBranch()
|
||||
@@ -93,53 +93,81 @@ func checkRepositoryAccess(ctx context.Context, resolved daemon.RepositoryAccess
|
||||
return response
|
||||
}
|
||||
response.Verified = true
|
||||
response.Message = "Account, repository and base branch verified. No changes were made."
|
||||
response.Operations = []string{config.OperationClone}
|
||||
canWrite, err := repositoryWritable(ctx, resolved.Connection.Type, resolved.Trees.BaseURL, resolved.Trees.Token, resolved.Repository.Owner, resolved.Repository.Name)
|
||||
if err != nil {
|
||||
response.Code = "write_unverified"
|
||||
response.Message = "Write access could not be checked."
|
||||
return response
|
||||
}
|
||||
for _, op := range []string{config.OperationPush, config.OperationPR} {
|
||||
if canWrite && resolved.Connection.Permitted(identityID, op) {
|
||||
response.Operations = append(response.Operations, op)
|
||||
}
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
// Account discovery is read-only and never follows redirects with credentials.
|
||||
func connectionAccount(ctx context.Context, typ, host, token string) (string, error) {
|
||||
var account struct {
|
||||
Login string `json:"login"`
|
||||
}
|
||||
if err := forgeGet(ctx, typ, host, token, "user", &account); err != nil || account.Login == "" {
|
||||
return "", errors.New("forge account unavailable")
|
||||
}
|
||||
return account.Login, nil
|
||||
}
|
||||
|
||||
// repositoryWritable reports whether the credential can push to the repository.
|
||||
func repositoryWritable(ctx context.Context, typ, host, token, owner, repo string) (bool, error) {
|
||||
var repository struct {
|
||||
Permissions struct {
|
||||
Push bool `json:"push"`
|
||||
} `json:"permissions"`
|
||||
}
|
||||
if err := forgeGet(ctx, typ, host, token, "repos/"+url.PathEscape(owner)+"/"+url.PathEscape(repo), &repository); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return repository.Permissions.Push, nil
|
||||
}
|
||||
|
||||
func forgeGet(ctx context.Context, typ, host, token, path string, out any) error {
|
||||
endpoint, err := url.Parse(host)
|
||||
if err != nil || endpoint.Host == "" || endpoint.User != nil {
|
||||
return "", errors.New("invalid forge host")
|
||||
return errors.New("invalid forge host")
|
||||
}
|
||||
authorization := "Bearer " + token
|
||||
switch typ {
|
||||
case "github":
|
||||
if endpoint.Hostname() == "github.com" {
|
||||
endpoint.Host = "api.github.com"
|
||||
endpoint.Path = "/user"
|
||||
endpoint.Path = "/" + path
|
||||
} else {
|
||||
endpoint.Path = strings.TrimRight(endpoint.Path, "/") + "/api/v3/user"
|
||||
endpoint.Path = strings.TrimRight(endpoint.Path, "/") + "/api/v3/" + path
|
||||
}
|
||||
case "gitea":
|
||||
authorization = "token " + token
|
||||
endpoint.Path = strings.TrimRight(endpoint.Path, "/") + "/api/v1/user"
|
||||
endpoint.Path = strings.TrimRight(endpoint.Path, "/") + "/api/v1/" + path
|
||||
default:
|
||||
return "", errors.New("account checks are unavailable for this forge")
|
||||
return errors.New("access checks are unavailable for this forge")
|
||||
}
|
||||
endpoint.RawQuery = ""
|
||||
endpoint.Fragment = ""
|
||||
request, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Authorization", authorization)
|
||||
request.Header.Set("Accept", "application/json")
|
||||
client := &http.Client{Timeout: 15 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
return "", errors.New("forge account unavailable")
|
||||
return errors.New("forge unavailable")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return "", errors.New("forge account refused")
|
||||
return errors.New("forge refused")
|
||||
}
|
||||
var account struct {
|
||||
Login string `json:"login"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 64<<10)).Decode(&account); err != nil || account.Login == "" {
|
||||
return "", errors.New("forge account unavailable")
|
||||
}
|
||||
return account.Login, nil
|
||||
return json.NewDecoder(io.LimitReader(response.Body, 256<<10)).Decode(out)
|
||||
}
|
||||
@@ -82,14 +82,17 @@ func (x *CheckRepositoryRequest) GetIdentityId() string {
|
||||
}
|
||||
|
||||
type CheckRepositoryResponse struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Verified bool `protobuf:"varint,1,opt,name=verified,proto3" json:"verified,omitempty"`
|
||||
Connection string `protobuf:"bytes,2,opt,name=connection,proto3" json:"connection,omitempty"`
|
||||
Operation string `protobuf:"bytes,3,opt,name=operation,proto3" json:"operation,omitempty"`
|
||||
Account string `protobuf:"bytes,4,opt,name=account,proto3" json:"account,omitempty"`
|
||||
BaseBranch string `protobuf:"bytes,5,opt,name=base_branch,json=baseBranch,proto3" json:"base_branch,omitempty"`
|
||||
Code string `protobuf:"bytes,6,opt,name=code,proto3" json:"code,omitempty"`
|
||||
Message string `protobuf:"bytes,7,opt,name=message,proto3" json:"message,omitempty"`
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Verified bool `protobuf:"varint,1,opt,name=verified,proto3" json:"verified,omitempty"`
|
||||
Connection string `protobuf:"bytes,2,opt,name=connection,proto3" json:"connection,omitempty"`
|
||||
Operation string `protobuf:"bytes,3,opt,name=operation,proto3" json:"operation,omitempty"`
|
||||
Account string `protobuf:"bytes,4,opt,name=account,proto3" json:"account,omitempty"`
|
||||
BaseBranch string `protobuf:"bytes,5,opt,name=base_branch,json=baseBranch,proto3" json:"base_branch,omitempty"`
|
||||
Code string `protobuf:"bytes,6,opt,name=code,proto3" json:"code,omitempty"`
|
||||
Message string `protobuf:"bytes,7,opt,name=message,proto3" json:"message,omitempty"`
|
||||
// Operations the identity can perform: clone was read, push and pr are
|
||||
// permitted and the credential can write.
|
||||
Operations []string `protobuf:"bytes,8,rep,name=operations,proto3" json:"operations,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
@@ -173,6 +176,13 @@ func (x *CheckRepositoryResponse) GetMessage() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *CheckRepositoryResponse) GetOperations() []string {
|
||||
if x != nil {
|
||||
return x.Operations
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_daemon_v1_daemon_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_daemon_v1_daemon_proto_rawDesc = "" +
|
||||
@@ -184,7 +194,7 @@ const file_daemon_v1_daemon_proto_rawDesc = "" +
|
||||
"repository\x18\x02 \x01(\tR\n" +
|
||||
"repository\x12\x1f\n" +
|
||||
"\videntity_id\x18\x03 \x01(\tR\n" +
|
||||
"identityId\"\xdc\x01\n" +
|
||||
"identityId\"\xfc\x01\n" +
|
||||
"\x17CheckRepositoryResponse\x12\x1a\n" +
|
||||
"\bverified\x18\x01 \x01(\bR\bverified\x12\x1e\n" +
|
||||
"\n" +
|
||||
@@ -195,7 +205,10 @@ const file_daemon_v1_daemon_proto_rawDesc = "" +
|
||||
"\vbase_branch\x18\x05 \x01(\tR\n" +
|
||||
"baseBranch\x12\x12\n" +
|
||||
"\x04code\x18\x06 \x01(\tR\x04code\x12\x18\n" +
|
||||
"\amessage\x18\a \x01(\tR\amessage2i\n" +
|
||||
"\amessage\x18\a \x01(\tR\amessage\x12\x1e\n" +
|
||||
"\n" +
|
||||
"operations\x18\b \x03(\tR\n" +
|
||||
"operations2i\n" +
|
||||
"\rDaemonService\x12X\n" +
|
||||
"\x0fCheckRepository\x12!.daemon.v1.CheckRepositoryRequest\x1a\".daemon.v1.CheckRepositoryResponseB\xac\x01\n" +
|
||||
"\rcom.daemon.v1B\vDaemonProtoP\x01ZIgithub.com/samcharles93/archie-core/internal/contracts/daemon/v1;daemonv1\xa2\x02\x03DXX\xaa\x02\tDaemon.V1\xca\x02\tDaemon\\V1\xe2\x02\x15Daemon\\V1\\GPBMetadata\xea\x02\n" +
|
||||
|
||||
Reference in new issue
Block a user