Documentation
¶
Overview ¶
Package api is the REST API under /api/v1. Reads and writes need a bearer token with the right scope, or the console's signed-in session (session.go); the OpenAPI description is public. Errors are JSON {"error", "code"} with stable codes.
Index ¶
- Constants
- Variables
- func OpenAPI() []byte
- func RetryAfter(d time.Duration) string
- func ScopeProblem(p store.Principal, scope string) string
- func SignInKind(next string) string
- func ToolMeta(name string) map[string]any
- func WaitWords(d time.Duration) string
- type Auth
- func (a *Auth) Attempt(r *http.Request, secret string, verify func() (accepted, genuine bool)) (time.Duration, error)
- func (a *Auth) Authenticate(r *http.Request, secret string) (store.Principal, bool)
- func (a *Auth) Blocked(r *http.Request) bool
- func (a *Auth) BlockedFor(r *http.Request) (time.Duration, bool)
- func (a *Auth) BlockedForSecret(r *http.Request, secret string) (time.Duration, bool)
- func (a *Auth) Check(r *http.Request, secret string) (store.Principal, time.Duration, error)
- func (a *Auth) CheckDevice(r *http.Request, secret string) (store.Principal, time.Duration, error)
- func (a *Auth) CheckToken(r *http.Request, secret string) (store.Token, time.Duration, error)
- func (a *Auth) Client(r *http.Request) netip.Addr
- func (a *Auth) ClientAddr(r *http.Request) netip.Addr
- func (a *Auth) CodeStep(r *http.Request, challenge, code string) (Step, error)
- func (a *Auth) Fail(r *http.Request)
- func (a *Auth) PasswordStep(r *http.Request, username, password, kind string) (Step, error)
- func (a *Auth) Recent(p store.Principal, within time.Duration) bool
- func (a *Auth) Reconfirm(r *http.Request, username, password, code string) error
- func (a *Auth) Remember(r *http.Request, secrets ...string)
- func (a *Auth) Separate() *Auth
- func (a *Auth) SignIn(r *http.Request, username, password string) (store.Account, error)
- func (a *Auth) StartSession(r *http.Request, step Step) (string, store.ConsoleSession, error)
- func (a *Auth) UserBlockedFor(r *http.Request, username string) (time.Duration, bool)
- func (a *Auth) Verify(r *http.Request, secret string) (store.Token, bool)
- type Console
- type ConsoleSession
- type DeviceUser
- type DeviceView
- type Features
- type LinkInfo
- type MCPPromptArg
- type MCPPromptSpec
- type Me
- type MeLinks
- type SectionView
- type Server
- type Session
- type SignInPolicy
- type SignedIn
- type Step
- type TwoFactorStatus
Constants ¶
const ( StepSignedIn = "signed_in" StepTwoFactor = "two_factor" // give a code from the authenticator app, or a recovery code StepTwoFactorSetup = "two_factor_setup" // set up an authenticator app, then give its first code )
Where a sign-in stands.
const ( DomainIdentity = "identity" DomainAlerts = "alerts" DomainMonitors = "monitors" DomainMachines = "machines" DomainSessions = "sessions" DomainVault = "vault" )
The areas MCP groups its tools in, as the console names its pages.
const CSRFHeader = "X-CSRF-Token"
CSRFHeader carries a console session's CSRF secret on a change.
const ConsoleCookie = "__Host-beacon-console"
ConsoleCookie holds the id of a console session. __Host- keeps it to this host, Secure and on /.
const DocsURL = "https://beacon.openlinkos.com/reference/api"
DocsURL is the API's reference, where an answer to a request without a valid token points: whoever sends one may not know Beacon yet, and that page says how to get a token and what it may do.
Variables ¶
var DefaultPolicy = SignInPolicy{SessionLifetime: 12 * time.Hour, RecentSignIn: 10 * time.Minute, Issuer: "Beacon"}
DefaultPolicy is the policy without settings: two-factor sign-in optional, 12-hour sign-ins, ten minutes of recent sign-in.
var ErrBlocked = errors.New("too many failed sign-ins; try again in a few minutes")
ErrBlocked means too many sign-ins failed recently, from this address or for this username.
var ErrInvalid = errors.New("the credential is unknown or has expired")
ErrInvalid means a secret is unknown, has expired or was revoked, or its holder may not act now.
var PublicRoutes = []string{"POST /api/v1/sign-in", "POST /api/v1/sign-in/two-factor"}
PublicRoutes are the routes open without a credential, for the test that keeps the OpenAPI description in step.
var Routes = []string{
"GET /api/v1/alerts", "GET /api/v1/alerts/{id}", "POST /api/v1/alerts/{id}/ack",
"GET /api/v1/silences", "DELETE /api/v1/silences/{id}", "GET /api/v1/audit",
"POST /api/v1/alerts/{id}/silence", "POST /api/v1/alerts/{id}/notes", "GET /api/v1/changes",
"PUT /api/v1/alerts/{id}/agent-runs/{delivery}", "GET /api/v1/agent-hooks/{name}/first-drill",
"POST /api/v1/probe-now",
"POST /api/v1/share-links/{kind}/{id}", "POST /api/v1/share-links/{kind}/{id}/revoke",
}
Routes lists the authenticated routes, for the test that keeps the OpenAPI description in step.
var SettingsSections = map[string]settingsSection{ "general": {/* contains filtered or unexported fields */}, "security": {/* contains filtered or unexported fields */}, "ssh": {/* contains filtered or unexported fields */}, "storage": {/* contains filtered or unexported fields */}, "vault": {/* contains filtered or unexported fields */}, "alerting": { // contains filtered or unexported fields }, }
SettingsSections are the sections and the settings in each.
Functions ¶
func RetryAfter ¶
RetryAfter is a lockout's wait in whole seconds, rounded up, for the Retry-After header. The gateway answers its lockouts the same way the API does, so it shares these words.
func ScopeProblem ¶
ScopeProblem says why p may not use scope ("a|b"): for an agent, what its preset within its owner's role allows, so that it knows whom to ask.
func SignInKind ¶
SignInKind is what a sign-in that goes on to next is for: an OAuth client's consent, or the console.
Types ¶
type Auth ¶
type Auth struct {
Store *store.Store
IP *clientip.Resolver
Now func() time.Time
// Policy is the sign-in policy from the settings (signin.go); nil is DefaultPolicy.
Policy func() SignInPolicy
// contains filtered or unexported fields
}
Auth checks bearer tokens and people's passwords, and limits failed attempts per client address and, for passwords, per username.
An address is an IPv4 address, or an IPv6 address's /64, which one machine or one household usually holds whole. Only a secret that Beacon did not issue counts as a failure: a genuine credential refused for what it may do, or because its owner is disabled, is not a guess. After maxFails failures in failWindow the address is locked out until the window ends. A secret is then refused unread unless it was accepted from the same address earlier (within goodFor), so a client with a working credential keeps working beside one that keeps failing, while the locked address cannot be used to try new secrets.
A username is locked after maxUserFails wrong passwords in failWindow, from anywhere. While it is, an address that has itself got that username wrong maxUserAddrFails times is refused for it; an address that has not, such as the person's own, may still sign in, and each wrong password from it counts. Wrong passwords from one address never hold another back for the username, so that someone guessing it cannot keep the person out.
func (*Auth) Attempt ¶
func (a *Auth) Attempt(r *http.Request, secret string, verify func() (accepted, genuine bool)) (time.Duration, error)
Attempt checks a secret under the address's lockout. verify does the checking and reports whether the secret is accepted, and whether a refused secret is nonetheless genuine (one Beacon issued), which does not count as a failure. While the address is locked out, verify runs only for a secret accepted from this address before; any other gets ErrBlocked and the wait, unread. A refused secret gets ErrInvalid.
func (*Auth) Authenticate ¶
Authenticate is Check without the reason for a refusal.
func (*Auth) BlockedFor ¶
BlockedFor reports how much of the address's lockout is left, and whether there is one: how long the client must wait before trying again with anything but a credential that already worked from here.
func (*Auth) BlockedForSecret ¶
BlockedForSecret is BlockedFor for a request that presents secret: a secret that worked from the address, or that Beacon issued to a client there (Remember), passes the lockout.
func (*Auth) Check ¶
Check checks a bearer secret for the API, MCP and the pages, counting failures: a token or an OAuth access token, resolved to who it is and what it may do now. It reports ErrBlocked with the wait, or ErrInvalid.
func (*Auth) CheckDevice ¶
CheckDevice checks a browser extension's device credential, counting failures like Check: it resolves only to the person it was issued to (store.AuthenticateDevice). Check never takes one.
func (*Auth) CheckToken ¶
CheckToken checks a token secret for the request, counting failures: the machines' check, on the gateway, where an agent's token carries no scopes of its own. It reports ErrBlocked with the wait, or ErrInvalid.
func (*Auth) ClientAddr ¶
ClientAddr is the request's client address, through the trusted proxies: what access rules limited to source addresses check (store.Principal.From). Without a resolver, the connection's own address.
func (*Auth) CodeStep ¶
CodeStep is the second step of a sign-in: the code for the challenge PasswordStep opened. It answers store.ErrNoChallenge when the challenge is unknown or over, ErrBlocked after too many failures, store.ErrBadCode for a wrong code (the challenge stays open for a few more), and store.ErrDisabled when the account was disabled meanwhile.
func (*Auth) Fail ¶
Fail counts a failed attempt from the request's client: a wrong code or refresh token on the OAuth token endpoint, say.
func (*Auth) PasswordStep ¶
PasswordStep is the first step of a sign-in to kind (store.SignInConsole or SignInOAuth). It answers ErrBlocked after too many failures, store.ErrBadCredentials for a wrong username or password, and store.ErrDisabled for a disabled account; otherwise the person is signed in, or the Step says what the second step asks for.
func (*Auth) Recent ¶
Recent reports whether p, a person in a console session (store.Principal.InConsoleSession), signed in or entered their password again within the security settings' recent sign-in window, or within, when that is shorter. The vault takes it as its vault.Reauth.
func (*Auth) Reconfirm ¶
Reconfirm checks the person's password again, and their code when they have two-factor sign-in, for a sensitive action: under the same limits as a sign-in, and written to the sign-in log as a failure when wrong. It answers ErrBlocked, store.ErrBadCredentials or store.ErrBadCode.
func (*Auth) Remember ¶
Remember notes secrets Beacon has just issued to the request's client as having worked from its address: an OAuth client's new access and refresh tokens, which replace the ones it signed in with, so that it keeps working there during a lockout as a token that worked there does.
func (*Auth) Separate ¶
Separate is an Auth on the same store and client addresses that counts its failures apart. The machines' gateway keeps its own, so that what people's and agents' clients get wrong never stops monitoring data, and the other way round.
func (*Auth) SignIn ¶
SignIn checks a person's username and password, limited per address and per username: ErrBlocked after too many failures, store.ErrBadCredentials for a wrong username or password.
func (*Auth) StartSession ¶
StartSession signs the person of a finished sign-in in to the console for the policy's lifetime, and writes the sign-in to the log. It returns the session's id, for the cookie.
func (*Auth) UserBlockedFor ¶
UserBlockedFor is BlockedFor for one username's wrong passwords, as they apply to the request's client: a locked username stays open to an address that has not got it wrong itself.
type Console ¶
type Console interface {
// APISession resolves the request's session cookie, as the console's own pages do; false when the
// request has none that is live.
APISession(r *http.Request) (ConsoleSession, bool)
// Language is the language the console shows the request's browser: its choice, else Beacon's.
Language(r *http.Request) i18n.Lang
// SetLanguage keeps the browser's choice of language.
SetLanguage(w http.ResponseWriter, l i18n.Lang)
}
Console is the console's side of the API, which web.Server implements: the session a request's cookie names, and the language the console shows a browser.
type ConsoleSession ¶
ConsoleSession is a signed-in console session as the API sees it: who it is now, the session's CSRF secret and when the session ends.
type DeviceUser ¶
type DeviceUser struct {
Username string `json:"username"`
DisplayName string `json:"display_name,omitempty"`
}
DeviceUser is whom a device credential belongs to.
type DeviceView ¶
type DeviceView struct {
ID string `json:"id"`
Name string `json:"name"`
Address string `json:"address,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Created time.Time `json:"created"`
LastSeen time.Time `json:"last_seen,omitzero"`
Expires time.Time `json:"expires"`
}
DeviceView is a browser extension's device credential as its person sees it: never its secret.
type Features ¶
type Features struct {
SSH bool `json:"ssh"`
SSHPaused bool `json:"ssh_paused"`
Config bool `json:"config"`
Settings bool `json:"settings"`
Status bool `json:"status"`
ProbeNow bool `json:"probe_now"`
Vault bool `json:"vault"`
}
Features are the parts of Beacon this instance runs.
type LinkInfo ¶
type LinkInfo struct {
Kind string `json:"kind"` // setup, invite or reset
Role string `json:"role,omitempty"` // an invitation's role (setup: owner)
Note string `json:"note,omitempty"` // an invitation's note from whoever made it
InvitedBy string `json:"invited_by,omitempty"` // who made an invitation
User string `json:"user,omitempty"` // the account a reset is for
Expires time.Time `json:"expires"`
// Tokens, for a reset, are the tokens of the person's agents that a new password leaves working
// unless the reset revokes them too.
Tokens []string `json:"tokens,omitempty"`
}
LinkInfo is a one-time link as its page shows it, never with its code.
type MCPPromptArg ¶
MCPPromptArg is an argument of an MCP prompt.
type MCPPromptSpec ¶
type MCPPromptSpec struct {
Name, Title, Domain, Description string
Arguments []MCPPromptArg
}
MCPPromptSpec is an MCP prompt: a workflow a person starts from their MCP client.
func MCPPrompts ¶
func MCPPrompts() []MCPPromptSpec
MCPPrompts are the MCP prompts: workflows a person starts from their MCP client (a slash command in Claude Code), the same steps the on-call harness gives an agent it wakes.
type Me ¶
type Me struct {
Kind string `json:"kind"` // person, agent or token
Name string `json:"name"` // as the audit log names the caller
Username string `json:"username,omitempty"` // the person; for an agent, the person it acts for
DisplayName string `json:"display_name,omitempty"` // the person's
Agent string `json:"agent,omitempty"`
Preset string `json:"preset,omitempty"` // an agent's preset
Token string `json:"token,omitempty"` // the token the caller came with
Subject string `json:"subject,omitempty"` // what a machine's or service's token writes as
// Credential is what the caller came with: a token, an OAuth sign-in (with its Client), or the
// console's session.
Credential string `json:"credential"`
Client string `json:"client,omitempty"`
Role string `json:"role,omitempty"` // the person's role; for an agent, its owner's
Admin bool `json:"admin"` // manages people and everyone's agents
Scopes []string `json:"scopes"`
MachineScope string `json:"machine_scope"` // all, listed or none
bridge.MachineAccessView
Language string `json:"language"` // en or zh: the console's language for the caller
Features Features `json:"features"`
Version string `json:"version"`
Links MeLinks `json:"links"`
Expires time.Time `json:"expires,omitzero"` // when the credential, or the console's session, ends
CSRF string `json:"csrf,omitempty"` // the console session's CSRF secret, for its writes
Groups []string `json:"groups"` // the person's user groups; for an agent, its owner's
}
Me is the caller as GET /api/v1/me shows it.
type MeLinks ¶
type MeLinks struct {
Docs string `json:"docs"` // the documentation, in the caller's language
API string `json:"api"` // the API's reference
OpenAPI string `json:"openapi"` // this API's description
MCP string `json:"mcp"` // the MCP endpoint
Repo string `json:"repo"` // the source
Home string `json:"home"` // the public site: the default status page, the first one, or Beacon's home page
}
MeLinks are where the console points people for more.
type SectionView ¶
type SectionView struct {
Version int64 `json:"version"`
Section string `json:"section"`
Settings map[string]any `json:"settings"`
Configured map[string]bool `json:"configured_secrets,omitempty"`
Hooks map[string]config.AgentSecretStatus `json:"configured_agent_secrets,omitempty"`
}
SectionView is one section: the settings' version, the section's keys and values, and which of its secrets are set (never their values); for alerting, which hooks have a secret or headers.
type Server ¶
type Server struct {
Bridge *bridge.Bridge
SSH *sshgate.Gate
SSHAddress string
Auth *Auth
Log *slog.Logger
Config *configdb.Service
Settings *config.Manager
Status *status.Service // the public status pages (status.go); nil: none
Vault *vault.Service
VaultConfigured bool // configured but temporarily unavailable when Vault is nil
Monitors *monitors.Service // the monitors (monitors.go); nil: none
Channels *channels.Service // the notification channels (channels.go); nil: none
Console Console // the console's signed-in sessions (session.go); nil: bearer credentials only
// contains filtered or unexported fields
}
Server serves the API. SSH is nil when the SSH channel is off; SSHAddress is where agents reach it. Config is the configuration Beacon holds (its routes are in config.go); nil leaves them out.
func (*Server) ConsoleSessionOf ¶
ConsoleSessionOf returns the console session the request carries, if it is a person's that has not ended and whose account may still act.
type Session ¶
type Session struct {
ID string
Session store.ConsoleSession
Principal store.Principal
Account store.Account
}
Session is a person signed in to the console: the session's id (from the cookie), the session, and who it resolves to now.
type SignInPolicy ¶
type SignInPolicy struct {
RequireTwoFactor bool
SessionLifetime time.Duration
RecentSignIn time.Duration
Issuer string // how authenticator apps name this Beacon
}
SignInPolicy is what signing in needs from the settings.
func PolicyOf ¶
func PolicyOf(c *config.Config) SignInPolicy
PolicyOf reads the sign-in policy from the settings.
type SignedIn ¶
type SignedIn struct {
Status string `json:"status"`
User bridge.UserView `json:"user"`
Expires time.Time `json:"expires"`
CSRF string `json:"csrf"`
Next string `json:"next,omitempty"`
RecoveryCodes []string `json:"recovery_codes,omitempty"`
Session bridge.SignInSessionView `json:"session"`
}
SignedIn is the answer to a finished sign-in: who, until when, the CSRF secret for changes, where to go next, and the recovery codes when two-factor sign-in was set up on the way.
type Step ¶
type Step struct {
Status string `json:"status"`
Challenge string `json:"challenge,omitempty"`
Expires time.Time `json:"expires,omitzero"`
Secret string `json:"secret,omitempty"`
URI string `json:"otpauth_uri,omitempty"`
RecoveryCodes []string `json:"recovery_codes,omitempty"`
Account store.Account `json:"-"`
Method string `json:"-"`
Kind string `json:"-"`
}
Step is where a sign-in stands after a step: signed in (the account, how), or waiting for its second step with the challenge to answer and, when setting up, the new authenticator's secret and otpauth URI. RecoveryCodes are shown once, after setting up.
type TwoFactorStatus ¶
type TwoFactorStatus struct {
Enabled bool `json:"enabled"`
Since time.Time `json:"since,omitzero"`
RecoveryCodesLeft int `json:"recovery_codes_left"`
Required bool `json:"required"` // the owner requires it of everyone
}
TwoFactorStatus is a person's two-factor sign-in as they see it.