Replace oras with native OCI registry client and add login

- Talk to OCI Distribution v2 registries directly over HTTP/TLS; oras is no longer required on PATH
- Add `orbit login <host>` and `orbit logout <host>`; credentials go to $ORBIT_ROOT/auth.json (mode 0600)
- Fall back to Docker config (credHelpers and credsStore) when no Orbit credentials exist
- Verify layer downloads with streaming SHA-256
- Fall back to the next channel only on HTTP 404 instead of matching oras error text
- Update README and moon.mod to drop oras references
This commit is contained in:
nandi committed 2026-11-27 15:46:41 -08:00
1 parent f8c9b99ee3
commit 0919ce105c
13 files changed
+2652 -184

No files matched your search

+442
View File
@@ -0,0 +1,442 @@
// Registry credentials stored in $ORBIT_ROOT/auth.json, with read fallback
// to $DOCKER_CONFIG/config.json (or ~/.docker/config.json).
///|
struct Creds {
username : String
password : String
}
///|
fn strip_url_scheme(s : String) -> String {
if has_prefix(s, "https://") {
drop_chars(s, 8)
} else if has_prefix(s, "http://") {
drop_chars(s, 7)
} else {
s
}
}
///|
fn ascii_lower(s : String) -> String {
let sb = StringBuilder()
for c in s {
if c >= 'A' && c <= 'Z' {
sb.write_char(Int32::unsafe_to_char(c.to_int() + 32))
} else {
sb.write_char(c)
}
}
sb.to_string()
}
///|
fn normalize_host(raw : String) -> String {
let t = trim(raw)
let no_scheme = if has_prefix(ascii_lower(t), "https://") {
drop_chars(t, 8)
} else if has_prefix(ascii_lower(t), "http://") {
drop_chars(t, 7)
} else {
t
}
let (host, _) = split_first(no_scheme, '/')
ascii_lower(trim(host))
}
///|
fn is_docker_hub_host(h : String) -> Bool {
h == "docker.io" || h == "registry-1.docker.io" || h == "index.docker.io"
}
///|
fn host_matches(stored_key : String, host : String) -> Bool {
let a = normalize_host(stored_key)
let b = normalize_host(host)
if a == "" || b == "" {
return false
}
if a == b {
return true
}
is_docker_hub_host(a) && is_docker_hub_host(b)
}
///|
fn auth_file_path(root : String) -> String {
root + "/auth.json"
}
///|
fn docker_config_path() -> String {
let dir = getenv("DOCKER_CONFIG")
if dir != "" {
dir + "/config.json"
} else {
getenv("HOME") + "/.docker/config.json"
}
}
///|
fn encode_basic_auth(creds : Creds) -> String {
let raw = utf8_encode(creds.username + ":" + creds.password)
@base64.encode(raw[:])
}
///|
fn decode_basic_auth(b64 : String) -> Creds? {
let raw = @base64.decode(trim(b64), ignore_whitespace=true) catch {
_ => return None
}
let decoded = utf8_decode(raw)
if !has_char(decoded, ':') {
return None
}
let (username, password) = split_first(decoded, ':')
if username == "" && password == "" {
return None
}
Some(Creds::{ username, password })
}
///|
async fn creds_from_helper(
helper : String,
server_url : String,
) -> Creds? noraise {
if helper == "" || has_char(helper, '/') || has_char(helper, ' ') {
return None
}
let bin = "docker-credential-" + helper
let (code, stdout) = run_with_stdin(bin, ["get"], server_url + "\n") catch {
_ => return None
}
if code != 0 {
return None
}
let obj = parse_object(stdout, "credential helper") catch { _ => return None }
let username = jstr(obj, "Username", "")
let password = jstr(obj, "Secret", "")
if username == "" && password == "" {
None
} else {
Some(Creds::{ username, password })
}
}
///|
async fn load_creds_from_file(path : String, host : String) -> Creds? noraise {
let text = match read_text(path) {
Some(t) => t
None => return None
}
let top = parse_object(text, path) catch { _ => return None }
match top.get("credHelpers") {
Some(Json::Object(helpers)) =>
for k, v in helpers {
if host_matches(k, host) {
match v {
Json::String(helper) =>
if helper != "" {
match creds_from_helper(helper, k) {
Some(c) => return Some(c)
None =>
if k != host {
match creds_from_helper(helper, host) {
Some(c) => return Some(c)
None => ()
}
}
}
}
_ => ()
}
}
}
_ => ()
}
match top.get("auths") {
Some(Json::Object(auths)) =>
for k, v in auths {
if host_matches(k, host) {
match v {
Json::Object(entry) => {
let b64 = jstr(entry, "auth", "")
if b64 != "" {
match decode_basic_auth(b64) {
Some(c) => return Some(c)
None => ()
}
}
let u = jstr(entry, "username", "")
let p = jstr(entry, "password", "")
if u != "" || p != "" {
return Some(Creds::{ username: u, password: p })
}
}
_ => ()
}
}
}
_ => ()
}
let store = jstr(top, "credsStore", "")
if store != "" {
match creds_from_helper(store, host) {
Some(c) => return Some(c)
None =>
if is_docker_hub_host(normalize_host(host)) {
return creds_from_helper(store, "https://index.docker.io/v1/")
}
}
}
None
}
///|
async fn load_creds(host : String) -> Creds? noraise {
let nhost = normalize_host(host)
if nhost == "" {
return None
}
match load_creds_from_file(auth_file_path(orbit_root()), nhost) {
Some(c) => Some(c)
None => load_creds_from_file(docker_config_path(), nhost)
}
}
///|
async fn save_creds(
root : String,
host : String,
creds : Creds,
) -> Unit raise OrbitError {
let nhost = normalize_host(host)
let path = auth_file_path(root)
let top = match read_text(path) {
Some(t) => parse_object(t, "auth.json")
None => Map([])
}
let auths : Map[String, Json] = match top.get("auths") {
Some(Json::Object(m)) => m
None => Map([])
_ => raise OrbitError("auth.json: expected 'auths' to be an object")
}
let stale : Array[String] = []
for k, _ in auths {
if host_matches(k, nhost) {
stale.push(k)
}
}
for k in stale {
auths.remove(k)
}
let entry : Map[String, Json] = Map([])
entry["auth"] = Json::string(encode_basic_auth(creds))
auths[nhost] = Json::object(entry)
top["auths"] = Json::object(auths)
@fs.mkdir(root, recursive=true, allow_exist=true) catch {
e => raise OrbitError("cannot create \{root}: \{e}")
}
write_secret_atomic(path, Json::object(top).stringify(indent=2) + "\n")
}
///|
async fn remove_creds(root : String, host : String) -> Bool raise OrbitError {
let nhost = normalize_host(host)
let path = auth_file_path(root)
let text = match read_text(path) {
Some(t) => t
None => return false
}
let top = parse_object(text, "auth.json")
let auths : Map[String, Json] = match top.get("auths") {
Some(Json::Object(m)) => m
None => return false
_ => raise OrbitError("auth.json: expected 'auths' to be an object")
}
let matching : Array[String] = []
for k, _ in auths {
if host_matches(k, nhost) {
matching.push(k)
}
}
if matching.length() == 0 {
return false
}
for k in matching {
auths.remove(k)
}
top["auths"] = Json::object(auths)
write_secret_atomic(path, Json::object(top).stringify(indent=2) + "\n")
true
}
///|
fn strip_trailing_cr(s : String) -> String {
let a = chars(s)
let mut end = a.length()
if end > 0 && a[end - 1] == '\r' {
end = end - 1
}
slice(a, 0, end)
}
///|
fn strip_trailing_newlines(s : String) -> String {
let a = chars(s)
let mut end = a.length()
while end > 0 && (a[end - 1] == '\n' || a[end - 1] == '\r') {
end = end - 1
}
slice(a, 0, end)
}
///|
async fn read_stdin_line() -> String raise OrbitError {
let line = @stdio.stdin.read_until("\n") catch {
e => raise OrbitError("cannot read stdin: \{e}")
}
match line {
Some(s) => strip_trailing_cr(s)
None => ""
}
}
///|
async fn read_password_prompt() -> String raise OrbitError {
c_print_err(utf8_encode("Password: "))
let echo_disabled = c_set_echo(0) == 0
defer {
if echo_disabled {
ignore(c_set_echo(1))
c_print_err(utf8_encode("\n"))
}
}
read_stdin_line()
}
///|
async fn resolve_login_plain_http(
raw_host : String,
host : String,
plain_http_flag : Bool,
) -> Bool raise OrbitError {
let lower = ascii_lower(trim(raw_host))
if plain_http_flag || has_prefix(lower, "http://") {
return true
}
if has_prefix(lower, "https://") {
return false
}
let ctx = load_ctx()
for ch in ctx.channels {
let (ch_host, _) = split_first(ch.url, '/')
if host_matches(ch_host, host) && ch.plain_http {
return true
}
}
false
}
///|
async fn cmd_login(
raw_host : String,
username_opt : String?,
password_opt : String?,
password_stdin : Bool,
plain_http_flag : Bool,
) -> Unit raise OrbitError {
if password_opt is Some(_) && password_stdin {
raise OrbitError("use either --password or --password-stdin, not both")
}
let host = normalize_host(raw_host)
if host == "" || has_char(host, ' ') || has_char(host, '\t') {
raise OrbitError("invalid registry host '\{raw_host}'")
}
let plain_http = resolve_login_plain_http(raw_host, host, plain_http_flag)
let username = match username_opt {
Some(u) => u
None => {
c_print_err(utf8_encode("Username: "))
trim(read_stdin_line())
}
}
let password = if password_stdin {
let raw = @stdio.stdin.read_all().text() catch {
e => raise OrbitError("cannot read password from stdin: \{e}")
}
strip_trailing_newlines(raw)
} else {
match password_opt {
Some(p) => p
None => read_password_prompt()
}
}
if username == "" && password == "" {
raise OrbitError("username or password is required")
}
let creds = Creds::{ username, password }
with_spinner("Logging in to \{host}", () => {
verify_login(host, plain_http, creds)
})
save_creds(orbit_root(), host, creds)
println("Login succeeded for \{host}")
}
///|
async fn cmd_logout(raw_host : String) -> Unit raise OrbitError {
let host = normalize_host(raw_host)
if host == "" {
raise OrbitError("invalid registry host '\{raw_host}'")
}
if remove_creds(orbit_root(), host) {
println("Removed credentials for \{host}")
} else {
println("No stored credentials for \{host}")
}
let docker_path = docker_config_path()
match load_creds_from_file(docker_path, host) {
Some(_) =>
println("note: credentials for \{host} still exist in \{docker_path}")
None => ()
}
}
///|
test "host normalization and matching" {
assert_eq(normalize_host("https://git.site.latha.org/nandi"), "git.site.latha.org")
assert_eq(normalize_host("http://localhost:5000/"), "localhost:5000")
assert_true(host_matches("https://index.docker.io/v1/", "registry-1.docker.io"))
assert_true(host_matches("docker.io", "index.docker.io"))
assert_false(host_matches("ghcr.io", "git.site.latha.org"))
}
///|
test "basic auth encode and decode roundtrip" {
let c = Creds::{ username: "nandi", password: "s:e:c/r+e=t" }
let b64 = encode_basic_auth(c)
let decoded = decode_basic_auth(b64).unwrap()
assert_eq(decoded.username, "nandi")
assert_eq(decoded.password, "s:e:c/r+e=t")
assert_true(decode_basic_auth("not-valid-b64!!!") is None)
assert_true(decode_basic_auth(@base64.encode(b"nocolon"[:])) is None)
}
///|
async test "save and remove credentials in auth.json" {
let root = @fs.tmpdir(prefix="orbit-auth-test-")
defer (@fs.rmdir(root, recursive=true, allow_missing=true) catch { _ => () })
let c = Creds::{ username: "alice", password: "secret" }
save_creds(root, "https://git.site.latha.org", c)
let loaded = load_creds_from_file(auth_file_path(root), "git.site.latha.org")
guard loaded is Some(got) else { fail("expected saved credentials") }
assert_eq(got.username, "alice")
assert_eq(got.password, "secret")
assert_true(remove_creds(root, "git.site.latha.org"))
assert_true(
load_creds_from_file(auth_file_path(root), "git.site.latha.org") is None,
)
assert_false(remove_creds(root, "git.site.latha.org"))
}