Documentation
¶
Overview ¶
Package bootstrap contains the domain concept definitions needed to support Magistrala bootstrap service functionality.
Index ¶
- Constants
- Variables
- type BindingContext
- type BindingRequest
- type BindingResolver
- type BindingSlot
- type BindingSnapshot
- type BindingStore
- type Config
- type ConfigReader
- type ConfigRepository
- type ConfigsPage
- type ContentFormat
- type DeviceContext
- type Filter
- type Hasher
- type Profile
- type ProfileRepository
- type ProfilesPage
- type RenderContext
- type Renderer
- type ResolveRequest
- type Service
- type Status
Constants ¶
const ( Disabled = "disabled" Enabled = "enabled" All = "all" Unknown = "unknown" )
String representation of bootstrap status values.
const ( Inactive = DisabledStatus Active = EnabledStatus )
Backward-compatible aliases kept while callers move off the old names.
Variables ¶
var ( // ErrExternalKey indicates a non-existent bootstrap configuration for given external key. ErrExternalKey = errors.NewAuthZError("failed to get bootstrap configuration for given external key") // ErrExternalKeySecure indicates error in getting bootstrap configuration for given encrypted external key. ErrExternalKeySecure = errors.NewAuthZError("failed to get bootstrap configuration for given encrypted external key") // ErrBootstrap indicates error in getting bootstrap configuration. ErrBootstrap = errors.New("failed to read bootstrap configuration") // ErrAddBootstrap indicates error in adding bootstrap configuration. ErrAddBootstrap = errors.NewServiceError("failed to add bootstrap configuration") // ErrBootstrapStatus indicates an invalid bootstrap status. ErrBootstrapStatus = errors.NewRequestError("invalid bootstrap status") )
var ErrRenderFailed = errors.New("failed to render profile template")
ErrRenderFailed is returned when template execution or output validation fails.
Functions ¶
This section is empty.
Types ¶
type BindingContext ¶ added in v0.30.0
BindingContext holds the resolved resource data available inside templates for a specific slot.
type BindingRequest ¶ added in v0.30.0
type BindingRequest struct {
Slot string `json:"slot"`
Type string `json:"type"` // "client" | "channel" | "cert"
ResourceID string `json:"resource_id"` // ID of the resource in its owning service
}
BindingRequest carries a user's intent to bind a named profile slot to a concrete resource.
type BindingResolver ¶ added in v0.30.0
type BindingResolver interface {
Resolve(ctx context.Context, req ResolveRequest) ([]BindingSnapshot, error)
}
BindingResolver validates that requested resources exist in their owning services, verifies type and slot compatibility, and returns snapshots ready for storage. It is called at binding time only; the render path must not call it.
func NewSDKResolver ¶ added in v0.30.0
func NewSDKResolver(sdk mgsdk.SDK) BindingResolver
NewSDKResolver returns a BindingResolver that validates resources against the Magistrala clients and channels services using the SDK. This resolver is called only at binding time; the render path must never call it.
type BindingSlot ¶ added in v0.30.0
type BindingSlot struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required"`
Fields []string `json:"fields,omitempty"`
}
BindingSlot declares a named resource placeholder that a profile template can use.
type BindingSnapshot ¶ added in v0.30.0
type BindingSnapshot struct {
ConfigID string `json:"config_id"`
Slot string `json:"slot"`
Type string `json:"type"`
ResourceID string `json:"resource_id"`
Snapshot map[string]any `json:"snapshot,omitempty"`
SecretSnapshot map[string]any `json:"secret_snapshot,omitempty"` // encrypted at rest
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
BindingSnapshot is a Bootstrap-owned point-in-time copy of the resource fields needed for template rendering. It is populated at binding time so that the render path never calls external services.
type BindingStore ¶ added in v0.30.0
type BindingStore interface {
// Save upserts all given snapshots for the config.
Save(ctx context.Context, configID string, bindings []BindingSnapshot) error
// Retrieve returns all snapshots for the given config.
Retrieve(ctx context.Context, configID string) ([]BindingSnapshot, error)
// Delete removes the snapshot for a specific slot of a config.
Delete(ctx context.Context, configID, slot string) error
}
BindingStore is the persistence interface for BindingSnapshots.
type Config ¶
type Config struct {
ID string `json:"id"`
DomainID string `json:"domain_id,omitempty"`
Name string `json:"name,omitempty"`
ClientCert string `json:"client_cert,omitempty"`
ClientKey string `json:"client_key,omitempty"`
CACert string `json:"ca_cert,omitempty"`
ExternalID string `json:"external_id"`
ExternalKey string `json:"external_key"`
Content string `json:"content,omitempty"`
Status Status `json:"status"`
ProfileID string `json:"profile_id,omitempty"`
RenderContext map[string]any `json:"render_context,omitempty"`
}
Config represents a bootstrap enrollment.
type ConfigReader ¶
ConfigReader is used to parse Config into format which will be encoded as a JSON and consumed from the client side. The purpose of this interface is to provide convenient way to generate custom configuration response based on the specific Config which will be consumed by the client.
func NewConfigReader ¶
func NewConfigReader(encKey []byte) ConfigReader
NewConfigReader return new reader which is used to generate response from the config.
type ConfigRepository ¶
type ConfigRepository interface {
// Save persists the Config. Successful operation is indicated by non-nil
// error response.
Save(ctx context.Context, cfg Config) (string, error)
// RetrieveByID retrieves the Config having the provided identifier, that is owned
// by the specified user.
RetrieveByID(ctx context.Context, domainID, id string) (Config, error)
// RetrieveAll retrieves a subset of Configs that belong to the given domain,
// with given filter parameters.
RetrieveAll(ctx context.Context, domainID string, filter Filter, offset, limit uint64) ConfigsPage
// RetrieveByExternalID returns Config for given external ID.
RetrieveByExternalID(ctx context.Context, externalID string) (Config, error)
// Update updates an existing Config. A non-nil error is returned
// to indicate operation failure.
Update(ctx context.Context, cfg Config) error
// AssignProfile sets the profile reference for the given Config.
AssignProfile(ctx context.Context, domainID, id, profileID string) error
// UpdateCerts updates and returns an existing Config certificate and domainID.
// A non-nil error is returned to indicate operation failure.
UpdateCert(ctx context.Context, domainID, id, clientCert, clientKey, caCert string) (Config, error)
// Remove removes the Config having the provided identifier, that is owned
// by the specified user.
Remove(ctx context.Context, domainID, id string) error
// ChangeStatus changes the Status of the Config owned by the specific user.
ChangeStatus(ctx context.Context, domainID, id string, status Status) error
}
ConfigRepository specifies a Config persistence API.
type ConfigsPage ¶
type ConfigsPage struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
Configs []Config `json:"configs"`
}
ConfigsPage contains page related metadata as well as list of Configs that belong to this page.
type ContentFormat ¶ added in v0.30.0
type ContentFormat string
ContentFormat enumerates the supported output formats for rendered profile templates.
const ( ContentFormatGoTemplate ContentFormat = "go-template" ContentFormatRaw ContentFormat = "raw" ContentFormatJSON ContentFormat = "json" ContentFormatYAML ContentFormat = "yaml" ContentFormatTOML ContentFormat = "toml" )
type DeviceContext ¶ added in v0.30.0
DeviceContext holds enrollment identity fields available inside templates.
type Hasher ¶ added in v0.30.0
type Hasher interface {
// Hash generates the hashed string from plain-text.
Hash(string) (string, error)
// Compare compares plain-text version to the hashed one. An error should
// indicate failed comparison.
Compare(string, string) error
}
Hasher specifies an API for generating hashes of arbitrary textual content.
type Profile ¶ added in v0.30.0
type Profile struct {
ID string `json:"id"`
DomainID string `json:"domain_id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
ContentFormat ContentFormat `json:"content_format"`
ContentTemplate string `json:"content_template,omitempty"`
Defaults map[string]any `json:"defaults,omitempty"`
BindingSlots []BindingSlot `json:"binding_slots,omitempty"`
Version int `json:"version,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
Profile is a user-managed device configuration template.
type ProfileRepository ¶ added in v0.30.0
type ProfileRepository interface {
// Save persists a new Profile and returns it with server-assigned fields set.
Save(ctx context.Context, p Profile) (Profile, error)
// RetrieveByID returns the Profile with the given ID inside the given domain.
RetrieveByID(ctx context.Context, domainID, id string) (Profile, error)
// RetrieveAll returns a page of Profiles belonging to the given domain, optionally filtered by name.
RetrieveAll(ctx context.Context, domainID string, offset, limit uint64, name string) (ProfilesPage, error)
// Update updates editable fields of the given Profile and returns the updated Profile.
Update(ctx context.Context, p Profile) (Profile, error)
// Delete removes the Profile with the given ID from the given domain.
Delete(ctx context.Context, domainID, id string) error
}
ProfileRepository specifies the persistence API for Profiles.
type ProfilesPage ¶ added in v0.30.0
type ProfilesPage struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
Profiles []Profile `json:"profiles"`
}
ProfilesPage contains pagination metadata and a slice of Profiles.
type RenderContext ¶ added in v0.30.0
type RenderContext struct {
Device DeviceContext
Vars map[string]any
Bindings map[string]BindingContext
}
RenderContext is the typed value injected into Go templates during rendering.
type Renderer ¶ added in v0.30.0
type Renderer interface {
Render(profile Profile, enrollment Config, bindings []BindingSnapshot) ([]byte, error)
}
Renderer renders a Profile's content template into a concrete device configuration. All input data must already be stored in Bootstrap — no external service calls are allowed inside Render.
func NewRenderer ¶ added in v0.30.0
func NewRenderer() Renderer
NewRenderer returns the default Renderer implementation using Go text/template.
type ResolveRequest ¶ added in v0.30.0
type ResolveRequest struct {
Enrollment Config
Token string
Requested []BindingRequest
}
ResolveRequest carries everything the BindingResolver needs to snapshot a set of resource bindings.
type Service ¶
type Service interface {
// Add adds new Client Config to the user identified by the provided token.
Add(ctx context.Context, session smqauthn.Session, token string, cfg Config) (Config, error)
// View returns Client Config with given ID belonging to the user identified by the given token.
View(ctx context.Context, session smqauthn.Session, id string) (Config, error)
// Update updates editable fields of the provided Config.
Update(ctx context.Context, session smqauthn.Session, cfg Config) error
// UpdateCert updates an existing Config certificate and token.
// A non-nil error is returned to indicate operation failure.
UpdateCert(ctx context.Context, session smqauthn.Session, id, clientCert, clientKey, caCert string) (Config, error)
// List returns subset of Configs with given search params that belong to the
// user identified by the given token.
List(ctx context.Context, session smqauthn.Session, filter Filter, offset, limit uint64) (ConfigsPage, error)
// Remove removes Config with specified token that belongs to the user identified by the given token.
Remove(ctx context.Context, session smqauthn.Session, id string) error
// Bootstrap returns Config to the Client with provided external ID using external key.
Bootstrap(ctx context.Context, externalKey, externalID string, secure bool) (Config, error)
// EnableConfig enables the Config so its device can successfully bootstrap.
EnableConfig(ctx context.Context, session smqauthn.Session, id string) (Config, error)
// DisableConfig disables the Config, preventing its device from bootstrapping.
DisableConfig(ctx context.Context, session smqauthn.Session, id string) (Config, error)
// CreateProfile persists a new device Profile.
CreateProfile(ctx context.Context, session smqauthn.Session, p Profile) (Profile, error)
// ViewProfile returns the Profile with the given ID.
ViewProfile(ctx context.Context, session smqauthn.Session, profileID string) (Profile, error)
// UpdateProfile updates editable fields of the given Profile and returns the updated Profile.
UpdateProfile(ctx context.Context, session smqauthn.Session, p Profile) (Profile, error)
// ListProfiles returns a page of Profiles belonging to the domain.
ListProfiles(ctx context.Context, session smqauthn.Session, offset, limit uint64, name string) (ProfilesPage, error)
// DeleteProfile removes the Profile with the given ID.
DeleteProfile(ctx context.Context, session smqauthn.Session, profileID string) error
// AssignProfile sets the ProfileID on an existing enrollment (Config).
AssignProfile(ctx context.Context, session smqauthn.Session, configID, profileID string) error
// BindResources resolves the requested bindings through their owning services,
// stores snapshots, and marks the enrollment renderable when all required slots
// are satisfied.
BindResources(ctx context.Context, session smqauthn.Session, token, configID string, bindings []BindingRequest) error
// ListBindings returns all stored binding snapshots for an enrollment.
ListBindings(ctx context.Context, session smqauthn.Session, configID string) ([]BindingSnapshot, error)
// RefreshBindings re-resolves all existing bindings for an enrollment and
// updates the stored snapshots.
RefreshBindings(ctx context.Context, session smqauthn.Session, token, configID string) error
}
Service specifies an API that must be fulfilled by the domain service implementation, and all of its decorators (e.g. logging & metrics).
func New ¶
func New( configs ConfigRepository, profiles ProfileRepository, bindings BindingStore, resolver BindingResolver, renderer Renderer, sdk mgsdk.SDK, hasher Hasher, encKey []byte, idp magistrala.IDProvider, ) Service
New returns new Bootstrap service.
type Status ¶ added in v0.30.0
type Status uint8
Status represents bootstrap enrollment availability.
const ( EnabledStatus Status = iota DisabledStatus // AllStatus is used for querying purposes to list configs irrespective // of their status. It is never stored in the database. AllStatus )
Possible bootstrap enrollment statuses.
func ToStatus ¶ added in v0.30.0
ToStatus converts a string or legacy numeric string value to Status.
func (Status) MarshalJSON ¶ added in v0.30.0
MarshalJSON renders bootstrap status as a string literal.
func (*Status) UnmarshalJSON ¶ added in v0.30.0
UnmarshalJSON accepts both string and legacy numeric bootstrap statuses.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api contains implementation of bootstrap service HTTP API.
|
Package api contains implementation of bootstrap service HTTP API. |
|
Package events provides the domain concept definitions needed to support bootstrap events functionality.
|
Package events provides the domain concept definitions needed to support bootstrap events functionality. |
|
producer
Package producer contains the domain events needed to support event sourcing of Bootstrap service actions.
|
Package producer contains the domain events needed to support event sourcing of Bootstrap service actions. |
|
Package postgres contains repository implementations using PostgreSQL as the underlying database.
|
Package postgres contains repository implementations using PostgreSQL as the underlying database. |
|
Package tracing provides tracing instrumentation for Magistrala Users service.
|
Package tracing provides tracing instrumentation for Magistrala Users service. |