Files
kandev/apps/backend/internal/azuredevops/service.go
T
2026-08-02 17:08:22 +01:00

484 lines
15 KiB
Go

package azuredevops
import (
"context"
"errors"
"fmt"
"net/url"
"regexp"
"strings"
"sync"
"time"
"go.uber.org/zap"
"github.com/kandev/kandev/internal/common/logger"
"github.com/kandev/kandev/internal/events/bus"
"github.com/kandev/kandev/internal/watchreset"
)
var (
// ErrInvalidConfig identifies user-supplied connection settings that cannot
// be persisted or tested.
ErrInvalidConfig = errors.New("azure devops: invalid configuration")
// ErrInvalidWorkspaceID prevents operations from falling back across
// workspace boundaries when the caller omits its scope.
ErrInvalidWorkspaceID = errors.New("azure devops: workspace id is required")
organizationNameRE = regexp.MustCompile(`^[A-Za-z0-9](?:[A-Za-z0-9-]{0,48}[A-Za-z0-9])?$`)
)
const (
authProbeTimeout = 15 * time.Second
authHealthWriteTimeout = 5 * time.Second
)
// SecretStore is the encrypted secret-store surface used by this integration.
type SecretStore interface {
Reveal(ctx context.Context, id string) (string, error)
Set(ctx context.Context, id, name, value string) error
Delete(ctx context.Context, id string) error
Exists(ctx context.Context, id string) (bool, error)
}
// Service coordinates workspace configuration, encrypted PATs, and auth
// probes.
type Service struct {
store *Store
secrets SecretStore
clientFn ClientFactory
log *logger.Logger
mock *MockClient
repoLookup RepositoryLookup
watchRepositoryLookup WatchRepositoryLookup
watchDependencyValidator WatchDependencyValidator
// workspaceAuthorizer is wired to the task service when per-user auth is
// enabled. A nil authorizer preserves the unscoped local-development path.
workspaceAuthorizer func(context.Context, string) error
eventBus bus.EventBus
mu sync.RWMutex
cascadeTaskDeleter watchreset.TaskDeleter
taskSessionChecker TaskSessionChecker
}
// SetEventBus wires watcher events and preserves the nil-safe local test path.
func (s *Service) SetEventBus(eventBus bus.EventBus) {
if s != nil {
s.mu.Lock()
defer s.mu.Unlock()
s.eventBus = eventBus
}
}
// SetCascadeTaskDeleter wires the shared task-tree cleanup used by watcher
// reset and delete operations. A nil deleter keeps persistence usable in
// local tests while production still clears reservations safely.
func (s *Service) SetCascadeTaskDeleter(deleter watchreset.TaskDeleter) {
if s != nil {
s.mu.Lock()
defer s.mu.Unlock()
s.cascadeTaskDeleter = deleter
}
}
func (s *Service) getCascadeTaskDeleter() watchreset.TaskDeleter {
if s == nil {
return nil
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.cascadeTaskDeleter
}
// SetTaskSessionChecker wires the user-engagement check used by the "auto"
// watcher cleanup policy. It is optional for local tests; production wires
// the same task repository adapter used by the other integrations.
func (s *Service) SetTaskSessionChecker(checker TaskSessionChecker) {
if s != nil {
s.mu.Lock()
defer s.mu.Unlock()
s.taskSessionChecker = checker
}
}
func (s *Service) getTaskSessionChecker() TaskSessionChecker {
if s == nil {
return nil
}
s.mu.RLock()
defer s.mu.RUnlock()
return s.taskSessionChecker
}
// MockClient returns the E2E mock when the provider is in mock mode.
func (s *Service) MockClient() *MockClient { return s.mock }
// SetWorkspaceAuthorizer installs the per-user workspace access boundary for
// Azure DevOps configuration and provider operations.
func (s *Service) SetWorkspaceAuthorizer(authorizer func(context.Context, string) error) {
if s != nil {
s.workspaceAuthorizer = authorizer
}
}
func (s *Service) authorizeWorkspaceAccess(ctx context.Context, workspaceID string) error {
if s == nil || s.workspaceAuthorizer == nil {
return nil
}
return s.workspaceAuthorizer(ctx, workspaceID)
}
// NewService constructs the Azure DevOps configuration service.
func NewService(store *Store, secrets SecretStore, clientFn ClientFactory, log *logger.Logger) *Service {
if clientFn == nil {
clientFn = DefaultClientFactory
}
if log == nil {
log = logger.Default()
}
return &Service{store: store, secrets: secrets, clientFn: clientFn, log: log}
}
// Store exposes the configuration store to health-poller adapters.
func (s *Service) Store() *Store {
return s.store
}
// ValidateOrganizationURL accepts Azure DevOps Services organization URLs and
// returns the canonical form without a trailing slash.
func ValidateOrganizationURL(raw string) (string, error) {
normalized := strings.TrimSuffix(raw, "/")
parsed, err := url.Parse(normalized)
if err != nil || parsed.Scheme != "https" || parsed.Host != "dev.azure.com" {
return "", errors.New("organizationUrl must use https://dev.azure.com")
}
if parsed.User != nil || parsed.Port() != "" || parsed.RawQuery != "" || parsed.Fragment != "" || parsed.RawPath != "" {
return "", errors.New("organizationUrl must be a canonical organization URL")
}
organization := strings.TrimPrefix(parsed.Path, "/")
if parsed.Path != "/"+organization || !organizationNameRE.MatchString(organization) || parsed.String() != normalized {
return "", errors.New("organizationUrl must contain exactly one valid organization name")
}
return normalized, nil
}
// GetConfigForWorkspace returns a redacted workspace configuration.
func (s *Service) GetConfigForWorkspace(ctx context.Context, workspaceID string) (*Config, error) {
if err := validateWorkspaceID(workspaceID); err != nil {
return nil, err
}
if err := s.authorizeWorkspaceAccess(ctx, workspaceID); err != nil {
return nil, err
}
cfg, err := s.store.GetConfig(ctx, workspaceID)
if err != nil || cfg == nil || s.secrets == nil {
return cfg, err
}
cfg.HasSecret, err = s.secrets.Exists(ctx, SecretKeyForWorkspace(workspaceID))
if err != nil {
return nil, fmt.Errorf("check azure devops PAT: %w", err)
}
return cfg, nil
}
// SetConfigForWorkspace upserts a workspace connection. An empty PAT retains
// the existing encrypted credential.
func (s *Service) SetConfigForWorkspace(
ctx context.Context,
workspaceID string,
req *SetConfigRequest,
) (*Config, error) {
if err := validateWorkspaceID(workspaceID); err != nil {
return nil, err
}
if err := s.authorizeWorkspaceAccess(ctx, workspaceID); err != nil {
return nil, err
}
cfg, err := configFromRequest(workspaceID, req)
if err != nil {
return nil, err
}
previousConfig, err := s.store.GetConfig(ctx, workspaceID)
if err != nil {
return nil, fmt.Errorf("read existing azure devops config: %w", err)
}
organizationChanged := previousConfig != nil && previousConfig.OrganizationURL != cfg.OrganizationURL
if req.PAT == "" {
if err := s.requireStoredPAT(ctx, workspaceID); err != nil {
return nil, err
}
if err := s.store.UpsertConfig(ctx, cfg); err != nil {
return nil, fmt.Errorf("upsert azure devops config: %w", err)
}
return s.finishConfigUpdate(ctx, workspaceID, organizationChanged)
}
return s.setConfigWithPAT(ctx, workspaceID, cfg, req.PAT, organizationChanged)
}
func (s *Service) setConfigWithPAT(
ctx context.Context,
workspaceID string,
cfg *Config,
pat string,
organizationChanged bool,
) (*Config, error) {
if s.secrets == nil {
return nil, errors.New("azure devops: no secret store configured")
}
previous, err := s.readStoredPAT(ctx, workspaceID)
if err != nil {
return nil, err
}
if err := s.secrets.Set(ctx, SecretKeyForWorkspace(workspaceID), "Azure DevOps PAT", pat); err != nil {
return nil, joinMutationError(
fmt.Errorf("store azure devops PAT: %w", err),
s.restorePAT(ctx, workspaceID, previous),
)
}
if err := s.store.UpsertConfig(ctx, cfg); err != nil {
return nil, joinMutationError(
fmt.Errorf("upsert azure devops config: %w", err),
s.restorePAT(ctx, workspaceID, previous),
)
}
patChanged := !previous.exists || previous.value != pat
return s.finishConfigUpdate(ctx, workspaceID, organizationChanged || patChanged)
}
func (s *Service) finishConfigUpdate(
ctx context.Context,
workspaceID string,
credentialsChanged bool,
) (*Config, error) {
if credentialsChanged {
if err := s.store.ResetAuthHealth(ctx, workspaceID); err != nil {
return nil, fmt.Errorf("reset azure devops auth health: %w", err)
}
s.RecordAuthHealthForWorkspace(ctx, workspaceID)
}
return s.GetConfigForWorkspace(ctx, workspaceID)
}
// DeleteConfigForWorkspace removes both configuration and its encrypted PAT.
func (s *Service) DeleteConfigForWorkspace(ctx context.Context, workspaceID string) error {
if err := validateWorkspaceID(workspaceID); err != nil {
return err
}
if err := s.authorizeWorkspaceAccess(ctx, workspaceID); err != nil {
return err
}
previous, err := s.readStoredPAT(ctx, workspaceID)
if err != nil {
return err
}
if previous.exists {
if err := s.secrets.Delete(ctx, SecretKeyForWorkspace(workspaceID)); err != nil {
return joinMutationError(
fmt.Errorf("delete azure devops PAT: %w", err),
s.restorePAT(ctx, workspaceID, previous),
)
}
}
if err := s.store.DeleteConfig(ctx, workspaceID); err != nil {
return joinMutationError(err, s.restorePAT(ctx, workspaceID, previous))
}
return nil
}
type storedPAT struct {
value string
exists bool
}
func (s *Service) readStoredPAT(ctx context.Context, workspaceID string) (storedPAT, error) {
if s.secrets == nil {
return storedPAT{}, nil
}
key := SecretKeyForWorkspace(workspaceID)
exists, err := s.secrets.Exists(ctx, key)
if err != nil {
return storedPAT{}, fmt.Errorf("check azure devops PAT: %w", err)
}
if !exists {
return storedPAT{}, nil
}
value, err := s.secrets.Reveal(ctx, key)
if err != nil {
return storedPAT{}, fmt.Errorf("read azure devops PAT: %w", err)
}
return storedPAT{value: value, exists: true}, nil
}
func (s *Service) restorePAT(ctx context.Context, workspaceID string, previous storedPAT) error {
if s.secrets == nil {
return nil
}
key := SecretKeyForWorkspace(workspaceID)
if previous.exists {
return s.secrets.Set(ctx, key, "Azure DevOps PAT", previous.value)
}
return s.secrets.Delete(ctx, key)
}
func joinMutationError(actionErr, rollbackErr error) error {
if rollbackErr == nil {
return actionErr
}
return errors.Join(actionErr, fmt.Errorf("restore prior Azure DevOps state: %w", rollbackErr))
}
// TestConnectionForWorkspace probes submitted credentials without persisting
// them, or falls back to that workspace's stored config and PAT.
func (s *Service) TestConnectionForWorkspace(
ctx context.Context,
workspaceID string,
req *SetConfigRequest,
) (*TestConnectionResult, error) {
cfg, pat, err := s.resolveCredentials(ctx, workspaceID, req)
if err != nil {
return &TestConnectionResult{OK: false, Error: err.Error()}, nil
}
return s.clientFn(cfg, pat).TestAuth(ctx)
}
// ProbeAuthForWorkspace tests the workspace's stored credentials.
func (s *Service) ProbeAuthForWorkspace(ctx context.Context, workspaceID string) (*TestConnectionResult, error) {
return s.TestConnectionForWorkspace(ctx, workspaceID, &SetConfigRequest{})
}
// RecordAuthHealth probes every configured workspace and persists each result.
func (s *Service) RecordAuthHealth(ctx context.Context) {
workspaceIDs, err := s.store.ListConfigWorkspaceIDs(ctx)
if err != nil {
s.log.Warn("azure devops: list configured workspaces failed", zap.Error(err))
return
}
for _, workspaceID := range workspaceIDs {
s.RecordAuthHealthForWorkspace(ctx, workspaceID)
}
}
// RecordAuthHealthForWorkspace probes and persists one workspace's status.
func (s *Service) RecordAuthHealthForWorkspace(ctx context.Context, workspaceID string) {
probeCtx, cancel := context.WithTimeout(ctx, authProbeTimeout)
result, err := s.ProbeAuthForWorkspace(probeCtx, workspaceID)
cancel()
if ctx.Err() != nil {
return
}
ok, errMsg := authHealthResult(result, err)
writeCtx, writeCancel := context.WithTimeout(ctx, authHealthWriteTimeout)
defer writeCancel()
if err := s.store.UpdateAuthHealth(writeCtx, workspaceID, ok, errMsg, time.Now().UTC()); err != nil {
s.log.Warn("azure devops: update auth health failed", zap.Error(err))
}
}
func (s *Service) resolveCredentials(
ctx context.Context,
workspaceID string,
req *SetConfigRequest,
) (*Config, string, error) {
if err := validateWorkspaceID(workspaceID); err != nil {
return nil, "", err
}
if err := s.authorizeWorkspaceAccess(ctx, workspaceID); err != nil {
return nil, "", err
}
if req == nil {
req = &SetConfigRequest{}
}
var cfg *Config
var err error
if req.OrganizationURL != "" {
cfg, err = configFromRequest(workspaceID, req)
} else {
cfg, err = s.store.GetConfig(ctx, workspaceID)
if err == nil && cfg == nil {
err = ErrNotConfigured
}
}
if err != nil {
return nil, "", err
}
pat := req.PAT
if pat == "" {
pat, err = s.revealPAT(ctx, workspaceID)
if err != nil {
return nil, "", err
}
}
return cfg, pat, nil
}
func (s *Service) requireStoredPAT(ctx context.Context, workspaceID string) error {
if s.secrets == nil {
return errors.New("azure devops: no secret store configured")
}
exists, err := s.secrets.Exists(ctx, SecretKeyForWorkspace(workspaceID))
if err != nil {
return fmt.Errorf("check azure devops PAT: %w", err)
}
if !exists {
return fmt.Errorf("%w: pat required", ErrInvalidConfig)
}
return nil
}
func (s *Service) revealPAT(ctx context.Context, workspaceID string) (string, error) {
if s.secrets == nil {
return "", ErrNotConfigured
}
pat, err := s.secrets.Reveal(ctx, SecretKeyForWorkspace(workspaceID))
if err != nil || pat == "" {
if err != nil {
return "", fmt.Errorf("read azure devops PAT: %w", err)
}
return "", ErrNotConfigured
}
return pat, nil
}
func configFromRequest(workspaceID string, req *SetConfigRequest) (*Config, error) {
if req == nil {
return nil, fmt.Errorf("%w: request required", ErrInvalidConfig)
}
organizationURL, err := ValidateOrganizationURL(req.OrganizationURL)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrInvalidConfig, err.Error())
}
authMethod := req.AuthMethod
if authMethod == "" {
authMethod = AuthMethodPAT
}
if authMethod != AuthMethodPAT {
return nil, fmt.Errorf("%w: authMethod must be pat", ErrInvalidConfig)
}
return &Config{
WorkspaceID: workspaceID,
OrganizationURL: organizationURL,
DefaultProjectID: req.DefaultProjectID,
DefaultProjectName: req.DefaultProjectName,
AuthMethod: authMethod,
}, nil
}
func validateWorkspaceID(workspaceID string) error {
if strings.TrimSpace(workspaceID) == "" {
return ErrInvalidWorkspaceID
}
return nil
}
func authHealthResult(result *TestConnectionResult, err error) (bool, string) {
if err != nil {
return false, err.Error()
}
if result == nil {
return false, "azure devops: empty authentication response"
}
if !result.OK {
return false, result.Error
}
return true, ""
}