Documentation
¶
Overview ¶
Package console is the human side of locksql: it holds the credentials and the only database connection, enforces the approved policy, shows every query to the human for approval, masks the results and writes the audit trail. Clients (the CLI and the MCP server) reach it over the per-profile socket and never see a secret.
Index ¶
- Constants
- Variables
- func ManagedCheck(cfg *config.Config, names []string) ([]string, error)
- func ManagedLines(err error) []string
- func Run(ctx context.Context, o Options) error
- func SameUserRefused(sys *sysconf.Config) error
- func WithAllowedUIDs(sys *sysconf.Config, uids []int) *sysconf.Config
- type Approver
- type Beller
- type Decision
- type IO
- type Key
- type KeySource
- type LineSource
- type Options
- type Outcome
- type Result
- type Server
- func (s *Server) ApplyPolicy(kp kconsole.Policy) error
- func (s *Server) AuditRecord(b kaudit.Base) kaudit.Record
- func (s *Server) CheckMask(pattern string) (string, error)
- func (s *Server) CheckPolicy()
- func (s *Server) Checks(ctx context.Context, sess kconsole.Session) []kconsole.Finding
- func (s *Server) Close()
- func (s *Server) Command(ctx context.Context, line string)
- func (s *Server) Connect(context.Context, kconsole.Credentials) (kconsole.Session, error)
- func (s *Server) ConnectionChanged(oldP, nextP kconsole.Policy) bool
- func (s *Server) End(reason string)
- func (s *Server) Ended() (string, bool)
- func (s *Server) FormatChanges(changes []kconfig.Change) []string
- func (s *Server) Handle(ctx context.Context, req ipc.Request) ipc.Response
- func (s *Server) Key(k Key)
- func (s *Server) LoadPolicy() (kconsole.Policy, error)
- func (s *Server) ManagedBounds(kp kconsole.Policy, m *managed.File) (managed.Bounds, error)
- func (s *Server) Methods() map[string]kconsole.Handler
- func (s *Server) PIIRules(kp kconsole.Policy) (mask, allow []string)
- func (s *Server) SaveMask(pattern string) error
- func (s *Server) Serve(ctx context.Context, ln net.Listener, lines <-chan string) error
- func (s *Server) Status(kconsole.Session) any
- func (s *Server) StatusAnswer(c kconsole.StatusResult, _ kconsole.Session) any
- func (s *Server) StatusLineParts(kconsole.StatusView) (string, []string)
- func (s *Server) StatusRows(v kconsole.StatusView) [][2]string
- func (s *Server) Tick()
- func (s *Server) WithMask(kp kconsole.Policy, pattern string) kconsole.Policy
- func (s *Server) Words() kconsole.Words
- type ServerConfig
- type Sized
- type StatusLine
- type Terminal
Constants ¶
const ( // The idle timeout and the maximum session length are the profile's // limits.idle_timeout and limits.max_session. ApprovalTimeout = kconsole.ApprovalTimeout PlanTTL = 10 * time.Minute // PolicyPoll is how often the config files are checked for changes. PolicyPoll = kconsole.PolicyPoll )
Session timeouts, spec §6.
const ( KeyShiftTab = kconsole.KeyShiftTab KeyCtrlO = kconsole.KeyCtrlO )
The control keys.
const ( Approved = kconsole.Approved Denied = kconsole.Denied Unanswered = kconsole.Unanswered RetypeRequested = kconsole.RetypeRequested NoFinger = kconsole.NoFinger )
Outcomes of an approval.
const DetachLine = "Ctrl-P Ctrl-Q detaches (the console keeps running) · Ctrl-C stops it · closing this terminal only detaches"
DetachLine is how the human leaves the console's terminal without ending it, word for word lox's (lockhub cmd/lox/attach.go detachLine): the console runs under `lox run` and is reached with `lox attach`, which detaches on Ctrl-P Ctrl-Q.
const FingerDeadline = kconsole.FingerDeadline
FingerDeadline bounds the wait for a finger after a typed yes.
const PIIDirEnv = "LOCKSQL_PII_DIR"
PIIDirEnv names the directory where the console keeps its PII rule sets (pii/<set>.toml) instead of the profile's directory: lox run sets it to the console's own volume, its profile copy being read-only.
const ResponseQuantum = kconsole.ResponseQuantum
ResponseQuantum levels the time a statement takes, as the client sees it: the answer to query.run, success or failure, leaves at the next multiple of it after the run started. A query cannot turn the time it takes into a fine-grained signal (a timing side channel).
Variables ¶
var ErrConfig = kconsole.ErrConfig
ErrConfig marks usage and configuration errors (exit code 3). It is lockkit's, which the isolation checks wrap too.
var ErrNoHub error = noHubError{}
ErrNoHub refuses a console started without its hub: the console runs hub-attached only, started by `lox run`. It is an ErrConfig (exit code 3).
var ErrTooManyNoMatch = kconsole.ErrTooManyNoMatch
ErrTooManyNoMatch is set on the third consecutive no-match: the caller ends the console.
Functions ¶
func ManagedCheck ¶ added in v0.18.0
ManagedCheck checks the profiles names of cfg against the bounds of the company's managed file, as the console does at start: the policy of each, its PII rule set and the managed rules included, through ManagedBounds and lockkit's managed.Check. It opens no connection and reads no credential. It returns one "blocked by company policy: …" line per violation; an error is a profile whose policy cannot be read.
func ManagedLines ¶ added in v0.18.0
ManagedLines are the lines that cite why the company's managed configuration refused a start, nil for any other error.
func Run ¶
Run is `locksql console`: the start-up sequence of spec §6, then the request loop until Ctrl-C, logout, :quit or a timeout.
func SameUserRefused ¶ added in v0.1.2
SameUserRefused is the refusal of a console without a separated setup (sys nil, as sysconf.Load returns it), or nil.
Types ¶
type Beller ¶ added in v0.5.0
The console's terminal and its optional abilities (lockkit's console).
type KeySource ¶ added in v0.5.0
The console's terminal and its optional abilities (lockkit's console).
type LineSource ¶
type LineSource = kconsole.LineSource
The console's terminal and its optional abilities (lockkit's console).
type Options ¶
type Options struct {
Profile string
// AllowUnmask lets clients ask for unmasked output (--allow-unmask).
AllowUnmask bool
// ShowResults starts with the clear results on (--show-results);
// Ctrl-O switches them while the console runs.
ShowResults bool
Cwd string
IO IO
// Now defaults to time.Now.
Now func() time.Time
// StateDir defaults to config.StateDir().
StateDir string
// PIIDir, when set (PIIDirEnv), is the directory of the PII rule sets
// (pii/<set>.toml) instead of the profile's: lox run's copy of the
// profile is read-only, and the console writes its first rule set and
// every rule it adds. A set it lacks is first copied from the
// profile's directory.
PIIDir string
// Version is the console version reported to clients.
Version string
// TTY is the console's terminal device (nil when unknown): its owner
// must be the console's account in a separated setup.
TTY *os.File
// Hub is the lockhub the console is decided on (--hub, --hub-key-file,
// --instance, as lox run passes them): required. The human decides in
// lockhub, query.run answers pending with a ticket.
Hub kconsole.HubOptions
// AllowedUIDs (--allowed-uids) may connect besides the members of the
// client group, as allowed_uids of the system setup: an agent in a
// container of its own whose uid has no account in the console's.
// Set only by whoever starts the console, never by a client.
AllowedUIDs []int
}
Options configure Run.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves client requests one at a time against one session. It is not safe for concurrent use: Serve runs every request on one goroutine.
The request loop, the live switches, :status, :review and the policy polling, the session limits and the reconnection are lockkit's console (k); Server is its backend (kserver.go): the SQL methods, the approval screen, the masking and what locksql shows in its own words.
func NewServer ¶
func NewServer(cfg ServerConfig) (*Server, error)
NewServer checks cfg and returns a server enforcing cfg.Policy.
func (*Server) ApplyPolicy ¶ added in v0.11.0
ApplyPolicy implements kconsole.PolicyApplier: it compiles the PII rules, the detectors and the SQL dialect of p, which then is in force.
func (*Server) AuditRecord ¶ added in v0.11.0
AuditRecord turns the console's own records (modes, policy, login) into locksql's, stamped with the database and its user.
func (*Server) CheckMask ¶ added in v0.11.0
CheckMask implements kconsole.PIIBackend: the canonical form of a db.table.column pattern (schema.table.column on PostgreSQL), refused when it names no column of the catalog (resolveMask).
func (*Server) CheckPolicy ¶
func (s *Server) CheckPolicy()
CheckPolicy re-reads the policy from the config files. Changes that only tighten are applied at once; a loosening waits for :review, and plans are refused with CodePolicyPending meanwhile. Without LoadPolicy (tests) there is nothing to poll.
func (*Server) Checks ¶ added in v0.11.0
Checks implements kconsole.Backend: the privilege audit of the tier in force. lockkit's console runs it when the profile becomes production.
func (*Server) Connect ¶ added in v0.11.0
Connect implements kconsole.Backend. locksql runs its own start-up (lifecycle.go) and reconnection (ServerConfig.Reconnect): lockkit's console never connects for it.
func (*Server) ConnectionChanged ¶ added in v0.11.0
ConnectionChanged implements kconsole.ConnectionComparer: a change of the connection settings, the transport (the tls settings, the ssh table, proxy_command) included, ends the session: the open connection no longer matches the policy.
func (*Server) FormatChanges ¶ added in v0.11.0
FormatChanges implements kconsole.ChangeFormatter.
func (*Server) Handle ¶
Handle serves one request. It is the whole client surface of the console: none of the methods loosens the policy.
func (*Server) LoadPolicy ¶ added in v0.11.0
LoadPolicy implements kconsole.Backend.
func (*Server) ManagedBounds ¶ added in v0.18.0
ManagedBounds implements kconsole.ManagedBackend: kp at its ceiling under the [locksql.bounds] of m, which lockkit's managed.Check compares kp with through config.Diff. A value outside an allowlist bound (credentials, engine, hosts, family.vault.required) is an error naming the managed key; lockkit's console refuses kp on it as on a violation.
func (*Server) Methods ¶ added in v0.11.0
Methods implements kconsole.Backend: the SQL methods. hello, status, pii.list, pii.add, change.request and logout are the console's.
func (*Server) PIIRules ¶ added in v0.11.0
PIIRules implements kconsole.PIIBackend (pii.list), sorted.
func (*Server) SaveMask ¶ added in v0.11.0
SaveMask implements kconsole.PIIBackend: the rule is added to the rule set's file as it is on disk, so that unconfirmed edits of the human are neither lost nor applied.
func (*Server) Serve ¶
Serve accepts clients on ln and serves their requests one at a time until ctx ends or the session ends (logout, timeouts, :quit). lines are the console commands typed by the human (nil for none); the control keys come from the IO when it is a KeySource. ln is closed on return, which removes the socket. It is lockkit's request loop.
func (*Server) Status ¶ added in v0.11.0
Status implements kconsole.Backend (unused: StatusAnswer answers).
func (*Server) StatusAnswer ¶ added in v0.11.0
StatusAnswer implements kconsole.StatusAnswerer: locksql's clients read a flat status answer.
func (*Server) StatusLineParts ¶ added in v0.11.0
func (s *Server) StatusLineParts(kconsole.StatusView) (string, []string)
StatusLineParts implements kconsole.StatusLiner: the tier follows the profile name, and the open plans are counted.
func (*Server) StatusRows ¶ added in v0.11.0
func (s *Server) StatusRows(v kconsole.StatusView) [][2]string
StatusRows implements kconsole.StatusRower: the :status screen.
func (*Server) Tick ¶
func (s *Server) Tick()
Tick ends the session at the maximum session length, as set by the policy in force (a policy change applies from the next tick), and closes the connection at the idle timeout (idleconn.go); without IdleConnect, the idle timeout ends the session.
type ServerConfig ¶
type ServerConfig struct {
// Policy is the approved policy the server enforces.
Policy config.Policy
// RulesPath is the file of the profile's PII rule set:
// <project>/.locksql/pii/<set>.toml, or pii/<set>.toml in the user
// config directory outside a project.
RulesPath string
// StateDir and ApprovedKey locate the approved policy file.
StateDir string
ApprovedKey string
Session engine.Session
// DBUser is the database account in use (it may have been asked for).
DBUser string
Databases []string
Audit *audit.Log
IO IO
// Approver takes every human decision: the hub's asynchronous
// approver, which keeps the console's own decisions (policy changes,
// :unmask) on this terminal. nil means Hub's around typed approvals.
Approver Approver
// Fingerprint is true when Approver is the fingerprint approver
// (level 2): a typed answer alone then never widens anything.
Fingerprint bool
// Boundary names the desktop boundary for the status screen ("" in
// the session shape).
Boundary string
Now func() time.Time
// AllowUnmask starts with unmasked output allowed: clients may ask for
// it, and each such query is decided in lockhub. Without it an
// unmask plan is refused before anything else. :unmask switches it
// while the console runs.
AllowUnmask bool
// ShowResults starts with the clear results on: the result of each
// masked run printed in the console, in clear; the client still gets
// it masked. Ctrl-O switches them while the console runs. Ignored on
// the desktop (Boundary set): levels 1 and 2 keep the results masked.
ShowResults bool
Version string
// LoadPolicy re-reads the current policy from the config files. Nil
// disables CheckPolicy.
LoadPolicy func() (config.Policy, error)
// Reconnect opens a new session after a lost connection, under p, the
// profile in force at that time. Nil means a lost connection ends the
// console.
Reconnect func(ctx context.Context, p config.Profile) (engine.Session, error)
// RefuseProduction re-applies the start-up refusals of a production
// profile (an X11 session, core dumps that could not be disabled) when
// the policy in force becomes production: an error ends the session.
// nil: none.
RefuseProduction func() error
// Quantum levels the response time of query.run (ResponseQuantum in
// the real console, 0 to disable).
Quantum time.Duration
// PeerAllowed decides which clients are served, from the kernel's
// credentials of the peer. Nil serves the console's own account only.
PeerAllowed func(ipc.Cred) bool
// Health is reported in status for locksql doctor.
Health *ipc.Health
// Refused is the fingerprint of a loosening the human refused at start,
// not announced again until the files change.
Refused string
// Hub is the lockhub the requests are decided on (required).
Hub *kconsole.Hub
// IdleConnect opens the session again after an idle end, on approved
// connect tickets (idleconn.go): the console then closes its
// connection at idle_timeout and keeps running. nil (tests of the
// server alone) ends the session at idle_timeout instead.
IdleConnect idleConnector
// Managed is the company's managed file in force at start (nil: none),
// and LoadManaged re-reads it at every policy poll (managed.Load in Run;
// nil keeps Managed for the session): a policy beyond its bounds is
// refused, never pending, and a stricter file ends the session or
// tightens the policy in force.
Managed *managed.File
LoadManaged func(context.Context) (*managed.File, error)
}
ServerConfig holds what a Server needs once the start-up sequence is done. Run builds it; tests build it directly.
type StatusLine ¶ added in v0.5.0
type StatusLine = kconsole.StatusLine
The console's terminal and its optional abilities (lockkit's console).