bootstrap

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

README

BOOTSTRAP SERVICE

New devices need to be configured properly and connected to the Magistrala. Bootstrap service is used in order to accomplish that. This service provides the following features:

  1. Creating new Magistrala Clients
  2. Providing basic configuration for the newly created Clients
  3. Enabling/disabling bootstrap enrollments

Pre-provisioning a new Client is as simple as sending Configuration data to the Bootstrap service. Once the Client is online, it sends a request for initial config to Bootstrap service. Bootstrap service provides an API for enabling and disabling bootstrap enrollments. Bootstrapping does not implicitly enable an enrollment; it has to be done manually.

In order to bootstrap successfully, the Client needs to send bootstrapping request to the specific URL, as well as a secret key. This key and URL are pre-provisioned during the manufacturing process. If the Client is provisioned on the Bootstrap service side, the corresponding configuration will be sent as a response. Otherwise, the Client will be saved so that it can be provisioned later.

Client Configuration Entity

Client Configuration consists of two logical parts: the custom configuration that can be interpreted by the Client itself and Magistrala-related configuration. Magistrala config contains:

  1. corresponding Magistrala Client ID
  2. corresponding Magistrala Client key
  3. list of the Magistrala channels the Client is connected to

Note: list of channels contains IDs of the Magistrala channels. These channels are pre-provisioned on the Magistrala side and, unlike corresponding Magistrala Client, Bootstrap service is not able to create Magistrala Channels.

Enabling and disabling a bootstrap enrollment is an enrollment toggle. Configuration keeps a status:

Status What it means
disabled Enrollment exists, but bootstrap is not allowed
enabled Enrollment can be used to fetch bootstrap configuration

Switching between statuses enabled and disabled enables and disables the enrollment, respectively.

Client configuration also contains the so-called external ID and external key. An external ID is a unique identifier of corresponding Client. For example, a device MAC address is a good choice for external ID. External key is a secret key that is used for authentication during the bootstrapping procedure.

Configuration

The service is configured using the environment variables presented in the following table. Note that any unset variables will be replaced with their default values.

Bootstrap uses two independent encryption layers:

  • MG_BOOTSTRAP_DB_ENCRYPTION_KEY is a service-owned 32-byte master key. HKDF-derived AES-256-GCM keys encrypt enrollment external keys and secret binding snapshots before PostgreSQL storage.
  • Every enrollment has a Bootstrap root key containing at least 10 characters. Its exact UTF-8 bytes are used as HKDF input. The service generates a strong random base64url string when external_key is omitted during enrollment creation. The device stores its Bootstrap URL, external ID, root key, and key version separately from the configuration it downloads.
  • The device never sends that root key. It requests a one-minute server challenge, returns an HMAC-SHA256 proof made with an HKDF-derived authentication key, and receives an AES-256-GCM encrypted configuration made with a different HKDF-derived response key. Each challenge is single-use, and the protocol does not require a device clock.

Replacing an enrollment's external key increments its key version and invalidates outstanding challenges. It does not change the database master key or any other device's Bootstrap key.

Device Bootstrap flow

  1. Request a challenge: POST /devices/bootstrap/challenges/{externalID}.
  2. Generate a random 32-byte device nonce.
  3. Encode the external key as UTF-8 and use those exact bytes as the root key. Derive a 32-byte authentication key with HKDF-SHA256 using the external ID as salt and magistrala-bootstrap-auth-v1 as info.
  4. HMAC the newline-separated values v1, external ID, challenge ID, server nonce, device nonce, and decimal key version.
  5. Send the unpadded-base64url device nonce and proof to POST /devices/bootstrap/configurations/{externalID}.
  6. Derive the response key with the same root key and salt but magistrala-bootstrap-response-v1 as info, then AES-GCM decrypt the returned JSON envelope as described in the OpenAPI document.
  7. Read content_type from the decrypted response and parse the content string accordingly (application/json, application/yaml, application/toml, or text/plain).

This application-layer encryption protects device authentication and Bootstrap content even when a constrained deployment uses plain HTTP. Plain HTTP still reveals network metadata and permits traffic blocking, replay attempts, and denial of service, so HTTPS should be used whenever the device supports it.

