fix: default new workspaces to host GitHub access (#2156)
This commit is contained in:
@@ -135,6 +135,8 @@ type Service struct {
|
||||
ghAccountLister func(context.Context) ([]GHAccount, error)
|
||||
mockAuth *MockAuthState
|
||||
workspaceAuthorizer func(context.Context, string) error
|
||||
freshDefaultsMu sync.Mutex
|
||||
freshDefaultsDone bool
|
||||
|
||||
// cleanupFailureMu guards cleanupFailureCounts; the cleanup loop is the
|
||||
// only writer but the global sweep + per-watch sweep can run concurrently
|
||||
|
||||
@@ -95,10 +95,10 @@ type connectionSecretIDLister interface {
|
||||
ListIDs(context.Context) ([]string, error)
|
||||
}
|
||||
|
||||
// handleWorkspaceDeleted removes encrypted GitHub credentials after the
|
||||
// workspace-owned metadata rows have been deleted by the database cascade.
|
||||
// Secret IDs are namespaced, so future multi-user connections are covered
|
||||
// without reading deleted user-connection rows.
|
||||
// handleWorkspaceDeleted removes workspace-owned GitHub metadata and encrypted
|
||||
// credentials after the task repository deletes the workspace row. Secret IDs
|
||||
// are namespaced, so future multi-user connections are covered without reading
|
||||
// deleted user-connection rows.
|
||||
func (s *Service) handleWorkspaceDeleted(ctx context.Context, event *bus.Event) error {
|
||||
workspaceID := workspaceIDFromEvent(event)
|
||||
if workspaceID == "" {
|
||||
@@ -110,6 +110,11 @@ func (s *Service) handleWorkspaceDeleted(ctx context.Context, event *bus.Event)
|
||||
if broker != nil {
|
||||
broker.RevokeWorkspace(workspaceID)
|
||||
}
|
||||
if s.store != nil {
|
||||
if err := s.store.DeleteWorkspaceSettings(ctx, workspaceID); err != nil {
|
||||
return fmt.Errorf("delete GitHub workspace settings: %w", err)
|
||||
}
|
||||
}
|
||||
if s.connectionSecrets == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -152,15 +152,30 @@ func TestHandleTaskEvents_MalformedPayloadIsNoop(t *testing.T) {
|
||||
|
||||
func TestHandleWorkspaceDeletedRemovesOnlyOwnedConnectionSecrets(t *testing.T) {
|
||||
svc, secrets := newWorkspaceConnectionService(t, "octocat")
|
||||
ctx := context.Background()
|
||||
if err := svc.store.UpsertWorkspaceSettings(ctx, &WorkspaceSettings{
|
||||
WorkspaceID: "ws-1",
|
||||
TaskGitCredentialsMode: TaskGitCredentialsModeExecutor,
|
||||
}); err != nil {
|
||||
t.Fatalf("seed workspace settings: %v", err)
|
||||
}
|
||||
secrets.values[WorkspacePATSecretKey("ws-1")] = "pat"
|
||||
secrets.values[UserAccessTokenSecretKey("ws-1", "user-1")] = "access"
|
||||
secrets.values[UserRefreshTokenSecretKey("ws-1", "user-1")] = "refresh"
|
||||
secrets.values[WorkspacePATSecretKey("ws-2")] = "other"
|
||||
|
||||
event := bus.NewEvent(events.WorkspaceDeleted, "task-service", map[string]interface{}{"id": "ws-1"})
|
||||
if err := svc.handleWorkspaceDeleted(context.Background(), event); err != nil {
|
||||
if err := svc.handleWorkspaceDeleted(ctx, event); err != nil {
|
||||
t.Fatalf("handleWorkspaceDeleted: %v", err)
|
||||
}
|
||||
var settingsCount int
|
||||
if err := svc.store.db.GetContext(ctx, &settingsCount,
|
||||
`SELECT COUNT(1) FROM github_workspace_settings WHERE workspace_id = ?`, "ws-1"); err != nil {
|
||||
t.Fatalf("count workspace settings: %v", err)
|
||||
}
|
||||
if settingsCount != 0 {
|
||||
t.Fatalf("workspace settings rows = %d, want 0", settingsCount)
|
||||
}
|
||||
for _, id := range []string{
|
||||
WorkspacePATSecretKey("ws-1"),
|
||||
UserAccessTokenSecretKey("ws-1", "user-1"),
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
type Store struct {
|
||||
db *sqlx.DB // writer
|
||||
ro *sqlx.DB // reader
|
||||
freshInstall bool
|
||||
deploymentAppPersistenceMu sync.Mutex
|
||||
appLifecycleLocksMu sync.Mutex
|
||||
appLifecycleLocks map[string]*appRegistrationLifecycleLock
|
||||
@@ -43,6 +44,8 @@ func NewStore(writer, reader *sqlx.DB) (*Store, error) {
|
||||
db: writer, ro: reader,
|
||||
appLifecycleLocks: make(map[string]*appRegistrationLifecycleLock),
|
||||
}
|
||||
s.freshInstall = !s.tableExists("github_workspace_settings") &&
|
||||
!s.tableExists("github_workspace_connections")
|
||||
legacyUpgrade := s.tableExists("github_workspace_settings") &&
|
||||
!s.tableExists("github_workspace_connections")
|
||||
if err := s.initSchema(legacyUpgrade); err != nil {
|
||||
@@ -2972,6 +2975,49 @@ func (s *Store) GetWorkspaceSettings(ctx context.Context, workspaceID string) (*
|
||||
return normalizeWorkspaceSettings(settings), nil
|
||||
}
|
||||
|
||||
// EnsureWorkspaceExecutorDefaults creates the initial task Git policy without
|
||||
// rewriting any settings that already exist. This is intentionally separate
|
||||
// from GetWorkspaceSettings' managed fallback so upgrades do not change the
|
||||
// behavior of existing workspaces that have never stored a settings row.
|
||||
func (s *Store) EnsureWorkspaceExecutorDefaults(ctx context.Context, workspaceID string) error {
|
||||
workspaceID = strings.TrimSpace(workspaceID)
|
||||
if workspaceID == "" {
|
||||
return fmt.Errorf("workspace_id is required")
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT OR IGNORE INTO github_workspace_settings (
|
||||
workspace_id, task_git_credentials_mode, repo_scope_mode, repo_scope_orgs, repo_scope_repos,
|
||||
saved_presets, default_query_presets, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
workspaceID, TaskGitCredentialsModeExecutor, RepoScopeModeAll, "[]", "[]", "[]", nil, now, now)
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteWorkspaceSettings removes the non-secret GitHub settings owned by a
|
||||
// workspace after the task repository has deleted the workspace row.
|
||||
func (s *Store) DeleteWorkspaceSettings(ctx context.Context, workspaceID string) error {
|
||||
workspaceID = strings.TrimSpace(workspaceID)
|
||||
if workspaceID == "" {
|
||||
return fmt.Errorf("workspace_id is required")
|
||||
}
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
`DELETE FROM github_workspace_settings WHERE workspace_id = ?`, workspaceID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) listWorkspaceIDs(ctx context.Context) ([]string, error) {
|
||||
if !s.tableExists("workspaces") {
|
||||
return nil, nil
|
||||
}
|
||||
var ids []string
|
||||
if err := s.ro.SelectContext(ctx, &ids, `
|
||||
SELECT id FROM workspaces WHERE TRIM(id) <> '' ORDER BY id`); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
// UpsertWorkspaceSettings stores per-workspace GitHub settings.
|
||||
func (s *Store) UpsertWorkspaceSettings(ctx context.Context, settings *WorkspaceSettings) error {
|
||||
settings = normalizeWorkspaceSettings(settings)
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var errWorkspaceDefaultsUnavailable = errors.New("workspace defaults unavailable")
|
||||
|
||||
const (
|
||||
workspaceDefaultsCLIValidationTimeout = 5 * time.Second
|
||||
freshWorkspaceDefaultsTimeout = 30 * time.Second
|
||||
)
|
||||
|
||||
// InitializeWorkspaceDefaults persists the task Git policy for a newly
|
||||
// created workspace and, when the caller is allowed to use the deployment
|
||||
// host identity, snapshots the active gh CLI account as a named connection.
|
||||
// Account discovery and validation are deliberately best-effort: a fresh
|
||||
// install without an authenticated gh CLI still gets executor credential
|
||||
// inheritance and can be configured later from workspace settings.
|
||||
func (s *Service) InitializeWorkspaceDefaults(ctx context.Context, workspaceID string) error {
|
||||
if s == nil || s.store == nil {
|
||||
return errWorkspaceDefaultsUnavailable
|
||||
}
|
||||
workspaceID = strings.TrimSpace(workspaceID)
|
||||
if workspaceID == "" {
|
||||
return fmt.Errorf("workspace_id is required")
|
||||
}
|
||||
if err := s.authorizeWorkspaceAccess(ctx, workspaceID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.store.EnsureWorkspaceExecutorDefaults(ctx, workspaceID); err != nil {
|
||||
return fmt.Errorf("persist workspace GitHub defaults: %w", err)
|
||||
}
|
||||
connection, err := s.store.GetWorkspaceConnection(ctx, workspaceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load existing workspace GitHub connection: %w", err)
|
||||
}
|
||||
if connection != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// An authenticated member may create a workspace, but must not receive the
|
||||
// operator's host gh identity. Keep the authorization boundary before any
|
||||
// account listing so member requests cannot observe operator accounts.
|
||||
if err := requireGHCLIOperator(ctx); err != nil {
|
||||
if errors.Is(err, ErrGHCLIOperatorRequired) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
accounts, err := s.ListGHAccounts(ctx)
|
||||
if err != nil {
|
||||
s.logWorkspaceDefaultsFallback("unable to discover host gh account", workspaceID, err)
|
||||
return nil
|
||||
}
|
||||
account, ok := activeGHAccount(accounts)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if s.connectionSecrets == nil {
|
||||
s.logWorkspaceDefaultsFallback("connection secret store is unavailable", workspaceID, errWorkspaceDefaultsUnavailable)
|
||||
return nil
|
||||
}
|
||||
bindCtx, cancel := context.WithTimeout(ctx, workspaceDefaultsCLIValidationTimeout)
|
||||
defer cancel()
|
||||
if _, err := s.SetWorkspaceConnection(bindCtx, workspaceID, SetWorkspaceConnectionRequest{
|
||||
Source: ConnectionSourceGHCLI,
|
||||
Host: account.Host,
|
||||
Login: account.Login,
|
||||
}); err != nil {
|
||||
s.logWorkspaceDefaultsFallback("unable to bind host gh account", workspaceID, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitializeFreshWorkspaceDefaults handles the repository-seeded initial
|
||||
// workspace. The store records whether its GitHub schema was absent before
|
||||
// startup; existing installations are intentionally left untouched.
|
||||
func (s *Service) InitializeFreshWorkspaceDefaults(ctx context.Context) error {
|
||||
if s == nil || s.store == nil || !s.store.freshInstall {
|
||||
return nil
|
||||
}
|
||||
if _, hasDeadline := ctx.Deadline(); !hasDeadline {
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel = context.WithTimeout(ctx, freshWorkspaceDefaultsTimeout)
|
||||
defer cancel()
|
||||
}
|
||||
s.freshDefaultsMu.Lock()
|
||||
defer s.freshDefaultsMu.Unlock()
|
||||
if s.freshDefaultsDone {
|
||||
return nil
|
||||
}
|
||||
|
||||
workspaceIDs, err := s.store.listWorkspaceIDs(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("list initial workspaces: %w", err)
|
||||
}
|
||||
for _, workspaceID := range workspaceIDs {
|
||||
if err := s.InitializeWorkspaceDefaults(ctx, workspaceID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
s.freshDefaultsDone = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func activeGHAccount(accounts []GHAccount) (GHAccount, bool) {
|
||||
// Prefer the canonical GitHub.com account when gh reports multiple active
|
||||
// hosts. Other active hosts are passed through the existing connection
|
||||
// validation path, where unsupported hosts degrade to disconnected access.
|
||||
for _, account := range accounts {
|
||||
if validActiveGHAccount(account) && strings.EqualFold(strings.TrimSpace(account.Host), defaultGitHubHost) {
|
||||
account.Host = strings.TrimSpace(account.Host)
|
||||
account.Login = strings.TrimSpace(account.Login)
|
||||
return account, true
|
||||
}
|
||||
}
|
||||
for _, account := range accounts {
|
||||
if validActiveGHAccount(account) {
|
||||
account.Host = strings.TrimSpace(account.Host)
|
||||
account.Login = strings.TrimSpace(account.Login)
|
||||
return account, true
|
||||
}
|
||||
}
|
||||
return GHAccount{}, false
|
||||
}
|
||||
|
||||
func validActiveGHAccount(account GHAccount) bool {
|
||||
if !account.Active || strings.TrimSpace(account.Host) == "" || strings.TrimSpace(account.Login) == "" {
|
||||
return false
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(account.State)) {
|
||||
case "active", checkStatusSuccess:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) logWorkspaceDefaultsFallback(message, workspaceID string, err error) {
|
||||
if s == nil || s.logger == nil {
|
||||
return
|
||||
}
|
||||
s.logger.Warn(message, zap.String("workspace_id", workspaceID), zap.Error(err))
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/kandev/kandev/internal/auth/authn"
|
||||
)
|
||||
|
||||
func TestInitializeWorkspaceDefaultsPersistsExecutorAndBindsActiveCLI(t *testing.T) {
|
||||
service, secrets := newWorkspaceConnectionService(t, "bob-cli")
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{
|
||||
{Host: "github.com", Login: "bob-cli", Active: true, State: "active"},
|
||||
}, nil
|
||||
}
|
||||
service.resolver.ghToken = func(context.Context, string, string) (string, error) {
|
||||
return "mock-gh-token", nil
|
||||
}
|
||||
|
||||
if err := service.InitializeWorkspaceDefaults(context.Background(), "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults: %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeExecutor {
|
||||
t.Fatalf("task Git mode = %q, want executor", settings.TaskGitCredentialsMode)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection == nil || connection.Source != ConnectionSourceGHCLI || connection.GitHubHost != "github.com" || connection.Login != "bob-cli" {
|
||||
t.Fatalf("connection = %+v, want exact active CLI account", connection)
|
||||
}
|
||||
if len(secrets.values) != 0 {
|
||||
t.Fatalf("workspace CLI token was persisted in secrets: %#v", secrets.values)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeWorkspaceDefaultsBoundsCLIValidationContext(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "active"}}, nil
|
||||
}
|
||||
var hasDeadline bool
|
||||
service.resolver.ghToken = func(ctx context.Context, _, _ string) (string, error) {
|
||||
_, hasDeadline = ctx.Deadline()
|
||||
return "mock-gh-token", nil
|
||||
}
|
||||
|
||||
if err := service.InitializeWorkspaceDefaults(context.Background(), "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults: %v", err)
|
||||
}
|
||||
if !hasDeadline {
|
||||
t.Fatal("CLI validation context has no deadline")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeWorkspaceDefaultsDoesNotBindOperatorCLIForMember(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
listCalls := 0
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
listCalls++
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "active"}}, nil
|
||||
}
|
||||
|
||||
memberCtx := authn.WithIdentity(context.Background(), authn.Identity{
|
||||
UserID: "member-1",
|
||||
Role: authn.RoleMember,
|
||||
})
|
||||
if err := service.InitializeWorkspaceDefaults(memberCtx, "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults(member): %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeExecutor {
|
||||
t.Fatalf("task Git mode = %q, want executor", settings.TaskGitCredentialsMode)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection != nil {
|
||||
t.Fatalf("member-created workspace inherited operator connection: %+v", connection)
|
||||
}
|
||||
if listCalls != 0 {
|
||||
t.Fatalf("CLI accounts listed %d times for member-created workspace, want 0", listCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeWorkspaceDefaultsSoftFailsWhenCLIUnavailable(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return nil, errors.New("gh is not authenticated")
|
||||
}
|
||||
|
||||
if err := service.InitializeWorkspaceDefaults(context.Background(), "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults: %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeExecutor {
|
||||
t.Fatalf("task Git mode = %q, want executor", settings.TaskGitCredentialsMode)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection != nil {
|
||||
t.Fatalf("unavailable CLI created a connection: %+v", connection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeWorkspaceDefaultsIgnoresInvalidActiveCLI(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "unexpected"}}, nil
|
||||
}
|
||||
service.resolver.ghToken = func(context.Context, string, string) (string, error) {
|
||||
return "mock-gh-token", nil
|
||||
}
|
||||
|
||||
if err := service.InitializeWorkspaceDefaults(context.Background(), "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults: %v", err)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection != nil {
|
||||
t.Fatalf("invalid active CLI account created a connection: %+v", connection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeWorkspaceDefaultsPreservesExistingWorkspaceState(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
ctx := context.Background()
|
||||
if err := service.store.UpsertWorkspaceSettings(ctx, &WorkspaceSettings{
|
||||
WorkspaceID: "ws-1",
|
||||
TaskGitCredentialsMode: TaskGitCredentialsModeManaged,
|
||||
RepoScopeMode: RepoScopeModeRepos,
|
||||
}); err != nil {
|
||||
t.Fatalf("UpsertWorkspaceSettings: %v", err)
|
||||
}
|
||||
if err := service.store.UpsertWorkspaceConnection(ctx, &WorkspaceConnection{
|
||||
WorkspaceID: "ws-1",
|
||||
Source: ConnectionSourcePAT,
|
||||
GitHubHost: defaultGitHubHost,
|
||||
Login: "existing-user",
|
||||
Status: ConnectionStatusActive,
|
||||
CredentialGeneration: 1,
|
||||
}); err != nil {
|
||||
t.Fatalf("UpsertWorkspaceConnection: %v", err)
|
||||
}
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "active"}}, nil
|
||||
}
|
||||
service.resolver.ghToken = func(context.Context, string, string) (string, error) {
|
||||
return "mock-gh-token", nil
|
||||
}
|
||||
|
||||
if err := service.InitializeWorkspaceDefaults(ctx, "ws-1"); err != nil {
|
||||
t.Fatalf("InitializeWorkspaceDefaults: %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(ctx, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeManaged || settings.RepoScopeMode != RepoScopeModeRepos {
|
||||
t.Fatalf("existing settings changed: %+v", settings)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(ctx, "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection == nil || connection.Source != ConnectionSourcePAT || connection.Login != "existing-user" {
|
||||
t.Fatalf("existing connection changed: %+v", connection)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeFreshWorkspaceDefaultsSeedsExistingInitialWorkspace(t *testing.T) {
|
||||
service, secrets := newWorkspaceConnectionService(t, "operator")
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "active"}}, nil
|
||||
}
|
||||
service.resolver.ghToken = func(context.Context, string, string) (string, error) {
|
||||
return "mock-gh-token", nil
|
||||
}
|
||||
if err := service.InitializeFreshWorkspaceDefaults(context.Background()); err != nil {
|
||||
t.Fatalf("InitializeFreshWorkspaceDefaults: %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeExecutor {
|
||||
t.Fatalf("task Git mode = %q, want executor", settings.TaskGitCredentialsMode)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection == nil || connection.Source != ConnectionSourceGHCLI || connection.Login != "operator" {
|
||||
t.Fatalf("connection = %+v, want initial active CLI account", connection)
|
||||
}
|
||||
if len(secrets.values) != 0 {
|
||||
t.Fatalf("initial workspace CLI token was persisted: %#v", secrets.values)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeFreshWorkspaceDefaultsBoundsStartupContext(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
service.ghAccountLister = func(ctx context.Context) ([]GHAccount, error) {
|
||||
if _, ok := ctx.Deadline(); !ok {
|
||||
t.Error("fresh workspace defaults context has no deadline")
|
||||
}
|
||||
return nil, errors.New("gh is not authenticated")
|
||||
}
|
||||
|
||||
if err := service.InitializeFreshWorkspaceDefaults(context.Background()); err != nil {
|
||||
t.Fatalf("InitializeFreshWorkspaceDefaults: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeFreshWorkspaceDefaultsSkipsExistingGitHubStore(t *testing.T) {
|
||||
service, _ := newWorkspaceConnectionService(t, "operator")
|
||||
service.store.freshInstall = false
|
||||
service.ghAccountLister = func(context.Context) ([]GHAccount, error) {
|
||||
return []GHAccount{{Host: "github.com", Login: "operator", Active: true, State: "active"}}, nil
|
||||
}
|
||||
|
||||
if err := service.InitializeFreshWorkspaceDefaults(context.Background()); err != nil {
|
||||
t.Fatalf("InitializeFreshWorkspaceDefaults: %v", err)
|
||||
}
|
||||
settings, err := service.store.GetWorkspaceSettings(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceSettings: %v", err)
|
||||
}
|
||||
if settings.TaskGitCredentialsMode != TaskGitCredentialsModeManaged {
|
||||
t.Fatalf("existing store task Git mode = %q, want managed fallback", settings.TaskGitCredentialsMode)
|
||||
}
|
||||
connection, err := service.store.GetWorkspaceConnection(context.Background(), "ws-1")
|
||||
if err != nil {
|
||||
t.Fatalf("GetWorkspaceConnection: %v", err)
|
||||
}
|
||||
if connection != nil {
|
||||
t.Fatalf("existing store was rebound to host CLI: %+v", connection)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user