mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-14 20:20:54 +08:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a20628d334 |
+2
-3
@@ -14,12 +14,11 @@
|
|||||||
FROM python:3.12-slim-bookworm
|
FROM python:3.12-slim-bookworm
|
||||||
ARG TARGETARCH
|
ARG TARGETARCH
|
||||||
# Common tools: ripgrep / curl / vim, plus a set of recon staples (add/remove as needed).
|
# Common tools: ripgrep / curl / vim, plus a set of recon staples (add/remove as needed).
|
||||||
# Node 22.x comes from NodeSource: bookworm's apt nodejs is 18, Playwright requires >=20,
|
# Node 20.x comes from NodeSource: bookworm's apt nodejs is 18, but Playwright requires >=20.
|
||||||
# and 20 is end-of-life. 22 matches the CI and release workflows.
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
ca-certificates ripgrep curl wget vim git jq unzip \
|
ca-certificates ripgrep curl wget vim git jq unzip \
|
||||||
dnsutils iputils-ping netcat-openbsd inetutils-telnet whois nmap \
|
dnsutils iputils-ping netcat-openbsd inetutils-telnet whois nmap \
|
||||||
&& curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
|
&& curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
||||||
&& apt-get install -y --no-install-recommends nodejs \
|
&& apt-get install -y --no-install-recommends nodejs \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
# Web-recon toolbox (drost-community take 1, #100): pinned release binaries installed
|
# Web-recon toolbox (drost-community take 1, #100): pinned release binaries installed
|
||||||
|
|||||||
+5
-1
@@ -88,7 +88,11 @@ func run() int {
|
|||||||
}
|
}
|
||||||
defer mgr.Close()
|
defer mgr.Close()
|
||||||
|
|
||||||
skillDir := config.SkillDir()
|
skillDir, err := config.SkillDir()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[config] %v", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
if abs, err := filepath.Abs(skillDir); err == nil {
|
if abs, err := filepath.Abs(skillDir); err == nil {
|
||||||
skillDir = abs
|
skillDir = abs
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-12
@@ -4,7 +4,9 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -96,16 +98,23 @@ func Path() string {
|
|||||||
return candidates[0]
|
return candidates[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load reads and parses the config file. A missing/unreadable file yields a zero
|
// Load reads and parses the config file. A missing file yields a zero Config (so
|
||||||
// Config (so callers fall back to defaults) rather than an error.
|
// callers fall back to defaults). A file that exists but cannot be read or parsed
|
||||||
func Load() Config {
|
// is an error: ignoring it would run with defaults the operator did not choose.
|
||||||
|
func Load() (Config, error) {
|
||||||
var c Config
|
var c Config
|
||||||
b, err := os.ReadFile(Path())
|
path := Path()
|
||||||
if err != nil {
|
b, err := os.ReadFile(path)
|
||||||
return c
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
_ = json.Unmarshal(b, &c)
|
if err != nil {
|
||||||
return c
|
return c, fmt.Errorf("read config file %s: %w", path, err)
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(b, &c); err != nil {
|
||||||
|
return Config{}, fmt.Errorf("parse config file %s: %w", path, err)
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SkillDir returns the skill root directory with precedence:
|
// SkillDir returns the skill root directory with precedence:
|
||||||
@@ -113,17 +122,23 @@ func Load() Config {
|
|||||||
// env ARTEX_SKILL_DIR > config file (skill_dir) > BaseDir()/skills
|
// env ARTEX_SKILL_DIR > config file (skill_dir) > BaseDir()/skills
|
||||||
//
|
//
|
||||||
// The directory is created if it does not exist.
|
// The directory is created if it does not exist.
|
||||||
func SkillDir() string {
|
func SkillDir() (string, error) {
|
||||||
var d string
|
var d string
|
||||||
if v := strings.TrimSpace(os.Getenv("ARTEX_SKILL_DIR")); v != "" {
|
if v := strings.TrimSpace(os.Getenv("ARTEX_SKILL_DIR")); v != "" {
|
||||||
d = v
|
d = v
|
||||||
} else if v := strings.TrimSpace(Load().SkillDir); v != "" {
|
} else {
|
||||||
|
c, err := Load()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if v := strings.TrimSpace(c.SkillDir); v != "" {
|
||||||
d = v
|
d = v
|
||||||
} else {
|
} else {
|
||||||
d = filepath.Join(BaseDir(), "skills")
|
d = filepath.Join(BaseDir(), "skills")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_ = os.MkdirAll(d, 0o755)
|
_ = os.MkdirAll(d, 0o755)
|
||||||
return d
|
return d, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostgresDSN resolves the connection string with precedence:
|
// PostgresDSN resolves the connection string with precedence:
|
||||||
@@ -138,7 +153,11 @@ func PostgresDSN() (dsn, source string, err error) {
|
|||||||
if v := strings.TrimSpace(os.Getenv("ARTEX_PG_DSN")); v != "" {
|
if v := strings.TrimSpace(os.Getenv("ARTEX_PG_DSN")); v != "" {
|
||||||
return v, "environment variable ARTEX_PG_DSN", nil
|
return v, "environment variable ARTEX_PG_DSN", nil
|
||||||
}
|
}
|
||||||
db := Load().Database
|
c, err := Load()
|
||||||
|
if err != nil {
|
||||||
|
return "", "", err
|
||||||
|
}
|
||||||
|
db := c.Database
|
||||||
if d := strings.TrimSpace(db.DSN); d != "" {
|
if d := strings.TrimSpace(db.DSN); d != "" {
|
||||||
return d, "config file " + Path() + " (database.dsn)", nil
|
return d, "config file " + Path() + " (database.dsn)", nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package config
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -40,3 +41,24 @@ func TestPostgresDSNPrecedence(t *testing.T) {
|
|||||||
t.Fatalf("file dsn verbatim, got %q err %v", got, err)
|
t.Fatalf("file dsn verbatim, got %q err %v", got, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A config file that exists but is not valid JSON must fail loudly, not run with
|
||||||
|
// defaults (or report "no database config") as if the file were absent.
|
||||||
|
func TestMalformedConfigIsAnError(t *testing.T) {
|
||||||
|
cfgPath := filepath.Join(t.TempDir(), "config.json")
|
||||||
|
// What install.sh used to write for a password containing a double quote.
|
||||||
|
os.WriteFile(cfgPath, []byte(`{"database":{"host":"h","password":"a"b","dbname":"d"},"skill_dir":"/s"}`), 0o644)
|
||||||
|
t.Setenv("ARTEX_CONFIG", cfgPath)
|
||||||
|
t.Setenv("ARTEX_PG_DSN", "")
|
||||||
|
t.Setenv("ARTEX_SKILL_DIR", "")
|
||||||
|
|
||||||
|
if _, _, err := PostgresDSN(); err == nil || !strings.Contains(err.Error(), "parse config file") {
|
||||||
|
t.Fatalf("PostgresDSN: want a parse error, got %v", err)
|
||||||
|
}
|
||||||
|
if d, err := SkillDir(); err == nil {
|
||||||
|
t.Fatalf("SkillDir: want a parse error, got dir %q", d)
|
||||||
|
}
|
||||||
|
if _, err := Load(); err == nil {
|
||||||
|
t.Fatal("Load: want a parse error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+19
-8
@@ -9,6 +9,16 @@ warn(){ printf '\033[33m[!]\033[0m %s\n' "$*"; }
|
|||||||
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
die(){ printf '\033[31m[x]\033[0m %s\n' "$*" >&2; exit 1; }
|
||||||
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
|
ask(){ local p="$1" d="${2:-}" a; read -rp "$p${d:+ [$d]}: " a; echo "${a:-$d}"; }
|
||||||
rand(){ head -c 18 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 24; }
|
rand(){ head -c 18 /dev/urandom | base64 | tr -dc 'A-Za-z0-9' | head -c 24; }
|
||||||
|
# json_str prints $1 as a JSON string literal: backslash, double quote and the
|
||||||
|
# control characters read -r can return are escaped.
|
||||||
|
json_str(){
|
||||||
|
local s="$1"
|
||||||
|
s="${s//\\/\\\\}"; s="${s//\"/\\\"}"
|
||||||
|
s="${s//$'\t'/\\t}"; s="${s//$'\r'/\\r}"
|
||||||
|
printf '"%s"' "$s"
|
||||||
|
}
|
||||||
|
# sed_repl escapes $1 for the replacement side of s|...|...|: \, | and &.
|
||||||
|
sed_repl(){ printf '%s' "$1" | sed 's/[\\|&]/\\&/g'; }
|
||||||
|
|
||||||
# ── docker environment check / auto-install ───────────────────
|
# ── docker environment check / auto-install ───────────────────
|
||||||
ensure_docker(){
|
ensure_docker(){
|
||||||
@@ -38,8 +48,8 @@ install_docker(){
|
|||||||
local pw key
|
local pw key
|
||||||
pw="$(ask 'Postgres password (press Enter to generate randomly)' "$(rand)")"
|
pw="$(ask 'Postgres password (press Enter to generate randomly)' "$(rand)")"
|
||||||
key="$(ask 'ANTHROPIC_API_KEY (may be empty, configure later in the UI)' '')"
|
key="$(ask 'ANTHROPIC_API_KEY (may be empty, configure later in the UI)' '')"
|
||||||
sed -i.bak "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=${pw}|" .env
|
sed -i.bak "s|^POSTGRES_PASSWORD=.*|POSTGRES_PASSWORD=$(sed_repl "$pw")|" .env
|
||||||
sed -i.bak "s|^ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=${key}|" .env
|
sed -i.bak "s|^ANTHROPIC_API_KEY=.*|ANTHROPIC_API_KEY=$(sed_repl "$key")|" .env
|
||||||
rm -f .env.bak
|
rm -f .env.bak
|
||||||
ok ".env generated (POSTGRES_PASSWORD set)"
|
ok ".env generated (POSTGRES_PASSWORD set)"
|
||||||
else
|
else
|
||||||
@@ -76,16 +86,17 @@ install_local(){
|
|||||||
DB_SSL="$(ask 'sslmode (disable/require)' disable)" ;;
|
DB_SSL="$(ask 'sslmode (disable/require)' disable)" ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# generate config.json
|
# generate config.json (string values JSON-escaped: a password may contain " or \)
|
||||||
|
case "$DB_PORT" in (*[!0-9]*|'') die "port must be a number: $DB_PORT" ;; esac
|
||||||
cat > config.json <<JSON
|
cat > config.json <<JSON
|
||||||
{
|
{
|
||||||
"database": {
|
"database": {
|
||||||
"host": "${DB_HOST}",
|
"host": $(json_str "$DB_HOST"),
|
||||||
"port": ${DB_PORT},
|
"port": ${DB_PORT},
|
||||||
"user": "${DB_USER}",
|
"user": $(json_str "$DB_USER"),
|
||||||
"password": "${DB_PASS}",
|
"password": $(json_str "$DB_PASS"),
|
||||||
"dbname": "${DB_NAME}",
|
"dbname": $(json_str "$DB_NAME"),
|
||||||
"sslmode": "${DB_SSL}"
|
"sslmode": $(json_str "$DB_SSL")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
JSON
|
JSON
|
||||||
|
|||||||
Reference in New Issue
Block a user