feat(plugins): add source-control extension contracts (#2117)
This commit is contained in:
@@ -24,6 +24,7 @@ type githubBrokerResolveRequest struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
Host string `json:"host"`
|
||||
Path string `json:"path,omitempty"`
|
||||
}
|
||||
|
||||
type githubBrokerCredential struct {
|
||||
@@ -154,9 +155,11 @@ func runGitHubCredentialHelper(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := validateGitCredentialScope(input, client.request); err != nil {
|
||||
request, err := gitCredentialRequestForInput(input, client.request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
client.request = request
|
||||
credential, err := client.resolve(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -179,8 +182,9 @@ func newGitHubCredentialBrokerClientForInput(
|
||||
return nil, fmt.Errorf("GitHub credential scopes are invalid: %w", err)
|
||||
}
|
||||
for _, scope := range scopes {
|
||||
if validateGitCredentialScope(input, scope) == nil {
|
||||
return newGitHubCredentialBrokerClientForRequest(getenv, httpClient, scope)
|
||||
request, err := gitCredentialRequestForInput(input, scope)
|
||||
if err == nil {
|
||||
return newGitHubCredentialBrokerClientForRequest(getenv, httpClient, request)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("git repository does not match any credential lease scope")
|
||||
@@ -230,22 +234,40 @@ func readGitCredentialInput(input io.Reader) (map[string]string, error) {
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func validateGitCredentialScope(input map[string]string, scope githubBrokerResolveRequest) error {
|
||||
// gitCredentialRequestForInput validates the Git credential helper request
|
||||
// against its issued lease scope, then carries the exact requested repository
|
||||
// path through to the broker. Git uses owner/repo fields on this compatibility
|
||||
// endpoint, so Repo retains any provider namespace after the first segment.
|
||||
func gitCredentialRequestForInput(input map[string]string, scope githubBrokerResolveRequest) (githubBrokerResolveRequest, error) {
|
||||
protocol := strings.TrimSpace(input["protocol"])
|
||||
if !strings.EqualFold(protocol, "https") {
|
||||
return fmt.Errorf("git credential protocol %q is not supported", protocol)
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("git credential protocol %q is not supported", protocol)
|
||||
}
|
||||
host := strings.TrimSpace(input["host"])
|
||||
if host == "" || !strings.EqualFold(host, scope.Host) {
|
||||
return fmt.Errorf("git credential host does not match credential lease scope")
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("git credential host does not match credential lease scope")
|
||||
}
|
||||
path, err := url.PathUnescape(strings.Trim(input["path"], "/"))
|
||||
path, err := url.PathUnescape(strings.TrimSpace(input["path"]))
|
||||
if err != nil {
|
||||
return fmt.Errorf("decode git credential path: %w", err)
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("decode git credential path: %w", err)
|
||||
}
|
||||
path = strings.TrimSuffix(path, ".git")
|
||||
if path == "" || !strings.EqualFold(path, scope.Owner+"/"+scope.Repo) {
|
||||
return fmt.Errorf("git repository does not match credential lease scope")
|
||||
path = "/" + strings.TrimLeft(path, "/")
|
||||
if path == "/" {
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("git repository does not match credential lease scope")
|
||||
}
|
||||
return nil
|
||||
if scope.Path != "" {
|
||||
if path != scope.Path {
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("git repository does not match credential lease scope")
|
||||
}
|
||||
} else {
|
||||
legacyPath := strings.TrimSuffix(strings.Trim(path, "/"), ".git")
|
||||
owner, repo, found := strings.Cut(legacyPath, "/")
|
||||
if !found || owner == "" || repo == "" || legacyPath != scope.Owner+"/"+scope.Repo {
|
||||
return githubBrokerResolveRequest{}, fmt.Errorf("git repository does not match credential lease scope")
|
||||
}
|
||||
scope.Owner = owner
|
||||
scope.Repo = repo
|
||||
}
|
||||
scope.Path = path
|
||||
return scope, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user