Variable Description Default
MG_BOOTSTRAP_LOG_LEVEL Log level for Bootstrap (debug, info, warn, error) info
MG_BOOTSTRAP_DB_HOST Database host address localhost
MG_BOOTSTRAP_DB_PORT Database host port 5432
MG_BOOTSTRAP_DB_USER Database user magistrala
MG_BOOTSTRAP_DB_PASS Database password magistrala
MG_BOOTSTRAP_DB_NAME Name of the database used by the service bootstrap
MG_BOOTSTRAP_DB_SSL_MODE Database connection SSL mode (disable, require, verify-ca, verify-full) disable
MG_BOOTSTRAP_DB_SSL_CERT Path to the PEM encoded certificate file ""
MG_BOOTSTRAP_DB_SSL_KEY Path to the PEM encoded key file ""
MG_BOOTSTRAP_DB_SSL_ROOT_CERT Path to the PEM encoded root certificate file ""
MG_BOOTSTRAP_DB_ENCRYPTION_KEY 32-byte master key used only to encrypt Bootstrap secrets stored in PostgreSQL 12345678910111213141516171819202
MG_BOOTSTRAP_DB_ENCRYPTION_KEY_ID Identifier written into database encryption envelopes primary
MG_BOOTSTRAP_HTTP_HOST Bootstrap service HTTP host ""
MG_BOOTSTRAP_HTTP_PORT Bootstrap service HTTP port 9013
MG_BOOTSTRAP_HTTP_SERVER_CERT Path to server certificate in pem format ""
MG_BOOTSTRAP_HTTP_SERVER_KEY Path to server key in pem format ""
MG_ES_URL Event store URL amqp://guest:guest@localhost:5682/
ATOM_URL ATOM HTTP endpoint used for identity, authorization and resource lookup required
ATOM_SERVICE_TOKEN Service token used for ATOM resource lookup and projection required
ATOM_JWKS_URL ATOM JWKS endpoint used to verify bearer tokens ${ATOM_URL}/.well-known/jwks.json
ATOM_JWT_ISSUER Expected ATOM access-token issuer ATOM_URL
ATOM_JWT_AUDIENCE Expected ATOM access-token audience magistrala
ATOM_TIMEOUT Timeout for ATOM requests 5s
MG_JAEGER_URL Jaeger server URL http://localhost:4318/v1/traces
MG_JAEGER_TRACE_RATIO Jaeger sampling ratio 1.0
MG_SEND_TELEMETRY Send telemetry to magistrala call home server true
MG_BOOTSTRAP_INSTANCE_ID Bootstrap service instance ID ""

Deployment

The service itself is distributed as Docker container. Check the bootstrap service section in docker-compose file to see how service is deployed.

To start the service outside of the container, execute the following shell script:

# download the latest version of the service
git clone https://github.com/absmach/magistrala

cd magistrala

# compile the servic e
make bootstrap

# copy binary to bin
make install

# set the environment variables and run the service
MG_BOOTSTRAP_LOG_LEVEL=info \
MG_BOOTSTRAP_DB_HOST=localhost \
MG_BOOTSTRAP_DB_PORT=5432 \
MG_BOOTSTRAP_DB_USER=magistrala \
MG_BOOTSTRAP_DB_PASS=magistrala \
MG_BOOTSTRAP_DB_NAME=bootstrap \
MG_BOOTSTRAP_DB_SSL_MODE=disable \
MG_BOOTSTRAP_DB_SSL_CERT="" \
MG_BOOTSTRAP_DB_SSL_KEY="" \
MG_BOOTSTRAP_DB_SSL_ROOT_CERT="" \
MG_BOOTSTRAP_HTTP_HOST=localhost \
MG_BOOTSTRAP_HTTP_PORT=9010 \
MG_BOOTSTRAP_HTTP_SERVER_CERT="" \
MG_BOOTSTRAP_HTTP_SERVER_KEY="" \
MG_ES_URL=amqp://guest:guest@localhost:5682/ \
ATOM_URL=http://localhost:8080 \
ATOM_SERVICE_TOKEN=<bootstrap-service-token> \
ATOM_JWKS_URL=http://localhost:8080/.well-known/jwks.json \
ATOM_JWT_ISSUER=http://localhost:8080 \
ATOM_JWT_AUDIENCE=magistrala \
ATOM_TIMEOUT=5s \
MG_JAEGER_URL=http://localhost:14268/api/traces \
MG_JAEGER_TRACE_RATIO=1.0 \
MG_SEND_TELEMETRY=true \
MG_BOOTSTRAP_INSTANCE_ID="" \
$GOBIN/magistrala-bootstrap

