Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 70 additions & 8 deletions internal/stackql/intrinsic/iac.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ package intrinsic

import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"path/filepath"
"strings"
"time"
Expand All @@ -18,11 +20,15 @@ import (
"github.com/stackql/stackql-parser/go/vt/sqlparser"
)

// stateDirName is where the ledger and run journals live when a query names no
// state directory. It sits under the application root, which is already local
// stateDirName is where the ledger and run journals live when the operator sets
// no iacStateRoot. It sits under the application root, which is already local
// disk: O_EXCL and link are unreliable on a network share.
const stateDirName = "iac"

// errStateEscapesRoot refuses a 'state' predicate that would put the ledger
// outside the state root.
var errStateEscapesRoot = errors.New("must name a directory inside the iac state root")

// runIDLayout stamps a run that names no run id, as omnisdk's Converge always has.
const runIDLayout = "20060102T150405Z"

Expand Down Expand Up @@ -123,8 +129,9 @@ func destroySelectFunc(ctx queryContext, node *sqlparser.Select) (func() interna
}

// ledgerPredicates reads the predicates every ledger backed relation takes: the collection it
// addresses, and the state directory and run id, each defaulted when absent. The predicates left
// over are returned for the caller.
// addresses, and the state directory and run id, each defaulted when absent. The state directory
// is a name under the operator's state root, never a path of the caller's choosing, and it is
// checked before anything touches disk. The predicates left over are returned for the caller.
func ledgerPredicates(
ctx queryContext,
node *sqlparser.Select,
Expand All @@ -141,17 +148,72 @@ func ledgerPredicates(
"and the correlation stamp, and it is how a later run addresses the same resources",
relation, collectionPredicate))
}
state := popPredicate(params, statePredicate)
runID := popPredicate(params, runIDPredicate)
if state == "" {
state = filepath.Join(ctx.GetRuntimeContext().ApplicationFilesRootPath, stateDirName)
root := previewCfg.getIaCStateRoot()
if root == "" {
root = filepath.Join(ctx.GetRuntimeContext().ApplicationFilesRootPath, stateDirName)
}
state, stateErr := resolveStateDir(root, popPredicate(params, statePredicate))
if stateErr != nil {
return nil, nil, refuse(fmt.Errorf("relation '%s': '%s' %w", relation, statePredicate, stateErr))
}
runID := popPredicate(params, runIDPredicate)
if runID == "" {
runID = time.Now().UTC().Format(runIDLayout)
}
return params, omnisdk.NewLedger(collection, state, runID), nil
}

// resolveStateDir places the named state directory under root, or returns root when no name is
// given. The name must be relative and stay inside root, lexically and once symlinks (and, on
// Windows, junctions) in its existing part are followed, so a link cannot carry it out either.
func resolveStateDir(root, name string) (string, error) {
if name == "" {
return root, nil
}
if !filepath.IsLocal(name) {
return "", errStateEscapesRoot
}
resolvedRoot, err := resolveExisting(root)
if err != nil {
return "", err
}
dir := filepath.Join(root, name)
resolvedDir, err := resolveExisting(dir)
if err != nil {
return "", err
}
rel, err := filepath.Rel(resolvedRoot, resolvedDir)
if err != nil || !filepath.IsLocal(rel) {
return "", errStateEscapesRoot
}
return dir, nil
}

// resolveExisting follows the links in the longest prefix of path that exists and appends the
// rest unchanged; a part that does not exist yet cannot be a link.
func resolveExisting(path string) (string, error) {
abs, err := filepath.Abs(path)
if err != nil {
return "", err
}
var rest []string
for {
resolved, evalErr := filepath.EvalSymlinks(abs)
if evalErr == nil {
return filepath.Join(append([]string{resolved}, rest...)...), nil
}
if !errors.Is(evalErr, fs.ErrNotExist) {
return "", evalErr
}
parent := filepath.Dir(abs)
if parent == abs {
return filepath.Join(append([]string{abs}, rest...)...), nil
}
rest = append([]string{filepath.Base(abs)}, rest...)
abs = parent
}
}

