jarld obtains and renews a certificate for another host; the host asks
for it with one command, waits, downloads it when it is newer than its
copy and runs a hook with the paths in JARL_CERT_* variables.
- Certificates have an owner user; another user can't take the name.
An existing one is obtained again only when a name is added or
removed. A new certificates grant (names=) says which names a user
may have; it is never part of "*". Users may own
certificates.max_per_user certificates (10) or their own cert_limit.
- POST /certificates/{name}/request; owners may list, show, export and
delete theirs; keys only over HTTPS, the socket or loopback; every
download is recorded and shown as "Pulled by".
- CLI: jarl cert get (saved settings, all saved when no name), cert
saved, cert forget, cert schedule (daily systemd timer, Linux, root).
- Migration 14. Tests: server, auth, and e2e against Pebble.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
175 lines
6.8 KiB
Go
175 lines
6.8 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// Changeset statuses.
|
|
const (
|
|
ChangesetOpen = "open"
|
|
ChangesetApplied = "applied"
|
|
ChangesetDiscarded = "discarded"
|
|
)
|
|
|
|
// Op is one operation in a changeset, for example "record.create".
|
|
type Op struct {
|
|
Seq int `json:"seq"`
|
|
Kind string `json:"op"`
|
|
// ID is the object the operation acts on. For create operations it is
|
|
// allocated when the operation is added, so later operations in the
|
|
// same changeset can refer to the new object.
|
|
ID int64 `json:"id,omitempty"`
|
|
// BaseVersion is the version of the object when the operation was
|
|
// staged. Applying fails with a conflict if the object has changed
|
|
// since (0 skips the check).
|
|
BaseVersion int `json:"base_version,omitempty"`
|
|
Data json.RawMessage `json:"data,omitempty"`
|
|
Summary string `json:"summary,omitempty"`
|
|
AddedAt time.Time `json:"added_at"`
|
|
}
|
|
|
|
// Changeset is a group of operations applied together.
|
|
type Changeset struct {
|
|
ID int64 `json:"id"`
|
|
Description string `json:"description"`
|
|
Status string `json:"status"`
|
|
UserID int64 `json:"user_id"`
|
|
Username string `json:"username"`
|
|
Via string `json:"via"` // web, api, cli, import, system
|
|
Ops []Op `json:"ops"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
AppliedAt *time.Time `json:"applied_at,omitempty"`
|
|
AppliedBy string `json:"applied_by,omitempty"`
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
Diff string `json:"diff,omitempty"`
|
|
}
|
|
|
|
// Grant gives access to a kind of resource. Filters narrow it: empty
|
|
// filters match everything.
|
|
type Grant struct {
|
|
Resource string `json:"resource"` // zones, records, views, acls, tsig, options, audit, *
|
|
Access string `json:"access"` // read or write (write implies read)
|
|
Zones []string `json:"zones,omitempty"`
|
|
Views []string `json:"views,omitempty"`
|
|
Names []string `json:"names,omitempty"`
|
|
Types []string `json:"types,omitempty"`
|
|
}
|
|
|
|
// User is a person or service account.
|
|
type User struct {
|
|
ID int64 `json:"id"`
|
|
Username string `json:"username"`
|
|
PasswordHash string `json:"-"`
|
|
HasPassword bool `json:"has_password"`
|
|
Admin bool `json:"admin"`
|
|
Grants []Grant `json:"grants"`
|
|
Disabled bool `json:"disabled"`
|
|
Comment string `json:"comment"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
LastLoginAt *time.Time `json:"last_login_at,omitempty"`
|
|
// TOTPEnabledAt is when the user turned on two-factor login (nil:
|
|
// off). The secrets never leave the server.
|
|
TOTPEnabledAt *time.Time `json:"totp_enabled_at,omitempty"`
|
|
TOTPSecret string `json:"-"`
|
|
TOTPPending string `json:"-"`
|
|
TOTPLastStep int64 `json:"-"`
|
|
// CertLimit is how many certificates the user may own (jarl cert get);
|
|
// nil: certificates.max_per_user in jarl.yaml.
|
|
CertLimit *int `json:"cert_limit,omitempty"`
|
|
}
|
|
|
|
// HasTOTP reports whether the user logs in with a TOTP code.
|
|
func (u *User) HasTOTP() bool { return u.TOTPSecret != "" }
|
|
|
|
// Token is an API key. The secret is shown once at creation and only its
|
|
// hash is stored.
|
|
type Token struct {
|
|
ID int64 `json:"id"`
|
|
UserID int64 `json:"user_id"`
|
|
Username string `json:"username,omitempty"`
|
|
Name string `json:"name"`
|
|
PublicID string `json:"public_id"`
|
|
SecretHash string `json:"-"`
|
|
// Grants restrict the token to a subset of its user's permissions;
|
|
// nil means the token has all of the user's permissions.
|
|
Grants []Grant `json:"grants"`
|
|
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
|
LastUsedIP string `json:"last_used_ip,omitempty"`
|
|
RevokedAt *time.Time `json:"revoked_at,omitempty"`
|
|
// RotatedAt is when the token last got a new secret (nil: never).
|
|
RotatedAt *time.Time `json:"rotated_at,omitempty"`
|
|
// After a rotation with a grace period, the previous secret keeps
|
|
// working until PrevExpiresAt.
|
|
PrevSecretHash string `json:"-"`
|
|
PrevExpiresAt *time.Time `json:"previous_secret_valid_until,omitempty"`
|
|
}
|
|
|
|
// TokenExpiryWarning is how long before a token expires Jarl starts to
|
|
// warn about it: on the dashboard, in jarld's log, and to the CLI using it.
|
|
const TokenExpiryWarning = 14 * 24 * time.Hour
|
|
|
|
// ExpiresWithin reports whether the token is still in use (not revoked)
|
|
// and expires within d from now. A token that has already expired counts.
|
|
func (t *Token) ExpiresWithin(now time.Time, d time.Duration) bool {
|
|
return t.RevokedAt == nil && t.ExpiresAt != nil && t.ExpiresAt.Sub(now) < d
|
|
}
|
|
|
|
// IssuedAt is when the token's current secret was made.
|
|
func (t *Token) IssuedAt() time.Time {
|
|
if t.RotatedAt != nil {
|
|
return *t.RotatedAt
|
|
}
|
|
return t.CreatedAt
|
|
}
|
|
|
|
// Session is a logged-in web session.
|
|
type Session struct {
|
|
IDHash string `json:"-"`
|
|
UserID int64 `json:"user_id"`
|
|
CSRFToken string `json:"-"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ExpiresAt time.Time `json:"expires_at"`
|
|
LastSeenAt time.Time `json:"last_seen_at"`
|
|
IP string `json:"ip"`
|
|
UserAgent string `json:"user_agent"`
|
|
// TOTPAt is when the session last proved a TOTP code (at login or
|
|
// since); nil for a session without two-factor login.
|
|
TOTPAt *time.Time `json:"totp_at,omitempty"`
|
|
}
|
|
|
|
// AuditEvent records an action.
|
|
type AuditEvent struct {
|
|
ID int64 `json:"id"`
|
|
Time time.Time `json:"time"`
|
|
UserID int64 `json:"user_id,omitempty"`
|
|
Username string `json:"username,omitempty"`
|
|
Auth string `json:"auth,omitempty"` // session, token, local
|
|
TokenID int64 `json:"token_id,omitempty"`
|
|
TokenName string `json:"token_name,omitempty"`
|
|
IP string `json:"ip,omitempty"`
|
|
UserAgent string `json:"user_agent,omitempty"`
|
|
Action string `json:"action"`
|
|
ObjectType string `json:"object_type,omitempty"`
|
|
ObjectID int64 `json:"object_id,omitempty"`
|
|
ObjectName string `json:"object_name,omitempty"`
|
|
ChangesetID int64 `json:"changeset_id,omitempty"`
|
|
Outcome string `json:"outcome"` // success, failure, denied
|
|
Message string `json:"message,omitempty"`
|
|
Details json.RawMessage `json:"details,omitempty"`
|
|
}
|
|
|
|
// DeployedFile records a file Jarl wrote, for drift detection.
|
|
type DeployedFile struct {
|
|
Path string `json:"path"`
|
|
SHA256 string `json:"sha256"`
|
|
Size int64 `json:"size"`
|
|
Kind string `json:"kind"`
|
|
WrittenAt time.Time `json:"written_at"`
|
|
ChangesetID int64 `json:"changeset_id"`
|
|
}
|