Setting MG_BOOTSTRAP_HTTP_SERVER_CERT and MG_BOOTSTRAP_HTTP_SERVER_KEY will enable TLS against the service. The service expects a file in PEM format for both the certificate and the key.

Bootstrap PostgreSQL is authoritative for enrollments, profiles, templates, certificates, encrypted external keys and encrypted binding snapshots. ATOM is authoritative for identity and authorization and receives only non-secret bootstrap resource projections.

Legacy bcrypt enrollment migration

This release does not retain the pre-HKDF Bootstrap request. A bcrypt hash cannot provide the recoverable root key needed for the challenge/proof protocol, so every bcrypt-backed enrollment must be re-enrolled before the release.

Re-enroll each legacy device as follows:

  1. Inventory bcrypt-backed Bootstrap rows and identify their device owners.
  2. Generate and deliver a new Bootstrap root key through the device's approved out-of-band provisioning channel; never recover or log the old key.
  3. Create a replacement enrollment with a unique replacement external ID and that new key; this stores an encrypted key envelope with bootstrap_key_version set to one.
  4. Update the device to use its replacement external ID with POST /devices/bootstrap/challenges/{externalID} followed by POST /devices/bootstrap/configurations/{externalID}, then verify one successful challenge/proof bootstrap.
  5. Disable and remove the bcrypt-backed enrollment only after the replacement has successfully bootstrapped. Keep a rollback record of the original enrollment metadata, never its key, until the migration window closes.

Usage

For more information about service capabilities and its usage, please check out the API documentation.

Documentation

Overview

Package bootstrap contains the domain concept definitions needed to support Magistrala bootstrap service functionality.

Index

Constants

View Source
const (
	DeviceBootstrapVersion = "v1"
	BootstrapKeySize       = 32
	BootstrapKeyMinLength  = 10
	BootstrapNonceSize     = 32
	BootstrapProofSize     = 32
	DefaultChallengeTTL    = time.Minute
)
View Source
const (
	Disabled = "disabled"
	Enabled  = "enabled"
	All      = "all"
	Unknown  = "unknown"
)

String representation of bootstrap status values.

Variables

View Source
var (
	// ErrDeviceBootstrapAuth hides which part of device authentication failed.
	ErrDeviceBootstrapAuth = errors.NewAuthZError("failed to authenticate bootstrap device")

	// 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")

	// ErrBootstrapKey indicates an invalid per-device Bootstrap key.
	ErrBootstrapKey = errors.NewRequestError("invalid bootstrap key")

	// ErrSecretEncryption indicates that encrypted secret storage is unavailable.
	ErrSecretEncryption = errors.NewServiceError("secret encryption is not configured")
)
View Source
var ErrRenderFailed = errors.New("failed to render profile template")

ErrRenderFailed is returned when template execution or output validation fails.

Functions

func ReconcileAtom added in v1.0.0

func ReconcileAtom(ctx context.Context, svc Service, projector atom.Projector) error

ReconcileAtom backfills Bootstrap's non-secret Atom projections and removes projections whose PostgreSQL records no longer exist. It must run before the authorization decorator so its empty session is never externally reachable.

Types

type BindingContext added in v0.30.0

type BindingContext struct {
	Type     string
	ID       string
	Snapshot map[string]any
	Secret   map[string]any
}

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"`        // "device" | "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 NewAtomResolver added in v1.0.0

func NewAtomResolver(client atomResourceReader) BindingResolver

NewAtomResolver resolves device and channel bindings from Atom. It copies only explicitly allow-listed, non-secret fields into bootstrap snapshots.

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 BootstrapChallenge added in v1.0.0

