The background fetch now repeats (models_refresh, default 300 s) instead of
running once at startup. It reads the price fields the provider publishes next
to each model and merges them over prices.conf (budget/request windows from the
file survive). Known limits survive a refresh, so ollama is not asked
/api/show for every model on every pass.
A pool can be built from a provider's current model list:
pool free from opencode-go-free match "*-free"
Rules keep a marker and resolve the pool per request, so a model that appears
later is routable without a config reload. Pools used as judges or ctx_fallback
resolve the same way.
Virtual keys grow a deny glob: the olivia key hides the olivia model from that
key's /v1/models and refuses it on the request path, while other keys still see
it.
164 lines
6.9 KiB
Bash
164 lines
6.9 KiB
Bash
#!/bin/bash
|
|
# Wird von tests/run.sh eingebunden: globale Modell-Politik (models allow/deny).
|
|
# Erwartet: $TMP, $PORT, ok/bad/contains, wait_port, ./junction, PIDS
|
|
|
|
echo "== 40. Modell-Politik: /v1/models filtern und Anfragen abweisen =="
|
|
|
|
MP=$((PORT+240))
|
|
MPS=$((PORT+241))
|
|
|
|
STUB_MODELS="foo,bar,baz,ma/bla,opencode-go0/x,other/x,deepseek-v4.1-flash" \
|
|
tests/stub $MPS "MODELS" & PIDS+=($!)
|
|
wait_port "$MPS" 10 || bad "the model-list stub did not come up"
|
|
|
|
cat > "$TMP/model-policy.conf" <<CFG
|
|
listen 127.0.0.1:$MP
|
|
endpoint bag {
|
|
base http://127.0.0.1:$MPS/v1
|
|
context 64000
|
|
timeout 10
|
|
idle_timeout 5
|
|
}
|
|
# Eine allow-Zeile schaltet "alles andere verboten" ein; deny gewinnt.
|
|
models allow "foo" "bar" "ma/bla" "opencode-go0/*" "deepseek-v4.1-flash"
|
|
req path "/v1/models" check none pass bag
|
|
req for "*" check none pass bag
|
|
CFG
|
|
|
|
./junction --config "$TMP/model-policy.conf" > "$TMP/model-policy.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port $MP 10
|
|
|
|
R=$(curl -s -m 10 "http://127.0.0.1:$MP/v1/models")
|
|
contains "$R" '"id":"foo"' "whitelisted model foo is listed"
|
|
contains "$R" '"id":"bar"' "whitelisted model bar is listed"
|
|
contains "$R" '"id":"ma/bla"' "a whitelisted model with a slash is listed"
|
|
contains "$R" '"id":"opencode-go0/x"' "a whitelisted glob keeps its models"
|
|
contains "$R" '"id":"deepseek-v4.1-flash"' "the whitelisted deepseek model is listed"
|
|
case "$R" in
|
|
*'"id":"baz"'*) bad "a non-whitelisted model leaked into /v1/models";;
|
|
*) ok "a non-whitelisted model is hidden from /v1/models";;
|
|
esac
|
|
case "$R" in
|
|
*'"id":"other/x"'*) bad "a non-whitelisted slash model leaked into /v1/models";;
|
|
*) ok "a non-whitelisted slash model is hidden";;
|
|
esac
|
|
|
|
# /v1/models muss lesbar beim Provider ankommen: eine gzip-Antwort würde am Filter
|
|
# vorbeigehen. Andere Anfragen behalten den Accept-Encoding-Wunsch des Clients.
|
|
H=$(curl -s -m 10 -D- -o /dev/null -H 'Accept-Encoding: gzip' \
|
|
"http://127.0.0.1:$MP/v1/models")
|
|
contains "$H" "X-Stub-Accept: identity" "/v1/models asks the provider for identity encoding"
|
|
H=$(curl -s -m 20 -D- -o /dev/null -X POST -H 'Content-Type: application/json' \
|
|
-H 'Accept-Encoding: gzip' \
|
|
-d '{"model":"foo","messages":[{"role":"user","content":"encoding"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP/v1/chat/completions")
|
|
contains "$H" "X-Stub-Accept: gzip" "other requests keep the client's Accept-Encoding"
|
|
|
|
# Chat mit einem nicht freigegebenen Modell: 403, bevor ein Endpunkt belegt wird.
|
|
CODE=$(curl -s -m 10 -o "$TMP/mp-denied.json" -w '%{http_code}' -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"model":"baz","messages":[{"role":"user","content":"nein"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP/v1/chat/completions")
|
|
if [ "$CODE" = "403" ]; then ok "a non-whitelisted model is refused (403)"; else bad "non-whitelisted model got HTTP $CODE"; fi
|
|
contains "$(cat "$TMP/mp-denied.json")" 'model_not_allowed' "the refusal names the policy"
|
|
|
|
# Ein freigegebenes Modell wird bedient; #variante erbt die Freigabe.
|
|
for M in "foo" "ma/bla" "opencode-go0/x" "deepseek-v4.1-flash#high"; do
|
|
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
|
|
-d "{\"model\":\"$M\",\"messages\":[{\"role\":\"user\",\"content\":\"ok\"}],\"max_tokens\":8}" \
|
|
"http://127.0.0.1:$MP/v1/chat/completions")
|
|
contains "$R" "served by MODELS" "the whitelisted model $M is served"
|
|
done
|
|
|
|
# deny gewinnt gegen allow "*": eine Ausnahme bleibt eine Ausnahme.
|
|
MP2=$((PORT+242))
|
|
cat > "$TMP/model-deny.conf" <<CFG
|
|
listen 127.0.0.1:$MP2
|
|
endpoint bag {
|
|
base http://127.0.0.1:$MPS/v1
|
|
context 64000
|
|
timeout 10
|
|
}
|
|
models allow "*"
|
|
models deny "baz"
|
|
req path "/v1/models" check none pass bag
|
|
req for "*" check none pass bag
|
|
CFG
|
|
./junction --config "$TMP/model-deny.conf" > "$TMP/model-deny.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port $MP2 10
|
|
|
|
R=$(curl -s -m 10 "http://127.0.0.1:$MP2/v1/models")
|
|
contains "$R" '"id":"foo"' "allow * keeps ordinary models"
|
|
case "$R" in
|
|
*'"id":"baz"'*) bad "deny did not remove baz from /v1/models";;
|
|
*) ok "deny removes its model even when allow * is set";;
|
|
esac
|
|
|
|
CODE=$(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"model":"baz","messages":[{"role":"user","content":"nein"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP2/v1/chat/completions")
|
|
if [ "$CODE" = "403" ]; then ok "deny wins on the request path too"; else bad "deny request got HTTP $CODE"; fi
|
|
|
|
# Ohne models-Zeile bleibt alles erlaubt (Regression: die Politik ist opt-in).
|
|
MP3=$((PORT+243))
|
|
cat > "$TMP/model-open.conf" <<CFG
|
|
listen 127.0.0.1:$MP3
|
|
endpoint bag {
|
|
base http://127.0.0.1:$MPS/v1
|
|
context 64000
|
|
timeout 10
|
|
}
|
|
req path "/v1/models" check none pass bag
|
|
req for "*" check none pass bag
|
|
CFG
|
|
./junction --config "$TMP/model-open.conf" > "$TMP/model-open.log" 2>&1 &
|
|
PIDS+=($!)
|
|
wait_port $MP3 10
|
|
R=$(curl -s -m 10 "http://127.0.0.1:$MP3/v1/models")
|
|
contains "$R" '"id":"baz"' "without models lines nothing is filtered"
|
|
R=$(curl -s -m 20 -X POST -H 'Content-Type: application/json' \
|
|
-d '{"model":"baz","messages":[{"role":"user","content":"frei"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP3/v1/chat/completions")
|
|
contains "$R" "served by MODELS" "without models lines the model is served"
|
|
|
|
# Ein vkey darf die globale Politik verengen: deny blendet ein Modell aus der
|
|
# /v1/models-Liste aus und weist es im Anfragepfad ab. So sieht Olivia sich
|
|
# selbst nicht im Modellwaehler, obwohl alle anderen das Modell sehen.
|
|
MP4=$((PORT+307))
|
|
cat > "$TMP/model-vkey.conf" <<CFG
|
|
listen 127.0.0.1:$MP4
|
|
endpoint bag {
|
|
base http://127.0.0.1:$MPS/v1
|
|
context 64000
|
|
timeout 10
|
|
}
|
|
models allow "*"
|
|
vkey "olivia" token_env VKEY_OLIVIA_TEST allow "*" deny "baz"
|
|
req path "/v1/models" check none pass bag
|
|
req for "*" check none pass bag
|
|
CFG
|
|
VKEY_OLIVIA_TEST=testkey ./junction --config "$TMP/model-vkey.conf" \
|
|
> "$TMP/model-vkey.log" 2>&1 & PIDS+=($!)
|
|
wait_port $MP4 10
|
|
R=$(curl -s -m 10 "http://127.0.0.1:$MP4/v1/models")
|
|
contains "$R" '"id":"baz"' "a model hidden from one vkey is still listed without a key"
|
|
R=$(curl -s -m 10 -H 'Authorization: Bearer testkey' "http://127.0.0.1:$MP4/v1/models")
|
|
case "$R" in
|
|
*'"id":"baz"'*) bad "a vkey deny did not hide the model from /v1/models" ;;
|
|
*) ok "a vkey deny hides its model from /v1/models" ;;
|
|
esac
|
|
contains "$R" '"id":"foo"' "a vkey deny leaves the other models listed"
|
|
CODE=$(curl -s -m 10 -o /dev/null -w '%{http_code}' -X POST \
|
|
-H 'Authorization: Bearer testkey' -H 'Content-Type: application/json' \
|
|
-d '{"model":"baz","messages":[{"role":"user","content":"nein"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP4/v1/chat/completions")
|
|
if [ "$CODE" = "403" ]; then ok "a vkey deny refuses the hidden model (403)"; else bad "vkey deny request got HTTP $CODE"; fi
|
|
R=$(curl -s -m 20 -X POST -H 'Authorization: Bearer testkey' \
|
|
-H 'Content-Type: application/json' \
|
|
-d '{"model":"foo","messages":[{"role":"user","content":"ok"}],"max_tokens":8}' \
|
|
"http://127.0.0.1:$MP4/v1/chat/completions")
|
|
contains "$R" "served by MODELS" "a vkey may still call the models it can see"
|