Skip to content
Closed
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## vX.X.X (YYYY-MM-DD)

### Fixed
- One invalid `challenge.yml` no longer takes every challenge down: it stopped Galvanize from starting, or made `/admin/reload-challs` fail. Files that cannot be parsed (including a bad compose file), unreadable directories and challenges declaring an already indexed `category/name` are now skipped and logged, and the other challenges are indexed
- A missing or unreadable challenge directory makes `BuildIndex` return an error instead of panicking; a failed reload keeps the current index

### Changed
- `/admin/reload-challs` returns `{"indexed": <count>, "skipped": [{"path", "reason"}]}` instead of an empty body

## v0.7.1 (2026-06-02)

### Added
Expand Down
34 changes: 33 additions & 1 deletion galvanize-instancer/api/openapi.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,17 @@ paths:
/admin/reload-challs:
post:
summary: Reload challenges index
description: Reload challenges index from disk and update the inmemory index
description: Reload challenges index from disk and update the inmemory index. Challenge files that cannot be parsed, or that duplicate another challenge's category/name, are skipped and listed in the response; the other challenges are indexed. If the challenge directory cannot be read, the current index is kept and 500 is returned.
operationId: reloadChallenges
security:
- bearerAuth: [ ]
responses:
'200':
description: Reload successful
content:
application/json:
schema:
$ref: '#/components/schemas/ReloadChallengesResponse'
'401':
description: Unauthorized
'403':
Expand Down Expand Up @@ -550,6 +554,34 @@ components:
- created_at
- updated_at

ReloadChallengesResponse:
type: object
properties:
indexed:
type: integer
description: Number of challenges in the index after the reload.
skipped:
type: array
description: Challenge files left out of the index.
items:
$ref: '#/components/schemas/SkippedChallenge'
required:
- indexed
- skipped

SkippedChallenge:
type: object
properties:
path:
type: string
description: Path of the challenge file.
reason:
type: string
description: Why it was skipped.
required:
- path
- reason