type BootstrapChallenge struct {
	ID          string
	ConfigID    string
	KeyVersion  uint64
	ServerNonce []byte
	CreatedAt   time.Time
	ExpiresAt   time.Time
	ConsumedAt  *time.Time
}

BootstrapChallenge is a short-lived server challenge bound to one enrollment and one version of its device Bootstrap key.

type BootstrapChallengeRepository added in v1.0.0

type BootstrapChallengeRepository interface {
	Create(ctx context.Context, challenge BootstrapChallenge) error
	Retrieve(ctx context.Context, challengeID, configID string) (BootstrapChallenge, error)
	Consume(ctx context.Context, challengeID, configID string, now time.Time) error
}

BootstrapChallengeRepository persists short-lived device challenges.

type BootstrapChallengeResponse added in v1.0.0

type BootstrapChallengeResponse struct {
	ChallengeID string    `json:"challenge_id"`
	ServerNonce string    `json:"server_nonce"`
	ExpiresAt   time.Time `json:"expires_at"`
	KeyVersion  uint64    `json:"key_version"`
}

BootstrapChallengeResponse is returned to a device before it proves possession of its enrollment-specific Bootstrap key.

type Config

type Config struct {
	ID                  string         `json:"id"`
	WorkspaceID         string         `json:"workspace_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"`
	BootstrapKeyVersion uint64         `json:"bootstrap_key_version"`
	Content             string         `json:"content,omitempty"`
	ContentType         ContentType    `json:"content_type,omitempty"`
	Status              Status         `json:"status"`
	ProfileID           string         `json:"profile_id,omitempty"`
	RenderContext       map[string]any `json:"render_context,omitempty"`

	BootstrapRootKey     []byte `json:"-"`
	BootstrapChallengeID string `json:"-"`
	BootstrapServerNonce string `json:"-"`
	BootstrapDeviceNonce string `json:"-"`
}

Config represents a bootstrap enrollment.

type ConfigReader

