diff --git a/pkg/provider/dir.go b/pkg/provider/dir.go index 50cf697fe0..6d25eeae38 100644 --- a/pkg/provider/dir.go +++ b/pkg/provider/dir.go @@ -2,7 +2,6 @@ package provider import ( "bytes" - "encoding/json" "fmt" "os" "path/filepath" @@ -59,16 +58,16 @@ func GetProviderBinariesDir(context, providerName string) (string, error) { } func GetMachineDir(context, machineID string) (string, error) { - if machineID == "" { - return "", fmt.Errorf("machine id is empty") + if err := requireID("machine id", machineID); err != nil { + return "", err } return config.DefaultPathManager().MachineDir(context, machineID) } func GetWorkspaceDir(context, workspaceID string) (string, error) { - if workspaceID == "" { - return "", fmt.Errorf("workspace id is empty") + if err := requireID("workspace id", workspaceID); err != nil { + return "", err } return config.DefaultPathManager().WorkspaceDir(context, workspaceID) @@ -78,8 +77,8 @@ func GetWorkspaceDir(context, workspaceID string) (string, error) { // (.../workspaces//agent). Container-side agent code keeps using // FindAgentHomeDir. func GetWorkspaceAgentDir(context, workspaceID string) (string, error) { - if workspaceID == "" { - return "", fmt.Errorf("workspace id is empty") + if err := requireID("workspace id", workspaceID); err != nil { + return "", err } return config.DefaultPathManager().WorkspaceAgentDir(context, workspaceID) @@ -94,8 +93,8 @@ func GetWorkspaceContentsDir(context string) (string, error) { // GetWorkspaceContentDir returns the host-side bind-mount source for a // workspace (.../contexts//contents/). func GetWorkspaceContentDir(context, workspaceID string) (string, error) { - if workspaceID == "" { - return "", fmt.Errorf("workspace id is empty") + if err := requireID("workspace id", workspaceID); err != nil { + return "", err } return config.DefaultPathManager().WorkspaceContentDir(context, workspaceID) @@ -104,16 +103,16 @@ func GetWorkspaceContentDir(context, workspaceID string) (string, error) { // GetWorkspaceLogDir returns the per-workspace log dir // (.../workspaces//logs) used by the desktop's streaming-log store. func GetWorkspaceLogDir(context, workspaceID string) (string, error) { - if workspaceID == "" { - return "", fmt.Errorf("workspace id is empty") + if err := requireID("workspace id", workspaceID); err != nil { + return "", err } return config.DefaultPathManager().WorkspaceLogDir(context, workspaceID) } func GetProInstanceDir(context, proInstanceHost string) (string, error) { - if proInstanceHost == "" { - return "", fmt.Errorf("pro instance host is empty") + if err := requireID("pro instance host", proInstanceHost); err != nil { + return "", err } return config.DefaultPathManager().ProInstanceDir(context, ToProInstanceID(proInstanceHost)) @@ -158,50 +157,16 @@ func SaveProviderConfig(context string, provider *ProviderConfig) error { return err } - // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. - err = os.MkdirAll(providerDir, 0o755) - if err != nil { - return err - } - - providerDirBytes, err := json.Marshal(provider) - if err != nil { - return err - } - - providerConfigFile := filepath.Join(providerDir, ProviderConfigFile) - err = WriteFileAtomic(providerConfigFile, providerDirBytes, 0o600) - if err != nil { - return err - } - - return nil + return saveJSON(providerDir, ProviderConfigFile, provider, 0o600) } func SaveProInstanceConfig(context string, proInstance *ProInstance) error { - providerDir, err := GetProInstanceDir(context, proInstance.Host) - if err != nil { - return err - } - - // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. - err = os.MkdirAll(providerDir, 0o755) - if err != nil { - return err - } - - proInstanceBytes, err := json.Marshal(proInstance) + proDir, err := GetProInstanceDir(context, proInstance.Host) if err != nil { return err } - proInstanceConfigFile := filepath.Join(providerDir, ProInstanceConfigFile) - err = WriteFileAtomic(proInstanceConfigFile, proInstanceBytes, 0o600) - if err != nil { - return err - } - - return nil + return saveJSON(proDir, ProInstanceConfigFile, proInstance, 0o600) } func SaveWorkspaceResult(workspace *Workspace, result *config2.Result) error { @@ -210,6 +175,8 @@ func SaveWorkspaceResult(workspace *Workspace, result *config2.Result) error { return err } + // Unlike saveJSON, this never creates the workspace dir: a result is only + // valid while the workspace config exists. if _, err := os.Stat(filepath.Join(workspaceDir, WorkspaceConfigFile)); err != nil { if os.IsNotExist(err) { return fmt.Errorf("workspace %s no longer exists", workspace.ID) @@ -217,18 +184,7 @@ func SaveWorkspaceResult(workspace *Workspace, result *config2.Result) error { return err } - resultBytes, err := json.Marshal(result) - if err != nil { - return err - } - - workspaceResultFile := filepath.Join(workspaceDir, WorkspaceResultFile) - err = WriteFileAtomic(workspaceResultFile, resultBytes, 0o600) - if err != nil { - return err - } - - return nil + return writeJSONFile(filepath.Join(workspaceDir, WorkspaceResultFile), result, 0o600) } func SaveWorkspaceConfig(workspace *Workspace) error { @@ -237,25 +193,7 @@ func SaveWorkspaceConfig(workspace *Workspace) error { return err } - // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. - err = os.MkdirAll(workspaceDir, 0o755) - if err != nil { - return err - } - - workspaceConfigBytes, err := json.Marshal(workspace) - if err != nil { - return err - } - - workspaceConfigFile := filepath.Join(workspaceDir, WorkspaceConfigFile) - // #nosec G306 -- TODO Consider using a more secure permission setting and ownership if needed. - err = WriteFileAtomic(workspaceConfigFile, workspaceConfigBytes, 0o644) - if err != nil { - return err - } - - return nil + return saveJSON(workspaceDir, WorkspaceConfigFile, workspace, 0o644) } func SaveMachineConfig(machine *Machine) error { @@ -264,24 +202,7 @@ func SaveMachineConfig(machine *Machine) error { return err } - // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. - err = os.MkdirAll(machineDir, 0o755) - if err != nil { - return err - } - - machineConfigBytes, err := json.Marshal(machine) - if err != nil { - return err - } - - machineConfigFile := filepath.Join(machineDir, MachineConfigFile) - err = WriteFileAtomic(machineConfigFile, machineConfigBytes, 0o600) - if err != nil { - return err - } - - return nil + return saveJSON(machineDir, MachineConfigFile, machine, 0o600) } func MachineExists(context, machineID string) bool { @@ -361,13 +282,7 @@ func LoadMachineConfig(context, machineID string) (*Machine, error) { } machineConfigFile := filepath.Join(machineDir, MachineConfigFile) - machineConfigBytes, err := os.ReadFile(machineConfigFile) - if err != nil { - return nil, err - } - - machineConfig := &Machine{} - err = json.Unmarshal(machineConfigBytes, machineConfig) + machineConfig, err := loadJSON[Machine](machineConfigFile) if err != nil { return nil, err } @@ -383,19 +298,7 @@ func LoadProInstanceConfig(context, proInstanceHost string) (*ProInstance, error return nil, err } - proConfigFile := filepath.Join(proDir, ProInstanceConfigFile) - proConfigBytes, err := os.ReadFile(proConfigFile) - if err != nil { - return nil, err - } - - proInstanceConfig := &ProInstance{} - err = json.Unmarshal(proConfigBytes, proInstanceConfig) - if err != nil { - return nil, err - } - - return proInstanceConfig, nil + return loadJSON[ProInstance](filepath.Join(proDir, ProInstanceConfigFile)) } func LoadWorkspaceConfig(context, workspaceID string) (*Workspace, error) { @@ -405,13 +308,7 @@ func LoadWorkspaceConfig(context, workspaceID string) (*Workspace, error) { } workspaceConfigFile := filepath.Join(workspaceDir, WorkspaceConfigFile) - workspaceConfigBytes, err := os.ReadFile(workspaceConfigFile) - if err != nil { - return nil, err - } - - workspaceConfig := &Workspace{} - err = json.Unmarshal(workspaceConfigBytes, workspaceConfig) + workspaceConfig, err := loadJSON[Workspace](workspaceConfigFile) if err != nil { return nil, err } @@ -427,19 +324,14 @@ func LoadWorkspaceResult(context, workspaceID string) (*config2.Result, error) { return nil, err } - workspaceResultFile := filepath.Join(workspaceDir, WorkspaceResultFile) - workspaceResultBytes, err := os.ReadFile(workspaceResultFile) + workspaceResult, err := loadJSON[config2.Result]( + filepath.Join(workspaceDir, WorkspaceResultFile), + ) if os.IsNotExist(err) { return nil, nil } else if err != nil { return nil, err } - workspaceResult := &config2.Result{} - err = json.Unmarshal(workspaceResultBytes, workspaceResult) - if err != nil { - return nil, err - } - return workspaceResult, nil } diff --git a/pkg/provider/export.go b/pkg/provider/export.go index 4d34aef331..c76b08bf2d 100644 --- a/pkg/provider/export.go +++ b/pkg/provider/export.go @@ -86,17 +86,16 @@ func ExportWorkspace(context, workspaceID string) (*ExportWorkspaceConfig, error return nil, err } - buf := &bytes.Buffer{} - err = extract.WriteTarExclude(buf, workspaceDir, true, excludedPaths) + data, err := archiveDirBase64(workspaceDir, "workspace") if err != nil { - return nil, fmt.Errorf("compress workspace dir: %w", err) + return nil, err } return &ExportWorkspaceConfig{ ID: workspaceID, UID: workspaceConfig.UID, Context: context, - Data: base64.RawStdEncoding.EncodeToString(buf.Bytes()), + Data: data, }, nil } @@ -111,16 +110,15 @@ func ExportMachine(context, machineID string) (*ExportMachineConfig, error) { return nil, err } - buf := &bytes.Buffer{} - err = extract.WriteTarExclude(buf, machineDir, true, excludedPaths) + data, err := archiveDirBase64(machineDir, "machine") if err != nil { - return nil, fmt.Errorf("compress machine dir: %w", err) + return nil, err } return &ExportMachineConfig{ ID: machineID, Context: context, - Data: base64.RawStdEncoding.EncodeToString(buf.Bytes()), + Data: data, }, nil } @@ -138,10 +136,9 @@ func ExportProvider( return nil, err } - buf := &bytes.Buffer{} - err = extract.WriteTarExclude(buf, providerDir, true, excludedPaths) + data, err := archiveDirBase64(providerDir, "provider") if err != nil { - return nil, fmt.Errorf("compress provider dir: %w", err) + return nil, err } var providerConfig *config.ProviderConfig @@ -154,7 +151,18 @@ func ExportProvider( return &ExportProviderConfig{ ID: providerID, Context: context, - Data: base64.RawStdEncoding.EncodeToString(buf.Bytes()), + Data: data, Config: providerConfig, }, nil } + +// archiveDirBase64 tars dir (skipping excludedPaths) and returns it base64-encoded. +// kind names the dir in the error message ("compress dir"). +func archiveDirBase64(dir, kind string) (string, error) { + buf := &bytes.Buffer{} + if err := extract.WriteTarExclude(buf, dir, true, excludedPaths); err != nil { + return "", fmt.Errorf("compress %s dir: %w", kind, err) + } + + return base64.RawStdEncoding.EncodeToString(buf.Bytes()), nil +} diff --git a/pkg/provider/persist.go b/pkg/provider/persist.go new file mode 100644 index 0000000000..032b97897a --- /dev/null +++ b/pkg/provider/persist.go @@ -0,0 +1,54 @@ +package provider + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" +) + +// requireID returns the " is empty" error used by the Get*Dir helpers +// when an identifier is missing. +func requireID(kind, id string) error { + if id == "" { + return fmt.Errorf("%s is empty", kind) + } + + return nil +} + +// saveJSON creates dir and atomically writes v as JSON to dir/file. +func saveJSON(dir, file string, v any, perm os.FileMode) error { + // #nosec G301 -- TODO Consider using a more secure permission setting and ownership if needed. + if err := os.MkdirAll(dir, 0o755); err != nil { + return err + } + + return writeJSONFile(filepath.Join(dir, file), v, perm) +} + +// writeJSONFile atomically writes v as JSON to path. The parent dir must exist. +func writeJSONFile(path string, v any, perm os.FileMode) error { + data, err := json.Marshal(v) + if err != nil { + return err + } + + return WriteFileAtomic(path, data, perm) +} + +// loadJSON reads and decodes the JSON file at path. Read errors are returned +// unwrapped so callers can test them with os.IsNotExist. +func loadJSON[T any](path string) (*T, error) { + data, err := os.ReadFile(filepath.Clean(path)) + if err != nil { + return nil, err + } + + v := new(T) + if err := json.Unmarshal(data, v); err != nil { + return nil, err + } + + return v, nil +} diff --git a/pkg/provider/persist_test.go b/pkg/provider/persist_test.go new file mode 100644 index 0000000000..4d1289da51 --- /dev/null +++ b/pkg/provider/persist_test.go @@ -0,0 +1,105 @@ +package provider + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/require" +) + +type persistSample struct { + Name string `json:"name"` +} + +func TestRequireID(t *testing.T) { + tests := []struct { + name string + kind string + id string + wantErr string + }{ + {name: "non-empty id passes", kind: "machine id", id: "m1"}, + {name: "empty machine id", kind: "machine id", wantErr: "machine id is empty"}, + {name: "empty workspace id", kind: "workspace id", wantErr: "workspace id is empty"}, + { + name: "empty pro instance host", + kind: "pro instance host", + wantErr: "pro instance host is empty", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := requireID(tt.kind, tt.id) + if tt.wantErr == "" { + require.NoError(t, err) + return + } + require.EqualError(t, err, tt.wantErr) + }) + } +} + +func TestSaveJSONRoundTrip(t *testing.T) { + tests := []struct { + name string + perm os.FileMode + }{ + {name: "owner-only file", perm: 0o600}, + {name: "world-readable file", perm: 0o644}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := filepath.Join(t.TempDir(), "missing", "parent") + path := filepath.Join(dir, "sample.json") + want := &persistSample{Name: "value"} + + require.NoError(t, saveJSON(dir, "sample.json", want, tt.perm)) + + got, err := loadJSON[persistSample](path) + require.NoError(t, err) + require.Equal(t, want, got) + + if runtime.GOOS != windowsOS { + info, err := os.Stat(path) + require.NoError(t, err) + require.Equal(t, tt.perm, info.Mode().Perm()) + } + }) + } +} + +func TestWriteJSONFileRequiresExistingDir(t *testing.T) { + dir := filepath.Join(t.TempDir(), "absent") + + err := writeJSONFile(filepath.Join(dir, "sample.json"), &persistSample{Name: "x"}, 0o600) + require.Error(t, err) + require.NoDirExists(t, dir) +} + +func TestLoadJSONErrors(t *testing.T) { + tests := []struct { + name string + content *string + wantNotExist bool + }{ + {name: "missing file", content: nil, wantNotExist: true}, + {name: "invalid JSON", content: new("{not json")}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "sample.json") + if tt.content != nil { + require.NoError(t, os.WriteFile(path, []byte(*tt.content), 0o600)) + } + + got, err := loadJSON[persistSample](path) + require.Error(t, err) + require.Nil(t, got) + require.Equal(t, tt.wantNotExist, errors.Is(err, fs.ErrNotExist)) + }) + } +}