RetryDeploymentRequest:
type: object
required:
Expand Down
3 changes: 3 additions & 0 deletions galvanize-instancer/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ var serveCmd = &cobra.Command{
if err != nil {
zap.S().Fatalf("Failed to initialize challenge index: %v", err)
}
if skipped := challIdx.Skipped(); len(skipped) > 0 {
zap.S().Warnf("%d challenge file(s) skipped (see the errors above); the other challenges are available", len(skipped))
}
// Seed the challenge index gauge with the initial count.
{
challs := challIdx.GetAll()
Expand Down
80 changes: 68 additions & 12 deletions galvanize-instancer/internal/challenge/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,23 @@ type ChallengeIndexer interface {
GetAllUnique() []*Challenge
GetAll() []*Challenge
BuildIndex(baseDir string) error
// Skipped lists the challenge files the last BuildIndex left out.
Skipped() []SkippedChallenge
}

// SkippedChallenge is a challenge file left out of the index, and why.
type SkippedChallenge struct {
Path string
Reason string
}

// Compile-time check that ChallengeIndex implements ChallengeIndexer.
var _ ChallengeIndexer = (*ChallengeIndex)(nil)

type ChallengeIndex struct {
mu sync.RWMutex
challs map[string]*Challenge
mu sync.RWMutex
challs map[string]*Challenge
skipped []SkippedChallenge
}

type Challenge struct {
Expand All @@ -144,32 +153,79 @@ func NewChallengeIndex(baseDir string) (*ChallengeIndex, error) {
return idx, nil
}

// BuildIndex (re)builds the index from the challenge files under baseDir.
//
// A challenge file that cannot be read or parsed, or that declares the same
// category/name as one already indexed, is skipped and reported (Skipped and
// the logs) instead of failing the whole index: one bad challenge.yml would
// otherwise stop Galvanize from starting, or make every reload fail.
// baseDir itself must be readable; otherwise an error is returned and the
// current index is kept as it is.
func (idx *ChallengeIndex) BuildIndex(baseDir string) error {
idx.mu.Lock()
defer idx.mu.Unlock()
idx.challs = make(map[string]*Challenge)
challs := make(map[string]*Challenge)
paths := make(map[string]string)
var skipped []SkippedChallenge
skip := func(path, reason string) {
skipped = append(skipped, SkippedChallenge{Path: path, Reason: reason})
zap.S().Errorf("Skipping challenge %s: %s", path, reason)
}

err := filepath.WalkDir(baseDir, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() && (d.Name() == ".git" || d.Name() == "node_modules" || d.Name() == "example") {
return filepath.SkipDir
if err != nil {
if path == baseDir {
return err
}
skip(path, err.Error())
if d != nil && d.IsDir() {
return filepath.SkipDir
}
return nil
}
if d.IsDir() {
if d.Name() == ".git" || d.Name() == "node_modules" || d.Name() == "example" {
return filepath.SkipDir
}
return nil
}
if err != nil || d.IsDir() || (d.Name() != "challenge.yml" && d.Name() != "challenge.yaml") {
return err
if d.Name() != "challenge.yml" && d.Name() != "challenge.yaml" {
return nil
}
// Parse challenge.yml to get category and name
chall, err := parseChallenge(path)
if err != nil {
return fmt.Errorf("parse %s: %w", path, err)
skip(path, err.Error())
return filepath.SkipDir
}
if chall.Type != "zync" {
return filepath.SkipDir
}
key := chall.Category + "/" + chall.Name
idx.challs[key] = chall
if first, ok := paths[key]; ok {
skip(path, fmt.Sprintf("duplicate of %s (both declare %s)", first, key))
return filepath.SkipDir
}
challs[key] = chall
paths[key] = path
zap.S().Infof("Registered challenge: %s", key)

return filepath.SkipDir
})
return err
if err != nil {
return fmt.Errorf("index %s: %w", baseDir, err)
}

idx.mu.Lock()
idx.challs = challs
idx.skipped = skipped
idx.mu.Unlock()
return nil
}

// Skipped returns the challenge files the last successful BuildIndex left out.
func (idx *ChallengeIndex) Skipped() []SkippedChallenge {
idx.mu.RLock()
defer idx.mu.RUnlock()
return append([]SkippedChallenge(nil), idx.skipped...)
}

func (idx *ChallengeIndex) Get(category, name string) (*Challenge, error) {
Expand Down
131 changes: 120 additions & 11 deletions galvanize-instancer/internal/challenge/challenge_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package challenge

import (
"fmt"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -152,11 +153,113 @@ deploy_parameters:
}

func TestBuildIndex_InvalidDir(t *testing.T) {
// WalkDir passes a nil DirEntry when the root path doesn't exist,
// which causes a panic in BuildIndex before the error can be returned.
assert.Panics(t, func() {
_, _ = NewChallengeIndex("/nonexistent/path/that/does/not/exist")
})
_, err := NewChallengeIndex("/nonexistent/path/that/does/not/exist")
assert.Error(t, err)
}

const validChall = `
name: %s
category: %s
playbook_name: http
type: zync
deploy_parameters:
unique: false
`

func TestBuildIndex_SkipsInvalidFile(t *testing.T) {
dir := t.TempDir()
writeChallenge(t, dir, "web/ok", fmt.Sprintf(validChall, "ok", "web"))
writeChallenge(t, dir, "web/broken", "name: [unterminated\n")
writeChallenge(t, dir, "pwn/ok", fmt.Sprintf(validChall, "ok", "pwn"))

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)

assert.Len(t, idx.GetAll(), 2)
_, err = idx.Get("web", "ok")
assert.NoError(t, err)
_, err = idx.Get("pwn", "ok")
assert.NoError(t, err)

skipped := idx.Skipped()
require.Len(t, skipped, 1)
assert.Equal(t, filepath.Join(dir, "web/broken/challenge.yml"), skipped[0].Path)
assert.NotEmpty(t, skipped[0].Reason)
}

func TestBuildIndex_SkipsDuplicate(t *testing.T) {
dir := t.TempDir()
writeChallenge(t, dir, "a", fmt.Sprintf(validChall, "login", "web"))
writeChallenge(t, dir, "b", fmt.Sprintf(validChall, "login", "web"))

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)

// WalkDir is lexical: a is indexed, b is the duplicate
assert.Len(t, idx.GetAll(), 1)
skipped := idx.Skipped()
require.Len(t, skipped, 1)
assert.Equal(t, filepath.Join(dir, "b/challenge.yml"), skipped[0].Path)
assert.Contains(t, skipped[0].Reason, filepath.Join(dir, "a/challenge.yml"))
assert.Contains(t, skipped[0].Reason, "web/login")
}

func TestBuildIndex_SameNameDifferentCategories(t *testing.T) {
dir := t.TempDir()
writeChallenge(t, dir, "web", fmt.Sprintf(validChall, "login", "web"))
writeChallenge(t, dir, "pwn", fmt.Sprintf(validChall, "login", "pwn"))

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
assert.Len(t, idx.GetAll(), 2)
assert.Empty(t, idx.Skipped())
}

func TestBuildIndex_UnreadableSubdirSkipped(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("root ignores directory permissions")
}
dir := t.TempDir()
writeChallenge(t, dir, "web", fmt.Sprintf(validChall, "ok", "web"))
locked := filepath.Join(dir, "locked")
require.NoError(t, os.MkdirAll(locked, 0o755))
require.NoError(t, os.Chmod(locked, 0))
t.Cleanup(func() { _ = os.Chmod(locked, 0o755) })

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
assert.Len(t, idx.GetAll(), 1)
require.Len(t, idx.Skipped(), 1)
assert.Equal(t, locked, idx.Skipped()[0].Path)
}

func TestBuildIndex_FailedRebuildKeepsIndex(t *testing.T) {
dir := t.TempDir()
writeChallenge(t, dir, "web", fmt.Sprintf(validChall, "ok", "web"))
writeChallenge(t, dir, "broken", "name: [unterminated\n")

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
require.Len(t, idx.GetAll(), 1)

assert.Error(t, idx.BuildIndex("/nonexistent/path/that/does/not/exist"))
assert.Len(t, idx.GetAll(), 1, "the previous index is kept")
assert.Len(t, idx.Skipped(), 1, "and so is its skipped list")
}

func TestBuildIndex_RebuildClearsSkipped(t *testing.T) {
dir := t.TempDir()
writeChallenge(t, dir, "web", fmt.Sprintf(validChall, "ok", "web"))
writeChallenge(t, dir, "broken", "name: [unterminated\n")

idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
require.Len(t, idx.Skipped(), 1)

writeChallenge(t, dir, "broken", fmt.Sprintf(validChall, "fixed", "web"))
require.NoError(t, idx.BuildIndex(dir))
assert.Len(t, idx.GetAll(), 2)
assert.Empty(t, idx.Skipped())
}

func TestGet_ExistingChallenge(t *testing.T) {
Expand Down Expand Up @@ -373,9 +476,12 @@ deploy_parameters:
compose_file: nope.yaml
`)

_, err := NewChallengeIndex(dir)
require.Error(t, err)
assert.Contains(t, err.Error(), "compose_file")
// The challenge is skipped, with the reason, instead of failing the index
idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
assert.Empty(t, idx.GetAll())
require.Len(t, idx.Skipped(), 1)
assert.Contains(t, idx.Skipped()[0].Reason, "compose_file")
}

func TestLoadComposeDefinition_NoServicesSectionErrors(t *testing.T) {
Expand All @@ -390,9 +496,12 @@ deploy_parameters:
`)
writeFile(t, dir, "web", "docker-compose.yml", "version: \"3\"\nnetworks:\n default: {}\n")

_, err := NewChallengeIndex(dir)
require.Error(t, err)
assert.Contains(t, err.Error(), "services")
// The challenge is skipped, with the reason, instead of failing the index
idx, err := NewChallengeIndex(dir)
require.NoError(t, err)
assert.Empty(t, idx.GetAll())
require.Len(t, idx.Skipped(), 1)
assert.Contains(t, idx.Skipped()[0].Reason, "services")
}

func TestLoadComposeDefinition_NoComposeFileLeavesHTTPUntouched(t *testing.T) {
Expand Down
Loading
Loading