type ConfigReader interface {
	ReadConfig(Config) (any, error)
}

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() 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, workspaceID, id string) (Config, error)

	// RetrieveAll retrieves a subset of Configs that belong to the given
	// workspace, with given filter parameters. A non-nil error is returned
	// to indicate operation failure, so that a lookup failure is never
	// mistaken for an empty workspace.
	RetrieveAll(ctx context.Context, workspaceID string, filter Filter, offset, limit uint64) (ConfigsPage, error)

	// 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, workspaceID, id, profileID string) error

	// UpdateCerts updates and returns an existing Config certificate and workspaceID.
	// A non-nil error is returned to indicate operation failure.
	UpdateCert(ctx context.Context, workspaceID, 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, workspaceID, id string) error

	// ChangeStatus changes the Status of the Config owned by the specific user.
	ChangeStatus(ctx context.Context, workspaceID, 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 ContentType added in v1.0.0

type ContentType string

ContentType identifies how a device should parse rendered profile content.

const (
	ContentTypeJSON      ContentType = "application/json"
	ContentTypeYAML      ContentType = "application/yaml"
	ContentTypeTOML      ContentType = "application/toml"
	ContentTypeTextPlain ContentType = "text/plain"
)

type DeviceBootstrapProof added in v1.0.0

type DeviceBootstrapProof struct {
	ChallengeID string `json:"challenge_id"`
	DeviceNonce string `json:"device_nonce"`
	Proof       string `json:"proof"`
}

DeviceBootstrapProof authenticates a device response to a server challenge.

type DeviceContext added in v0.30.0

type DeviceContext struct {
	ID          string
	ExternalID  string
	WorkspaceID string
}

DeviceContext holds enrollment identity fields available inside templates.

type EncryptedBootstrapConfig added in v1.0.0

type EncryptedBootstrapConfig struct {
	Version     string `json:"version"`
	KeyVersion  uint64 `json:"key_version"`
	ChallengeID string `json:"challenge_id"`
	Nonce       string `json:"nonce"`
	Ciphertext  string `json:"ciphertext"`
}

EncryptedBootstrapConfig is the application-encrypted device configuration.

func (EncryptedBootstrapConfig) Code added in v1.0.0

func (res EncryptedBootstrapConfig) Code() int

func (EncryptedBootstrapConfig) Empty added in v1.0.0

func (res EncryptedBootstrapConfig) Empty() bool

func (EncryptedBootstrapConfig) Headers added in v1.0.0

func (res EncryptedBootstrapConfig) Headers() map[string]string

type Filter

type Filter struct {
	FullMatch    map[string]string
	PartialMatch map[string]string
}

Filter is used for the search filters.

type PreviousKey added in v1.0.0

type PreviousKey struct {
	ID  string
	Key []byte
}

PreviousKey is a retired master key, kept so that secrets sealed under it remain readable after the active key has been rotated.

type Profile added in v0.30.0

type Profile struct {
	ID              string         `json:"id"`
	WorkspaceID     string         `json:"workspace_id,omitempty"`
	Name            string         `json:"name"`
	Description     string         `json:"description,omitempty"`
	ContentFormat   ContentFormat  `json:"content_format"`
	ContentType     ContentType    `json:"content_type"`
	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 workspace.
	RetrieveByID(ctx context.Context, workspaceID, id string) (Profile, error)

	// RetrieveAll returns a page of Profiles belonging to the given workspace, optionally filtered by name.
	RetrieveAll(ctx context.Context, workspaceID 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 workspace.
	Delete(ctx context.Context, workspaceID, 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 SecretCipher added in v1.0.0

type SecretCipher struct {
	// contains filtered or unexported fields
}

SecretCipher encrypts Bootstrap secrets at rest. A purpose-specific AES key is derived from the database master key so config keys and binding snapshots do not share the same encryption key.

func NewSecretCipher added in v1.0.0

func NewSecretCipher(masterKey []byte, keyID string, previous ...PreviousKey) (*SecretCipher, error)

NewSecretCipher creates an at-rest secret cipher. Bootstrap deliberately requires a 256-bit master key.

Rotation is performed by promoting the new key to (masterKey, keyID) and passing the outgoing one as a PreviousKey: new writes use the active key while existing envelopes stay readable. Without that, every secret sealed under the old key ID would fail to open and the service would not start.

func (*SecretCipher) KeyID added in v1.0.0

func (sc *SecretCipher) KeyID() string

type Service

type Service interface {
	// Add adds new Device Config to the user identified by the provided token.
	Add(ctx context.Context, session smqauthn.Session, token string, cfg Config) (Config, error)

	// View returns Device 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

	// IssueBootstrapChallenge creates a short-lived challenge for an enrollment.
	IssueBootstrapChallenge(ctx context.Context, externalID string) (BootstrapChallengeResponse, error)

	// Bootstrap authenticates a device proof and returns its rendered Config.
	Bootstrap(ctx context.Context, externalID string, proof DeviceBootstrapProof) (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 workspace.
	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,
	challenges BootstrapChallengeRepository,
	resolver BindingResolver,
	renderer Renderer,
	encKey []byte,
	encKeyID string,
	idp magistrala.IDProvider,
	previousKeys ...PreviousKey,
) (Service, error)

New returns a new Bootstrap service.

An unusable encryption key is reported here rather than deferred: without a cipher the service constructs cleanly and then fails every enrollment write at runtime.

previousKeys carries retired encryption keys so that secrets sealed before a key rotation remain readable; see NewSecretCipher.

challenges may be nil for a deployment that does not serve device bootstrap; the challenge and bootstrap endpoints then fail closed.

func WithAtom added in v1.0.0

func WithAtom(svc Service, projector atom.Projector) Service

WithAtom projects non-secret bootstrap metadata into Atom. PostgreSQL remains authoritative for enrollments, templates, certificates, keys and snapshots.

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

func ToStatus(status string) (Status, error)

ToStatus converts a string or legacy numeric string value to Status.

func (Status) MarshalJSON added in v0.30.0

func (s Status) MarshalJSON() ([]byte, error)

MarshalJSON renders bootstrap status as a string literal.

func (Status) String added in v0.30.0

func (s Status) String() string

String returns string representation of Status.

func (*Status) UnmarshalJSON added in v0.30.0

func (s *Status) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts both string and legacy numeric bootstrap statuses.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL