fetch_ml/cmd/configlint/main.go
Jeremie Fraeys ea15af1833 Fix multi-user authentication and clean up debug code
- Fix YAML tags in auth config struct (json -> yaml)
- Update CLI configs to use pre-hashed API keys
- Remove double hashing in WebSocket client
- Fix port mapping (9102 -> 9103) in CLI commands
- Update permission keys to use jobs:read, jobs:create, etc.
- Clean up all debug logging from CLI and server
- All user roles now authenticate correctly:
  * Admin: Can queue jobs and see all jobs
  * Researcher: Can queue jobs and see own jobs
  * Analyst: Can see status (read-only access)

Multi-user authentication is now fully functional.
2025-12-06 12:35:32 -05:00

121 lines
2.5 KiB
Go

// Package main implements the fetch_ml configuration linter
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/jfraeys/fetch_ml/internal/fileutil"
"github.com/xeipuuv/gojsonschema"
"gopkg.in/yaml.v3"
)
func main() {
var (
schemaPath string
failFast bool
)
flag.StringVar(&schemaPath, "schema", "configs/schema.yaml", "Path to JSON schema in YAML format")
flag.BoolVar(&failFast, "fail-fast", false, "Stop on first error")
flag.Parse()
if flag.NArg() == 0 {
log.Fatalf("usage: configlint [--schema path] [--fail-fast] <config files...>")
}
schemaLoader, err := loadSchema(schemaPath)
if err != nil {
log.Fatalf("failed to load schema: %v", err)
}
var hadError bool
for _, configPath := range flag.Args() {
if err := validateConfig(schemaLoader, configPath); err != nil {
hadError = true
fmt.Fprintf(os.Stderr, "configlint: %s: %v\n", configPath, err)
if failFast {
os.Exit(1)
}
}
}
if hadError {
os.Exit(1)
}
fmt.Println("All configuration files are valid.")
}
func loadSchema(schemaPath string) (gojsonschema.JSONLoader, error) {
data, err := fileutil.SecureFileRead(schemaPath)
if err != nil {
return nil, err
}
var schemaYAML any
if err := yaml.Unmarshal(data, &schemaYAML); err != nil {
return nil, err
}
schemaJSON, err := json.Marshal(schemaYAML)
if err != nil {
return nil, err
}
tmpFile, err := os.CreateTemp("", "fetchml-schema-*.json")
if err != nil {
return nil, err
}
defer func() {
_ = tmpFile.Close()
_ = os.Remove(tmpFile.Name())
}()
if _, err := tmpFile.Write(schemaJSON); err != nil {
return nil, err
}
return gojsonschema.NewReferenceLoader("file://" + filepath.ToSlash(tmpFile.Name())), nil
}
func validateConfig(schemaLoader gojsonschema.JSONLoader, configPath string) error {
data, err := fileutil.SecureFileRead(configPath)
if err != nil {
return err
}
var configYAML interface{}
if err := yaml.Unmarshal(data, &configYAML); err != nil {
return fmt.Errorf("failed to parse YAML: %w", err)
}
configJSON, err := json.Marshal(configYAML)
if err != nil {
return err
}
result, err := gojsonschema.Validate(schemaLoader, gojsonschema.NewBytesLoader(configJSON))
if err != nil {
return err
}
if result.Valid() {
fmt.Printf("%s: valid\n", configPath)
return nil
}
var builder strings.Builder
for _, issue := range result.Errors() {
builder.WriteString("- ")
builder.WriteString(issue.String())
builder.WriteByte('\n')
}
return fmt.Errorf("validation failed:\n%s", builder.String())
}