// countSupplied counts the non-empty values.
func countSupplied(values ...string) int {
count := 0
Expand Down
98 changes: 98 additions & 0 deletions internal/stackql/intrinsic/iac_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package intrinsic //nolint:testpackage // tests unexported iac state resolution

import (
"errors"
"os"
"path/filepath"
"testing"
)

func TestResolveStateDirDefaultsToRoot(t *testing.T) {
root := filepath.Join(t.TempDir(), "iac")
got, err := resolveStateDir(root, "")
if err != nil {
t.Fatalf("resolveStateDir(root, \"\") error = %v", err)
}
if got != root {
t.Errorf("resolveStateDir(root, \"\") = %q, want %q", got, root)
}
}

func TestResolveStateDirAllowsNamesUnderRoot(t *testing.T) {
root := filepath.Join(t.TempDir(), "iac")
for _, name := range []string{"prod", "team/prod", "team/../prod", "./prod"} {
got, err := resolveStateDir(root, name)
if err != nil {
t.Errorf("resolveStateDir(%q) error = %v", name, err)
continue
}
if want := filepath.Join(root, name); got != want {
t.Errorf("resolveStateDir(%q) = %q, want %q", name, got, want)
}
}
}

func TestResolveStateDirRejectsEscapes(t *testing.T) {
base := t.TempDir()
root := filepath.Join(base, "iac")
outside := filepath.Join(base, "outside")
for _, name := range []string{outside, "..", "../outside", "team/../../outside", "/"} {
if _, err := resolveStateDir(root, name); !errors.Is(err, errStateEscapesRoot) {
t.Errorf("resolveStateDir(%q) error = %v, want errStateEscapesRoot", name, err)
}
}
// A refusal is decided before anything is written.
for _, dir := range []string{root, outside} {
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
t.Errorf("%q exists after refused names, stat error = %v", dir, err)
}
}
}

func TestResolveStateDirRejectsLinkOutOfRoot(t *testing.T) {
base := t.TempDir()
root := filepath.Join(base, "iac")
outside := filepath.Join(base, "outside")
for _, dir := range []string{root, outside} {
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
}
if err := os.Symlink(outside, filepath.Join(root, "escape")); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
for _, name := range []string{"escape", "escape/prod"} {
if _, err := resolveStateDir(root, name); !errors.Is(err, errStateEscapesRoot) {
t.Errorf("resolveStateDir(%q) error = %v, want errStateEscapesRoot", name, err)
}
}
}

func TestResolveStateDirFollowsLinkedRoot(t *testing.T) {
base := t.TempDir()
target := filepath.Join(base, "target")
if err := os.MkdirAll(filepath.Join(target, "prod"), 0o755); err != nil {
t.Fatal(err)
}
root := filepath.Join(base, "iac")
if err := os.Symlink(target, root); err != nil {
t.Skipf("symlinks unavailable: %v", err)
}
// The operator may point the root itself at another disk.
got, err := resolveStateDir(root, "prod")
if err != nil {
t.Fatalf("resolveStateDir under a linked root error = %v", err)
}
if want := filepath.Join(root, "prod"); got != want {
t.Errorf("resolveStateDir under a linked root = %q, want %q", got, want)
}
}

func TestInitReadsIaCStateRoot(t *testing.T) {
previous := previewCfg
t.Cleanup(func() { previewCfg = previous })
Init(`{"unstable": true, "iacStateRoot": " /srv/stackql/iac "}`)
if got := previewCfg.getIaCStateRoot(); got != "/srv/stackql/iac" {
t.Errorf("getIaCStateRoot() = %q, want %q", got, "/srv/stackql/iac")
}
}
11 changes: 11 additions & 0 deletions internal/stackql/intrinsic/omnisdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ type backendInput interface {
getUnstableEnabled() bool
getStagingEnabled() bool
getOmniAll() bool
getIaCStateRoot() string
}

type standardBackendInput struct {
Expand All @@ -786,6 +787,7 @@ type standardBackendInput struct {
unstableEnabled bool
stagingEnabled bool
omniAll bool
iacStateRoot string
}

// previewCfg is the parsed --preview argument. Cobra binds the raw string in
Expand All @@ -811,6 +813,12 @@ type previewCfgDTO struct {
Staging bool `json:"staging"`
// Omni "all" routes every provider through omnisdk, never any-sdk.
Omni string `json:"omni"`
// IaCStateRoot is the directory every stackql_preview.iac ledger lives
// under. A query's 'state' predicate names a directory inside it, never
// one outside, so where state is written stays the operator's choice.
// Collections naming the same directory share its ledger, each under its
// own collection key.
IaCStateRoot string `json:"iacStateRoot"`
}

func (c previewCfgDTO) endpoint() string {
Expand Down Expand Up @@ -844,6 +852,7 @@ func newBackendInput(cfg previewCfgDTO) backendInput {
unstableEnabled: cfg.Unstable,
stagingEnabled: cfg.Staging,
omniAll: strings.EqualFold(cfg.Omni, omniAll),
iacStateRoot: strings.TrimSpace(cfg.IaCStateRoot),
}
if cfg.BatchSize > 0 {
rv.batchSize = cfg.BatchSize
Expand All @@ -868,6 +877,8 @@ func (b *standardBackendInput) getStagingEnabled() bool { return b.stagingEnable

func (b *standardBackendInput) getOmniAll() bool { return b.omniAll }

func (b *standardBackendInput) getIaCStateRoot() string { return b.iacStateRoot }

// sourceKey is the row key a column reads from: its own name, unless an alias
// renamed it.
func (c column) sourceKey() string {
Expand Down
Loading