You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
253 lines
8.8 KiB
Go
253 lines
8.8 KiB
Go
package update
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
testCurrent = "1111111111111111111111111111111111111111"
|
|
testRemote = "2222222222222222222222222222222222222222"
|
|
)
|
|
|
|
type recordedCommand struct {
|
|
directory string
|
|
command string
|
|
args []string
|
|
env []string
|
|
}
|
|
|
|
type fakeExecutor struct {
|
|
directory string
|
|
changed string
|
|
failMatch string
|
|
mu sync.Mutex
|
|
calls []recordedCommand
|
|
}
|
|
|
|
func (f *fakeExecutor) Run(_ context.Context, directory string, environment []string, command string, arguments ...string) ([]byte, error) {
|
|
f.mu.Lock()
|
|
f.calls = append(f.calls, recordedCommand{directory: directory, command: command, args: append([]string(nil), arguments...), env: append([]string(nil), environment...)})
|
|
f.mu.Unlock()
|
|
joined := command + " " + strings.Join(arguments, " ")
|
|
if f.failMatch != "" && strings.Contains(joined, f.failMatch) {
|
|
return nil, errors.New("sensitive fake command diagnostic")
|
|
}
|
|
if command == "git" {
|
|
switch {
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "rev-parse --show-toplevel"):
|
|
return []byte(f.directory + "\n"), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "symbolic-ref"):
|
|
return []byte("go\n"), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "remote get-url"):
|
|
return []byte(RequiredRemoteURL + "\n"), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "status"):
|
|
return nil, nil
|
|
case strings.Join(arguments, " ") == "rev-parse HEAD":
|
|
return []byte(testCurrent + "\n"), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "ls-remote"):
|
|
return []byte(testRemote + "\trefs/heads/go\n"), nil
|
|
case strings.Join(arguments, " ") == "rev-parse refs/remotes/origin/go":
|
|
return []byte(testRemote + "\n"), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "diff --name-only"):
|
|
return []byte(f.changed), nil
|
|
case strings.HasPrefix(strings.Join(arguments, " "), "ls-tree -z"):
|
|
return []byte("100644 blob 3333333333333333333333333333333333333333\tweb/xcat\x00"), nil
|
|
default:
|
|
return nil, nil
|
|
}
|
|
}
|
|
if command == "systemctl" && len(arguments) > 0 && arguments[0] == "show" {
|
|
return []byte("LoadState=loaded\nActiveState=active\n"), nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func newTestManager(t *testing.T, executor *fakeExecutor) (*Manager, *StateStore) {
|
|
t.Helper()
|
|
directory := t.TempDir()
|
|
executor.directory = directory
|
|
if err := os.MkdirAll(filepath.Join(directory, "web"), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(directory, "web", "xcat"), []byte("<?php\n"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
config := validTestConfig(directory)
|
|
state, err := OpenState(config.StatePath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manager, err := NewManager(config, state, executor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return manager, state
|
|
}
|
|
|
|
func waitJob(t *testing.T, state *StateStore, id string) Job {
|
|
t.Helper()
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
job, _ := state.Job(id)
|
|
if job.Status == "succeeded" || job.Status == "failed" || job.Status == "manual_intervention" {
|
|
return job
|
|
}
|
|
time.Sleep(5 * time.Millisecond)
|
|
}
|
|
job, _ := state.Job(id)
|
|
t.Fatalf("job did not finish: %#v", job)
|
|
return Job{}
|
|
}
|
|
|
|
func TestCheckIsReadOnlyAndDoesNotPutTokenInArguments(t *testing.T) {
|
|
executor := &fakeExecutor{}
|
|
manager, state := newTestManager(t, executor)
|
|
if err := state.SetAccessToken("private-token-value"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := manager.Check(context.Background())
|
|
if err != nil || result.CurrentCommit != testCurrent || result.RemoteCommit != testRemote || !result.UpdateAvailable ||
|
|
result.Status != "update_available" || result.Repository != RequiredRemoteURL {
|
|
t.Fatalf("check result=%#v err=%v", result, err)
|
|
}
|
|
for _, call := range executor.calls {
|
|
if strings.Contains(strings.Join(call.args, " "), "private-token-value") {
|
|
t.Fatal("access token was placed in process arguments")
|
|
}
|
|
if call.command == "git" && len(call.args) > 0 && call.args[0] == "fetch" {
|
|
t.Fatal("read-only check fetched into the repository")
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestApplyRunsCheckpointedFastForward(t *testing.T) {
|
|
executor := &fakeExecutor{changed: "web/app.php\x00gateway/update/server.go\x00"}
|
|
manager, state := newTestManager(t, executor)
|
|
expectedWebDirectory, err := filepath.EvalSymlinks(filepath.Join(executor.directory, "web"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job, err := manager.StartApply(testCurrent, testRemote)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job = waitJob(t, state, job.ID)
|
|
if job.Status != "succeeded" || job.Checkpoint != "completed" || job.PreviousCommit != testCurrent || job.AppliedCommit != testRemote {
|
|
t.Fatalf("unexpected completed job: %#v", job)
|
|
}
|
|
for _, call := range executor.calls {
|
|
if call.command == "/usr/bin/php" && strings.Join(call.args, " ") == "xcat cloudInit" &&
|
|
call.directory != expectedWebDirectory {
|
|
t.Fatalf("schema initialization ran in %q instead of web directory", call.directory)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestApplyRejectsDependencyAndDeployChangesBeforeStopping(t *testing.T) {
|
|
for _, changed := range []string{"deploy/virt-cloud.service\x00", "gateway/go.sum\x00", "web/composer.lock\x00"} {
|
|
executor := &fakeExecutor{changed: changed}
|
|
manager, state := newTestManager(t, executor)
|
|
job, err := manager.StartApply(testCurrent, testRemote)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job = waitJob(t, state, job.ID)
|
|
if job.Status != "failed" || job.ErrorCode != "manual_upgrade_required" {
|
|
t.Fatalf("changed=%q job=%#v", changed, job)
|
|
}
|
|
for _, call := range executor.calls {
|
|
if call.command == "systemctl" {
|
|
t.Fatalf("services were touched for denied change %q", changed)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSchemaInitializationFailureHoldsMaintenanceLock(t *testing.T) {
|
|
executor := &fakeExecutor{changed: "web/app.php\x00", failMatch: "/usr/bin/php xcat cloudInit"}
|
|
manager, state := newTestManager(t, executor)
|
|
job, err := manager.StartApply(testCurrent, testRemote)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job = waitJob(t, state, job.ID)
|
|
if job.Status != "manual_intervention" || job.ErrorCode != "schema_init_result_uncertain" || !state.MaintenanceLocked() {
|
|
t.Fatalf("schema initialization failure was not fail-closed: %#v lock=%v", job, state.MaintenanceLocked())
|
|
}
|
|
if _, err := manager.StartApply(testCurrent, testRemote); err == nil {
|
|
t.Fatal("new apply accepted while manual reconciliation is required")
|
|
}
|
|
}
|
|
|
|
func TestPreflightFailureStopsBeforeAnyMutation(t *testing.T) {
|
|
executor := &fakeExecutor{changed: "web/app.php\x00", failMatch: "systemctl show"}
|
|
manager, state := newTestManager(t, executor)
|
|
job, err := manager.StartApply(testCurrent, testRemote)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job = waitJob(t, state, job.ID)
|
|
if job.Status != "failed" || job.ErrorCode != "host_preflight_failed" || state.MaintenanceLocked() {
|
|
t.Fatalf("preflight failure was not a safe failure: %#v lock=%v", job, state.MaintenanceLocked())
|
|
}
|
|
for _, call := range executor.calls {
|
|
if call.command == "systemctl" && len(call.args) > 0 && (call.args[0] == "stop" || call.args[0] == "start") {
|
|
t.Fatalf("preflight failure mutated service state: %#v", call)
|
|
}
|
|
if call.command == "git" && len(call.args) > 0 && call.args[0] == "merge" {
|
|
t.Fatalf("preflight failure mutated the checkout: %#v", call)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPreflightRejectsSchemaInitializerSymlink(t *testing.T) {
|
|
executor := &fakeExecutor{changed: "web/app.php\x00"}
|
|
manager, state := newTestManager(t, executor)
|
|
xcat := filepath.Join(executor.directory, "web", "xcat")
|
|
if err := os.Remove(xcat); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Symlink("elsewhere", xcat); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job, err := manager.StartApply(testCurrent, testRemote)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job = waitJob(t, state, job.ID)
|
|
if job.Status != "failed" || job.ErrorCode != "host_preflight_failed" {
|
|
t.Fatalf("schema initializer symlink was not rejected before mutation: %#v", job)
|
|
}
|
|
}
|
|
|
|
func TestApplyRejectsNoOpCommitPair(t *testing.T) {
|
|
manager, _ := newTestManager(t, &fakeExecutor{})
|
|
if _, err := manager.StartApply(testCurrent, testCurrent); err == nil {
|
|
t.Fatal("no-op update was accepted")
|
|
}
|
|
}
|
|
|
|
func TestCommandEnvironmentDropsGitAndLoaderInjection(t *testing.T) {
|
|
t.Setenv("GIT_DIR", "/attacker/repository")
|
|
t.Setenv("GIT_CONFIG_KEY_0", "core.hooksPath")
|
|
t.Setenv("GIT_CONFIG_VALUE_0", "/attacker/hooks")
|
|
t.Setenv("DYLD_INSERT_LIBRARIES", "/attacker/library")
|
|
values := commandEnvironment([]string{"GIT_TERMINAL_PROMPT=0"})
|
|
joined := strings.Join(values, "\n")
|
|
for _, forbidden := range []string{"GIT_DIR=", "GIT_CONFIG_KEY_0=", "GIT_CONFIG_VALUE_0=", "DYLD_INSERT_LIBRARIES="} {
|
|
if strings.Contains(joined, forbidden) {
|
|
t.Fatalf("unsafe environment survived: %s", forbidden)
|
|
}
|
|
}
|
|
if !strings.Contains(joined, "GIT_TERMINAL_PROMPT=0") {
|
|
t.Fatal("explicit safe override was lost")
|
|
}
|
|
}
|