Documentation
¶
Overview ¶
Package config loads and resolves the cinc CLI credentials file: a TOML document holding a set of named connection profiles, in the same shape as Chef's `~/.chef/credentials` file.
Index ¶
- Variables
- func DefaultPath() (string, error)
- func EnvProfile() string
- func ExpandHome(path string) (string, error)
- func ServerURLProblem(raw string) error
- func TOMLProblem(err error) string
- func UpdateProfile(path, name string, mutate func(*Profile) error) error
- func ValidateSiteURL(raw string) error
- func WriteProfile(path, name string, p Profile) error
- func WriteProfileWithExtras(path, name string, p Profile, extra map[string]any) error
- type Config
- type Profile
- func (p Profile) ResolveTrustedCertsDir() (dir string, explicit bool, err error)
- func (p Profile) SupermarketIdentity() (name, keyPath string)
- func (p Profile) Validate() error
- func (p Profile) ValidateIdentity() error
- func (p Profile) ValidateSupermarketIdentity() error
- func (p Profile) WithName(name string) Profile
- type ValidationIssue
Constants ¶
This section is empty.
Variables ¶
var ErrMissingServerURL = errors.New("config: profile is missing cinc_server_url (or chef_server_url)")
ErrMissingServerURL is Validate's error for a profile that names no server, such as a Supermarket-only profile or the one first-run setup writes when every prompt is left at its default.
Functions ¶
func DefaultPath ¶
DefaultPath returns the default credentials file location, ~/.cinc/credentials.
func EnvProfile ¶ added in v0.26.0
func EnvProfile() string
EnvProfile returns the profile name selected by environment variables, preferring CINC_PROFILE over CHEF_PROFILE, or "" when neither is set.
func ExpandHome ¶ added in v0.24.0
ExpandHome expands a leading "~" or "~/" to the user's home directory and returns any other path unchanged. Every path the credentials file holds (client_key, supermarket_key, secret_file, trusted_certs_dir) goes through it when it is read, never when the file is written, so a rewritten profile keeps the portable ~ form rather than an absolute home path.
func ServerURLProblem ¶ added in v0.30.0
ServerURLProblem returns a one-sentence explanation of what is wrong with raw as a Cinc Server URL, or nil when it is fine. It suits a report line, like a `config validate` check, where a profile's full explanation would be too long.
func TOMLProblem ¶ added in v0.30.0
TOMLProblem describes a decode error from a credentials file as a phrase naming the line at fault, such as `line 2 isn't valid TOML (expected value but found "tim" instead)`. It suits the middle of a sentence. An error that names no line is returned as its own text.
func UpdateProfile ¶ added in v0.24.0
UpdateProfile applies mutate to the profile named name in the credentials file at path and writes the result back.
This is the entry point callers should reach for when they are changing an existing profile. WriteProfile replaces every key cinc manages, so a caller that assembles a Profile from only the values it collected clears the ones it did not: `config create` never prompts for secret_file, supermarket_client_name or supermarket_key, so writing a Profile built from its answers alone silently deletes them. UpdateProfile starts from what is already on disk, so an unmentioned key keeps its value by construction rather than by every caller remembering to carry it.
A profile that does not exist yet starts empty, so this also serves create-or-update callers. Keys cinc does not model are preserved by WriteProfile regardless.
func ValidateSiteURL ¶
ValidateSiteURL reports whether raw is a usable Supermarket site URL: one with a scheme and a host.
func WriteProfile ¶
WriteProfile creates or updates one profile in the credentials file at path. Other profiles are left untouched, and within the profile being written only the keys cinc manages are rewritten; anything else the file holds is carried across verbatim. Comments and original key ordering are not retained, because the file is re-encoded as TOML.
func WriteProfileWithExtras ¶ added in v0.24.0
WriteProfileWithExtras is WriteProfile for a profile that also carries keys cinc does not model, copied from somewhere other than the file being written: first-run migration moves a knife profile's node_name, validation_key and the like across this way. Each extra key is written verbatim, replacing any value the file already has for it. A key cinc manages is ignored in extra, because p is the source of truth for those.
Types ¶
type Config ¶
Config is the parsed contents of a cinc credentials file.
func Load ¶
Load reads and parses the credentials file at path. Each top-level TOML section becomes a named profile.
func (*Config) Profile ¶
Profile resolves a connection profile by name. An empty name selects the profile named by $CINC_PROFILE, then $CHEF_PROFILE, falling back to "default". When both env vars are set CINC_PROFILE wins.
func (*Config) Validate ¶
func (c *Config) Validate() []ValidationIssue
Validate reports configuration problems across every loaded profile. It is intentionally local-only: it validates TOML shape and URL/profile fields but does not perform network calls or authenticate.
type Profile ¶
type Profile struct {
ServerURL string
Org string
SupermarketSite string
ClientName string
KeyPath string
SSLVerifyMode string
// SupermarketClientName and SupermarketKey optionally override the
// identity used to sign Chef Supermarket uploads, so a user can
// publish to the public Supermarket under a different username or key
// than the one they use against their Cinc Server. Each field falls
// back independently: the effective Supermarket username is
// SupermarketClientName or, when empty, ClientName; the effective key
// is SupermarketKey or, when empty, KeyPath. These are cinc-only keys
// (knife has no equivalent). See SupermarketIdentity.
SupermarketClientName string
SupermarketKey string
// SecretFile is the path to the encrypted data bag secret used to
// encrypt and decrypt `cinc databag secret` items. The on-disk key
// is `secret_file`, matching knife's `knife[:secret_file]`, so the
// same key serves cinc and chef users.
SecretFile string
// TrustedCertsDir is the trusted_certs_dir key exactly as written in
// the credentials file: a directory of extra CA certificates to trust
// on top of the system pool. The key name matches knife's, so the same
// file serves cinc and chef users. It is kept unexpanded here so a
// rewrite of the profile never bakes an absolute home path into the
// file; ResolveTrustedCertsDir expands it and applies the defaults.
TrustedCertsDir string
// RawServerURL is the server URL exactly as written in the config, before
// it is split into ServerURL + Org. It is preserved even when the URL is
// malformed (ServerURL/Org are then empty) so validation can report the
// precise problem instead of the whole load failing.
RawServerURL string
// contains filtered or unexported fields
}
Profile describes how to connect to a single Cinc Server. It is the resolved form of an on-disk credentials entry: the configured server URL has already been split into a bare server URL and an organization name.
func NewProfile ¶
func NewProfile(serverURL, clientName, clientKey, sslVerifyMode, supermarketSite string) (Profile, error)
NewProfile builds a Profile from user-supplied configure values.
func (Profile) ResolveTrustedCertsDir ¶ added in v0.24.0
ResolveTrustedCertsDir returns the directory of extra CA certificates this profile trusts, and whether the profile configured it explicitly.
An explicit trusted_certs_dir has a leading ~ expanded to the home directory; any other path is returned as written, so a relative path is relative to the working directory, exactly as client_key is. The directory is not checked here: a missing explicit directory is the caller's error to report.
With nothing configured, the first of ~/.cinc/trusted_certs and ~/.chef/trusted_certs that exists as a directory is returned, with explicit false. When neither exists the result is "", meaning trust the system pool only.
func (Profile) SupermarketIdentity ¶
SupermarketIdentity returns the username and key path used to sign Chef Supermarket uploads. Each field falls back independently: the username is supermarket_client_name or, when unset, client_name; the key is supermarket_key or, when unset, client_key. This lets a user publish to the public Supermarket under a different identity than their Cinc Server client while overriding only the field that differs.
func (Profile) Validate ¶
Validate reports whether the profile has every field required to open a server connection.
func (Profile) ValidateIdentity ¶
ValidateIdentity reports whether the profile has the fields needed to sign requests that target a Cinc Server organization.
func (Profile) ValidateSupermarketIdentity ¶
ValidateSupermarketIdentity reports whether the profile resolves to an identity that can sign Supermarket uploads, accounting for the supermarket_client_name/supermarket_key overrides and their fallback to client_name/client_key.
type ValidationIssue ¶
type ValidationIssue struct {
Profile string `json:"profile"`
Field string `json:"field"`
Message string `json:"message"`
}
ValidationIssue describes one profile-level configuration problem.