Documentation
¶
Overview ¶
Package apiknowledge is a vendor's knowledge for an API catalog (#2880): the archive of pages and automations the catalog's vendor publishes at a URL, read under limits, checked, compared with what is serving, and applied or held for an administrator. It is pulled only while a trusted connection references the catalog, and seen only by a caller whose persona reaches one.
Index ¶
- Constants
- Variables
- func DisplayURL(raw string) string
- func MaskedURL(raw string) string
- func Scrub(text, raw string) string
- func SourceHash(source string) string
- func Summary(r Report) string
- func TooLarge(maxBytes int64) error
- func ValidName(s string) bool
- func ValidSlug(s string) bool
- func ValidateURL(raw string) error
- type Archive
- type Automation
- type Backend
- type Change
- type Checker
- type Fetched
- type Fetcher
- type Finding
- type Gate
- type Item
- type ItemChange
- type Libraries
- type Limits
- type LoaderScript
- type Loop
- type Notice
- type Outcome
- type Owners
- type Page
- type RefusedError
- type Report
- type Result
- type Service
- func (s *Service) Accept(ctx context.Context, catalog, by string) (Result, error)
- func (s *Service) Check(ctx context.Context, catalog, trigger, author string) (Result, error)
- func (s *Service) Discard(ctx context.Context, catalog, by string) error
- func (s *Service) Revoked(ctx context.Context) error
- type Settings
- type Source
- type Store
- func (s *Store) AddChange(ctx context.Context, c Change, pending *Archive, etag string) (string, error)
- func (s *Store) Automation(ctx context.Context, catalog, name string, version int) (*VendorAutomation, error)
- func (s *Store) CurrentAutomations(ctx context.Context, catalogs []string) ([]VendorAutomation, error)
- func (s *Store) Decide(ctx context.Context, id, status, by string) (bool, error)
- func (s *Store) Delete(ctx context.Context, catalog string) (bool, error)
- func (s *Store) Due(ctx context.Context, now time.Time) ([]string, error)
- func (s *Store) Get(ctx context.Context, catalog string) (*Source, error)
- func (s *Store) History(ctx context.Context, catalog string, limit int) ([]Change, error)
- func (s *Store) Items(ctx context.Context, catalog string) ([]Item, error)
- func (s *Store) LatestVersion(ctx context.Context, catalog, name string) (int, error)
- func (s *Store) LoaderOwners(ctx context.Context, catalog string) (Owners, error)
- func (s *Store) Loaders(ctx context.Context, catalog, name string) (map[int]int, error)
- func (s *Store) Pending(ctx context.Context, catalog string) (*Change, error)
- func (s *Store) Put(ctx context.Context, catalog string, set Settings, by string) error
- func (s *Store) PutAutomation(ctx context.Context, a VendorAutomation) error
- func (s *Store) Record(ctx context.Context, catalog string, o Outcome) error
- func (s *Store) ReplaceItems(ctx context.Context, catalog string, items []Item) error
- func (s *Store) RetireAutomations(ctx context.Context, catalog string, kept []string) error
- func (s *Store) Sources(ctx context.Context) ([]Source, error)
- func (s *Store) VersionHash(ctx context.Context, catalog, name string, version int) (string, error)
- type VendorAutomation
Constants ¶
const ( DefaultMaxArchiveBytes = 20 << 20 DefaultMaxPages = 200 DefaultMaxPageBytes = 256 << 10 DefaultMaxAutomations = 100 DefaultMaxAutomationBytes = 256 << 10 )
The defaults, and the configuration keys a refusal names.
const ( KindPage = "page" KindAutomation = "automation" )
Kinds of item an archive holds.
const ( TriggerSchedule = "schedule" TriggerManual = "manual" )
Triggers a check records.
const ( ChangeApplied = "applied" ChangePending = "pending" ChangeDiscarded = "discarded" )
Change statuses.
const ( Added = "added" Changed = "changed" Removed = "removed" )
Item changes a report names.
const ( ResultUnchanged = "unchanged" ResultApplied = "applied" ResultHeld = "held" ResultFailed = "failed" ResultUntrusted = "untrusted" )
Results a check records.
const ( StatusServing = "serving" StatusHeld = "held" StatusSkipped = "skipped" )
Item statuses.
const ( KindTemplate = "template" KindLibrary = "library" )
Automation kinds.
const FormatVersion = 1
FormatVersion is the archive format this platform reads.
const LockKey int64 = 4713210406
LockKey is the advisory lock a pass holds, so one replica checks at a time.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is a catalog with no knowledge URL, or a template or change that does not exist.
var ErrUntrusted = errors.New("no connection of this catalog is trusted, so nothing is imported")
ErrUntrusted refuses an Accept while no connection of the catalog is trusted.
Functions ¶
func DisplayURL ¶
DisplayURL is the URL without its query, user and fragment: what an error says the archive was fetched from.
func MaskedURL ¶
MaskedURL is the URL an administrator is shown: its query's names, each value masked. A name with no value may be the key itself (?<token>), so it is masked whole.
func Scrub ¶
Scrub removes the knowledge URL's query, and each value in it as written and escaped, from text, leaving the URL as DisplayURL writes it. Every sentence a check stores or shows passes through it.
func SourceHash ¶
SourceHash is the hash an automation's version is compared by.
func TooLarge ¶
TooLarge is the refusal of an archive whose download passed max bytes, naming the key that sets it.
func ValidateURL ¶
ValidateURL refuses a knowledge URL that is not an absolute http(s) address with a host. Whether http and a private host are allowed is the fetch's guard to decide, as it decides for a spec URL.
Types ¶
type Archive ¶
type Archive struct {
Vendor string `json:"vendor"`
Entry string `json:"entry"`
Pages []Page `json:"pages"`
Automations []Automation `json:"automations"`
Recordings map[string][]byte `json:"recordings,omitempty"`
}
Archive is a vendor's archive as read: its manifest, its pages and its automations, in name order, and the recorded runs its automations' tests replay, by run id.
type Automation ¶
type Automation struct {
Name string `json:"name"`
Source string `json:"source"`
Description string `json:"description,omitempty"`
Version int `json:"version,omitempty"`
Params []script.Param `json:"params,omitempty"`
}
Automation is one script: automations/<name>.star, exactly the source a person would save, with what the manifest says of it. Whether it is a template or a library is read from the source; a library names its version, which never changes once published.
type Backend ¶
type Backend interface {
Get(ctx context.Context, catalog string) (*Source, error)
Sources(ctx context.Context) ([]Source, error)
Due(ctx context.Context, now time.Time) ([]string, error)
Record(ctx context.Context, catalog string, o Outcome) error
ReplaceItems(ctx context.Context, catalog string, items []Item) error
AddChange(ctx context.Context, c Change, pending *Archive, etag string) (string, error)
Pending(ctx context.Context, catalog string) (*Change, error)
Decide(ctx context.Context, id, status, by string) (bool, error)
CurrentAutomations(ctx context.Context, catalogs []string) ([]VendorAutomation, error)
Automation(ctx context.Context, catalog, name string, version int) (*VendorAutomation, error)
PutAutomation(ctx context.Context, a VendorAutomation) error
RetireAutomations(ctx context.Context, catalog string, kept []string) error
VersionHash(ctx context.Context, catalog, name string, version int) (string, error)
LatestVersion(ctx context.Context, catalog, name string) (int, error)
LoaderOwners(ctx context.Context, catalog string) (Owners, error)
}
Backend is the knowledge tables a service reads and writes (*Store).
type Change ¶
type Change struct {
ID string `json:"id"`
Catalog string `json:"catalog"`
Status string `json:"status"`
Trigger string `json:"trigger"`
Report Report `json:"report"`
CreatedBy string `json:"created_by"`
CreatedAt time.Time `json:"created_at"`
DecidedBy string `json:"decided_by,omitempty"`
DecidedAt *time.Time `json:"decided_at,omitempty"`
// Held and HeldETag are a pending update's archive and the ETag it was
// fetched with, read by Pending alone and never served.
Held *Archive `json:"-"`
HeldETag string `json:"-"`
}
Change is one update of a catalog's archive and what became of it.
type Checker ¶
type Checker struct {
// Registered lists every tool the deployment registers.
Registered func() []string
// Secrets finds credentials, the deployment's stored values included.
Secrets secretscan.Detector
}
Checker is what every page and automation passes before it applies: invisible, bidirectional and control characters removed, no tool the deployment does not register, no credential shape and none of the deployment's own secret values.
type Fetcher ¶
Fetcher reads the archive at url, sending etag as If-None-Match. Its error may hold the URL; the service never stores or shows it unscrubbed.
type Finding ¶
type Finding struct {
Kind string `json:"kind"`
Name string `json:"name"`
Reason string `json:"reason"`
}
Finding is one item held for an administrator, and why: the field and the kind of what was found, never the value.
type Gate ¶
type Gate func(ctx context.Context, catalog string, a Automation, carried map[string][]byte, libs scriptlib.Source) string
Gate is why the platform's save gate refuses one vendor automation, "" when it saves. carried is the archive's recorded runs, and libs reads the archive's libraries before the stored ones.
type Item ¶
type Item struct {
Kind string `json:"kind"`
Name string `json:"name"`
Status string `json:"status"`
Reason string `json:"reason,omitempty"`
}
Item is what the last import made of one page or automation.
type ItemChange ¶
type ItemChange struct {
Name string `json:"name"`
// Kind is an automation's: template or library. Empty for a page.
Kind string `json:"kind,omitempty"`
Change string `json:"change"`
Version int `json:"version,omitempty"`
From int `json:"from,omitempty"`
Before string `json:"before,omitempty"`
After string `json:"after,omitempty"`
}
ItemChange is one page or automation an update adds, changes or removes. Before and After are a page's bodies, for the diff a held update shows.
type Libraries ¶
Libraries is where every run, draft, test and save reads a vendor library (lib:<catalog>/<name>@<version>) from. A version once published is read whatever the catalog's trust: a script keeps running the version it names. Only a source starting to load one needs a trusted connection (Admit).
type Limits ¶
type Limits struct {
MaxArchiveBytes int64 `yaml:"max_archive_bytes"`
MaxPages int `yaml:"max_pages"`
MaxPageBytes int64 `yaml:"max_page_bytes"`
MaxAutomations int `yaml:"max_automations"`
MaxAutomationBytes int64 `yaml:"max_automation_bytes"`
}
Limits bound one archive. A non-positive field takes its default. They are the apigateway.catalog_fetch.knowledge configuration section, so the key a refusal names is the key an operator raises.
func (Limits) Normalized ¶
Normalized returns the limits with every non-positive field at its default.
type LoaderScript ¶
LoaderScript is an automation that loads one of a catalog's libraries.
type Loop ¶
Loop checks every catalog whose scheduled knowledge check has come.
func (*Loop) RunOnce ¶
RunOnce checks the due catalogs under the advisory lock, returning bgloop.ErrSkipped when another replica holds it. A catalog whose check fails is stamped with the failure and checked again an interval on.
type Notice ¶
type Notice struct {
Key string
Catalog string
Title string
Body string
// Owners is set when the catalog stopped being trusted: the automations
// loading its libraries, by owner, each owner to be told.
Owners Owners
}
Notice is what a check tells the deployment's channels.
type Outcome ¶
type Outcome struct {
Result string
Error string
ETag *string
Vendor string
Entry string
Applied bool
}
Outcome is what one check records on the source.
type Owners ¶
type Owners map[string][]LoaderScript
Owners is the automations loading the catalog's libraries, by owner.
type Page ¶
type Page struct {
Slug string `json:"slug"`
Title string `json:"title"`
Summary string `json:"summary"`
Tags []string `json:"tags"`
Body string `json:"body"`
}
Page is one knowledge page: pages/<slug>.md, front matter then markdown.
type RefusedError ¶
type RefusedError struct{ Reason string }
RefusedError is an archive refused whole: nothing from it applies. Its text is the reason an administrator reads on the catalog page.
func (*RefusedError) Error ¶
func (e *RefusedError) Error() string
type Report ¶
type Report struct {
Pages []ItemChange `json:"pages"`
Automations []ItemChange `json:"automations"`
Held []Finding `json:"held"`
Skipped []Finding `json:"skipped"`
First bool `json:"first,omitempty"`
}
Report is what an update changes: each page and automation added, changed or removed, and the items held or skipped.
type Result ¶
type Result struct {
Result string `json:"result"`
Error string `json:"error,omitempty"`
Report *Report `json:"report,omitempty"`
}
Result is what one check did.
type Service ¶
type Service struct {
Store Backend
Pages knowledgepage.VendorReconciler
Checker Checker
Gate Gate
// Trusted is each catalog's trusted connections as this replica serves
// them now.
Trusted func(ctx context.Context) map[string][]string
Fetch Fetcher
Limits Limits
Notify func(ctx context.Context, n Notice)
}
Service keeps catalogs' vendor knowledge current.
func (*Service) Check ¶
Check fetches the catalog's archive and applies it, holds it, or records why it could not.
func (*Service) Revoked ¶
Revoked records, and tells administrators and the owners of automations loading its libraries, every catalog whose knowledge was imported while a connection of it was trusted and has none trusted now. The loop runs it every minute, so a trust taken away is told within one, not at the next scheduled check; serving stops at once on its own, since every read asks the live connections.
type Settings ¶
Settings is what an administrator sets on a catalog's knowledge. A nil URL keeps the one stored: the URL is written, never read back.
type Source ¶
type Source struct {
Catalog string `json:"catalog"`
URL string `json:"-"`
Hold bool `json:"hold"`
Interval int `json:"refresh_interval_seconds"`
ETag string `json:"-"`
Vendor string `json:"vendor"`
Entry string `json:"entry"`
LastCheckedAt *time.Time `json:"last_checked_at,omitempty"`
LastResult string `json:"last_result"`
LastError string `json:"last_error,omitempty"`
LastAppliedAt *time.Time `json:"last_applied_at,omitempty"`
UpdatedBy string `json:"updated_by"`
UpdatedAt time.Time `json:"updated_at"`
}
Source is where a catalog's archive is and how its last check went. URL is the knowledge URL as written; nothing outside the admin catalog routes reads it.
type Store ¶
Store is the knowledge tables of migration 000278.
func (*Store) AddChange ¶
func (s *Store) AddChange(ctx context.Context, c Change, pending *Archive, etag string) (string, error)
AddChange records an update. A pending one carries the checked archive and supersedes the catalog's earlier pending update.
func (*Store) Automation ¶
func (s *Store) Automation(ctx context.Context, catalog, name string, version int) (*VendorAutomation, error)
Automation is one version of a vendor automation; version 0 is the current one.
func (*Store) CurrentAutomations ¶
func (s *Store) CurrentAutomations(ctx context.Context, catalogs []string) ([]VendorAutomation, error)
CurrentAutomations is the current version of each automation of the catalogs, by catalog then name.
func (*Store) Decide ¶
Decide marks the pending update applied or discarded by an administrator, and reports whether one was pending.
func (*Store) Delete ¶
Delete removes the catalog's knowledge URL with its items, held change and current automations; the versions a script loads are kept.
func (*Store) LatestVersion ¶
LatestVersion is the highest version of the automation, 0 for none.
func (*Store) LoaderOwners ¶
LoaderOwners is every automation whose current source loads one of the catalog's libraries, by owner.
func (*Store) Loaders ¶
Loaders counts the scripts whose current source loads each version of the library, by version.
func (*Store) Pending ¶
Pending is the catalog's update waiting for an administrator, with the archive it holds, or ErrNotFound.
func (*Store) Put ¶
Put saves the catalog's knowledge settings. A changed URL forgets the stored ETag and makes the catalog due at once.
func (*Store) PutAutomation ¶
func (s *Store) PutAutomation(ctx context.Context, a VendorAutomation) error
PutAutomation makes a the catalog's current version of its name: the version it names when it is new, the current one untouched when its source is unchanged.
func (*Store) ReplaceItems ¶
ReplaceItems makes items the catalog's item list.
func (*Store) RetireAutomations ¶
RetireAutomations takes the current version away from each of the catalog's automations not in kept; every version stays loadable.
type VendorAutomation ¶
type VendorAutomation struct {
Catalog string `json:"catalog"`
Name string `json:"name"`
Version int `json:"version"`
Kind string `json:"kind"`
Description string `json:"description"`
Source string `json:"source,omitempty"`
Params []script.Param `json:"params"`
Recordings map[string][]byte `json:"-"`
Current bool `json:"current"`
CreatedAt time.Time `json:"created_at"`
}
VendorAutomation is one version of a vendor's template or library.