Skip to content

Commit

Permalink
improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pchila committed Jun 20, 2024
1 parent e378e7a commit 96af3cf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 15 additions & 3 deletions internal/pkg/agent/storage/encrypted_disk_storage_debug_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,30 @@ func printWindowsFileInfo(path string, logF loggerFunc) {

sd, err := windows.GetNamedSecurityInfo(path, windows.SE_FILE_OBJECT, windows.OWNER_SECURITY_INFORMATION|windows.GROUP_SECURITY_INFORMATION)
if err != nil {
panic(err)
panic(fmt.Errorf("getting security descriptor for %s: %w", path, err))
}

logF("%s security descriptor: %v\n", path, sd)
logF("%s security descriptor: %s", path, sd)

ownerSid, _, err := sd.Owner()
if err != nil {
panic(fmt.Errorf("getting owner from security descriptor %s: %w", sd, err))
}

account, domain, accType, err := ownerSid.LookupAccount("")
if err != nil {
panic(fmt.Errorf("looking up account for %s: %w", ownerSid, err))
}

logF("owner for %s: %s\\%s account type %x", path, domain, account, accType)
}

func DumpFilesystemInfo(path string, logF loggerFunc) {
stat, staterr := os.Stat(path)
if staterr != nil {
logF("Error stat()ing %s: %s\n", path, staterr)
} else {
logF("%s stat:\n%s\n", path, printFileInfo(stat))
logF("%s stat:\n%s", path, printFileInfo(stat))
printWindowsFileInfo(path, logF)
}
}
16 changes: 11 additions & 5 deletions internal/pkg/agent/storage/encrypted_disk_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/elastic/elastic-agent-libs/file"
"github.com/elastic/elastic-agent/pkg/core/logger"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/secret"
Expand Down Expand Up @@ -118,10 +119,16 @@ func (d *EncryptedDiskStore) ensureKey(ctx context.Context) error {
// Save will write the encrypted storage to disk.
// Specifically it will write to a .tmp file then rotate the file to the target name to ensure that an error does not corrupt the previously written file.
func (d *EncryptedDiskStore) Save(in io.Reader) error {

l, logErr := logger.New("encrypted-disk-store-debug", true)
if logErr != nil {
fmt.Fprintf(os.Stderr, "error instantiating debug logger: %s", logErr)
}

if d.target == paths.AgentConfigFile() {
fmt.Fprintf(os.Stderr, "Save of %s started at %s\n", paths.AgentConfigFile(), time.Now())
l.Infof("Save of %s started at %s\n", paths.AgentConfigFile(), time.Now())
defer func() {
fmt.Fprintf(os.Stderr, "Save of %s finished at %s\n", paths.AgentConfigFile(), time.Now())
l.Infof("Save of %s finished at %s\n", paths.AgentConfigFile(), time.Now())
}()
}

Expand Down Expand Up @@ -190,11 +197,10 @@ func (d *EncryptedDiskStore) Save(in io.Reader) error {
errors.M(errors.MetaKeyPath, tmpFile))
}

DumpFilesystemInfo(d.target, func(fmtString string, args ...any) { fmt.Fprintf(os.Stderr, fmtString, args) })

DumpFilesystemInfo(d.target, l.Infof)
if err := file.SafeFileRotate(d.target, tmpFile); err != nil {

DumpFilesystemInfo(d.target, func(fmtString string, args ...any) { fmt.Fprintf(os.Stderr, fmtString, args) })
DumpFilesystemInfo(d.target, l.Infof)

return errors.New(err,
fmt.Sprintf("could not replace target file %s", d.target),
Expand Down

0 comments on commit 96af3cf

Please sign in to comment.