package main // Exhibit 29. An app's creator works with its data without a shell (tools PLATFORM.md, "Your app's // data"; storage README, "Apps / / data"): storage shows a copy of each environment's data, live // read-only "as of" when root copied it, and a file put into staging or preview goes in. Storage // holds no authority here: it files the request with account's assertion for the person, root checks // they create the app, copies as the app (SQLite through its backup API) or refuses, and logs both. // // The robot creates the sample app zoo-broken, whose page keeps one SQLite table, who was here. The // check, as the robot: visit zoo-broken (its own row gets the visit's time), ask storage for a fresh // copy of live and put a file into live, wait for root's minute tick, then download the copy, open it // here (the zoo's own SQLite, on a copy in its data) and find that visit; the put must be refused. // // Live only: staging's storage shows no app's data on purpose (account-staging's usernames are // anyone's to register, so a name there proves nothing), and the staging zoo checks exactly that. import "core:encoding/json" import "core:fmt" import "core:strings" import "ratcloud:rc" DATA_APP :: "zoo-broken" DATA_DB :: "broken.db" // A new round (one copy, one put) at most once an hour: root logs every copy and every refusal (the // app's data.json, syslog), and an hourly round proves the door as well as one every five minutes // would. A red round is tried again at the next run. DATA_EVERY :: 3600 // Root answers storage's requests on its minute tick, and a copy may take a while: the five-minute // job waits up to 110 s, a "check now" in the browser up to 20 (a request ends at 30). A round still // waiting is picked up by the next run. DATA_WAIT_JOB :: 110 DATA_WAIT_WEB :: 20 data_api :: proc(b: ^Browser, op: string, in_json := "{}") -> (int, json.Value) { r := post(b, fmt.tprintf("%s/api/%s", rc.app_url("storage"), op), in_json) if r.j == nil do return r.status, parse(fmt.tprintf(`{{"error":%s}}`, jq(clip(r.body, 160)))) return r.status, r.j } // Staging: the robot (account-staging's zoo-robot, a name anyone could have taken) sees no data. data_staging :: proc() -> Check { b, err := robot_at("storage") if err != "" do return fail(err) c1, all := data_api(b, "data") c2, _ := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) c3, _ := data_api(b, "datafetch", fmt.tprintf(`{{"app":%s,"env":"live"}}`, jq(DATA_APP))) data := fmt.tprintf(`{{"apps":%s,"one":%d,"fetch":%d}}`, to_json(jget(all, "apps")), c2, c3) if c1 != 200 || to_json(jget(all, "apps")) != "[]" do return {false, cat("staging's storage shows the robot some app's data: ", to_json(all)), data} if c2 != 404 || c3 != 404 do return {false, fmt.tprintf("staging's storage answered %d and %d for %s's data, not 404", c2, c3, DATA_APP), data} return {true, "staging's storage shows no app's data and asks root for none, as it should (account-staging's names are anyone's to register); the live zoo shows the copy", data} } Pending :: struct { seen: i64 `json:"seen"`, asked: i64 `json:"asked"`, copy: json.Value `json:"copy"`, put: json.Value `json:"put"`, } E29_Data :: struct { asked: string `json:"asked"`, seen: string `json:"seen"`, copy: json.Value `json:"copy"`, put: json.Value `json:"put"`, as_of: string `json:"as_of,omitempty"`, after_s: i64 `json:"after_s"`, file: json.Value `json:"file,omitempty"`, quick_check: string `json:"quick_check,omitempty"`, rows: i64 `json:"rows"`, } // One round on live: start it (or pick up the one waiting), wait up to `wait` seconds for root, judge // it. ok = false and detail "": root has not answered yet. data_round :: proc(wait: i64) -> (Check, bool) { b, err := robot() if err != "" do return fail(err), true st := rc.app_url("storage") pv, _ := meta("data.pending") p: Pending pending := pv != "" && json_into(pv, &p) if pending && now() - p.asked > 300 { meta_set("data.pending", "") return fail(fmt.tprintf("root did not answer storage's requests within 5 minutes (asked %s UTC)", hms(p.asked)), p), true } if !pending { r := visit(b, cat(rc.app_url(DATA_APP), "/")) i := strings.index(r.body, `data-seen-at="`) if r.status != 200 || i < 0 do return fail(fmt.tprintf("the robot's visit to %s was not noted in its data (%d)", DATA_APP, r.status)), true seen_at := atoi(r.body[i + 14:]) if s := visit(b, cat(st, "/")); s.status != 200 do return fail(fmt.tprintf("storage answered the robot with %d", s.status)), true code, j := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) if code != 200 do return fail(fmt.tprintf("the robot's storage does not offer %s's data (%d): %s", DATA_APP, code, jstr(j, "error")), j), true if w, isb := jget(j, "envs", "live", "writable").(json.Boolean); !isb || w do return fail("storage offers to write into live", jget(j, "envs")), true asked := now() code2, f := data_api(b, "datafetch", fmt.tprintf(`{{"app":%s,"env":"live"}}`, jq(DATA_APP))) if code2 != 200 || !jhas(f, "id") do return fail(fmt.tprintf("storage would not ask root for a copy of live (%d): %s", code2, jstr(f, "error")), f), true hs := make(map[string]string) hs["X-RC"] = "1" u := go(b, "POST", fmt.tprintf("%s/api/dataput?app=%s&env=live&path=zoo-was-here.txt", st, DATA_APP), "The zoo's robot tried to put this into live (exhibit 29). Root must refuse it.\n", "application/octet-stream", hs) if u.status != 200 || !jhas(u.j, "id") do return fail(fmt.tprintf("storage would not pass a put into live on to root (%d)", u.status), u.j), true p = {seen_at, asked, jget(f, "id"), jget(u.j, "id")} meta_set("data.pending", to_json(p)) } else { visit(b, cat(st, "/")) // this instance's robot, signed in at storage } end := now() + wait c, u: json.Value for { _, c = data_api(b, "datastate", fmt.tprintf(`{{"id":%s}}`, to_json(p.copy))) _, u = data_api(b, "datastate", fmt.tprintf(`{{"id":%s}}`, to_json(p.put))) cr, ur := jstr(c, "result") or_else "waiting", jstr(u, "result") or_else "waiting" if cr != "waiting" && ur != "waiting" do break if now() >= end do return {}, false rc.sleep(3000) } meta_set("data.pending", "") return data_judge(b, p, c, u), true } data_judge :: proc(b: ^Browser, p: Pending, c, u: json.Value) -> Check { st := rc.app_url("storage") data := E29_Data{asked = iso(p.asked), seen = iso(p.seen), copy = c, put = u} if jstr(c, "result") != "done" do return fail(fmt.tprintf("root did not copy %s's live data: %s %s", DATA_APP, jstr(c, "result") or_else "?", jstr(c, "message")), data) _, j := data_api(b, "data", fmt.tprintf(`{{"app":%s}}`, jq(DATA_APP))) cp := jget(j, "envs", "live", "copy") at := parse_time(jstr(cp, "at")) for f in jarr(cp, "files") do if jstr(f, "path") == DATA_DB do data.file = f data.as_of, data.after_s = jstr(cp, "at"), at - p.asked if data.file == nil do return fail(cat("the copy of live has no ", DATA_DB), data) // Within a tick: root's minute tick, plus the copy itself. if at < p.asked - 5 || at - p.asked > 120 do return fail(fmt.tprintf("the copy is as of %s, not within a tick of asking at %s", jstr(cp, "at") or_else "never", hms(p.asked)), data) r := get(b, fmt.tprintf("%s/data/%s/live/%s", st, DATA_APP, DATA_DB)) if r.status != 200 || !strings.has_prefix(r.body, "SQLite format 3\x00") do return fail(fmt.tprintf("%s did not download as a SQLite file (%d)", DATA_DB, r.status), data) // Open it here: a copy in the zoo's own data, opened with the zoo's own SQLite, then removed. name := cat("copy-", random_hex(4)) if rc.write_file(cat(name, ".db"), transmute([]u8)r.body) != 0 do return fail("could not keep the copy to open it", data) defer for x in ([]string{".db", ".db-journal", ".db-wal", ".db-shm"}) do rc.delete_file(cat(name, x)) d, ok := rc.db(name) if !ok do return fail(cat("the copy of ", DATA_DB, " does not open"), data) q := rc.query_rows(d, "PRAGMA quick_check") for rc.next(&q) do data.quick_check = strings.clone(rc.col_text(&q, 0)) if q.err != "" do return fail(cat("the copy of ", DATA_DB, " does not open: ", q.err), data) n := rc.query_rows(d, "SELECT count(*) FROM seen") for rc.next(&n) do data.rows = rc.col_int(&n, 0) visit_at: i64 = 0 v := rc.query_rows(d, "SELECT at FROM seen WHERE user_id = ?", robot_user().id) for rc.next(&v) do visit_at = rc.col_int(&v, 0) if data.quick_check != "ok" do return fail(cat("the copy of ", DATA_DB, " fails SQLite's quick_check: ", data.quick_check), data) // The visit noted before asking, or a later one of the robot's (another run's), never an older one. if visit_at < p.seen || visit_at > at do return fail(fmt.tprintf("the copy lacks the robot's visit at %s UTC%s", hms(p.seen), fmt.tprintf(" (it holds %s)", hms(visit_at)) if visit_at > 0 else ""), data) if jstr(u, "result") != "refused" do return fail(fmt.tprintf("a put into live was %s: live must take nothing that way", jstr(u, "result") or_else "?"), data) return pass(fmt.tprintf("root copied %s's live data into the robot's storage %d s after it asked (as of %s UTC); %s opened here with the robot's visit in it; a put into live was refused: %s", DATA_APP, at - p.asked, hms(at), DATA_DB, jstr(u, "message")), data) } // The preview's data, as root says it, from exhibit 30's last round, which loads a copy of live's data // into the robot's preview of zoo-broken. data_preview_line :: proc() -> string { d, _, _ := stored_data(30, E30_Data) for l in d.status { t := strings.trim_space(l) if strings.has_prefix(t, "data:") do return fmt.tprintf(`
  • preview%s (exhibit 30 loads it)
  • `, h(strings.trim_space(t[5:]))) } return "" } e29 :: proc() -> Exhibit { return { n = 29, wing = "Building", title = "Real data to build with", promise = "An app's creator finds a copy of its live data in their storage, and can put data into staging or preview; live takes nothing that way.", block = "storage shows each of your apps' data under Apps / / data: live as a read-only copy root makes as the app, staging and preview with \"Put a file in\". Laptops: dev/ratcloud data pull | push in the app's folder (the same rules, over ssh). The preview also takes a whole copy of live's or staging's data: Load on the app's page at apps, or ratcloud load live|staging.", src = {"e29_real_data.odin", #load("e29_real_data.odin", string)}, show = proc(me: ^rc.User) -> string { st := rc.app_url("storage") mine := fmt.tprintf(`

    Your own apps' data is in your storage, under Apps, if you create one.

    `, h(st)) if env().env != "live" do return cat(`

    Staging's storage shows no app's data: account-staging's usernames are anyone's to register. The live zoo shows the copy.

    `, mine) d, s, found := stored_data(29, E29_Data) app := h(rc.app_url(DATA_APP)) if !found || !jhas(d.file) do return cat(`

    The robot creates the sample app `, DATA_APP, `; its copy has not been checked yet.

    `, mine) tables := make([dynamic]string) if t, ok := jget(d.file, "tables").(json.Object); ok do for k, v in t do append(&tables, fmt.tprintf("%s: %d row%s", h(k), jint(v), "" if jint(v) == 1 else "s")) b := strings.builder_make() rc.render_to(&b, `

    The robot creates the sample app {{2}}, which notes who was here. In the robot's storage, Apps / Zoo-broken / data / live:

      `, {{"1", rc.app_url(DATA_APP)}, {"2", DATA_APP}}) rc.render_to(&b, `
    • copy{{2}}, {{3}} bytes{{4}}, as of {{5}}, {{6}} s after the robot asked
    • `, {{"1", "ok" if s.ok else "bad"}, {"2", DATA_DB}, {"3", jint(d.file, "size")}, {"4", rc.raw(cat(" (", strings.join(tables[:], ", "), ")") if len(tables) > 0 else "")}, {"5", rc.raw(ago(parse_time(d.as_of)))}, {"6", d.after_s}}) rc.render_to(&b, `
    • openedhere, with SQLite: the robot's visit at {{2}} UTC is in it
    • `, {{"1", "ok" if d.rows > 0 else "bad"}, {"2", hms(parse_time(d.seen))}}) rc.render_to(&b, `
    • put into live{{2}}: {{3}}
    • `, {{"1", "ok" if jstr(d.put, "result") == "refused" else "bad"}, {"2", jstr(d.put, "result") or_else "?"}, {"3", jstr(d.put, "message")}}) strings.write_string(&b, data_preview_line()) strings.write_string(&b, "
    ") return cat(strings.to_string(b), mine) }, check = proc() -> Check { if env().env != "live" do return data_staging() d, s, found := stored_data(29, E29_Data) round := parse_time(d.asked) pv, _ := meta("data.pending") if found && s.ok && pv == "" && now() - round < DATA_EVERY do return {true, s.detail, s.data} c, answered := data_round(DATA_WAIT_JOB if g_in_job else DATA_WAIT_WEB) if answered do return c if found && d.asked != "" do return {s.ok, s.detail, s.data} // the last round stands until root answers this one return fail(fmt.tprintf("asked root for a copy of %s's live data; no answer yet (root answers on its minute tick)", DATA_APP)) }, } }