cli/pkg/workspace/service.go
Snider feff6f7a09 Add configuration documentation to README (#304)
* docs: add configuration documentation to README

Added a new 'Configuration' section to README.md as per the
Documentation Audit Report (PR #209).

Included:
- Default configuration file location (~/.core/config.yaml)
- Configuration file format (YAML) with examples
- Layered configuration resolution order
- Environment variable mapping for config overrides (CORE_CONFIG_*)
- Common environment variables (CORE_DAEMON, NO_COLOR, MCP_ADDR, etc.)

* docs: add configuration documentation and fix CI/CD auto-merge

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.

.github/workflows/auto-merge.yml:
- Replaced broken reusable workflow with a local implementation.
- Added actions/checkout step to provide necessary Git context.
- Fixed 'not a git repository' error by providing explicit repo context
  to the 'gh' CLI via the -R flag.
- Maintained existing bot trust and author association logic.

pkg/io/local/client.go:
- Fixed code formatting to ensure QA checks pass.

* docs: update environment variable description and fix merge conflict

- Refined the description of environment variable mapping to be more accurate,
  clarifying that the prefix is stripped before conversion.
- Resolved merge conflict in .github/workflows/auto-merge.yml.
- Maintained the local auto-merge implementation to ensure Git context
  for the 'gh' CLI.

* docs: configuration documentation, security fixes, and CI improvements

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.
- Added documentation for UniFi configuration options.

.github/workflows/auto-merge.yml:
- Replaced broken reusable workflow with a local implementation.
- Added actions/checkout step to provide necessary Git context.
- Fixed 'not a git repository' error by providing explicit repo context
  to the 'gh' CLI via the -R flag.

pkg/unifi:
- Fixed security vulnerability (CodeQL) by making TLS verification
  configurable instead of always skipped.
- Added 'unifi.insecure' config key and UNIFI_INSECURE env var.
- Updated New and NewFromConfig signatures to handle insecure flag.

internal/cmd/unifi:
- Added --insecure flag to 'config' command to skip TLS verification.
- Updated all UniFi subcommands to support the new configuration logic.

pkg/io/local/client.go:
- Fixed code formatting to ensure QA checks pass.

* docs: configuration documentation, tests, and CI/CD fixes

README.md:
- Added comprehensive 'Configuration' section as per audit report #209.
- Documented file format, location, and layered resolution order.
- Provided environment variable mapping rules and common examples.
- Documented UniFi configuration options.

pkg/unifi:
- Fixed security vulnerability by making TLS verification configurable.
- Added pkg/unifi/config_test.go and pkg/unifi/client_test.go to provide
  unit test coverage for new and existing logic (satisfying Codecov).

.github/workflows/auto-merge.yml:
- Added actions/checkout@v4 to provide the required Git context for the
  'gh' CLI, fixing 'not a git repository' errors.

pkg/framework/core/core.go:
- Fixed compilation errors in Workspace() and Crypt() methods due to
  upstream changes in MustServiceFor() return signature.
- Added necessary error handling to pkg/workspace/service.go.

These changes ensure that the project documentation is up-to-date and that
the CI/CD pipeline is stable and secure.
2026-02-05 10:56:49 +00:00

148 lines
4.1 KiB
Go

package workspace
import (
"crypto/sha256"
"encoding/hex"
"os"
"path/filepath"
"sync"
core "github.com/host-uk/core/pkg/framework/core"
"github.com/host-uk/core/pkg/io"
)
// Service implements the core.Workspace interface.
type Service struct {
core *core.Core
activeWorkspace string
rootPath string
medium io.Medium
mu sync.RWMutex
}
// New creates a new Workspace service instance.
func New(c *core.Core) (any, error) {
home, err := os.UserHomeDir()
if err != nil {
return nil, core.E("workspace.New", "failed to determine home directory", err)
}
rootPath := filepath.Join(home, ".core", "workspaces")
s := &Service{
core: c,
rootPath: rootPath,
medium: io.Local,
}
if err := s.medium.EnsureDir(rootPath); err != nil {
return nil, core.E("workspace.New", "failed to ensure root directory", err)
}
return s, nil
}
// CreateWorkspace creates a new encrypted workspace.
// Identifier is hashed (SHA-256 as proxy for LTHN) to create the directory name.
// A PGP keypair is generated using the password.
func (s *Service) CreateWorkspace(identifier, password string) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
// 1. Identification (LTHN hash proxy)
hash := sha256.Sum256([]byte(identifier))
wsID := hex.EncodeToString(hash[:])
wsPath := filepath.Join(s.rootPath, wsID)
if s.medium.Exists(wsPath) {
return "", core.E("workspace.CreateWorkspace", "workspace already exists", nil)
}
// 2. Directory structure
dirs := []string{"config", "log", "data", "files", "keys"}
for _, d := range dirs {
if err := s.medium.EnsureDir(filepath.Join(wsPath, d)); err != nil {
return "", core.E("workspace.CreateWorkspace", "failed to create directory: "+d, err)
}
}
// 3. PGP Keypair generation
crypt, err := s.core.Crypt()
if err != nil {
return "", core.E("workspace.CreateWorkspace", "failed to retrieve crypt service", err)
}
privKey, err := crypt.CreateKeyPair(identifier, password)
if err != nil {
return "", core.E("workspace.CreateWorkspace", "failed to generate keys", err)
}
// Save private key
if err := s.medium.Write(filepath.Join(wsPath, "keys", "private.key"), privKey); err != nil {
return "", core.E("workspace.CreateWorkspace", "failed to save private key", err)
}
return wsID, nil
}
// SwitchWorkspace changes the active workspace.
func (s *Service) SwitchWorkspace(name string) error {
s.mu.Lock()
defer s.mu.Unlock()
wsPath := filepath.Join(s.rootPath, name)
if !s.medium.IsDir(wsPath) {
return core.E("workspace.SwitchWorkspace", "workspace not found: "+name, nil)
}
s.activeWorkspace = name
return nil
}
// WorkspaceFileGet retrieves the content of a file from the active workspace.
// In a full implementation, this would involve decryption using the workspace key.
func (s *Service) WorkspaceFileGet(filename string) (string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
if s.activeWorkspace == "" {
return "", core.E("workspace.WorkspaceFileGet", "no active workspace", nil)
}
path := filepath.Join(s.rootPath, s.activeWorkspace, "files", filename)
return s.medium.Read(path)
}
// WorkspaceFileSet saves content to a file in the active workspace.
// In a full implementation, this would involve encryption using the workspace key.
func (s *Service) WorkspaceFileSet(filename, content string) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.activeWorkspace == "" {
return core.E("workspace.WorkspaceFileSet", "no active workspace", nil)
}
path := filepath.Join(s.rootPath, s.activeWorkspace, "files", filename)
return s.medium.Write(path, content)
}
// HandleIPCEvents handles workspace-related IPC messages.
func (s *Service) HandleIPCEvents(c *core.Core, msg core.Message) error {
switch m := msg.(type) {
case map[string]any:
action, _ := m["action"].(string)
switch action {
case "workspace.create":
id, _ := m["identifier"].(string)
pass, _ := m["password"].(string)
_, err := s.CreateWorkspace(id, pass)
return err
case "workspace.switch":
name, _ := m["name"].(string)
return s.SwitchWorkspace(name)
}
}
return nil
}
// Ensure Service implements core.Workspace.
var _ core.Workspace = (*Service)(nil)