Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable logging to the systemd journal if a user config is provided #958

Merged
merged 2 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions internal/exec/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/coreos/go-systemd/v22/journal"
"github.com/coreos/ignition/v2/config"
"github.com/coreos/ignition/v2/config/shared/errors"
latest "github.com/coreos/ignition/v2/config/v3_1_experimental"
"github.com/coreos/ignition/v2/config/v3_1_experimental/types"
"github.com/coreos/ignition/v2/internal/exec/stages"
executil "github.com/coreos/ignition/v2/internal/exec/util"
"github.com/coreos/ignition/v2/internal/log"
"github.com/coreos/ignition/v2/internal/platform"
"github.com/coreos/ignition/v2/internal/providers"
Expand All @@ -45,6 +48,9 @@ import (

const (
DefaultFetchTimeout = 2 * time.Minute
// This variable will help to identify ignition journal messages
// related to the user/base config.
ignitionFetchedConfigMsgId = "57124006b5c94805b77ce473e92a8aeb"
)

// Engine represents the entity that fetches and executes a configuration.
Expand Down Expand Up @@ -75,6 +81,25 @@ func (e Engine) Run(stageName string) error {
return err
}

baseConfigFound := true
if err == providers.ErrNoProvider {
baseConfigFound = false
}

configExists, err := executil.PathExists(e.ConfigCache)
if err != nil {
e.Logger.Info("failed to check for %s : %v", e.ConfigCache, err)
}
// This is a hack to emit the journald message
// only once for the base config. Change this when
// (https://github.com/coreos/ignition/issues/950)
// is implemented.
if !configExists && baseConfigFound {
if err := logStructuredJournalEntry("base", "system", false); err != nil {
e.Logger.Info("failed to log systemd journal entry: %v", err)
}
}

cfg, err := e.acquireConfig()
if err == errors.ErrEmpty {
e.Logger.Info("%v: ignoring user-provided config", err)
Expand Down Expand Up @@ -103,6 +128,26 @@ func (e Engine) Run(stageName string) error {
return nil
}

// logStructuredJournalEntry logs information related to
// a user/base config into the systemd journal log.
func logStructuredJournalEntry(config string, src string, isReferenced bool) error {
ignitionInfo := map[string]string{
"IGNITION_CONFIG_TYPE": config,
"IGNITION_CONFIG_SRC": src,
"IGNITION_CONFIG_REFERENCED": strconv.FormatBool(isReferenced),
"MESSAGE_ID": ignitionFetchedConfigMsgId,
}
referenced := ""
if isReferenced {
referenced = "referenced "
}
msg := fmt.Sprintf("fetched %s%s config from %q", referenced, config, src)
if err := journal.Send(msg, journal.PriInfo, ignitionInfo); err != nil {
return err
}
return nil
}

// acquireConfig returns the configuration, first checking a local cache
// before attempting to fetch it from the provider.
func (e *Engine) acquireConfig() (cfg types.Config, err error) {
Expand Down Expand Up @@ -184,19 +229,20 @@ func (e *Engine) acquireConfig() (cfg types.Config, err error) {
// is unavailable. This will also render the config (see renderConfig) before
// returning.
func (e *Engine) fetchProviderConfig() (types.Config, error) {
fetchers := []providers.FuncFetchConfig{
cmdline.FetchConfig,
system.FetchConfig,
e.PlatformConfig.FetchFunc(),
fetchers := map[string]providers.FuncFetchConfig{
"cmdline": cmdline.FetchConfig,
"system": system.FetchConfig,
e.PlatformConfig.Name(): e.PlatformConfig.FetchFunc(),
}

var cfg types.Config
var r report.Report
var err error
for _, fetcher := range fetchers {
var providerKey string
for key, fetcher := range fetchers {
cfg, r, err = fetcher(e.Fetcher)
if err != providers.ErrNoProvider {
// successful, or failed on another error
providerKey = key
break
}
}
Expand All @@ -206,6 +252,9 @@ func (e *Engine) fetchProviderConfig() (types.Config, error) {
return types.Config{}, err
}

if err := logStructuredJournalEntry("user", providerKey, false); err != nil {
e.Logger.Info("failed to log systemd journal entry: %v", err)
}
// Replace the HTTP client in the fetcher to be configured with the
// timeouts of the config
err = e.Fetcher.UpdateHttpTimeoutsAndCAs(cfg.Ignition.Timeouts, cfg.Ignition.Security.TLS.CertificateAuthorities, cfg.Ignition.Proxy)
Expand Down Expand Up @@ -295,6 +344,10 @@ func (e *Engine) fetchReferencedConfig(cfgRef types.ConfigReference) (types.Conf
e.Logger.Debug("fetched referenced config from data url with SHA512: %s", hex.EncodeToString(hash[:]))
}

if err := logStructuredJournalEntry("user", u.Path, true); err != nil {
e.Logger.Info("failed to log systemd journal entry: %v", err)
}

if err := util.AssertValid(cfgRef.Verification, rawCfg); err != nil {
return types.Config{}, err
}
Expand Down
11 changes: 11 additions & 0 deletions internal/exec/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ func (u Util) JoinPath(path ...string) (string, error) {

return filepath.Join(u.DestDir, realpath, last), nil
}

// PathExists checks if the path exists for a given config.
func PathExists(path string) (bool, error) {
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return false, err
}
return false, nil
}
return true, nil
}
225 changes: 225 additions & 0 deletions vendor/github.com/coreos/go-systemd/v22/journal/journal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ github.com/aws/aws-sdk-go/service/sts
github.com/coreos/go-semver/semver
# github.com/coreos/go-systemd/v22 v22.0.0
github.com/coreos/go-systemd/v22/dbus
github.com/coreos/go-systemd/v22/journal
github.com/coreos/go-systemd/v22/unit
# github.com/coreos/vcontext v0.0.0-20190529201340-22b159166068
github.com/coreos/vcontext/json
Expand Down