Skip to content

Modern Docker Management, Designed for Everyone

fix: allow defining git identity for pushes (#3935)

Kyle Mendell372f16894070
diff --git a/backend/go.mod b/backend/go.mod
index ba1dd96c1..f3932184d 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -13,6 +13,7 @@ replace (
require (
emperror.dev/emperror v0.33.0
emperror.dev/errors v0.8.1
+ github.com/ProtonMail/go-crypto v1.4.1
github.com/anthdm/hollywood v1.0.5
github.com/aws/aws-sdk-go-v2 v1.47.0
github.com/aws/aws-sdk-go-v2/config v1.33.4
@@ -104,7 +105,6 @@ require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/semver/v3 v3.5.0 // indirect
github.com/Microsoft/go-winio v0.6.3-0.20251027160822-ad3df93bed29 // indirect
- github.com/ProtonMail/go-crypto v1.4.1 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/aquasecurity/go-pep440-version v0.0.2-0.20260224065243-f9bba28c51a4 // indirect
github.com/aquasecurity/go-version v0.0.1 // indirect
diff --git a/backend/internal/environment/environment_proxy.go b/backend/internal/environment/environment_proxy.go
index 43d2b5402..c3c7b1990 100644
--- a/backend/internal/environment/environment_proxy.go
+++ b/backend/internal/environment/environment_proxy.go
@@ -406,14 +406,17 @@ func (s *EnvironmentService) SyncRepositoriesToEnvironment(ctx context.Context,
return s.fanOutSyncToEnvironment(ctx, environmentID, "git repositories", "/api/git-repositories/sync",
func(repo gitrepo.GitRepository) (gitops.RepositorySync, bool, error) {
item := gitops.RepositorySync{
- ID: repo.ID,
- Name: repo.Name,
- URL: repo.URL,
- AuthType: repo.AuthType,
- Username: repo.Username,
- Description: repo.Description,
- Enabled: repo.Enabled,
- CreatedAt: repo.CreatedAt,
+ ID: repo.ID,
+ Name: repo.Name,
+ URL: repo.URL,
+ AuthType: repo.AuthType,
+ Username: repo.Username,
+ SSHHostKeyVerification: repo.SSHHostKeyVerification,
+ CommitAuthorName: repo.CommitAuthorName,
+ CommitAuthorEmail: repo.CommitAuthorEmail,
+ Description: repo.Description,
+ Enabled: repo.Enabled,
+ CreatedAt: repo.CreatedAt,
}
if repo.UpdatedAt != nil {
item.UpdatedAt = *repo.UpdatedAt
@@ -435,6 +438,22 @@ func (s *EnvironmentService) SyncRepositoriesToEnvironment(ctx context.Context,
item.SSHKey = decryptedSSHKey
}
+ if repo.SigningKey != "" {
+ decryptedSigningKey, err := crypto.Decrypt(repo.SigningKey)
+ if err != nil {
+ return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt signing key for repository %s for sync: %w", repo.ID, err)
+ }
+ item.SigningKey = decryptedSigningKey
+ }
+
+ if repo.SigningKeyPassphrase != "" {
+ decryptedPassphrase, err := crypto.Decrypt(repo.SigningKeyPassphrase)
+ if err != nil {
+ return gitops.RepositorySync{}, false, fmt.Errorf("failed to decrypt signing key passphrase for repository %s for sync: %w", repo.ID, err)
+ }
+ item.SigningKeyPassphrase = decryptedPassphrase
+ }
+
return item, true, nil
},
func(items []gitops.RepositorySync) gitops.RepositorySyncRequest {
diff --git a/backend/internal/gitops/backup.go b/backend/internal/gitops/backup.go
index a6330e67a..f7c356967 100644
--- a/backend/internal/gitops/backup.go
+++ b/backend/internal/gitops/backup.go
@@ -2,9 +2,16 @@ package gitops
import (
"context"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json/v2"
"fmt"
+ "io/fs"
"log/slog"
"path"
+ "path/filepath"
+ "slices"
+ "sort"
"strings"
"sync"
"time"
@@ -15,8 +22,11 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/event"
projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
+ "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/types/v2/gitops"
schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
+ "go.getarcane.app/acfs"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
@@ -27,13 +37,10 @@ const (
backupPushAttempts = 3
backupStatusRunning = "running"
backupStatusConflict = "conflict"
- backupAuthorName = "Arcane"
- backupAuthorEmail = "arcane@localhost"
backupCommitFileLines = 20
)
-// backupRuntimeInternal holds the in-memory coordination for backup syncs:
-// per-branch write serialization and the save debounce timers.
+// backupRuntimeInternal holds the in-memory coordination for backup syncs
type backupRuntimeInternal struct {
mu sync.Mutex
branches map[string]*sync.Mutex
@@ -104,8 +111,7 @@ type backupCreateConfigInternal struct {
backupOnSave bool
}
-// prepareBackupCreateInternal validates a backup-mode create request against
-// the project, its compose files, and other backups on the same branch.
+// prepareBackupCreateInternal validates a backup create request against the project and other backups on the branch.
func (s *GitOpsSyncService) prepareBackupCreateInternal(ctx context.Context, tx *gorm.DB, req gitops.CreateSyncRequest) (*backupCreateConfigInternal, error) {
if req.HasDeploymentOptions() {
return nil, common.Classify(common.ErrValidation, errors.WithDetails(errors.New("deployment options cannot be set on a backup sync"), "field", "mode"))
@@ -130,7 +136,10 @@ func (s *GitOpsSyncService) prepareBackupCreateInternal(ctx context.Context, tx
return nil, common.Classify(common.ErrConflict, errors.New("project already has a Git backup; disconnect it first"))
}
- directory, err := normalizeBackupDirectoryInternal(req.BackupDirectory)
+ directory, err := utils.NormalizeRelativePath(req.BackupDirectory)
+ if err == nil && slices.Contains(strings.Split(directory, "/"), ".git") {
+ err = errors.New("backup directory must not contain a .git segment")
+ }
if err != nil {
return nil, common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "backupDirectory"))
}
@@ -161,8 +170,7 @@ func (s *GitOpsSyncService) prepareBackupCreateInternal(ctx context.Context, tx
return config, nil
}
-// lockProjectForSyncInternal loads a project inside tx with a row lock so the
-// one-Git-relationship-per-project checks and the sync insert are atomic.
+// lockProjectForSyncInternal row-locks the project so the one-Git-relationship check and the insert are atomic.
func lockProjectForSyncInternal(tx *gorm.DB, projectID string) (*projectpkg.Project, error) {
var project projectpkg.Project
if err := tx.Clauses(clause.Locking{Strength: "UPDATE"}).Where("id = ?", projectID).First(&project).Error; err != nil {
@@ -174,8 +182,7 @@ func lockProjectForSyncInternal(tx *gorm.DB, projectID string) (*projectpkg.Proj
return &project, nil
}
-// ensureBackupDestinationFreeInternal rejects a destination that equals or
-// nests inside another backup's destination on the same repository branch.
+// ensureBackupDestinationFreeInternal rejects a destination that overlaps another backup on the same branch.
func ensureBackupDestinationFreeInternal(tx *gorm.DB, excludeSyncID, repositoryID, branch, directory string) error {
var others []projectpkg.GitOpsSync
q := tx.
@@ -188,28 +195,21 @@ func ensureBackupDestinationFreeInternal(tx *gorm.DB, excludeSyncID, repositoryI
return errors.WrapIf(err, "failed to check backup destinations")
}
for _, other := range others {
- if backupDirectoriesOverlapInternal(other.BackupDirectory, directory) {
+ if other.BackupDirectory == directory || strings.HasPrefix(other.BackupDirectory, directory+"/") || strings.HasPrefix(directory, other.BackupDirectory+"/") {
return common.Classify(common.ErrConflict, errors.Errorf("backup directory %q overlaps with an existing backup at %q on this branch", directory, other.BackupDirectory))
}
}
return nil
}
-// applyModeUpdatesInternal validates the mode-specific fields of an update and
-// appends the resulting column updates.
+// applyModeUpdatesInternal validates mode-specific update fields and appends the column updates.
func (s *GitOpsSyncService) applyModeUpdatesInternal(ctx context.Context, current *projectpkg.GitOpsSync, req gitops.UpdateSyncRequest, updates map[string]any) error {
- if current.IsBackup() {
- return s.applyBackupUpdateInternal(ctx, current, req, updates)
- }
- if req.HasBackupOptions() {
- return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("backup options cannot be set on a deployment sync"), "field", "mode"))
+ if !current.IsBackup() {
+ if req.HasBackupOptions() {
+ return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("backup options cannot be set on a deployment sync"), "field", "mode"))
+ }
+ return nil
}
- return nil
-}
-
-// applyBackupUpdateInternal validates backup-mode update fields and appends the
-// resulting column updates.
-func (s *GitOpsSyncService) applyBackupUpdateInternal(ctx context.Context, current *projectpkg.GitOpsSync, req gitops.UpdateSyncRequest, updates map[string]any) error {
if req.HasDeploymentOptions() {
return common.Classify(common.ErrValidation, errors.WithDetails(errors.New("deployment options cannot be set on a backup sync"), "field", "mode"))
}
@@ -253,33 +253,37 @@ func (s *GitOpsSyncService) applyBackupUpdateInternal(ctx context.Context, curre
return nil
}
-// performBackupInternal snapshots the project, reconciles it against the
-// branch, and pushes a commit when the snapshot differs. adopt replaces
-// whatever the remote holds in the backup directory.
+// performBackupInternal snapshots the project and pushes a commit when it differs; adopt overwrites the remote.
func (s *GitOpsSyncService) performBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, actor common.User, result *gitops.SyncResult, adopt bool) (*gitops.SyncResult, error) {
if sync.Repository == nil {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Repository not found", errors.New("repository not found"))
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Repository not found", errors.New("repository not found"), false)
}
if sync.ProjectID == nil || strings.TrimSpace(*sync.ProjectID) == "" {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not linked", errors.New("backup sync has no project"))
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not linked", errors.New("backup sync has no project"), false)
}
project, found, err := s.lookupProjectByIDInternal(ctx, *sync.ProjectID)
if err != nil {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Failed to load project", err)
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Failed to load project", err, false)
}
if !found {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not found", errors.Errorf("project %s no longer exists", *sync.ProjectID))
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project not found", errors.Errorf("project %s no longer exists", *sync.ProjectID), false)
}
if err := s.projectService.EnsureProjectPathUnderRoot(ctx, project, true); err != nil {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project directory is unavailable", err)
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureProjectMissing, "Project directory is unavailable", err, false)
}
authConfig, err := s.repoService.GetAuthConfig(ctx, sync.Repository)
if err != nil {
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureAuth, "Failed to get authentication config", err)
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureAuth, "Failed to get authentication config", err, false)
+ }
+ identity, err := s.repoService.GetCommitIdentity(ctx, sync.Repository)
+ if err != nil {
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureAuth, "Failed to load commit identity", err, false)
}
startedAt := time.Now()
- s.updateBackupRunningInternal(ctx, sync.ID)
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", sync.ID).Update("last_sync_status", backupStatusRunning).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to mark git backup running", "error", err, "syncId", sync.ID)
+ }
lock := s.backups.branchLock(sync.RepositoryID, sync.Branch)
lock.Lock()
@@ -287,84 +291,101 @@ func (s *GitOpsSyncService) performBackupInternal(ctx context.Context, sync *pro
snapshot, err := s.buildBackupSnapshotInternal(ctx, sync, project)
if err != nil {
- return result, s.failBackupInternal(ctx, sync, result, actor, backupSnapshotFailureReasonInternal(err), "Failed to snapshot project files", err)
+ return result, s.failBackupInternal(ctx, sync, result, actor, backupFailureReasonInternal(err), "Failed to snapshot project files", err, false)
}
baseline := parseBackupSnapshotInternal(sync.LastBackupSnapshot)
for attempt := 1; attempt <= backupPushAttempts; attempt++ {
- outcome, retry, err := s.runBackupAttemptInternal(ctx, sync, project, actor, result, authConfig, snapshot, baseline, adopt, startedAt)
+ retry, err := s.commitBackupInternal(ctx, sync, project, actor, result, authConfig, identity, snapshot, baseline, adopt, startedAt)
if retry {
slog.InfoContext(ctx, "git backup push rejected; retrying with a fresh checkout", "syncId", sync.ID, "attempt", attempt, "error", err)
continue
}
- return outcome, err
+ return result, err
}
- return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailurePushRejected, "Push rejected by remote", errors.Errorf("another writer kept updating branch %s; giving up after %d attempts", sync.Branch, backupPushAttempts))
+ return result, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailurePushRejected, "Push rejected by remote", errors.Errorf("another writer kept updating branch %s; giving up after %d attempts", sync.Branch, backupPushAttempts), false)
}
-func (s *GitOpsSyncService) runBackupAttemptInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User, result *gitops.SyncResult, authConfig git.AuthConfig, snapshot *backupSnapshotInternal, baseline map[string]string, adopt bool, startedAt time.Time) (*gitops.SyncResult, bool, error) {
+// commitBackupInternal runs one checkout-analyze-push attempt; retry reports a rejected push worth repeating.
+func (s *GitOpsSyncService) commitBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project, actor common.User, result *gitops.SyncResult, authConfig git.AuthConfig, identity git.CommitIdentity, snapshot *backupSnapshotInternal, baseline map[string]string, adopt bool, startedAt time.Time) (bool, error) {
checkout, err := s.repoService.CheckoutForWrite(ctx, sync.Repository.URL, sync.Branch, authConfig)
if err != nil {
- return result, false, s.failBackupInternal(ctx, sync, result, actor, backupCheckoutFailureReasonInternal(err), "Failed to prepare repository checkout", err)
+ return false, s.failBackupInternal(ctx, sync, result, actor, backupFailureReasonInternal(err), "Failed to prepare repository checkout", err, false)
}
- defer func() {
- if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
- slog.WarnContext(ctx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
- }
- }()
+ defer s.repoService.Discard(ctx, checkout.RepoPath)
- remote, err := readRemoteBackupStateInternal(ctx, checkout.RepoPath, sync.BackupDirectory)
+ analysis, err := analyzeBackupInternal(ctx, checkout.RepoPath, sync.BackupDirectory, snapshot, baseline, adopt)
if err != nil {
- return result, false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Failed to read remote backup", err)
+ return false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureRepository, "Failed to read remote backup", err, false)
}
- analysis := analyzeBackupInternal(snapshot, remote, baseline, adopt)
-
switch analysis.state {
case backupPreviewClean:
- s.recordBackupSuccessInternal(ctx, sync, snapshot, checkout.HeadCommit, false, startedAt)
+ if err := s.recordBackupSuccessInternal(ctx, sync, snapshot, checkout.HeadCommit, false, startedAt); err != nil {
+ return false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureSnapshot, "Failed to record backup", err, false)
+ }
result.Success = true
result.Message = "Repository already contains the current files for project " + project.Name
- return result, false, nil
+ return false, nil
case backupPreviewConflict:
- return result, false, s.failBackupNeedsAttentionInternal(ctx, sync, result, actor, gitops.BackupFailureConflict, "Backup files changed in the repository", errors.Errorf("%d backup file(s) changed in the repository since the last backup: %s", len(analysis.conflicts), summarizeBackupChangesInternal(analysis.conflicts)))
+ names := make([]string, 0, backupCommitFileLines+1)
+ for _, change := range analysis.conflicts[:min(len(analysis.conflicts), backupCommitFileLines)] {
+ names = append(names, change.Path)
+ }
+ if len(analysis.conflicts) > backupCommitFileLines {
+ names = append(names, "...")
+ }
+ return false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureConflict, "Backup files changed in the repository", errors.Errorf("%d backup file(s) changed in the repository since the last backup: %s", len(analysis.conflicts), strings.Join(names, ", ")), true)
case backupPreviewOccupied:
- return result, false, s.failBackupNeedsAttentionInternal(ctx, sync, result, actor, gitops.BackupFailureDestinationOccupied, "Backup directory already contains unrelated files", errors.Errorf("directory %s on branch %s already contains files that are not an Arcane backup", sync.BackupDirectory, sync.Branch))
+ return false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureDestinationOccupied, "Backup directory already contains unrelated files", errors.Errorf("directory %s on branch %s already contains files that are not an Arcane backup", sync.BackupDirectory, sync.Branch), true)
}
- now := time.Now()
- manifest, err := buildBackupManifestInternal(sync, project.Name, snapshot, now)
- if err != nil {
- return result, false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureSnapshot, "Failed to build backup manifest", err)
+ var message strings.Builder
+ fmt.Fprintf(&message, "Back up %s from Arcane", project.Name)
+ if actor.Username != "" && actor.ID != common.SystemUser.ID {
+ fmt.Fprintf(&message, " (%s)", actor.Username)
+ }
+ message.WriteString("\n")
+ for _, change := range analysis.changes[:min(len(analysis.changes), backupCommitFileLines)] {
+ fmt.Fprintf(&message, "\n%s %s", change.Change, change.Path)
+ }
+ if extra := len(analysis.changes) - backupCommitFileLines; extra > 0 {
+ fmt.Fprintf(&message, "\n... and %d more", extra)
}
request := git.CommitRequest{
- Message: backupCommitMessageInternal(project.Name, actor, analysis.changes),
- AuthorName: backupAuthorName,
- AuthorEmail: backupAuthorEmail,
+ Message: message.String(),
+ AuthorName: identity.Name,
+ AuthorEmail: identity.Email,
+ SignKey: identity.SignKey,
}
for _, file := range snapshot.files {
request.Files = append(request.Files, git.CommitFile{Path: path.Join(sync.BackupDirectory, file.Path), Content: file.Content, Executable: file.Executable})
}
- request.Files = append(request.Files, git.CommitFile{Path: path.Join(sync.BackupDirectory, gitops.BackupManifestFileName), Content: manifest})
for _, removed := range analysis.remove {
request.Remove = append(request.Remove, path.Join(sync.BackupDirectory, removed))
}
+ legacyManifest := path.Join(sync.BackupDirectory, legacyBackupManifestFileNameInternal)
+ if exists, existsErr := acfs.Exists(ctx, checkout.RepoPath, "/"+legacyManifest); existsErr == nil && exists {
+ request.Remove = append(request.Remove, legacyManifest)
+ }
commit, committed, err := s.repoService.CommitAndPush(ctx, checkout, request, authConfig)
if err != nil {
if errors.Is(err, git.ErrPushRejected) {
- return result, true, err
+ return true, err
}
- return result, false, s.failBackupInternal(ctx, sync, result, actor, backupCheckoutFailureReasonInternal(err), "Failed to push backup", err)
+ return false, s.failBackupInternal(ctx, sync, result, actor, backupFailureReasonInternal(err), "Failed to push backup", err, false)
}
- s.recordBackupSuccessInternal(ctx, sync, snapshot, commit, committed, startedAt)
+ if err := s.recordBackupSuccessInternal(ctx, sync, snapshot, commit, committed, startedAt); err != nil {
+ return false, s.failBackupInternal(ctx, sync, result, actor, gitops.BackupFailureSnapshot, "Failed to record backup", err, false)
+ }
result.Success = true
result.Message = fmt.Sprintf("Backed up %d file(s) for project %s to %s", len(snapshot.files), project.Name, sync.BackupDirectory)
if _, eventErr := s.eventService.CreateEvent(ctx, event.CreateEventRequest{
Type: event.EventTypeGitSyncRun,
Severity: event.EventSeveritySuccess,
Title: "Git backup completed",
- Description: fmt.Sprintf("Backed up project '%s' to '%s' (%s)", project.Name, sync.BackupDirectory, shortCommitInternal(commit)),
+ Description: fmt.Sprintf("Backed up project '%s' to '%s' (%s)", project.Name, sync.BackupDirectory, commit[:min(len(commit), 12)]),
ResourceType: new("git_sync"),
ResourceID: new(sync.ID),
ResourceName: new(sync.Name),
@@ -375,57 +396,18 @@ func (s *GitOpsSyncService) runBackupAttemptInternal(ctx context.Context, sync *
slog.WarnContext(ctx, "Failed to record git backup audit event", "syncId", sync.ID, "commit", commit, "error", eventErr)
}
slog.InfoContext(ctx, "Git backup completed", "syncId", sync.ID, "project", project.Name, "commit", commit, "committed", committed)
- return result, false, nil
-}
-
-func shortCommitInternal(commit string) string {
- if len(commit) > 12 {
- return commit[:12]
- }
- return commit
+ return false, nil
}
-func summarizeBackupChangesInternal(changes []gitops.BackupFileChange) string {
- names := make([]string, 0, min(len(changes), backupCommitFileLines))
- for i, change := range changes {
- if i >= backupCommitFileLines {
- names = append(names, "...")
- break
- }
- names = append(names, change.Path)
- }
- return strings.Join(names, ", ")
-}
-
-func backupCommitMessageInternal(projectName string, actor common.User, changes []gitops.BackupFileChange) string {
- var builder strings.Builder
- fmt.Fprintf(&builder, "Back up %s from Arcane", projectName)
- if actor.Username != "" && actor.ID != common.SystemUser.ID {
- fmt.Fprintf(&builder, " (%s)", actor.Username)
- }
- builder.WriteString("\n")
- for i, change := range changes {
- if i >= backupCommitFileLines {
- fmt.Fprintf(&builder, "\n... and %d more", len(changes)-backupCommitFileLines)
- break
- }
- fmt.Fprintf(&builder, "\n%s %s", change.Change, change.Path)
- }
- return builder.String()
-}
-
-func backupSnapshotFailureReasonInternal(err error) string {
+func backupFailureReasonInternal(err error) string {
switch {
- case errors.Is(err, errBackupUnreadableInternal):
+ case errors.Is(err, git.ErrSelectionUnreadable):
return gitops.BackupFailureUnreadableFiles
- case errors.Is(err, errBackupLimitsInternal):
+ case errors.Is(err, git.ErrSelectionLimits):
return gitops.BackupFailureLimits
- default:
+ case errors.Is(err, git.ErrSelectionInvalid):
return gitops.BackupFailureSnapshot
}
-}
-
-func backupCheckoutFailureReasonInternal(err error) string {
message := strings.ToLower(err.Error())
if strings.Contains(message, "authentication") || strings.Contains(message, "authorization") || strings.Contains(message, "ssh") || strings.Contains(message, "credential") {
return gitops.BackupFailureAuth
@@ -433,23 +415,21 @@ func backupCheckoutFailureReasonInternal(err error) string {
return gitops.BackupFailureRepository
}
-func (s *GitOpsSyncService) updateBackupRunningInternal(ctx context.Context, id string) {
- if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", id).Update("last_sync_status", backupStatusRunning).Error; err != nil {
- slog.ErrorContext(ctx, "Failed to mark git backup running", "error", err, "syncId", id)
- }
-}
-
-func (s *GitOpsSyncService) recordBackupSuccessInternal(ctx context.Context, sync *projectpkg.GitOpsSync, snapshot *backupSnapshotInternal, commit string, committed bool, startedAt time.Time) {
+func (s *GitOpsSyncService) recordBackupSuccessInternal(ctx context.Context, sync *projectpkg.GitOpsSync, snapshot *backupSnapshotInternal, commit string, committed bool, startedAt time.Time) error {
now := time.Now()
paths := make([]string, 0, len(snapshot.files))
for _, file := range snapshot.files {
paths = append(paths, file.Path)
}
+ hashes, err := json.Marshal(snapshot.hashes, json.Deterministic(true))
+ if err != nil {
+ return errors.WrapIf(err, "failed to encode backup snapshot")
+ }
updates := map[string]any{
"last_sync_at": now,
"last_sync_status": "success",
"last_sync_error": nil,
- "last_backup_snapshot": marshalBackupSnapshotInternal(snapshot.hashes),
+ "last_backup_snapshot": string(hashes),
"synced_files": marshalSyncedFiles(paths),
"backup_conflict": false,
"backup_failure_reason": nil,
@@ -463,48 +443,38 @@ func (s *GitOpsSyncService) recordBackupSuccessInternal(ctx context.Context, syn
if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", sync.ID).Updates(updates).Error; err != nil {
slog.ErrorContext(ctx, "Failed to record git backup success", "error", err, "syncId", sync.ID)
}
- s.clearBackupPendingInternal(ctx, sync.ID, startedAt)
-}
-
-// clearBackupPendingInternal clears the pending flag unless a save arrived
-// after the run started, in which case the next run picks it up.
-func (s *GitOpsSyncService) clearBackupPendingInternal(ctx context.Context, id string, startedAt time.Time) {
if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).
- Where("id = ? AND (backup_pending_since IS NULL OR backup_pending_since <= ?)", id, startedAt).
+ Where("id = ? AND (backup_pending_since IS NULL OR backup_pending_since <= ?)", sync.ID, startedAt).
Updates(map[string]any{"backup_pending": false, "backup_pending_since": nil}).Error; err != nil {
- slog.ErrorContext(ctx, "Failed to clear git backup pending flag", "error", err, "syncId", id)
+ slog.ErrorContext(ctx, "Failed to clear git backup pending flag", "error", err, "syncId", sync.ID)
}
+ return nil
}
-func (s *GitOpsSyncService) failBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, result *gitops.SyncResult, actor common.User, reason, message string, failure error) error {
- errMsg := failure.Error()
- result.Message = message
- result.Error = new(errMsg)
- s.updateBackupFailureInternal(ctx, sync.ID, "failed", false, reason, errMsg)
- s.logSyncError(ctx, sync, actor, errMsg)
- return errors.WithMessage(failure, message)
-}
-
-func (s *GitOpsSyncService) failBackupNeedsAttentionInternal(ctx context.Context, sync *projectpkg.GitOpsSync, result *gitops.SyncResult, actor common.User, reason, message string, failure error) error {
+// failBackupInternal records the failure on the sync; needsAttention marks it as a conflict the user must resolve.
+func (s *GitOpsSyncService) failBackupInternal(ctx context.Context, sync *projectpkg.GitOpsSync, result *gitops.SyncResult, actor common.User, reason, message string, failure error, needsAttention bool) error {
errMsg := failure.Error()
result.Message = message
result.Error = new(errMsg)
- s.updateBackupFailureInternal(ctx, sync.ID, backupStatusConflict, true, reason, errMsg)
- s.logSyncError(ctx, sync, actor, errMsg)
- return common.Classify(common.ErrConflict, errors.WithMessage(failure, message))
-}
-
-func (s *GitOpsSyncService) updateBackupFailureInternal(ctx context.Context, id, status string, conflict bool, reason, errMsg string) {
+ status := "failed"
+ if needsAttention {
+ status = backupStatusConflict
+ }
updates := map[string]any{
"last_sync_at": time.Now(),
"last_sync_status": status,
"last_sync_error": errMsg,
- "backup_conflict": conflict,
+ "backup_conflict": needsAttention,
"backup_failure_reason": reason,
}
- if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", id).Updates(updates).Error; err != nil {
- slog.ErrorContext(ctx, "Failed to record git backup failure", "error", err, "syncId", id)
+ if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", sync.ID).Updates(updates).Error; err != nil {
+ slog.ErrorContext(ctx, "Failed to record git backup failure", "error", err, "syncId", sync.ID)
+ }
+ s.logSyncError(ctx, sync, actor, errMsg)
+ if needsAttention {
+ return common.Classify(common.ErrConflict, errors.WithMessage(failure, message))
}
+ return errors.WithMessage(failure, message)
}
// PreviewBackup reports what the next backup run would commit or why it needs attention.
@@ -541,16 +511,11 @@ func (s *GitOpsSyncService) PreviewBackup(ctx context.Context, environmentID, id
if err != nil {
return nil, errors.WrapIf(err, "failed to clone repository")
}
- defer func() {
- if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
- slog.WarnContext(previewCtx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
- }
- }()
- remote, err := readRemoteBackupStateInternal(previewCtx, checkout.RepoPath, syncRecord.BackupDirectory)
+ defer s.repoService.Discard(previewCtx, checkout.RepoPath)
+ analysis, err := analyzeBackupInternal(previewCtx, checkout.RepoPath, syncRecord.BackupDirectory, snapshot, parseBackupSnapshotInternal(syncRecord.LastBackupSnapshot), false)
if err != nil {
return nil, err
}
- analysis := analyzeBackupInternal(snapshot, remote, parseBackupSnapshotInternal(syncRecord.LastBackupSnapshot), false)
files := make([]string, 0, len(snapshot.files))
for _, file := range snapshot.files {
files = append(files, file.Path)
@@ -576,58 +541,49 @@ func (s *GitOpsSyncService) ResolveBackupConflict(ctx context.Context, environme
return s.performSyncAdmittedInternal(ctx, syncRecord.EnvironmentID, syncRecord.ID, actor, true)
}
-// SubscribeProjectFileChanges marks backups pending when project files are
-// saved and, when automatic backup is on, runs them after a short debounce.
+// SubscribeProjectFileChanges marks backups pending on save and runs them after a debounce when auto backup is on.
func (s *GitOpsSyncService) SubscribeProjectFileChanges(ctx context.Context) {
if s.projectService == nil {
return
}
runCtx := s.jobs.Context(ctx)
s.projectService.FilesChanged().Subscribe(func(projectID string) {
- s.onProjectFilesChangedInternal(runCtx, projectID)
- })
-}
-
-func (s *GitOpsSyncService) onProjectFilesChangedInternal(ctx context.Context, projectID string) {
- var syncs []projectpkg.GitOpsSync
- if err := s.db.WithContext(ctx).
- Where("mode = ? AND project_id = ?", gitops.SyncModeBackup, projectID).
- Find(&syncs).Error; err != nil {
- slog.ErrorContext(ctx, "Failed to load git backups for changed project", "projectId", projectID, "error", err)
- return
- }
- now := time.Now()
- for i := range syncs {
- syncRecord := syncs[i]
- if err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).
- Updates(map[string]any{"backup_pending": true, "backup_pending_since": now}).Error; err != nil {
- slog.ErrorContext(ctx, "Failed to mark git backup pending", "syncId", syncRecord.ID, "error", err)
- continue
- }
- if !syncRecord.AutoSync || !syncRecord.BackupOnSave {
- continue
- }
- syncID, environmentID := syncRecord.ID, syncRecord.EnvironmentID
- s.backups.schedule(syncID, func() { s.triggerBackupInternal(ctx, syncID, environmentID) })
- }
-}
-
-func (s *GitOpsSyncService) triggerBackupInternal(ctx context.Context, syncID, environmentID string) {
- if s.jobs.Enabled() {
- if _, err := s.jobs.Scheduler().Submit(ctx, schedulertypes.Request{JobID: s.jobs.JobName(syncID), EnvironmentID: "0", Trigger: "save"}); err != nil {
- slog.ErrorContext(ctx, "git backup admission after save failed", "syncId", syncID, "error", err)
+ var syncs []projectpkg.GitOpsSync
+ if err := s.db.WithContext(runCtx).
+ Where("mode = ? AND project_id = ?", gitops.SyncModeBackup, projectID).
+ Find(&syncs).Error; err != nil {
+ slog.ErrorContext(runCtx, "Failed to load git backups for changed project", "projectId", projectID, "error", err)
+ return
}
- return
- }
- go func() {
- if _, err := s.PerformSync(ctx, environmentID, syncID, common.SystemUser); err != nil {
- slog.ErrorContext(ctx, "git backup after save failed", "syncId", syncID, "error", err)
+ now := time.Now()
+ for _, syncRecord := range syncs {
+ if err := s.db.WithContext(runCtx).Model(&projectpkg.GitOpsSync{}).Where("id = ?", syncRecord.ID).
+ Updates(map[string]any{"backup_pending": true, "backup_pending_since": now}).Error; err != nil {
+ slog.ErrorContext(runCtx, "Failed to mark git backup pending", "syncId", syncRecord.ID, "error", err)
+ continue
+ }
+ if !syncRecord.AutoSync || !syncRecord.BackupOnSave {
+ continue
+ }
+ syncID, environmentID := syncRecord.ID, syncRecord.EnvironmentID
+ s.backups.schedule(syncID, func() {
+ if s.jobs.Enabled() {
+ if _, err := s.jobs.Scheduler().Submit(runCtx, schedulertypes.Request{JobID: s.jobs.JobName(syncID), EnvironmentID: "0", Trigger: "save"}); err != nil {
+ slog.ErrorContext(runCtx, "git backup admission after save failed", "syncId", syncID, "error", err)
+ }
+ return
+ }
+ go func() {
+ if _, err := s.PerformSync(runCtx, environmentID, syncID, common.SystemUser); err != nil {
+ slog.ErrorContext(runCtx, "git backup after save failed", "syncId", syncID, "error", err)
+ }
+ }()
+ })
}
- }()
+ })
}
-// ReconcileInterruptedBackupsOnStartup turns backups left in the running state
-// by a restart into pending failures so they are retried or surfaced.
+// ReconcileInterruptedBackupsOnStartup turns backups left running by a restart into pending failures.
func (s *GitOpsSyncService) ReconcileInterruptedBackupsOnStartup(ctx context.Context) error {
err := s.db.WithContext(ctx).Model(&projectpkg.GitOpsSync{}).
Where("mode = ? AND last_sync_status = ?", gitops.SyncModeBackup, backupStatusRunning).
@@ -643,3 +599,326 @@ func (s *GitOpsSyncService) ReconcileInterruptedBackupsOnStartup(ctx context.Con
}
return nil
}
+
+const (
+ backupSnapshotMaxRetry = 3
+ legacyBackupManifestFileNameInternal = ".arcane-backup.json"
+ backupPreviewClean = "clean"
+ backupPreviewChanges = "changes"
+ backupPreviewConflict = "conflict"
+ backupPreviewOccupied = "destination_occupied"
+ backupChangeAdded = "added"
+ backupChangeModified = "modified"
+ backupChangeRemoved = "removed"
+)
+
+// backupSnapshotInternal is the set of project files a backup run commits.
+type backupSnapshotInternal struct {
+ files []git.CommitFile
+ hashes map[string]string
+}
+
+// backupAnalysisInternal is the decision for one backup run.
+type backupAnalysisInternal struct {
+ state string
+ changes []gitops.BackupFileChange
+ conflicts []gitops.BackupFileChange
+ remove []string
+}
+
+func hashBackupContentInternal(content []byte) string {
+ sum := sha256.Sum256(content)
+ return hex.EncodeToString(sum[:])
+}
+
+func normalizeBackupPathsInternal(raw []string) ([]string, error) {
+ seen := make(map[string]struct{}, len(raw))
+ normalized := make([]string, 0, len(raw))
+ for _, entry := range raw {
+ cleaned, err := utils.NormalizeRelativePath(entry)
+ if err != nil {
+ return nil, errors.WrapIff(err, "invalid backup path %q", entry)
+ }
+ base := path.Base(cleaned)
+ if base == projects.GitSourceEnvFileName || base == projects.GlobalEnvFileName || slices.Contains(strings.Split(cleaned, "/"), ".git") {
+ return nil, errors.Errorf("backup path %q is reserved", entry)
+ }
+ if _, ok := seen[cleaned]; ok {
+ continue
+ }
+ seen[cleaned] = struct{}{}
+ normalized = append(normalized, cleaned)
+ }
+ sort.Strings(normalized)
+ return normalized, nil
+}
+
+// backupSelectionCoversInternal reports whether the selection includes file directly or via a parent directory.
+func backupSelectionCoversInternal(paths []string, file string) bool {
+ for _, selected := range paths {
+ if selected == file || strings.HasPrefix(file, selected+"/") {
+ return true
+ }
+ }
+ return false
+}
+
+// backupComposeFilesInternal resolves the primary compose file and sibling overrides as project-relative paths.
+func (s *GitOpsSyncService) backupComposeFilesInternal(ctx context.Context, project *projectpkg.Project) (string, []string, error) {
+ composePath, err := s.projectService.ResolveProjectComposeFile(ctx, project)
+ if err != nil {
+ return "", nil, err
+ }
+ relative, err := filepath.Rel(project.Path, composePath)
+ if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
+ return "", nil, errors.Errorf("compose file %s is outside the project directory", composePath)
+ }
+ primary := filepath.ToSlash(relative)
+ composeFiles := []string{primary}
+ composeDir := path.Dir(primary)
+ for _, candidate := range projects.ComposeOverrideFileCandidates() {
+ candidatePath := path.Join(composeDir, candidate)
+ if exists, existsErr := acfs.Exists(ctx, project.Path, "/"+candidatePath); existsErr == nil && exists {
+ composeFiles = append(composeFiles, candidatePath)
+ }
+ }
+ return primary, composeFiles, nil
+}
+
+// buildBackupSnapshotInternal reads the selected project files, retrying when they change mid-read.
+func (s *GitOpsSyncService) buildBackupSnapshotInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project) (*backupSnapshotInternal, error) {
+ paths := []string(sync.BackupPaths)
+ if len(paths) == 0 {
+ return nil, errors.WrapIf(git.ErrSelectionInvalid, "no files are selected for backup")
+ }
+ primary, _, err := s.backupComposeFilesInternal(ctx, project)
+ if err != nil {
+ return nil, errors.WrapIf(git.ErrSelectionInvalid, err.Error())
+ }
+ maxFiles, maxTotalSize, _ := s.getEffectiveSyncLimits(ctx, sync)
+ options := git.CollectOptions{
+ MaxFiles: maxFiles,
+ MaxTotalSize: maxTotalSize,
+ SkipDir: projects.IsInternalScratchDirName,
+ SkipFile: func(name string) bool {
+ return name == projects.GitSourceEnvFileName || name == projects.GlobalEnvFileName || name == projects.EffectiveEnvFileName || strings.HasPrefix(name, ".env.") || strings.HasSuffix(name, ".env")
+ },
+ }
+
+ for range backupSnapshotMaxRetry {
+ files, err := git.CollectFiles(ctx, project.Path, paths, options)
+ if err != nil {
+ return nil, err
+ }
+ snapshot := &backupSnapshotInternal{files: files, hashes: make(map[string]string, len(files))}
+ for _, file := range files {
+ snapshot.hashes[file.Path] = hashBackupContentInternal(file.Content)
+ }
+ if _, ok := snapshot.hashes[primary]; !ok {
+ return nil, errors.WrapIff(git.ErrSelectionInvalid, "compose file %s is not included in the backup selection", primary)
+ }
+
+ stable := true
+ for _, file := range files {
+ content, err := acfs.ReadFile(ctx, project.Path, "/"+file.Path)
+ if err != nil {
+ return nil, errors.WrapIff(git.ErrSelectionUnreadable, "cannot re-read %s: %v", file.Path, err)
+ }
+ if hashBackupContentInternal(content) != snapshot.hashes[file.Path] {
+ stable = false
+ break
+ }
+ }
+ if stable {
+ return snapshot, nil
+ }
+ }
+ return nil, errors.WrapIf(git.ErrSelectionInvalid, "project files changed while the backup snapshot was being taken")
+}
+
+// analyzeBackupInternal compares the snapshot with the checkout; baseline is the last push and the only proof the directory is ours.
+func analyzeBackupInternal(ctx context.Context, repoPath, directory string, snapshot *backupSnapshotInternal, baseline map[string]string, adopt bool) (backupAnalysisInternal, error) {
+ analysis := backupAnalysisInternal{conflicts: []gitops.BackupFileChange{}}
+ entries, err := acfs.List(ctx, repoPath, "/"+directory)
+ if err != nil && !errors.Is(err, fs.ErrNotExist) {
+ return analysis, errors.WrapIf(err, "failed to inspect remote backup directory")
+ }
+ occupied := err == nil && len(entries) > 0
+ remote := make(map[string]string)
+ if occupied {
+ for _, owned := range []map[string]string{baseline, snapshot.hashes} {
+ for file := range owned {
+ if _, done := remote[file]; done {
+ continue
+ }
+ content, readErr := acfs.ReadFile(ctx, repoPath, "/"+path.Join(directory, file))
+ if readErr != nil {
+ if errors.Is(readErr, fs.ErrNotExist) {
+ continue
+ }
+ return analysis, errors.WrapIff(readErr, "failed to read remote backup file %s", file)
+ }
+ remote[file] = hashBackupContentInternal(content)
+ }
+ }
+ }
+
+ analysis.changes = diffBackupHashesInternal(snapshot.hashes, remote)
+ for file := range remote {
+ if _, ok := snapshot.hashes[file]; !ok {
+ analysis.remove = append(analysis.remove, file)
+ }
+ }
+ sort.Strings(analysis.remove)
+
+ switch {
+ case len(analysis.changes) == 0:
+ analysis.state = backupPreviewClean
+ case adopt:
+ analysis.state = backupPreviewChanges
+ case baseline == nil && occupied:
+ analysis.state = backupPreviewOccupied
+ case baseline == nil:
+ analysis.state = backupPreviewChanges
+ default:
+ analysis.conflicts = diffBackupHashesInternal(remote, baseline)
+ analysis.state = backupPreviewChanges
+ if len(analysis.conflicts) > 0 {
+ analysis.state = backupPreviewConflict
+ }
+ }
+ return analysis, nil
+}
+
+func diffBackupHashesInternal(local, remote map[string]string) []gitops.BackupFileChange {
+ var changes []gitops.BackupFileChange
+ for file, hash := range local {
+ remoteHash, ok := remote[file]
+ switch {
+ case !ok:
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeAdded})
+ case remoteHash != hash:
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeModified})
+ }
+ }
+ for file := range remote {
+ if _, ok := local[file]; !ok {
+ changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeRemoved})
+ }
+ }
+ sort.Slice(changes, func(i, j int) bool { return changes[i].Path < changes[j].Path })
+ if changes == nil {
+ changes = []gitops.BackupFileChange{}
+ }
+ return changes
+}
+
+func parseBackupSnapshotInternal(raw *string) map[string]string {
+ if raw == nil || *raw == "" {
+ return nil
+ }
+ var hashes map[string]string
+ if err := json.Unmarshal([]byte(*raw), &hashes); err != nil {
+ return nil
+ }
+ return hashes
+}
+
+const defaultBackupHistoryLimit = 20
+
+func (s *GitOpsSyncService) getBackupSyncInternal(ctx context.Context, environmentID, id string) (*projectpkg.GitOpsSync, error) {
+ syncRecord, err := s.GetSyncByID(ctx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ if !syncRecord.IsBackup() {
+ return nil, common.Classify(common.ErrBadRequest, errors.New("sync does not back up to Git"))
+ }
+ if syncRecord.Repository == nil {
+ return nil, common.Classify(common.ErrNotFound, errors.New("repository not found"))
+ }
+ return syncRecord, nil
+}
+
+// cloneBackupBranchInternal clones the backup branch for read-only inspection
+func (s *GitOpsSyncService) cloneBackupBranchInternal(ctx context.Context, syncRecord *projectpkg.GitOpsSync) (string, func(), error) {
+ authConfig, err := s.repoService.GetAuthConfig(ctx, syncRecord.Repository)
+ if err != nil {
+ return "", func() {}, err
+ }
+ checkout, err := s.repoService.CheckoutForWrite(ctx, syncRecord.Repository.URL, syncRecord.Branch, authConfig)
+ if err != nil {
+ return "", func() {}, errors.WrapIf(err, "failed to clone repository")
+ }
+ cleanup := func() { s.repoService.Discard(ctx, checkout.RepoPath) }
+ if !checkout.BranchExists {
+ return "", cleanup, nil
+ }
+ return checkout.RepoPath, cleanup, nil
+}
+
+// GetBackupHistory lists revisions that touched the backup directory.
+func (s *GitOpsSyncService) GetBackupHistory(ctx context.Context, environmentID, id string, limit int) (*gitops.BackupHistoryResponse, error) {
+ historyCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
+ defer cancel()
+
+ syncRecord, err := s.getBackupSyncInternal(historyCtx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ repoPath, cleanup, err := s.cloneBackupBranchInternal(historyCtx, syncRecord)
+ defer cleanup()
+ if err != nil {
+ return nil, err
+ }
+ response := &gitops.BackupHistoryResponse{Entries: []gitops.BackupHistoryEntry{}}
+ if repoPath == "" {
+ return response, nil
+ }
+ if limit <= 0 {
+ limit = defaultBackupHistoryLimit
+ }
+ entries, err := s.repoService.DirectoryHistory(historyCtx, repoPath, syncRecord.BackupDirectory, limit)
+ if err != nil {
+ return nil, err
+ }
+ for _, entry := range entries {
+ response.Entries = append(response.Entries, gitops.BackupHistoryEntry{Commit: entry.Hash, Author: entry.Author, Message: entry.Message, Date: entry.Date, Files: entry.Files})
+ }
+ return response, nil
+}
+
+// GetBackupRevision returns one revision with per-file diffs inside the backup directory.
+func (s *GitOpsSyncService) GetBackupRevision(ctx context.Context, environmentID, id, commit string) (*gitops.BackupRevision, error) {
+ commit = strings.TrimSpace(commit)
+ revisionCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
+ defer cancel()
+
+ syncRecord, err := s.getBackupSyncInternal(revisionCtx, environmentID, id)
+ if err != nil {
+ return nil, err
+ }
+ repoPath, cleanup, err := s.cloneBackupBranchInternal(revisionCtx, syncRecord)
+ defer cleanup()
+ if err != nil {
+ return nil, err
+ }
+ if repoPath == "" {
+ return nil, common.Classify(common.ErrNotFound, errors.New("revision not found"))
+ }
+ entry, diffs, err := s.repoService.CommitDiff(revisionCtx, repoPath, commit, syncRecord.BackupDirectory)
+ if errors.Is(err, git.ErrInvalidCommit) {
+ return nil, common.Classify(common.ErrBadRequest, err)
+ }
+ if err != nil {
+ return nil, common.Classify(common.ErrNotFound, err)
+ }
+ revision := &gitops.BackupRevision{
+ Entry: gitops.BackupHistoryEntry{Commit: entry.Hash, Author: entry.Author, Message: entry.Message, Date: entry.Date, Files: entry.Files},
+ Diffs: []gitops.BackupFileDiff{},
+ }
+ for _, diff := range diffs {
+ revision.Diffs = append(revision.Diffs, gitops.BackupFileDiff{Path: diff.Path, Patch: diff.Patch})
+ }
+ return revision, nil
+}
diff --git a/backend/internal/gitops/backup_history.go b/backend/internal/gitops/backup_history.go
deleted file mode 100644
index e620fdf1e..000000000
--- a/backend/internal/gitops/backup_history.go
+++ /dev/null
@@ -1,130 +0,0 @@
-package gitops
-
-import (
- "context"
- "log/slog"
- "strings"
-
- "emperror.dev/errors"
- "github.com/getarcaneapp/arcane/backend/v2/internal/common"
- projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
- git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
- "github.com/getarcaneapp/arcane/types/v2/gitops"
-)
-
-const defaultBackupHistoryLimit = 20
-
-func (s *GitOpsSyncService) getBackupSyncInternal(ctx context.Context, environmentID, id string) (*projectpkg.GitOpsSync, error) {
- syncRecord, err := s.GetSyncByID(ctx, environmentID, id)
- if err != nil {
- return nil, err
- }
- if !syncRecord.IsBackup() {
- return nil, common.Classify(common.ErrBadRequest, errors.New("sync does not back up to Git"))
- }
- if syncRecord.Repository == nil {
- return nil, common.Classify(common.ErrNotFound, errors.New("repository not found"))
- }
- return syncRecord, nil
-}
-
-// cloneBackupBranchInternal clones the backup branch for read-only inspection.
-// A missing branch or empty repository yields an empty path.
-func (s *GitOpsSyncService) cloneBackupBranchInternal(ctx context.Context, syncRecord *projectpkg.GitOpsSync) (string, func(), error) {
- authConfig, err := s.repoService.GetAuthConfig(ctx, syncRecord.Repository)
- if err != nil {
- return "", func() {}, err
- }
- checkout, err := s.repoService.CheckoutForWrite(ctx, syncRecord.Repository.URL, syncRecord.Branch, authConfig)
- if err != nil {
- return "", func() {}, errors.WrapIf(err, "failed to clone repository")
- }
- cleanup := func() {
- if cleanupErr := s.repoService.Cleanup(checkout.RepoPath); cleanupErr != nil {
- slog.WarnContext(ctx, "Failed to cleanup repository", "path", checkout.RepoPath, "error", cleanupErr)
- }
- }
- if !checkout.BranchExists {
- return "", cleanup, nil
- }
- return checkout.RepoPath, cleanup, nil
-}
-
-func historyEntryToAPIInternal(entry git.HistoryEntry) gitops.BackupHistoryEntry {
- return gitops.BackupHistoryEntry{
- Commit: entry.Hash,
- Author: entry.Author,
- Message: entry.Message,
- Date: entry.Date,
- Files: entry.Files,
- }
-}
-
-// GetBackupHistory lists revisions that touched the backup directory.
-func (s *GitOpsSyncService) GetBackupHistory(ctx context.Context, environmentID, id string, limit int) (*gitops.BackupHistoryResponse, error) {
- historyCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
- defer cancel()
-
- syncRecord, err := s.getBackupSyncInternal(historyCtx, environmentID, id)
- if err != nil {
- return nil, err
- }
- repoPath, cleanup, err := s.cloneBackupBranchInternal(historyCtx, syncRecord)
- defer cleanup()
- if err != nil {
- return nil, err
- }
- response := &gitops.BackupHistoryResponse{Entries: []gitops.BackupHistoryEntry{}}
- if repoPath == "" {
- return response, nil
- }
- if limit <= 0 {
- limit = defaultBackupHistoryLimit
- }
- entries, err := s.repoService.DirectoryHistory(historyCtx, repoPath, syncRecord.BackupDirectory, limit)
- if err != nil {
- return nil, err
- }
- for _, entry := range entries {
- response.Entries = append(response.Entries, historyEntryToAPIInternal(entry))
- }
- return response, nil
-}
-
-// GetBackupRevision returns one revision with per-file diffs inside the backup directory.
-func (s *GitOpsSyncService) GetBackupRevision(ctx context.Context, environmentID, id, commit string) (*gitops.BackupRevision, error) {
- commit = strings.TrimSpace(commit)
- if len(commit) < 7 || len(commit) > 64 {
- return nil, common.Classify(common.ErrBadRequest, errors.New("invalid commit hash"))
- }
- for _, r := range commit {
- if (r < '0' || r > '9') && (r < 'a' || r > 'f') && (r < 'A' || r > 'F') {
- return nil, common.Classify(common.ErrBadRequest, errors.New("invalid commit hash"))
- }
- }
-
- revisionCtx, cancel := context.WithTimeout(ctx, defaultGitSyncTimeout)
- defer cancel()
-
- syncRecord, err := s.getBackupSyncInternal(revisionCtx, environmentID, id)
- if err != nil {
- return nil, err
- }
- repoPath, cleanup, err := s.cloneBackupBranchInternal(revisionCtx, syncRecord)
- defer cleanup()
- if err != nil {
- return nil, err
- }
- if repoPath == "" {
- return nil, common.Classify(common.ErrNotFound, errors.New("revision not found"))
- }
- entry, diffs, err := s.repoService.CommitDiff(revisionCtx, repoPath, commit, syncRecord.BackupDirectory)
- if err != nil {
- return nil, common.Classify(common.ErrNotFound, err)
- }
- revision := &gitops.BackupRevision{Entry: historyEntryToAPIInternal(entry), Diffs: []gitops.BackupFileDiff{}}
- for _, diff := range diffs {
- revision.Diffs = append(revision.Diffs, gitops.BackupFileDiff{Path: diff.Path, Patch: diff.Patch})
- }
- return revision, nil
-}
diff --git a/backend/internal/gitops/backup_snapshot.go b/backend/internal/gitops/backup_snapshot.go
deleted file mode 100644
index fa2244e54..000000000
--- a/backend/internal/gitops/backup_snapshot.go
+++ /dev/null
@@ -1,472 +0,0 @@
-package gitops
-
-import (
- "context"
- "crypto/sha256"
- "encoding/hex"
- "encoding/json/jsontext"
- "encoding/json/v2"
- "io/fs"
- "os"
- "path"
- "path/filepath"
- "slices"
- "sort"
- "strings"
- "time"
-
- "emperror.dev/errors"
- projectpkg "github.com/getarcaneapp/arcane/backend/v2/internal/project"
- git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/projects"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
- "github.com/getarcaneapp/arcane/types/v2/gitops"
- "go.getarcane.app/acfs"
- acfstypes "go.getarcane.app/acfs/types"
-)
-
-const backupSnapshotAttemptsInternal = 3
-
-var (
- errBackupUnreadableInternal = errors.Sentinel("backup selection is unreadable")
- errBackupLimitsInternal = errors.Sentinel("backup selection exceeds sync limits")
- errBackupSnapshotInternal = errors.Sentinel("backup selection is invalid")
-)
-
-// backupSnapshotInternal is the set of project files a backup run commits.
-type backupSnapshotInternal struct {
- files []git.CommitFile
- hashes map[string]string
- composeFiles []string
-}
-
-// remoteBackupStateInternal describes the backup directory as it exists on the branch.
-type remoteBackupStateInternal struct {
- manifest *gitops.BackupManifest
- hashes map[string]string
- occupied bool
-}
-
-// backupAnalysisInternal is the decision for one backup run.
-type backupAnalysisInternal struct {
- state string
- changes []gitops.BackupFileChange
- conflicts []gitops.BackupFileChange
- remove []string
-}
-
-const (
- backupPreviewClean = "clean"
- backupPreviewChanges = "changes"
- backupPreviewConflict = "conflict"
- backupPreviewOccupied = "destination_occupied"
- backupChangeAdded = "added"
- backupChangeModified = "modified"
- backupChangeRemoved = "removed"
- backupManifestVersion = 1
- backupSnapshotMaxRetry = backupSnapshotAttemptsInternal
-)
-
-func hashBackupContentInternal(content []byte) string {
- sum := sha256.Sum256(content)
- return hex.EncodeToString(sum[:])
-}
-
-// isBackupEnvFileInternal reports whether a base name looks like an environment
-// file; these are excluded from directory expansion and only backed up when
-// listed explicitly.
-func isBackupEnvFileInternal(name string) bool {
- return name == projects.EffectiveEnvFileName || strings.HasPrefix(name, ".env.") || strings.HasSuffix(name, ".env")
-}
-
-// isBackupReservedFileInternal reports whether a base name is Arcane bookkeeping
-// that never belongs in a backup.
-func isBackupReservedFileInternal(name string) bool {
- return name == projects.GitSourceEnvFileName || name == projects.GlobalEnvFileName || name == gitops.BackupManifestFileName || name == ".git"
-}
-
-func containsGitSegmentInternal(relative string) bool {
- for segment := range strings.SplitSeq(relative, "/") {
- if segment == ".git" {
- return true
- }
- }
- return false
-}
-
-func normalizeBackupDirectoryInternal(raw string) (string, error) {
- normalized, err := utils.NormalizeRelativePath(raw)
- if err != nil {
- return "", err
- }
- if containsGitSegmentInternal(normalized) {
- return "", errors.New("backup directory must not contain a .git segment")
- }
- return normalized, nil
-}
-
-func normalizeBackupPathsInternal(raw []string) ([]string, error) {
- seen := make(map[string]struct{}, len(raw))
- normalized := make([]string, 0, len(raw))
- for _, entry := range raw {
- cleaned, err := utils.NormalizeRelativePath(entry)
- if err != nil {
- return nil, errors.WrapIff(err, "invalid backup path %q", entry)
- }
- if isBackupReservedFileInternal(path.Base(cleaned)) || containsGitSegmentInternal(cleaned) {
- return nil, errors.Errorf("backup path %q is reserved", entry)
- }
- if _, ok := seen[cleaned]; ok {
- continue
- }
- seen[cleaned] = struct{}{}
- normalized = append(normalized, cleaned)
- }
- sort.Strings(normalized)
- return normalized, nil
-}
-
-// backupSelectionCoversInternal reports whether the selection includes file
-// directly or through one of its parent directories.
-func backupSelectionCoversInternal(paths []string, file string) bool {
- for _, selected := range paths {
- if selected == file || strings.HasPrefix(file, selected+"/") {
- return true
- }
- }
- return false
-}
-
-func backupDirectoriesOverlapInternal(a, b string) bool {
- return a == b || strings.HasPrefix(a, b+"/") || strings.HasPrefix(b, a+"/")
-}
-
-// backupComposeFilesInternal resolves the project's primary compose file and any
-// sibling overrides as project-relative paths.
-func (s *GitOpsSyncService) backupComposeFilesInternal(ctx context.Context, project *projectpkg.Project) (string, []string, error) {
- composePath, err := s.projectService.ResolveProjectComposeFile(ctx, project)
- if err != nil {
- return "", nil, err
- }
- relative, err := filepath.Rel(project.Path, composePath)
- if err != nil || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
- return "", nil, errors.Errorf("compose file %s is outside the project directory", composePath)
- }
- primary := filepath.ToSlash(relative)
- composeFiles := []string{primary}
- composeDir := path.Dir(primary)
- for _, candidate := range projects.ComposeOverrideFileCandidates() {
- candidatePath := path.Join(composeDir, candidate)
- if exists, existsErr := acfs.Exists(ctx, project.Path, "/"+candidatePath); existsErr == nil && exists {
- composeFiles = append(composeFiles, candidatePath)
- }
- }
- return primary, composeFiles, nil
-}
-
-// buildBackupSnapshotInternal reads the selected project files, verifying that
-// none changed while being read.
-func (s *GitOpsSyncService) buildBackupSnapshotInternal(ctx context.Context, sync *projectpkg.GitOpsSync, project *projectpkg.Project) (*backupSnapshotInternal, error) {
- paths := []string(sync.BackupPaths)
- if len(paths) == 0 {
- return nil, errors.WrapIf(errBackupSnapshotInternal, "no files are selected for backup")
- }
- primary, composeFiles, err := s.backupComposeFilesInternal(ctx, project)
- if err != nil {
- return nil, errors.WrapIf(errBackupSnapshotInternal, err.Error())
- }
- maxFiles, maxTotalSize, _ := s.getEffectiveSyncLimits(ctx, sync)
-
- var lastErr error
- for range backupSnapshotMaxRetry {
- snapshot, err := collectBackupFilesInternal(ctx, project.Path, paths, maxFiles, maxTotalSize)
- if err != nil {
- return nil, err
- }
- if _, ok := snapshot.hashes[primary]; !ok {
- return nil, errors.WrapIff(errBackupSnapshotInternal, "compose file %s is not included in the backup selection", primary)
- }
- stable, err := backupSnapshotStableInternal(ctx, project.Path, snapshot)
- if err != nil {
- return nil, err
- }
- if stable {
- for _, composeFile := range composeFiles {
- if _, ok := snapshot.hashes[composeFile]; ok {
- snapshot.composeFiles = append(snapshot.composeFiles, composeFile)
- }
- }
- return snapshot, nil
- }
- lastErr = errors.WrapIf(errBackupSnapshotInternal, "project files changed while the backup snapshot was being taken")
- }
- return nil, lastErr
-}
-
-// backupCollectorInternal accumulates the selected project files under the sync limits.
-type backupCollectorInternal struct {
- ctx context.Context
- projectPath string
- maxFiles int
- maxTotalSize int64
- totalSize int64
- snapshot *backupSnapshotInternal
-}
-
-func collectBackupFilesInternal(ctx context.Context, projectPath string, paths []string, maxFiles int, maxTotalSize int64) (*backupSnapshotInternal, error) {
- collector := &backupCollectorInternal{
- ctx: ctx,
- projectPath: projectPath,
- maxFiles: maxFiles,
- maxTotalSize: maxTotalSize,
- snapshot: &backupSnapshotInternal{hashes: make(map[string]string)},
- }
- for _, selected := range paths {
- if err := ctx.Err(); err != nil {
- return nil, err
- }
- if err := collector.addPathInternal(selected); err != nil {
- return nil, err
- }
- }
- if len(collector.snapshot.files) == 0 {
- return nil, errors.WrapIf(errBackupSnapshotInternal, "the backup selection contains no files")
- }
- sort.Slice(collector.snapshot.files, func(i, j int) bool { return collector.snapshot.files[i].Path < collector.snapshot.files[j].Path })
- return collector.snapshot, nil
-}
-
-func (c *backupCollectorInternal) addPathInternal(selected string) error {
- logical := "/" + selected
- entry, err := acfs.Stat(c.ctx, c.projectPath, logical, false)
- if err != nil {
- if errors.Is(err, fs.ErrNotExist) {
- return errors.WrapIff(errBackupUnreadableInternal, "selected path %s does not exist", selected)
- }
- return errors.WrapIff(errBackupUnreadableInternal, "cannot inspect %s: %v", selected, err)
- }
- if entry.IsSymlink {
- return errors.WrapIff(errBackupSnapshotInternal, "%s is a symbolic link and cannot be backed up", selected)
- }
- if !entry.IsDirectory {
- return c.addFileInternal(entry)
- }
- walkErr := acfs.Walk(c.ctx, c.projectPath, logical, c.visitDirectoryEntryInternal)
- if walkErr == nil {
- return nil
- }
- if errors.Is(walkErr, errBackupUnreadableInternal) || errors.Is(walkErr, errBackupLimitsInternal) || errors.Is(walkErr, errBackupSnapshotInternal) {
- return walkErr
- }
- return errors.WrapIff(errBackupUnreadableInternal, "cannot walk %s: %v", selected, walkErr)
-}
-
-func (c *backupCollectorInternal) visitDirectoryEntryInternal(child acfstypes.Entry) error {
- if child.IsDirectory {
- if child.Name == ".git" || projects.IsInternalScratchDirName(child.Name) {
- return fs.SkipDir
- }
- return nil
- }
- if child.IsSymlink || isBackupReservedFileInternal(child.Name) || isBackupEnvFileInternal(child.Name) {
- return nil
- }
- return c.addFileInternal(child)
-}
-
-func (c *backupCollectorInternal) addFileInternal(entry acfstypes.Entry) error {
- relative := strings.TrimPrefix(entry.Path, "/")
- if _, seen := c.snapshot.hashes[relative]; seen {
- return nil
- }
- if c.maxFiles > 0 && len(c.snapshot.files) >= c.maxFiles {
- return errors.WrapIff(errBackupLimitsInternal, "file count limit exceeded (max %d files)", c.maxFiles)
- }
- content, err := acfs.ReadFile(c.ctx, c.projectPath, entry.Path)
- if err != nil {
- return errors.WrapIff(errBackupUnreadableInternal, "cannot read %s: %v", relative, err)
- }
- if isBinaryContentForBackupInternal(content) {
- return errors.WrapIff(errBackupSnapshotInternal, "%s is not a text file; only text configuration files can be backed up", relative)
- }
- c.totalSize += int64(len(content))
- if c.maxTotalSize > 0 && c.totalSize > c.maxTotalSize {
- return errors.WrapIff(errBackupLimitsInternal, "total size limit exceeded (max %d bytes)", c.maxTotalSize)
- }
- c.snapshot.files = append(c.snapshot.files, git.CommitFile{
- Path: relative,
- Content: content,
- Executable: os.FileMode(entry.UnixMode)&0o111 != 0,
- })
- c.snapshot.hashes[relative] = hashBackupContentInternal(content)
- return nil
-}
-
-func backupSnapshotStableInternal(ctx context.Context, projectPath string, snapshot *backupSnapshotInternal) (bool, error) {
- for _, file := range snapshot.files {
- content, err := acfs.ReadFile(ctx, projectPath, "/"+file.Path)
- if err != nil {
- return false, errors.WrapIff(errBackupUnreadableInternal, "cannot re-read %s: %v", file.Path, err)
- }
- if hashBackupContentInternal(content) != snapshot.hashes[file.Path] {
- return false, nil
- }
- }
- return true, nil
-}
-
-func isBinaryContentForBackupInternal(content []byte) bool {
- if len(content) == 0 {
- return false
- }
- return slices.Contains(content[:min(len(content), 8000)], 0)
-}
-
-func buildBackupManifestInternal(sync *projectpkg.GitOpsSync, projectName string, snapshot *backupSnapshotInternal, now time.Time) ([]byte, error) {
- manifest := gitops.BackupManifest{
- Version: backupManifestVersion,
- SyncID: sync.ID,
- ProjectName: projectName,
- EnvironmentID: sync.EnvironmentID,
- GeneratedAt: now.UTC(),
- ComposeFiles: snapshot.composeFiles,
- Files: snapshot.hashes,
- }
- if manifest.ComposeFiles == nil {
- manifest.ComposeFiles = []string{}
- }
- data, err := json.Marshal(manifest, json.Deterministic(true), jsontext.WithIndent(" "))
- if err != nil {
- return nil, errors.WrapIf(err, "failed to encode backup manifest")
- }
- return append(data, '\n'), nil
-}
-
-// readRemoteBackupStateInternal inspects the backup directory in a checkout.
-func readRemoteBackupStateInternal(ctx context.Context, repoPath, directory string) (*remoteBackupStateInternal, error) {
- state := &remoteBackupStateInternal{hashes: make(map[string]string)}
- manifestPath := "/" + path.Join(directory, gitops.BackupManifestFileName)
- data, err := acfs.ReadFile(ctx, repoPath, manifestPath)
- if err != nil {
- if !errors.Is(err, fs.ErrNotExist) {
- return nil, errors.WrapIf(err, "failed to read remote backup manifest")
- }
- entries, listErr := acfs.List(ctx, repoPath, "/"+directory)
- if listErr != nil {
- if errors.Is(listErr, fs.ErrNotExist) {
- return state, nil
- }
- return nil, errors.WrapIf(listErr, "failed to inspect remote backup directory")
- }
- state.occupied = len(entries) > 0
- return state, nil
- }
-
- var manifest gitops.BackupManifest
- if err := json.Unmarshal(data, &manifest); err != nil {
- return nil, errors.WrapIf(err, "remote backup manifest is not valid")
- }
- state.manifest = &manifest
- for file := range manifest.Files {
- relative, err := utils.NormalizeRelativePath(file)
- if err != nil {
- continue
- }
- content, readErr := acfs.ReadFile(ctx, repoPath, "/"+path.Join(directory, relative))
- if readErr != nil {
- if errors.Is(readErr, fs.ErrNotExist) {
- continue
- }
- return nil, errors.WrapIff(readErr, "failed to read remote backup file %s", relative)
- }
- state.hashes[relative] = hashBackupContentInternal(content)
- }
- return state, nil
-}
-
-func diffBackupHashesInternal(local, remote map[string]string) []gitops.BackupFileChange {
- var changes []gitops.BackupFileChange
- for file, hash := range local {
- remoteHash, ok := remote[file]
- switch {
- case !ok:
- changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeAdded})
- case remoteHash != hash:
- changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeModified})
- }
- }
- for file := range remote {
- if _, ok := local[file]; !ok {
- changes = append(changes, gitops.BackupFileChange{Path: file, Change: backupChangeRemoved})
- }
- }
- sort.Slice(changes, func(i, j int) bool { return changes[i].Path < changes[j].Path })
- if changes == nil {
- changes = []gitops.BackupFileChange{}
- }
- return changes
-}
-
-// analyzeBackupInternal decides whether the run is a no-op, a commit, or needs
-// attention. baseline is the last snapshot Arcane successfully pushed; adopt
-// skips the conflict and occupancy checks.
-func analyzeBackupInternal(snapshot *backupSnapshotInternal, remote *remoteBackupStateInternal, baseline map[string]string, adopt bool) backupAnalysisInternal {
- analysis := backupAnalysisInternal{
- changes: diffBackupHashesInternal(snapshot.hashes, remote.hashes),
- conflicts: []gitops.BackupFileChange{},
- }
- for file := range remote.hashes {
- if _, ok := snapshot.hashes[file]; !ok {
- analysis.remove = append(analysis.remove, file)
- }
- }
- sort.Strings(analysis.remove)
-
- if len(analysis.changes) == 0 {
- analysis.state = backupPreviewClean
- return analysis
- }
- if adopt {
- analysis.state = backupPreviewChanges
- return analysis
- }
- if remote.manifest == nil {
- if remote.occupied {
- analysis.state = backupPreviewOccupied
- } else {
- analysis.state = backupPreviewChanges
- }
- return analysis
- }
- if baseline == nil {
- analysis.conflicts = diffBackupHashesInternal(remote.hashes, map[string]string{})
- analysis.state = backupPreviewConflict
- return analysis
- }
- analysis.conflicts = diffBackupHashesInternal(remote.hashes, baseline)
- if len(analysis.conflicts) > 0 {
- analysis.state = backupPreviewConflict
- return analysis
- }
- analysis.state = backupPreviewChanges
- return analysis
-}
-
-func parseBackupSnapshotInternal(raw *string) map[string]string {
- if raw == nil || *raw == "" {
- return nil
- }
- var hashes map[string]string
- if err := json.Unmarshal([]byte(*raw), &hashes); err != nil {
- return nil
- }
- return hashes
-}
-
-func marshalBackupSnapshotInternal(hashes map[string]string) *string {
- data, err := json.Marshal(hashes, json.Deterministic(true))
- if err != nil {
- return nil
- }
- return new(string(data))
-}
diff --git a/backend/internal/gitops/backup_test.go b/backend/internal/gitops/backup_test.go
index e14cd6e7f..0779a3ff3 100644
--- a/backend/internal/gitops/backup_test.go
+++ b/backend/internal/gitops/backup_test.go
@@ -2,7 +2,6 @@ package gitops
import (
"context"
- "encoding/json"
"os"
"path/filepath"
"sync"
@@ -230,16 +229,7 @@ func (e *backupTestEnvInternal) pushRemoteCommitInternal(t *testing.T, branch, m
require.NoError(t, err)
}
-func readBackupManifestInternal(t *testing.T, repoPath, directory string) gitops.BackupManifest {
- t.Helper()
- data, err := os.ReadFile(filepath.Join(repoPath, filepath.FromSlash(directory), gitops.BackupManifestFileName))
- require.NoError(t, err)
- var manifest gitops.BackupManifest
- require.NoError(t, json.Unmarshal(data, &manifest))
- return manifest
-}
-
-func TestGitOpsBackup_FirstRunPushesFilesAndManifest(t *testing.T) {
+func TestGitOpsBackup_FirstRunPushesSelectedFiles(t *testing.T) {
env := setupGitOpsBackupTestServiceInternal(t)
writeBackupProjectFileInternal(t, env.projectPath, "config/app.conf", "key = value\n")
@@ -266,12 +256,11 @@ func TestGitOpsBackup_FirstRunPushesFilesAndManifest(t *testing.T) {
assert.Contains(t, string(composeBytes), "nginx:1.27-alpine")
assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "app.conf"))
- manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
- assert.Equal(t, syncRecord.ID, manifest.SyncID)
- assert.Equal(t, "demo-project", manifest.ProjectName)
- assert.Contains(t, manifest.ComposeFiles, "compose.yaml")
- assert.Len(t, manifest.Files, 2)
- assert.Equal(t, hashBackupContentInternal(composeBytes), manifest.Files["compose.yaml"])
+ assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", legacyBackupManifestFileNameInternal))
+
+ snapshot := parseBackupSnapshotInternal(stored.LastBackupSnapshot)
+ assert.Len(t, snapshot, 2)
+ assert.Equal(t, hashBackupContentInternal(composeBytes), snapshot["compose.yaml"])
}
func TestGitOpsBackup_UnchangedContentMakesNoNewCommit(t *testing.T) {
@@ -316,10 +305,6 @@ func TestGitOpsBackup_AddsAndRemovesFilesAndKeepsUnrelatedRemoteFiles(t *testing
assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "app.conf"))
assert.FileExists(t, filepath.Join(repoPath, "docs", "readme.md"))
assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "extra.conf"))
-
- manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
- assert.NotContains(t, manifest.Files, "config/extra.conf")
- assert.Contains(t, manifest.Files, "scripts/run.sh")
}
func TestGitOpsBackup_EnvFilesOnlyIncludedWhenListedExplicitly(t *testing.T) {
@@ -342,16 +327,13 @@ func TestGitOpsBackup_EnvFilesOnlyIncludedWhenListedExplicitly(t *testing.T) {
env.createBackupInternal(t, gitops.CreateSyncRequest{BackupPaths: test.paths})
repoPath := env.checkoutRemoteInternal(t, "main")
- manifest := readBackupManifestInternal(t, repoPath, "backups/demo")
- assert.Contains(t, manifest.Files, "compose.yaml")
- assert.Contains(t, manifest.Files, "config/app.conf")
- assert.NotContains(t, manifest.Files, "config/.env")
- assert.NotContains(t, manifest.Files, "config/staging.env")
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "compose.yaml"))
+ assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "app.conf"))
+ assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", "config", ".env"))
+ assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", "config", "staging.env"))
if test.wantEnvFile {
- assert.Contains(t, manifest.Files, ".env")
assert.FileExists(t, filepath.Join(repoPath, "backups", "demo", ".env"))
} else {
- assert.NotContains(t, manifest.Files, ".env")
assert.NoFileExists(t, filepath.Join(repoPath, "backups", "demo", ".env"))
}
})
diff --git a/backend/internal/gitops/gitops_sync.go b/backend/internal/gitops/gitops_sync.go
index 1d8a7839e..721c06b58 100644
--- a/backend/internal/gitops/gitops_sync.go
+++ b/backend/internal/gitops/gitops_sync.go
@@ -1084,11 +1084,7 @@ func (s *GitOpsSyncService) performSyncAdmittedInternal(ctx context.Context, env
source, err := s.prepareSyncSource(syncCtx, syncRecord, result, actor)
if source != nil && source.repoPath != "" {
- defer func() {
- if cleanupErr := s.repoService.Cleanup(source.repoPath); cleanupErr != nil {
- slog.WarnContext(syncCtx, "Failed to cleanup repository", "path", source.repoPath, "error", cleanupErr)
- }
- }()
+ defer s.repoService.Discard(syncCtx, source.repoPath)
}
if err != nil {
return result, err
@@ -1618,11 +1614,7 @@ func (s *GitOpsSyncService) BrowseFiles(ctx context.Context, environmentID, id s
if err != nil {
return nil, errors.WrapIf(err, "failed to clone repository")
}
- defer func() {
- if cleanupErr := s.repoService.Cleanup(repoPath); cleanupErr != nil {
- slog.WarnContext(browseCtx, "Failed to cleanup repository", "path", repoPath, "error", cleanupErr)
- }
- }()
+ defer s.repoService.Discard(browseCtx, repoPath)
// Browse the tree
files, err := s.repoService.BrowseTree(browseCtx, repoPath, path)
diff --git a/backend/internal/gitrepo/model.go b/backend/internal/gitrepo/model.go
index 964b08c8f..e26d7a5e7 100644
--- a/backend/internal/gitrepo/model.go
+++ b/backend/internal/gitrepo/model.go
@@ -12,6 +12,10 @@ type GitRepository struct {
Token string `json:"token" search:"token,password,credentials,secret,auth"` // encrypted
SSHKey string `json:"sshKey" search:"ssh,key,private,public,certificate"` // encrypted
SSHHostKeyVerification string `json:"sshHostKeyVerification" gorm:"default:accept_new"` // strict, accept_new, skip
+ CommitAuthorName string `json:"commitAuthorName" gorm:"column:commit_author_name"`
+ CommitAuthorEmail string `json:"commitAuthorEmail" gorm:"column:commit_author_email"`
+ SigningKey string `json:"signingKey" gorm:"column:signing_key"` // encrypted
+ SigningKeyPassphrase string `json:"signingKeyPassphrase" gorm:"column:signing_key_passphrase"` // encrypted
Description *string `json:"description,omitempty" sortable:"true"`
Enabled bool `json:"enabled" sortable:"true" search:"enabled,active,disabled"`
}
@@ -29,3 +33,8 @@ func (r GitRepository) HasToken() bool {
func (r GitRepository) HasSshKey() bool {
return r.SSHKey != ""
}
+
+// HasSigningKey reports whether an OpenPGP signing key is stored.
+func (r GitRepository) HasSigningKey() bool {
+ return r.SigningKey != ""
+}
diff --git a/backend/internal/gitrepo/service.go b/backend/internal/gitrepo/service.go
index 3d6ab91fb..05cfb859c 100644
--- a/backend/internal/gitrepo/service.go
+++ b/backend/internal/gitrepo/service.go
@@ -115,6 +115,11 @@ func (s *GitRepositoryService) FindEnabledRepositoryByURL(ctx context.Context, r
return nil, nil
}
+const (
+ defaultCommitAuthorName = "Arcane"
+ defaultCommitAuthorEmail = "arcane@localhost"
+)
+
func (s *GitRepositoryService) CreateRepository(ctx context.Context, req gitops.CreateRepositoryRequest, actor common.User) (*GitRepository, error) {
if err := normalization.Normalize(&req); err != nil {
return nil, err
@@ -125,6 +130,8 @@ func (s *GitRepositoryService) CreateRepository(ctx context.Context, req gitops.
AuthType: req.AuthType,
Username: req.Username,
SSHHostKeyVerification: req.SSHHostKeyVerification,
+ CommitAuthorName: req.CommitAuthorName,
+ CommitAuthorEmail: req.CommitAuthorEmail,
Description: req.Description,
Enabled: true,
}
@@ -155,6 +162,15 @@ func (s *GitRepositoryService) CreateRepository(ctx context.Context, req gitops.
repository.SSHKey = encrypted
}
+ if req.SigningKey != "" {
+ signingKey, passphrase, err := encryptSigningKeyInternal(req.SigningKey, req.SigningKeyPassphrase)
+ if err != nil {
+ return nil, err
+ }
+ repository.SigningKey = signingKey
+ repository.SigningKeyPassphrase = passphrase
+ }
+
if err := s.db.WithContext(ctx).Create(&repository).Error; err != nil {
return nil, errors.WrapIf(err, "failed to create repository")
}
@@ -224,6 +240,12 @@ func (s *GitRepositoryService) UpdateRepository(ctx context.Context, id string,
if req.SSHHostKeyVerification != nil {
updates["ssh_host_key_verification"] = *req.SSHHostKeyVerification
}
+ if req.CommitAuthorName != nil {
+ updates["commit_author_name"] = *req.CommitAuthorName
+ }
+ if req.CommitAuthorEmail != nil {
+ updates["commit_author_email"] = *req.CommitAuthorEmail
+ }
if req.Token != nil {
if *req.Token == "" {
@@ -249,6 +271,10 @@ func (s *GitRepositoryService) UpdateRepository(ctx context.Context, id string,
}
}
+ if err := applySigningKeyUpdateInternal(repository, req, updates); err != nil {
+ return nil, err
+ }
+
if len(updates) > 0 {
if err := s.db.WithContext(ctx).Model(repository).Updates(updates).Error; err != nil {
return nil, errors.WrapIf(err, "failed to update repository")
@@ -382,6 +408,98 @@ func (s *GitRepositoryService) GetAuthConfig(ctx context.Context, repository *Gi
return authConfig, nil
}
+// GetCommitIdentity returns the author for commits Arcane pushes to the
+// repository, with the signing key unlocked when one is stored.
+func (s *GitRepositoryService) GetCommitIdentity(ctx context.Context, repository *GitRepository) (git.CommitIdentity, error) {
+ identity := git.CommitIdentity{Name: repository.CommitAuthorName, Email: repository.CommitAuthorEmail}
+ if identity.Name == "" {
+ identity.Name = defaultCommitAuthorName
+ }
+ if identity.Email == "" {
+ identity.Email = defaultCommitAuthorEmail
+ }
+ if repository.SigningKey == "" {
+ return identity, nil
+ }
+ armored, err := crypto.Decrypt(repository.SigningKey)
+ if err != nil {
+ return identity, errors.WrapIf(err, "failed to decrypt signing key")
+ }
+ passphrase := ""
+ if repository.SigningKeyPassphrase != "" {
+ if passphrase, err = crypto.Decrypt(repository.SigningKeyPassphrase); err != nil {
+ return identity, errors.WrapIf(err, "failed to decrypt signing key passphrase")
+ }
+ }
+ identity.SignKey, err = git.ParseSigningKey(armored, passphrase)
+ if err != nil {
+ return identity, err
+ }
+ return identity, nil
+}
+
+// encryptSigningKeyInternal validates that armored unlocks with passphrase and
+// returns both encrypted for storage.
+func encryptSigningKeyInternal(armored, passphrase string) (string, string, error) {
+ if _, err := git.ParseSigningKey(armored, passphrase); err != nil {
+ return "", "", common.Classify(common.ErrValidation, errors.WithDetails(err, "field", "signingKey"))
+ }
+ encryptedKey, err := crypto.Encrypt(armored)
+ if err != nil {
+ return "", "", errors.WrapIf(err, "failed to encrypt signing key")
+ }
+ encryptedPassphrase := ""
+ if passphrase != "" {
+ if encryptedPassphrase, err = crypto.Encrypt(passphrase); err != nil {
+ return "", "", errors.WrapIf(err, "failed to encrypt signing key passphrase")
+ }
+ }
+ return encryptedKey, encryptedPassphrase, nil
+}
+
+// applySigningKeyUpdateInternal resolves the signing key and passphrase an
+// update leaves in place, re-validating the pair whenever either changes.
+func applySigningKeyUpdateInternal(current *GitRepository, req gitops.UpdateRepositoryRequest, updates map[string]any) error {
+ if req.SigningKey == nil && req.SigningKeyPassphrase == nil {
+ return nil
+ }
+ if req.SigningKey != nil && *req.SigningKey == "" {
+ updates["signing_key"] = ""
+ updates["signing_key_passphrase"] = ""
+ return nil
+ }
+ armored := ""
+ if req.SigningKey != nil {
+ armored = *req.SigningKey
+ } else if current.SigningKey != "" {
+ decrypted, err := crypto.Decrypt(current.SigningKey)
+ if err != nil {
+ return errors.WrapIf(err, "failed to decrypt signing key")
+ }
+ armored = decrypted
+ }
+ if armored == "" {
+ return nil
+ }
+ passphrase := ""
+ if req.SigningKeyPassphrase != nil {
+ passphrase = *req.SigningKeyPassphrase
+ } else if current.SigningKeyPassphrase != "" {
+ decrypted, err := crypto.Decrypt(current.SigningKeyPassphrase)
+ if err != nil {
+ return errors.WrapIf(err, "failed to decrypt signing key passphrase")
+ }
+ passphrase = decrypted
+ }
+ encryptedKey, encryptedPassphrase, err := encryptSigningKeyInternal(armored, passphrase)
+ if err != nil {
+ return err
+ }
+ updates["signing_key"] = encryptedKey
+ updates["signing_key_passphrase"] = encryptedPassphrase
+ return nil
+}
+
func (s *GitRepositoryService) ListBranches(ctx context.Context, id string) ([]gitops.BranchInfo, error) {
settings := s.settingsService.GetSettingsConfig()
listCtx, cancel := context.WithTimeout(ctx, timeouts.GetDuration(settings.GitOperationTimeout.AsInt(), timeouts.DefaultGitOperation))
@@ -433,12 +551,7 @@ func (s *GitRepositoryService) BrowseFiles(ctx context.Context, id, branch, path
if err != nil {
return nil, errors.WrapIf(err, "failed to clone repository")
}
- defer func() {
- if cleanupErr := s.Cleanup(repoPath); cleanupErr != nil {
- // Log cleanup error but don't fail the operation
- _ = cleanupErr
- }
- }()
+ defer s.Discard(ctx, repoPath)
// Browse the tree
files, err := s.BrowseTree(ctx, repoPath, path)
@@ -500,7 +613,10 @@ func (s *GitRepositoryService) processSyncItem(ctx context.Context, item gitops.
}
func (s *GitRepositoryService) updateExistingRepository(ctx context.Context, item gitops.RepositorySync, existing *GitRepository) error {
- needsUpdate := s.checkRepositoryNeedsUpdate(item, existing)
+ needsUpdate, err := s.checkRepositoryNeedsUpdate(item, existing)
+ if err != nil {
+ return errors.WrapIff(err, "failed to reconcile repository %s", item.ID)
+ }
if needsUpdate {
// Use Save to trigger GORM callbacks including UpdatedAt
@@ -512,38 +628,65 @@ func (s *GitRepositoryService) updateExistingRepository(ctx context.Context, ite
return nil
}
-func (s *GitRepositoryService) checkRepositoryNeedsUpdate(item gitops.RepositorySync, existing *GitRepository) bool {
+func (s *GitRepositoryService) checkRepositoryNeedsUpdate(item gitops.RepositorySync, existing *GitRepository) (bool, error) {
needsUpdate := utils.ApplyChanged(&existing.Name, mo.Some(item.Name))
needsUpdate = utils.ApplyChanged(&existing.URL, mo.Some(item.URL)) || needsUpdate
needsUpdate = utils.ApplyChanged(&existing.AuthType, mo.Some(item.AuthType)) || needsUpdate
needsUpdate = utils.ApplyChanged(&existing.Username, mo.Some(item.Username)) || needsUpdate
needsUpdate = utils.ApplyChanged(&existing.SSHHostKeyVerification, mo.Some(item.SSHHostKeyVerification)) || needsUpdate
+ needsUpdate = utils.ApplyChanged(&existing.CommitAuthorName, mo.Some(item.CommitAuthorName)) || needsUpdate
+ needsUpdate = utils.ApplyChanged(&existing.CommitAuthorEmail, mo.Some(item.CommitAuthorEmail)) || needsUpdate
needsUpdate = utils.ApplyNullable(&existing.Description, mo.PointerToOption(item.Description)) || needsUpdate
needsUpdate = utils.ApplyChanged(&existing.Enabled, mo.Some(item.Enabled)) || needsUpdate
- // Handle Token update
- if item.Token != "" {
- encryptedToken, err := crypto.Encrypt(item.Token)
- if err == nil {
- needsUpdate = utils.ApplyChanged(&existing.Token, mo.Some(encryptedToken)) || needsUpdate
+ signingKey, passphrase := item.SigningKey, item.SigningKeyPassphrase
+ if signingKey != "" {
+ if _, err := git.ParseSigningKey(signingKey, passphrase); err != nil {
+ signingKey, passphrase = "", ""
}
- } else if existing.Token != "" {
- existing.Token = ""
- needsUpdate = true
}
-
- // Handle SSH Key update
- if item.SSHKey != "" {
- encryptedSSHKey, err := crypto.Encrypt(item.SSHKey)
- if err == nil {
- needsUpdate = utils.ApplyChanged(&existing.SSHKey, mo.Some(encryptedSSHKey)) || needsUpdate
+ for _, credential := range []struct {
+ field *string
+ plaintext string
+ }{
+ {&existing.Token, item.Token},
+ {&existing.SSHKey, item.SSHKey},
+ {&existing.SigningKey, signingKey},
+ {&existing.SigningKeyPassphrase, passphrase},
+ } {
+ changed, err := applyEncryptedInternal(credential.field, credential.plaintext)
+ if err != nil {
+ return false, err
}
- } else if existing.SSHKey != "" {
- existing.SSHKey = ""
- needsUpdate = true
+ needsUpdate = changed || needsUpdate
}
+ return needsUpdate, nil
+}
- return needsUpdate
+// applyEncryptedInternal stores plaintext encrypted in field only when it differs from the stored value.
+func applyEncryptedInternal(field *string, plaintext string) (bool, error) {
+ if plaintext == "" {
+ if *field == "" {
+ return false, nil
+ }
+ *field = ""
+ return true, nil
+ }
+ if *field != "" {
+ current, err := crypto.Decrypt(*field)
+ if err != nil {
+ return false, errors.WrapIf(err, "failed to decrypt stored credential")
+ }
+ if current == plaintext {
+ return false, nil
+ }
+ }
+ encrypted, err := crypto.Encrypt(plaintext)
+ if err != nil {
+ return false, errors.WrapIf(err, "failed to encrypt credential")
+ }
+ *field = encrypted
+ return true, nil
}
func (s *GitRepositoryService) createNewRepository(ctx context.Context, item gitops.RepositorySync) error {
@@ -564,6 +707,14 @@ func (s *GitRepositoryService) createNewRepository(ctx context.Context, item git
}
}
+ var encryptedSigningKey, encryptedPassphrase string
+ if item.SigningKey != "" {
+ encryptedSigningKey, encryptedPassphrase, err = encryptSigningKeyInternal(item.SigningKey, item.SigningKeyPassphrase)
+ if err != nil {
+ return errors.WrapIff(err, "failed to encrypt signing key for repository %s", item.ID)
+ }
+ }
+
sshHostKeyVerification := item.SSHHostKeyVerification
if sshHostKeyVerification == "" {
sshHostKeyVerification = "accept_new"
@@ -577,6 +728,10 @@ func (s *GitRepositoryService) createNewRepository(ctx context.Context, item git
Token: encryptedToken,
SSHKey: encryptedSSHKey,
SSHHostKeyVerification: sshHostKeyVerification,
+ CommitAuthorName: item.CommitAuthorName,
+ CommitAuthorEmail: item.CommitAuthorEmail,
+ SigningKey: encryptedSigningKey,
+ SigningKeyPassphrase: encryptedPassphrase,
Description: item.Description,
Enabled: item.Enabled,
ID: item.ID,
diff --git a/backend/pkg/gitutil/git.go b/backend/pkg/gitutil/git.go
index 6e5715a47..9aa0919ad 100644
--- a/backend/pkg/gitutil/git.go
+++ b/backend/pkg/gitutil/git.go
@@ -561,6 +561,13 @@ func (c *Client) Cleanup(repoPath string) error {
return os.RemoveAll(repoPath)
}
+// Discard removes a scratch checkout, logging instead of failing when removal breaks.
+func (c *Client) Discard(ctx context.Context, repoPath string) {
+ if err := c.Cleanup(repoPath); err != nil {
+ slog.WarnContext(ctx, "Failed to cleanup repository", "path", repoPath, "error", err)
+ }
+}
+
// PurgeScratchDirs removes clone scratch dirs ("gitops-*") under the work dir
// whose mtime is older than maxAge. maxAge <= 0 removes all (boot sweep).
func (c *Client) PurgeScratchDirs(ctx context.Context, maxAge time.Duration) (int, error) {
@@ -760,7 +767,7 @@ func (c *Client) appendSyncFile(ctx context.Context, syncDir string, entry acfst
}
fileSize := int64(len(content))
- isBinary := isBinaryContent(content)
+ isBinary := IsBinaryContent(content)
if isBinary && limits.maxBinarySize > 0 && fileSize > limits.maxBinarySize {
result.SkippedBinaries++
@@ -798,12 +805,11 @@ func (c *Client) isBinarySyncFile(ctx context.Context, syncDir, logicalPath stri
return false, err
}
- return isBinaryContent(buf[:n]), nil
+ return IsBinaryContent(buf[:n]), nil
}
-// isBinaryContent detects if content is binary using HTTP content type detection.
-// Returns true for binary content, false for text content.
-func isBinaryContent(content []byte) bool {
+// IsBinaryContent reports whether content looks binary rather than text.
+func IsBinaryContent(content []byte) bool {
if len(content) == 0 {
return false
}
diff --git a/backend/pkg/gitutil/git_test.go b/backend/pkg/gitutil/git_test.go
index 9a2cb0445..651c77b8f 100644
--- a/backend/pkg/gitutil/git_test.go
+++ b/backend/pkg/gitutil/git_test.go
@@ -535,7 +535,7 @@ func TestWalkDirectory_MaxTotalSizeUnlimited(t *testing.T) {
func TestWalkDirectory_MaxBinarySizeSkips(t *testing.T) {
tmpDir := t.TempDir()
writeFileInternal(t, tmpDir, "compose.yaml", minimalCompose())
- // Binary content: null bytes cause isBinaryContent to return true
+ // Binary content: null bytes cause IsBinaryContent to return true
binaryContent := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13}
writeFileInternal(t, tmpDir, "data.bin", binaryContent)
diff --git a/backend/pkg/gitutil/git_write.go b/backend/pkg/gitutil/git_write.go
index 82d3bb6a4..37fbf8bbe 100644
--- a/backend/pkg/gitutil/git_write.go
+++ b/backend/pkg/gitutil/git_write.go
@@ -13,6 +13,7 @@ import (
"time"
"emperror.dev/errors"
+ "github.com/ProtonMail/go-crypto/openpgp"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
@@ -20,10 +21,17 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport"
"go.getarcane.app/acfs"
+ acfstypes "go.getarcane.app/acfs/types"
)
-// ErrPushRejected reports that the remote branch advanced between checkout and push.
-var ErrPushRejected = errors.Sentinel("push rejected: remote branch was updated by another writer")
+var (
+ // ErrPushRejected reports that the remote branch advanced between checkout and push.
+ ErrPushRejected = errors.Sentinel("push rejected: remote branch was updated by another writer")
+ ErrInvalidCommit = errors.Sentinel("invalid commit hash")
+ ErrSelectionUnreadable = errors.Sentinel("selection is unreadable")
+ ErrSelectionLimits = errors.Sentinel("selection exceeds limits")
+ ErrSelectionInvalid = errors.Sentinel("selection is invalid")
+)
// WriteCheckout is a scratch clone prepared for committing to one branch.
type WriteCheckout struct {
@@ -42,6 +50,95 @@ type CommitFile struct {
Executable bool
}
+// CollectOptions bounds CollectFiles; SkipDir and SkipFile see base names while directories expand.
+type CollectOptions struct {
+ MaxFiles int
+ MaxTotalSize int64
+ SkipDir func(name string) bool
+ SkipFile func(name string) bool
+}
+
+type fileCollectorInternal struct {
+ root string
+ opts CollectOptions
+ files []CommitFile
+ seen map[string]struct{}
+ totalSize int64
+}
+
+// CollectFiles reads the selected files and directories under root as text commit files, sorted by path.
+func CollectFiles(ctx context.Context, root string, selection []string, opts CollectOptions) ([]CommitFile, error) {
+ c := &fileCollectorInternal{root: root, opts: opts, seen: make(map[string]struct{})}
+ for _, selected := range selection {
+ if err := ctx.Err(); err != nil {
+ return nil, err
+ }
+ logical := "/" + selected
+ entry, err := acfs.Stat(ctx, root, logical, false)
+ if err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil, errors.WrapIff(ErrSelectionUnreadable, "selected path %s does not exist", selected)
+ }
+ return nil, errors.WrapIff(ErrSelectionUnreadable, "cannot inspect %s: %v", selected, err)
+ }
+ if entry.IsSymlink {
+ return nil, errors.WrapIff(ErrSelectionInvalid, "%s is a symbolic link", selected)
+ }
+ if !entry.IsDirectory {
+ if err := c.addInternal(ctx, entry); err != nil {
+ return nil, err
+ }
+ continue
+ }
+ err = acfs.Walk(ctx, root, logical, func(child acfstypes.Entry) error { return c.visitInternal(ctx, child) })
+ if err != nil && !errors.Is(err, ErrSelectionUnreadable) && !errors.Is(err, ErrSelectionLimits) && !errors.Is(err, ErrSelectionInvalid) {
+ return nil, errors.WrapIff(ErrSelectionUnreadable, "cannot walk %s: %v", selected, err)
+ }
+ if err != nil {
+ return nil, err
+ }
+ }
+ if len(c.files) == 0 {
+ return nil, errors.WrapIf(ErrSelectionInvalid, "the selection contains no files")
+ }
+ sort.Slice(c.files, func(i, j int) bool { return c.files[i].Path < c.files[j].Path })
+ return c.files, nil
+}
+
+func (c *fileCollectorInternal) visitInternal(ctx context.Context, child acfstypes.Entry) error {
+ if child.IsDirectory && (child.Name == ".git" || (c.opts.SkipDir != nil && c.opts.SkipDir(child.Name))) {
+ return fs.SkipDir
+ }
+ if child.IsDirectory || child.IsSymlink || (c.opts.SkipFile != nil && c.opts.SkipFile(child.Name)) {
+ return nil
+ }
+ return c.addInternal(ctx, child)
+}
+
+func (c *fileCollectorInternal) addInternal(ctx context.Context, entry acfstypes.Entry) error {
+ relative := strings.TrimPrefix(entry.Path, "/")
+ if _, ok := c.seen[relative]; ok {
+ return nil
+ }
+ if c.opts.MaxFiles > 0 && len(c.files) >= c.opts.MaxFiles {
+ return errors.WrapIff(ErrSelectionLimits, "file count limit exceeded (max %d files)", c.opts.MaxFiles)
+ }
+ content, err := acfs.ReadFile(ctx, c.root, entry.Path)
+ if err != nil {
+ return errors.WrapIff(ErrSelectionUnreadable, "cannot read %s: %v", relative, err)
+ }
+ if IsBinaryContent(content) {
+ return errors.WrapIff(ErrSelectionInvalid, "%s is not a text file; only text files can be committed", relative)
+ }
+ c.totalSize += int64(len(content))
+ if c.opts.MaxTotalSize > 0 && c.totalSize > c.opts.MaxTotalSize {
+ return errors.WrapIff(ErrSelectionLimits, "total size limit exceeded (max %d bytes)", c.opts.MaxTotalSize)
+ }
+ c.seen[relative] = struct{}{}
+ c.files = append(c.files, CommitFile{Path: relative, Content: content, Executable: os.FileMode(entry.UnixMode)&0o111 != 0})
+ return nil
+}
+
// CommitRequest describes the tree mutation to commit and push.
type CommitRequest struct {
Files []CommitFile
@@ -49,6 +146,46 @@ type CommitRequest struct {
Message string
AuthorName string
AuthorEmail string
+ SignKey *openpgp.Entity
+}
+
+// CommitIdentity is the author Arcane commits as for one repository.
+type CommitIdentity struct {
+ Name string
+ Email string
+ SignKey *openpgp.Entity
+}
+
+// ParseSigningKey loads an armored OpenPGP private key and unlocks it with
+// passphrase when the key material is encrypted.
+func ParseSigningKey(armored, passphrase string) (*openpgp.Entity, error) {
+ entities, err := openpgp.ReadArmoredKeyRing(strings.NewReader(armored))
+ if err != nil {
+ return nil, errors.WrapIf(err, "signing key is not an armored OpenPGP key")
+ }
+ for _, entity := range entities {
+ if entity.PrivateKey == nil {
+ continue
+ }
+ if entity.PrivateKey.Encrypted {
+ if passphrase == "" {
+ return nil, errors.New("signing key is protected by a passphrase")
+ }
+ if err := entity.PrivateKey.Decrypt([]byte(passphrase)); err != nil {
+ return nil, errors.WrapIf(err, "signing key passphrase is incorrect")
+ }
+ }
+ for _, subkey := range entity.Subkeys {
+ if subkey.PrivateKey == nil || !subkey.PrivateKey.Encrypted {
+ continue
+ }
+ if err := subkey.PrivateKey.Decrypt([]byte(passphrase)); err != nil {
+ return nil, errors.WrapIf(err, "signing key passphrase is incorrect")
+ }
+ }
+ return entity, nil
+ }
+ return nil, errors.New("signing key does not contain a private key")
}
// HistoryEntry is one commit touching a directory.
@@ -171,7 +308,7 @@ func (c *Client) CommitAndPush(ctx context.Context, checkout *WriteCheckout, req
}
signature := &object.Signature{Name: req.AuthorName, Email: req.AuthorEmail, When: time.Now()}
- hash, err := worktree.Commit(req.Message, &git.CommitOptions{Author: signature, Committer: signature})
+ hash, err := worktree.Commit(req.Message, &git.CommitOptions{Author: signature, Committer: signature, SignKey: req.SignKey})
if err != nil {
if errors.Is(err, git.ErrEmptyCommit) {
return checkout.HeadCommit, false, nil
@@ -335,11 +472,18 @@ func (c *Client) CommitDiff(ctx context.Context, repoPath, commitHash, directory
if err := ctx.Err(); err != nil {
return HistoryEntry{}, nil, err
}
+ if len(commitHash) < 7 || len(commitHash) > 64 || strings.ContainsFunc(commitHash, func(r rune) bool { return !strings.ContainsRune("0123456789abcdefABCDEF", r) }) {
+ return HistoryEntry{}, nil, ErrInvalidCommit
+ }
repo, err := git.PlainOpen(repoPath)
if err != nil {
return HistoryEntry{}, nil, errors.WrapIf(err, "failed to open repository")
}
- commit, err := repo.CommitObject(plumbing.NewHash(commitHash))
+ hash, err := repo.ResolveRevision(plumbing.Revision(commitHash))
+ if err != nil {
+ return HistoryEntry{}, nil, errors.WrapIf(err, "commit not found")
+ }
+ commit, err := repo.CommitObject(*hash)
if err != nil {
return HistoryEntry{}, nil, errors.WrapIf(err, "commit not found")
}
diff --git a/backend/resources/migrations/postgres/086_add_git_repository_commit_identity.sql b/backend/resources/migrations/postgres/086_add_git_repository_commit_identity.sql
new file mode 100644
index 000000000..9996da3dd
--- /dev/null
+++ b/backend/resources/migrations/postgres/086_add_git_repository_commit_identity.sql
@@ -0,0 +1,11 @@
+-- +goose Up
+ALTER TABLE git_repositories ADD COLUMN commit_author_name TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN commit_author_email TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN signing_key TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN signing_key_passphrase TEXT NOT NULL DEFAULT '';
+
+-- +goose Down
+ALTER TABLE git_repositories DROP COLUMN IF EXISTS signing_key_passphrase;
+ALTER TABLE git_repositories DROP COLUMN IF EXISTS signing_key;
+ALTER TABLE git_repositories DROP COLUMN IF EXISTS commit_author_email;
+ALTER TABLE git_repositories DROP COLUMN IF EXISTS commit_author_name;
diff --git a/backend/resources/migrations/sqlite/086_add_git_repository_commit_identity.sql b/backend/resources/migrations/sqlite/086_add_git_repository_commit_identity.sql
new file mode 100644
index 000000000..8a303d696
--- /dev/null
+++ b/backend/resources/migrations/sqlite/086_add_git_repository_commit_identity.sql
@@ -0,0 +1,11 @@
+-- +goose Up
+ALTER TABLE git_repositories ADD COLUMN commit_author_name TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN commit_author_email TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN signing_key TEXT NOT NULL DEFAULT '';
+ALTER TABLE git_repositories ADD COLUMN signing_key_passphrase TEXT NOT NULL DEFAULT '';
+
+-- +goose Down
+ALTER TABLE git_repositories DROP COLUMN signing_key_passphrase;
+ALTER TABLE git_repositories DROP COLUMN signing_key;
+ALTER TABLE git_repositories DROP COLUMN commit_author_email;
+ALTER TABLE git_repositories DROP COLUMN commit_author_name;
diff --git a/cli/pkg/repos/cmd.go b/cli/pkg/repos/cmd.go
index 325438988..8347832eb 100644
--- a/cli/pkg/repos/cmd.go
+++ b/cli/pkg/repos/cmd.go
@@ -43,6 +43,10 @@ var (
repoCreateUsername string
repoCreateSSHKey string
repoCreateSSHHostKeyVerify string
+ repoCreateAuthorName string
+ repoCreateAuthorEmail string
+ repoCreateSigningKey string
+ repoCreateSigningKeyPass string
repoCreateDescription string
repoCreateEnabled bool
)
@@ -56,6 +60,10 @@ var (
repoUpdateUsername string
repoUpdateSSHKey string
repoUpdateSSHHostKeyVerify string
+ repoUpdateAuthorName string
+ repoUpdateAuthorEmail string
+ repoUpdateSigningKey string
+ repoUpdateSigningKeyPass string
repoUpdateDescription string
repoUpdateEnabled bool
)
@@ -133,6 +141,22 @@ var createCmd = &cobra.Command{
if cmd.Flags().Changed("ssh-host-key-verification") {
req.SSHHostKeyVerification = repoCreateSSHHostKeyVerify
}
+ if cmd.Flags().Changed("author-name") {
+ req.CommitAuthorName = repoCreateAuthorName
+ }
+ if cmd.Flags().Changed("author-email") {
+ req.CommitAuthorEmail = repoCreateAuthorEmail
+ }
+ if cmd.Flags().Changed("signing-key") {
+ signingKeyData, err := os.ReadFile(repoCreateSigningKey)
+ if err != nil {
+ return errors.WrapIf(err, "failed to read signing key file")
+ }
+ req.SigningKey = string(signingKeyData)
+ }
+ if cmd.Flags().Changed("signing-key-passphrase") {
+ req.SigningKeyPassphrase = repoCreateSigningKeyPass
+ }
if cmd.Flags().Changed("description") {
req.Description = &repoCreateDescription
}
@@ -190,6 +214,10 @@ var getCmd = &cobra.Command{
if resolved.SSHHostKeyVerification != "" {
output.KeyValue("SSH Host Key Verification", resolved.SSHHostKeyVerification)
}
+ if resolved.CommitAuthorName != "" || resolved.CommitAuthorEmail != "" {
+ output.KeyValue("Commit Author", strings.TrimSpace(resolved.CommitAuthorName+" <"+resolved.CommitAuthorEmail+">"))
+ }
+ output.KeyValue("Signing Key", resolved.HasSigningKey)
if resolved.Description != nil {
output.KeyValue("Description", *resolved.Description)
}
@@ -243,6 +271,26 @@ var updateCmd = &cobra.Command{
if cmd.Flags().Changed("ssh-host-key-verification") {
req.SSHHostKeyVerification = &repoUpdateSSHHostKeyVerify
}
+ if cmd.Flags().Changed("author-name") {
+ req.CommitAuthorName = &repoUpdateAuthorName
+ }
+ if cmd.Flags().Changed("author-email") {
+ req.CommitAuthorEmail = &repoUpdateAuthorEmail
+ }
+ if cmd.Flags().Changed("signing-key") {
+ signingKey := ""
+ if repoUpdateSigningKey != "" {
+ signingKeyData, err := os.ReadFile(repoUpdateSigningKey)
+ if err != nil {
+ return errors.WrapIf(err, "failed to read signing key file")
+ }
+ signingKey = string(signingKeyData)
+ }
+ req.SigningKey = &signingKey
+ }
+ if cmd.Flags().Changed("signing-key-passphrase") {
+ req.SigningKeyPassphrase = &repoUpdateSigningKeyPass
+ }
if cmd.Flags().Changed("description") {
req.Description = &repoUpdateDescription
}
@@ -490,6 +538,10 @@ func init() {
createCmd.Flags().StringVar(&repoCreateUsername, "username", "", "Username for HTTP authentication")
createCmd.Flags().StringVar(&repoCreateSSHKey, "ssh-key", "", "Path to SSH key file")
createCmd.Flags().StringVar(&repoCreateSSHHostKeyVerify, "ssh-host-key-verification", "", "SSH host key verification (strict, accept_new, skip)")
+ createCmd.Flags().StringVar(&repoCreateAuthorName, "author-name", "", "Author name for commits Arcane pushes")
+ createCmd.Flags().StringVar(&repoCreateAuthorEmail, "author-email", "", "Author email for commits Arcane pushes")
+ createCmd.Flags().StringVar(&repoCreateSigningKey, "signing-key", "", "Path to an armored OpenPGP private key used to sign commits")
+ createCmd.Flags().StringVar(&repoCreateSigningKeyPass, "signing-key-passphrase", "", "Passphrase for the signing key")
createCmd.Flags().StringVar(&repoCreateDescription, "description", "", "Repository description")
createCmd.Flags().BoolVar(&repoCreateEnabled, "enabled", true, "Enable the repository")
createCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")
@@ -508,6 +560,10 @@ func init() {
updateCmd.Flags().StringVar(&repoUpdateUsername, "username", "", "Username for HTTP authentication")
updateCmd.Flags().StringVar(&repoUpdateSSHKey, "ssh-key", "", "Path to SSH key file")
updateCmd.Flags().StringVar(&repoUpdateSSHHostKeyVerify, "ssh-host-key-verification", "", "SSH host key verification (strict, accept_new, skip)")
+ updateCmd.Flags().StringVar(&repoUpdateAuthorName, "author-name", "", "Author name for commits Arcane pushes")
+ updateCmd.Flags().StringVar(&repoUpdateAuthorEmail, "author-email", "", "Author email for commits Arcane pushes")
+ updateCmd.Flags().StringVar(&repoUpdateSigningKey, "signing-key", "", "Path to an armored OpenPGP private key used to sign commits (empty clears it)")
+ updateCmd.Flags().StringVar(&repoUpdateSigningKeyPass, "signing-key-passphrase", "", "Passphrase for the signing key")
updateCmd.Flags().StringVar(&repoUpdateDescription, "description", "", "Repository description")
updateCmd.Flags().BoolVar(&repoUpdateEnabled, "enabled", true, "Enable the repository")
updateCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")
diff --git a/frontend/messages/en.json b/frontend/messages/en.json
index f4ddc28ff..b7938c9b5 100644
--- a/frontend/messages/en.json
+++ b/frontend/messages/en.json
@@ -3543,6 +3543,14 @@
"git_repository_ssh_key_url_change": "The repository URL has changed. Re-enter the stored SSH key or select Clear stored SSH key before saving.",
"git_repository_clear_token": "Clear stored token",
"git_repository_clear_ssh_key": "Clear stored SSH key",
+ "commit_identity": "Commit identity",
+ "commit_identity_description": "Used for commits Arcane pushes, such as project backups. Leave blank to commit as Arcane.",
+ "commit_author_name": "Author name",
+ "commit_author_email": "Author email",
+ "signing_key": "GPG signing key",
+ "signing_key_placeholder": "Paste an armored OpenPGP private key to sign commits",
+ "signing_key_passphrase": "Signing key passphrase",
+ "git_repository_clear_signing_key": "Clear stored signing key",
"host_ip_placeholder": "0.0.0.0",
"container_port_placeholder": "80",
"protocol_tcp": "TCP",
diff --git a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
index 7d78a6729..db0ae82da 100644
--- a/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
+++ b/frontend/src/lib/components/dialogs/gitops-sync-dialog.svelte
@@ -305,8 +305,8 @@
const isBackupMode = $derived(selectedMode === 'backup');
const directionOptions = [
- { value: 'deploy', label: m.deploy_from_git(), description: m.deploy_from_git_description() },
- { value: 'backup', label: m.back_up_to_git(), description: m.back_up_to_git_description() }
+ { value: 'deploy', label: m.pull(), description: m.deploy_from_git_description() },
+ { value: 'backup', label: m.push(), description: m.back_up_to_git_description() }
] satisfies { value: GitOpsSyncMode; label: string; description: string }[];
const selectedDirection = $derived(directionOptions.find((option) => option.value === selectedMode));
@@ -704,7 +704,7 @@
<form id="sync-form" onsubmit={preventDefault(handleSubmit)} class="grid gap-5 py-2">
{#if isEditMode}
<p class="text-xs text-muted-foreground">
- <span class="font-medium text-foreground">{isBackupMode ? m.back_up_to_git() : m.deploy_from_git()}</span>
+ <span class="font-medium text-foreground">{isBackupMode ? m.push() : m.pull()}</span>
· {isBackupMode ? m.back_up_to_git_description() : m.deploy_from_git_description()}
</p>
{:else}
diff --git a/frontend/src/lib/components/gitops/project-git-backup-summary.svelte b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
index 396a9a34b..7b6d3b187 100644
--- a/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
+++ b/frontend/src/lib/components/gitops/project-git-backup-summary.svelte
@@ -126,53 +126,54 @@
{:else}
<DetailMetaStrip items={metaItems}>
<BackupStateBadge {sync} />
- </DetailMetaStrip>
-
- {#if showError}
- <p class="text-sm break-words text-destructive">{sync.lastSyncError}</p>
- {/if}
- <div class="flex flex-wrap items-center gap-2">
- {#if canSync}
- <ArcaneButton
- action="base"
- tone="outline-primary"
- icon={UploadIcon}
- customLabel={m.back_up_now()}
- loading={backingUp}
- disabled={backingUp || disconnecting}
- onclick={backUpNow}
- />
- {/if}
-
- <RowActionsMenu>
- <DropdownMenu.Item onclick={() => (historyOpen = true)}>
- <ClockIcon class="size-4" />
- {m.history()}
- </DropdownMenu.Item>
-
- <DropdownMenu.Item onclick={() => goto(`/environments/${environmentId}/gitops?action=edit&syncId=${sync.id}`)}>
- <SettingsIcon class="size-4" />
- {m.settings()}
- </DropdownMenu.Item>
-
- {#if needsAttention && canSync}
- <DropdownMenu.Item onclick={() => (resolveOpen = true)}>
- <ShieldAlertIcon class="size-4" />
- {m.resolve()}
- </DropdownMenu.Item>
+ <div class="ml-auto flex items-center gap-2">
+ {#if canSync}
+ <ArcaneButton
+ action="base"
+ tone="outline-primary"
+ size="sm"
+ icon={UploadIcon}
+ customLabel={m.back_up_now()}
+ loading={backingUp}
+ disabled={backingUp || disconnecting}
+ onclick={backUpNow}
+ />
{/if}
- {#if canDelete}
- <DropdownMenu.Separator />
+ <RowActionsMenu>
+ <DropdownMenu.Item onclick={() => (historyOpen = true)}>
+ <ClockIcon class="size-4" />
+ {m.history()}
+ </DropdownMenu.Item>
- <DropdownMenu.Item variant="destructive" disabled={disconnecting} onclick={disconnect}>
- <TrashIcon class="size-4" />
- {m.common_disconnect()}
+ <DropdownMenu.Item onclick={() => goto(`/environments/${environmentId}/gitops?action=edit&syncId=${sync.id}`)}>
+ <SettingsIcon class="size-4" />
+ {m.settings()}
</DropdownMenu.Item>
- {/if}
- </RowActionsMenu>
- </div>
+
+ {#if needsAttention && canSync}
+ <DropdownMenu.Item onclick={() => (resolveOpen = true)}>
+ <ShieldAlertIcon class="size-4" />
+ {m.resolve()}
+ </DropdownMenu.Item>
+ {/if}
+
+ {#if canDelete}
+ <DropdownMenu.Separator />
+
+ <DropdownMenu.Item variant="destructive" disabled={disconnecting} onclick={disconnect}>
+ <TrashIcon class="size-4" />
+ {m.common_disconnect()}
+ </DropdownMenu.Item>
+ {/if}
+ </RowActionsMenu>
+ </div>
+ </DetailMetaStrip>
+
+ {#if showError}
+ <p class="text-sm break-words text-destructive">{sync.lastSyncError}</p>
+ {/if}
{/if}
</DetailSection>
diff --git a/frontend/src/lib/components/sheets/git-repository-sheet.svelte b/frontend/src/lib/components/sheets/git-repository-sheet.svelte
index d84c975a0..9ada661cd 100644
--- a/frontend/src/lib/components/sheets/git-repository-sheet.svelte
+++ b/frontend/src/lib/components/sheets/git-repository-sheet.svelte
@@ -16,6 +16,7 @@
open: boolean;
clearToken?: boolean;
clearSshKey?: boolean;
+ clearSigningKey?: boolean;
repositoryToEdit: GitRepository | null;
onSubmit: (detail: { repository: GitRepositoryCreateDto | GitRepositoryUpdateDto; isEditMode: boolean }) => void;
isLoading: boolean;
@@ -25,6 +26,7 @@
open = $bindable(false),
clearToken = $bindable(false),
clearSshKey = $bindable(false),
+ clearSigningKey = $bindable(false),
repositoryToEdit = $bindable(),
onSubmit,
isLoading
@@ -40,6 +42,10 @@
token: z.string().optional(),
sshKey: z.string().optional(),
sshHostKeyVerification: z.enum(['strict', 'accept_new', 'skip']).default('accept_new'),
+ commitAuthorName: z.string().optional(),
+ commitAuthorEmail: z.union([z.literal(''), z.email(m.common_invalid_email())]).optional(),
+ signingKey: z.string().optional(),
+ signingKeyPassphrase: z.string().optional(),
description: z.string().optional(),
enabled: z.boolean().default(true)
});
@@ -52,6 +58,10 @@
token: '',
sshKey: '',
sshHostKeyVerification: (repositoryToEdit?.sshHostKeyVerification || 'accept_new') as 'strict' | 'accept_new' | 'skip',
+ commitAuthorName: repositoryToEdit?.commitAuthorName || '',
+ commitAuthorEmail: repositoryToEdit?.commitAuthorEmail || '',
+ signingKey: '',
+ signingKeyPassphrase: '',
description: repositoryToEdit?.description || '',
enabled: repositoryToEdit?.enabled ?? true
}));
@@ -61,6 +71,7 @@
let hasToken = $derived(!!repositoryToEdit?.hasToken);
let hasSshKey = $derived(!!repositoryToEdit?.hasSshKey);
+ let hasSigningKey = $derived(!!repositoryToEdit?.hasSigningKey);
let urlChanged = $derived(isEditMode && inputs.url.value.trim() !== repositoryToEdit?.url);
let tokenNeedsAttention = $derived(urlChanged && hasToken && !clearToken && !inputs.token?.value?.trim());
let sshKeyNeedsAttention = $derived(urlChanged && hasSshKey && !clearSshKey && !inputs.sshKey?.value?.trim());
@@ -127,12 +138,21 @@
if (hasSshKey && clearSshKey) payload.sshKey = '';
else if ((selectedAuthType.value === 'ssh' || hasSshKey) && data.sshKey) payload.sshKey = data.sshKey;
+ payload.commitAuthorName = data.commitAuthorName?.trim() ?? '';
+ payload.commitAuthorEmail = data.commitAuthorEmail?.trim() ?? '';
+ if (hasSigningKey && clearSigningKey) payload.signingKey = '';
+ else {
+ if (data.signingKey) payload.signingKey = data.signingKey;
+ if (data.signingKeyPassphrase) payload.signingKeyPassphrase = data.signingKeyPassphrase;
+ }
+
onSubmit({ repository: payload, isEditMode });
}
function handleOpenChange(newOpenState: boolean) {
clearToken = false;
clearSshKey = false;
+ clearSigningKey = false;
open = newOpenState;
}
</script>
@@ -267,6 +287,45 @@
</div>
{/if}
+ <div class="space-y-3 border-t border-border/50 pt-4">
+ <div>
+ <p class="text-sm font-medium">{m.commit_identity()}</p>
+ <p class="text-xs text-muted-foreground">{m.commit_identity_description()}</p>
+ </div>
+ <div class="grid gap-3 sm:grid-cols-2">
+ <FormInput label={m.commit_author_name()} type="text" placeholder="Arcane" bind:input={inputs.commitAuthorName} />
+ <FormInput
+ label={m.commit_author_email()}
+ type="text"
+ placeholder="arcane@localhost"
+ bind:input={inputs.commitAuthorEmail}
+ />
+ </div>
+ <FormInput
+ label={m.signing_key()}
+ type="textarea"
+ placeholder={hasSigningKey ? m.common_keep_placeholder() : m.signing_key_placeholder()}
+ disabled={clearSigningKey}
+ rows={4}
+ bind:input={inputs.signingKey}
+ />
+ {#if hasSigningKey}
+ <SwitchWithLabel
+ id="clearRepositorySigningKey"
+ label={m.git_repository_clear_signing_key()}
+ bind:checked={clearSigningKey}
+ />
+ {/if}
+ {#if !clearSigningKey}
+ <FormInput
+ label={m.signing_key_passphrase()}
+ type="password"
+ placeholder={hasSigningKey ? m.common_keep_placeholder() : ''}
+ bind:input={inputs.signingKeyPassphrase}
+ />
+ {/if}
+ </div>
+
<FormInput
label={m.common_description()}
type="text"
diff --git a/frontend/src/lib/types/automation.ts b/frontend/src/lib/types/automation.ts
index 564552325..d7696aec2 100644
--- a/frontend/src/lib/types/automation.ts
+++ b/frontend/src/lib/types/automation.ts
@@ -86,6 +86,10 @@ export interface GitRepositoryCreateDto {
token?: string;
sshKey?: string;
sshHostKeyVerification?: string;
+ commitAuthorName?: string;
+ commitAuthorEmail?: string;
+ signingKey?: string;
+ signingKeyPassphrase?: string;
description?: string;
enabled?: boolean;
}
@@ -98,6 +102,10 @@ export interface GitRepositoryUpdateDto {
token?: string;
sshKey?: string;
sshHostKeyVerification?: string;
+ commitAuthorName?: string;
+ commitAuthorEmail?: string;
+ signingKey?: string;
+ signingKeyPassphrase?: string;
description?: string;
enabled?: boolean;
}
@@ -109,8 +117,11 @@ export interface GitRepository {
authType: string;
hasToken: boolean;
hasSshKey: boolean;
+ hasSigningKey: boolean;
username?: string;
sshHostKeyVerification?: string;
+ commitAuthorName?: string;
+ commitAuthorEmail?: string;
description?: string;
enabled: boolean;
createdAt: string;
diff --git a/frontend/src/routes/(app)/customize/git-repositories/+page.svelte b/frontend/src/routes/(app)/customize/git-repositories/+page.svelte
index 16a306ede..a34f86be6 100644
--- a/frontend/src/routes/(app)/customize/git-repositories/+page.svelte
+++ b/frontend/src/routes/(app)/customize/git-repositories/+page.svelte
@@ -18,6 +18,7 @@
let repositorySession = $state(0);
let clearToken = $state(false);
let clearSshKey = $state(false);
+ let clearSigningKey = $state(false);
let repositoryToEdit = $state<GitRepository | null>(null);
let requestOptions = $derived(data.repositoryRequestOptions);
@@ -44,6 +45,7 @@
repositoryToEdit = null;
clearToken = false;
clearSshKey = false;
+ clearSigningKey = false;
repositorySession += 1;
isRepositoryDialogOpen = true;
}
@@ -52,6 +54,7 @@
repositoryToEdit = repository;
clearToken = false;
clearSshKey = false;
+ clearSigningKey = false;
repositorySession += 1;
isRepositoryDialogOpen = true;
}
@@ -78,6 +81,7 @@
repositories = await gitRepositoryService.getRepositories(requestOptions);
clearToken = false;
clearSshKey = false;
+ clearSigningKey = false;
isRepositoryDialogOpen = false;
})()
);
@@ -127,6 +131,7 @@
<GitRepositoryFormSheet
bind:clearToken
bind:clearSshKey
+ bind:clearSigningKey
bind:open={isRepositoryDialogOpen}
bind:repositoryToEdit
onSubmit={handleRepositoryDialogSubmit}
diff --git a/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte b/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
index 983ea61e5..482adab16 100644
--- a/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
+++ b/frontend/src/routes/(app)/environments/[id]/gitops/sync-table.svelte
@@ -291,10 +291,10 @@
<span class="inline-flex items-center gap-1.5 text-sm">
{#if isBackup(item)}
<UploadIcon class="size-3.5 text-muted-foreground" />
- {m.back_up_to_git()}
+ {m.push()}
{:else}
<DownloadIcon class="size-3.5 text-muted-foreground" />
- {m.deploy_from_git()}
+ {m.pull()}
{/if}
</span>
{/snippet}
@@ -412,7 +412,7 @@
fields={[
{
label: m.direction(),
- getValue: (item: GitOpsSync) => (isBackup(item) ? m.back_up_to_git() : m.deploy_from_git()),
+ getValue: (item: GitOpsSync) => (isBackup(item) ? m.push() : m.pull()),
icon: isBackup(item) ? UploadIcon : DownloadIcon,
iconVariant: 'gray' as const,
show: mobileFieldVisibility['mode'] ?? true
diff --git a/types/gitops/gitops.go b/types/gitops/gitops.go
index 939998dc5..6822d7f04 100644
--- a/types/gitops/gitops.go
+++ b/types/gitops/gitops.go
@@ -36,9 +36,6 @@ const (
BackupConflictUseArcane = "use_arcane"
)
-// BackupManifestFileName is the manifest written beside backed-up files.
-const BackupManifestFileName = ".arcane-backup.json"
-
// GitRepository represents a reusable Git repository with credentials.
type GitRepository struct {
// ID of the git repository.
@@ -77,6 +74,19 @@ type GitRepository struct {
// Required: false
SSHHostKeyVerification string `json:"sshHostKeyVerification,omitempty"`
+ // CommitAuthorName is the author name on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorName string `json:"commitAuthorName,omitempty"`
+
+ // CommitAuthorEmail is the author email on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorEmail string `json:"commitAuthorEmail,omitempty"`
+
+ // HasSigningKey indicates whether an OpenPGP signing key is stored.
+ HasSigningKey bool `json:"hasSigningKey"`
+
// Description of the git repository.
//
// Required: false
@@ -401,6 +411,26 @@ type CreateRepositoryRequest struct {
// Required: false
SSHHostKeyVerification string `json:"sshHostKeyVerification,omitempty" binding:"omitempty,oneof=strict accept_new skip"`
+ // CommitAuthorName is the author name on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorName string `json:"commitAuthorName,omitempty" unorm:"nfc" trim:"true"`
+
+ // CommitAuthorEmail is the author email on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorEmail string `json:"commitAuthorEmail,omitempty" binding:"omitempty,email" trim:"true"`
+
+ // SigningKey is an armored OpenPGP private key used to sign commits.
+ //
+ // Required: false
+ SigningKey string `json:"signingKey,omitempty"`
+
+ // SigningKeyPassphrase unlocks SigningKey when it is passphrase-protected.
+ //
+ // Required: false
+ SigningKeyPassphrase string `json:"signingKeyPassphrase,omitempty"`
+
// Description of the git repository.
//
// Required: false
@@ -450,6 +480,26 @@ type UpdateRepositoryRequest struct {
// Required: false
SSHHostKeyVerification *string `json:"sshHostKeyVerification,omitzero" binding:"omitempty,oneof=strict accept_new skip"`
+ // CommitAuthorName is the author name on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorName *string `json:"commitAuthorName,omitzero" unorm:"nfc" trim:"true"`
+
+ // CommitAuthorEmail is the author email on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorEmail *string `json:"commitAuthorEmail,omitzero" binding:"omitempty,email" trim:"true"`
+
+ // SigningKey is an armored OpenPGP private key used to sign commits. Empty clears it.
+ //
+ // Required: false
+ SigningKey *string `json:"signingKey,omitzero"`
+
+ // SigningKeyPassphrase unlocks SigningKey when it is passphrase-protected. Empty clears it.
+ //
+ // Required: false
+ SigningKeyPassphrase *string `json:"signingKeyPassphrase,omitzero"`
+
// Description of the git repository.
//
// Required: false
@@ -956,6 +1006,26 @@ type RepositorySync struct {
// Required: false
SSHHostKeyVerification string `json:"sshHostKeyVerification,omitempty"`
+ // CommitAuthorName is the author name on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorName string `json:"commitAuthorName,omitempty"`
+
+ // CommitAuthorEmail is the author email on commits Arcane pushes.
+ //
+ // Required: false
+ CommitAuthorEmail string `json:"commitAuthorEmail,omitempty"`
+
+ // SigningKey is the armored OpenPGP private key used to sign commits.
+ //
+ // Required: false
+ SigningKey string `json:"signingKey,omitempty"`
+
+ // SigningKeyPassphrase unlocks SigningKey.
+ //
+ // Required: false
+ SigningKeyPassphrase string `json:"signingKeyPassphrase,omitempty"`
+
// Description of the git repository.
//
// Required: false
@@ -1123,45 +1193,6 @@ type ImportGitOpsSyncResponse struct {
Errors []string `json:"errors"`
}
-// BackupManifest is written beside backed-up files so a backup can be
-// recognized and recovered without Arcane's database.
-type BackupManifest struct {
- // Version of the manifest format.
- //
- // Required: true
- Version int `json:"version"`
-
- // SyncID is the Arcane sync that produced the backup.
- //
- // Required: true
- SyncID string `json:"syncId"`
-
- // ProjectName is the name of the backed-up project.
- //
- // Required: true
- ProjectName string `json:"projectName"`
-
- // EnvironmentID is the Arcane environment that owns the project.
- //
- // Required: true
- EnvironmentID string `json:"environmentId"`
-
- // GeneratedAt is when the snapshot was taken.
- //
- // Required: true
- GeneratedAt time.Time `json:"generatedAt"`
-
- // ComposeFiles lists the backed-up Compose entrypoints and overrides.
- //
- // Required: true
- ComposeFiles []string `json:"composeFiles"`
-
- // Files maps each backed-up path to its SHA-256 content hash.
- //
- // Required: true
- Files map[string]string `json:"files"`
-}
-
// BackupFileChange describes one file difference between Arcane and the repository.
type BackupFileChange struct {
// Path relative to the backup directory.