Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/typescript/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1913,7 +1913,7 @@ export interface components {
* @description How people sign in to this server.
* @enum {string}
*/
LoginMode: "password" | "trusted_proxy" | "oidc";
LoginMode: "password" | "trusted_proxy" | "oidc" | "disabled";
/** @description A running calendar schedule whose last date is at most a day away, or which has none left. */
LowCalendar: {
/**
Expand Down
2 changes: 2 additions & 0 deletions crates/sqlbuild-cli/src/native_commands/main/_run_dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::path::Path;

use sqlbuild_orchestration::api::models::DevelopmentScope;
use sqlbuild_orchestration::credentials::main::identities::development_person::development_person;
use sqlbuild_orchestration::credentials::main::locations::development_location::development_location;
use sqlbuild_orchestration::credentials::models::AuditContext;
use sqlbuild_orchestration::declarations::main::pause_development_triggers::pause_development_triggers;
Expand Down Expand Up @@ -76,6 +77,7 @@ async fn development_store(
location_id,
target: target.into(),
triggers: flag(namespace, "development_triggers"),
principal_id: development_person(&store, &context).await?,
};
if !scope.triggers {
pause_development_triggers(&store, (location_id, target)).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use std::net::SocketAddr;

use axum::{Extension, Router};
use axum::{Extension, Router, middleware};

use crate::api::_helpers::authentication::{oidc, password_sessions, trusted_proxy};
use crate::api::_helpers::authentication::{development, oidc, password_sessions, trusted_proxy};
use crate::api::models::{ApiAuthentication, DevelopmentScope, GroupRole, HumanLogin, LoginMode};
use crate::store::errors::ControlError;
use crate::store::models::ControlStore;
Expand All @@ -31,6 +31,7 @@ pub(crate) async fn configure(
let login_mode = match &login {
HumanLogin::Oidc(_) => LoginMode::Oidc,
HumanLogin::TrustedProxy(_) => LoginMode::TrustedProxy,
HumanLogin::Development(_) => LoginMode::Disabled,
_ => LoginMode::Password,
};
let (development, routes) = match login {
Expand All @@ -41,7 +42,14 @@ pub(crate) async fn configure(
"disabled authentication requires a loopback listener".into(),
));
}
(Some(scope), Router::new())
let person = scope.principal_id;
(
Some(scope),
password_sessions::person_routes().route_layer(middleware::from_fn_with_state(
person,
development::authenticate,
)),
)
}
HumanLogin::Password(origin) => (None, password_sessions::router(store, origin)?),
HumanLogin::TrustedProxy(settings) => (None, trusted_proxy::router(store, settings)?),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Development person requests: `local_request` already confined them to a loopback host and origin.

use std::net::SocketAddr;

use axum::extract::{ConnectInfo, Request, State};
use axum::middleware::Next;
use axum::response::Response;
use uuid::Uuid;

use crate::credentials::models::{AuditContext, AuthenticatedPerson};

pub(crate) async fn authenticate(
State(principal_id): State<Uuid>,
mut request: Request,
next: Next,
) -> Response {
let peer = request
.extensions()
.get::<ConnectInfo<SocketAddr>>()
.map(|info| info.0.ip().to_canonical());
request.extensions_mut().insert(AuditContext {
actor: format!("principal:{principal_id}"),
granting_role: "development".into(),
request_id: Uuid::now_v7(),
ip_address: peer,
});
request
.extensions_mut()
.insert(AuthenticatedPerson(principal_id, None));
next.run(request).await
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub(crate) mod bearer;
pub(crate) mod cli_login;
pub(crate) mod configure;
pub(crate) mod development;
pub(crate) mod oidc;
pub(crate) mod oidc_provider;
pub(crate) mod password_sessions;
Expand Down
4 changes: 4 additions & 0 deletions crates/sqlbuild-orchestration/src/api/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ pub enum LoginMode {
Password,
TrustedProxy,
Oidc,
/// `sqb dev` on loopback: no sign-in, every request acts as the local development person.
Disabled,
}

/// What the UI needs first: how people sign in, and what this person may see and do.
Expand All @@ -232,6 +234,8 @@ pub struct DevelopmentScope {
pub location_id: uuid::Uuid,
pub target: String,
pub triggers: bool,
/// The local person every loopback request acts as.
pub principal_id: uuid::Uuid,
}

/// The one way people sign in to this server, if any.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! `sqb dev` acts as one local administrator, recreated or restored on startup when missing.

use uuid::Uuid;

use crate::authorization::main::grant_admin::grant_admin;
use crate::credentials::main::identities::create_principal::create_principal;
use crate::credentials::models::AuditContext;
use crate::store::errors::ControlError;
use crate::store::main::read_records::read_records;
use crate::store::models::{ControlStore, Parameter};

pub async fn development_person(
store: &ControlStore,
context: &AuditContext,
) -> Result<Uuid, ControlError> {
let rows: Vec<serde_json::Value> = read_records(
store,
"SELECT p.principal_id, p.disabled_at, a.assignment_id FROM sqb_principals p \
LEFT JOIN sqb_role_assignments a ON a.principal_id=p.principal_id AND a.role_key='admin' \
AND a.location_id IS NULL AND a.target IS NULL WHERE p.name=$1",
vec![Parameter::Text("local-development".into())],
)
.await?;
let id = match rows.first() {
Some(row) => serde_json::from_value(row["principal_id"].clone())
.map_err(|_| ControlError::Schema("invalid development person identity".into()))?,
None => create_principal(store, context, "local-development").await?,
};
let administrator = rows
.first()
.is_some_and(|row| row["disabled_at"].is_null() && !row["assignment_id"].is_null());
if !administrator {
grant_admin(store, context, id).await?;
}
Ok(id)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod create_principal;
pub mod development_person;
pub(crate) mod insert_principal;
pub mod list_people;
27 changes: 27 additions & 0 deletions crates/sqlbuild-orchestration/tests/api/test_development.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use uuid::Uuid;

use crate::api::models::{ApiAuthentication, DevelopmentScope, HumanLogin};
use crate::credentials::main::identities::development_person::development_person;
use crate::credentials::main::locations::development_location::development_location;
use crate::credentials::models::AuditContext;
use crate::declarations::main::{
Expand Down Expand Up @@ -82,19 +83,45 @@ fn given_development_when_defaults_publish_then_identity_audit_and_trigger_pause
.await
.expect("target-specific identity");
assert_ne!(location, other);
let person = development_person(store, &context)
.await
.expect("development person");
assert_eq!(
development_person(store, &context)
.await
.expect("retained development person"),
person
);
let api = start_api_with_authentication(
store,
ApiAuthentication {
login: HumanLogin::Development(DevelopmentScope {
location_id: location,
target: "development".into(),
triggers: false,
principal_id: person,
}),
..ApiAuthentication::default()
},
)
.await;
let client = reqwest::Client::new();
let bootstrap: serde_json::Value = client
.get(format!("{}/api/control/bootstrap", api.url))
.send()
.await
.expect("bootstrap")
.error_for_status()
.expect("loopback person needs no sign-in")
.json()
.await
.expect("bootstrap body");
assert_eq!(bootstrap["login_mode"], "disabled", "{bootstrap}");
assert_eq!(
bootstrap["access"]["principal_id"],
person.to_string(),
"{bootstrap}"
);
let url = format!(
"{}/api/locations/{location}/targets/development/snapshots",
api.url
Expand Down
11 changes: 11 additions & 0 deletions ui/src/lib/api/http-audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,18 @@ describe('toAuditEntry', () => {
after: '{"token_id":"t-1"}',
scope: { locationId: null, subject: 'run_id=run-4' }
});
expect(entry.summary).toBe('');
expect(auditMatches(entry, { outcome: 'denied' })).toBe(false);
expect(auditMatches(entry, { actor: 'scheduler/s-1', action: 'token.issued' })).toBe(true);
});

it('says what work a location was asked for, and which run a read was of', () => {
const allowed = (action: string, after_json: Record<string, unknown>) =>
toAuditEntry({ ...base, actor: 'principal:p-1', actor_name: 'ana', action, granting_role: 'admin', scope_json: { location_id: 'loc-1', target: 'prod' }, after_json }).summary;
const work = (work: Record<string, unknown>) => allowed('location.work_requested', { request_id: 'q-1', reason: 'plan', work });
expect(work({ kind: 'plan', command: ['build', '--select', 'orders'], selection: ['model:orders'], settings: {} })).toBe('Plan sqb build --select orders');
expect(work({ kind: 'sync_warehouse' })).toBe('Sync the warehouse copy');
expect(allowed('error.read', { run_id: 'd36ac8b2-314c-46aa', found: true })).toBe('Run d36ac8b2');
expect(allowed('output.read', { run_id: 'd36ac8b2-314c-46aa', found: false })).toBe('Run d36ac8b2, not found');
});
});
20 changes: 19 additions & 1 deletion ui/src/lib/api/http-audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ function scopeOf(scope: Record<string, unknown>): AuditEntry['scope'] {
};
}

const WORK: Record<string, string> = {
check_connection: 'Test the warehouse connection',
sync_warehouse: 'Sync the warehouse copy',
evaluate_sensor: 'Evaluate a sensor'
};

/** What an allowed action did, from the fields the server records with it; empty when nothing reads better than the raw change. */
function allowedSummary(action: string, after: Record<string, unknown>): string {
if (action === 'location.work_requested') {
const work = fieldsOf(after.work);
const kind = textOf(work.kind) ?? '';
if (kind === 'plan' && Array.isArray(work.command)) return `Plan sqb ${work.command.join(' ')}`;
return WORK[kind] ?? kind;
}
const run = textOf(after.run_id);
return run ? `Run ${run.slice(0, 8)}${after.found === false ? ', not found' : ''}` : '';
}

/** A denial is listed under the action it refused, with its decision ID and reason. */
export function toAuditEntry(wire: Wire['SecurityAuditEvent']): AuditEntry {
const denied = wire.action === DENIED;
Expand All @@ -48,7 +66,7 @@ export function toAuditEntry(wire: Wire['SecurityAuditEvent']): AuditEntry {
grantingRole: denied ? null : wire.granting_role,
decisionId: denied ? textOf(after.decision_id) : null,
evidenceRead: wire.action === 'evidence.read',
summary: denied ? (textOf(after.reason) ?? '') : '',
summary: denied ? (textOf(after.reason) ?? '') : allowedSummary(wire.action, after),
before: jsonText(wire.before_json),
after: denied ? null : jsonText(wire.after_json),
ip: wire.ip_address ?? '',
Expand Down
10 changes: 8 additions & 2 deletions ui/src/lib/api/http-jobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,14 @@ describe('schedule due times', () => {
const calendar = trigger('t-cal', 'business_days', 'orders', { description: 'Business days', job_key: 'orders', schedule: { kind: 'calendar', times, refresh_at: '2026-11-08T00:00:00Z' } });
const detailOf = (wire: W['PersonTriggerRecord']) => triggerDetailOf(toTrigger(wire, [], null), wire, [], []);

it('reports the last stored calendar time and the next refresh', () => {
expect(detailOf(calendar).calendar).toMatchObject({ storedUntil: '2026-11-10T09:00:00.000Z', nextRefreshAt: '2026-11-08T00:00:00Z', horizon: null, low: false });
it('reports the last stored calendar time, the next refresh, and whether it is running out of dates', () => {
const at = (now: string) => triggerDetailOf(toTrigger(calendar, [], null), calendar, [], [], null, Date.parse(now)).calendar;
expect(at('2026-11-08T12:00:00Z')).toMatchObject({ storedUntil: '2026-11-10T09:00:00.000Z', nextRefreshAt: '2026-11-08T00:00:00Z', horizon: null, low: false });
// The last date is 23 hours away: under the server's one-day warning.
expect(at('2026-11-09T10:00:00Z')).toMatchObject({ low: true, lowHorizonWarningBelow: '1 day of dates' });
// The server warns only about running calendars.
const paused = { ...calendar, status: 'paused' as const };
expect(triggerDetailOf(toTrigger(paused, [], null), paused, [], [], null, Date.parse('2026-11-09T10:00:00Z')).calendar?.low).toBe(false);
expect(detailOf(daily).calendar).toBeNull();
});

Expand Down
14 changes: 10 additions & 4 deletions ui/src/lib/api/http-jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,23 @@ function cursorMovedAt(state: Wire['PersonSensorState'], history: TriggerStateEv
return times.length ? times.reduce((a, b) => (Date.parse(a) >= Date.parse(b) ? a : b)) : null;
}

/** A calendar is running out of dates once its last one is this close, as the server judges it. */
const CALENDAR_LOW_MS = 24 * 60 * 60 * 1000;

export function triggerDetailOf(
trigger: Trigger,
wire: Wire['PersonTriggerRecord'],
ticks: Tick[],
history: TriggerStateEvent[],
state: Wire['PersonSensorState'] | null = null
state: Wire['PersonSensorState'] | null = null,
now = Date.now()
): TriggerDetail {
const d = wire.definition_json;
const evaluations = state?.evaluations.map(toEvaluation) ?? null;
const lastEvaluatedFingerprint = state?.evaluations[0]?.definition_fingerprint ?? null;
const calendar = isSchedule(d) && d.schedule.kind === 'calendar' ? d.schedule : null;
const stored = calendar ? calendar.times.map(Date.parse) : [];
const last = stored.length ? Math.max(...stored) : null;
return {
...trigger,
definitionChanged: !!state && lastEvaluatedFingerprint !== null && lastEvaluatedFingerprint !== state.definition_fingerprint,
Expand All @@ -151,11 +156,12 @@ export function triggerDetailOf(
upcoming: isSchedule(d) ? wire.upcoming : null,
calendar: calendar && {
horizon: null,
storedUntil: stored.length ? new Date(Math.max(...stored)).toISOString() : null,
storedUntil: last === null ? null : new Date(last).toISOString(),
lastRefreshedAt: null,
nextRefreshAt: calendar.refresh_at,
lowHorizonWarningBelow: null,
low: false
// The server warns when a running calendar's last date is a day away or less.
lowHorizonWarningBelow: '1 day of dates',
low: wire.status === 'running' && (last === null || last <= now + CALENDAR_LOW_MS)
},
sensor: isSchedule(d)
? null
Expand Down
32 changes: 32 additions & 0 deletions ui/src/lib/build-area/changes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from 'vitest';
import type { ChangeOp, NodeChange } from '$lib/api';
import { nodeChangeGroups, nodeChangeLine } from './changes';

const change = (key: string, op: ChangeOp = 'added'): NodeChange => ({ key, kind: null, op, formerKey: null, compiledOnly: false, detail: '', columns: [] });
const many = (n: number, prefix: string, op: ChangeOp = 'added') => Array.from({ length: n }, (_, i) => change(`${prefix}:n${i}`, op));

describe('node change summaries', () => {
it('counts a first deploy by change and kind, largest first, in plain nouns', () => {
const nodes = [...many(3, 'model'), ...many(1200, 'audit'), change('udf:margin'), ...many(2, 'model', 'removed'), change('widget:x')];
expect(nodeChangeGroups(nodes)).toEqual([
{ op: 'added', prefix: 'audit', count: 1200, noun: 'checks' },
{ op: 'added', prefix: 'model', count: 3, noun: 'models' },
{ op: 'removed', prefix: 'model', count: 2, noun: 'models' },
{ op: 'added', prefix: 'udf', count: 1, noun: 'function' },
{ op: 'added', prefix: 'widget', count: 1, noun: 'widget' }
]);
expect(nodeChangeLine(nodes)).toBe('added 1,200 checks · added 3 models · removed 2 models · 2 more kinds');
});

it('keeps SQL and Python hooks apart, so each kind is its own group', () => {
expect(nodeChangeGroups([change('sql_hook:a'), change('hook:b')]).map((g) => [g.prefix, g.noun])).toEqual([
['hook', 'Python hook'],
['sql_hook', 'SQL hook']
]);
});

it('names the nodes when there are only a few', () => {
expect(nodeChangeLine([change('model:orders', 'changed'), change('seed:carriers')])).toBe('changed model:orders · added seed:carriers');
expect(nodeChangeLine([])).toBe('No node changes');
});
});
Loading
Loading