Skip to content

Commit

Permalink
wrap errors and fix some docs typo and convention
Browse files Browse the repository at this point in the history
  • Loading branch information
AndersonQ committed Jul 19, 2022
1 parent 21c4812 commit e3dcded
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
16 changes: 8 additions & 8 deletions internal/pkg/agent/application/info/agent_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func getInfoFromStore(s ioStore, logLevel string) (*persistentAgentInfo, error)
agentConfigFile := paths.AgentConfigFile()
reader, err := s.Load()
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to load from ioStore: %w", err)
}

// reader is closed by this function
Expand Down Expand Up @@ -195,20 +195,20 @@ func loadAgentInfo(forceUpdate bool, logLevel string, createAgentID bool) (*pers
agentConfigFile := paths.AgentConfigFile()
diskStore := storage.NewEncryptedDiskStore(agentConfigFile)

agentinfo, err := getInfoFromStore(diskStore, logLevel)
agentInfo, err := getInfoFromStore(diskStore, logLevel)
if err != nil {
return nil, err
return nil, fmt.Errorf("could not get agent info from store: %w", err)
}

if agentinfo != nil && !forceUpdate && (agentinfo.ID != "" || !createAgentID) {
return agentinfo, nil
if agentInfo != nil && !forceUpdate && (agentInfo.ID != "" || !createAgentID) {
return agentInfo, nil
}

if err := updateID(agentinfo, diskStore); err != nil {
return nil, err
if err := updateID(agentInfo, diskStore); err != nil {
return nil, fmt.Errorf("could not update agent ID on disk store: %w", err)
}

return agentinfo, nil
return agentInfo, nil
}

func updateID(agentInfo *persistentAgentInfo, s ioStore) error {
Expand Down
7 changes: 4 additions & 3 deletions internal/pkg/agent/application/info/agent_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"runtime"
"strings"

"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/release"
"github.com/elastic/go-sysinfo"
"github.com/elastic/go-sysinfo/types"

"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
"github.com/elastic/elastic-agent/internal/pkg/release"
)

// ECSMeta is a collection of agent related metadata in ECS compliant object form.
Expand Down Expand Up @@ -123,7 +124,7 @@ const (
func Metadata() (*ECSMeta, error) {
agentInfo, err := NewAgentInfo(false)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create new agent info: %w", err)
}

meta, err := agentInfo.ECSMetadata()
Expand Down
3 changes: 2 additions & 1 deletion 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 (
"github.com/hectane/go-acl"

"github.com/elastic/elastic-agent-libs/file"

"github.com/elastic/elastic-agent/internal/pkg/agent/application/paths"
"github.com/elastic/elastic-agent/internal/pkg/agent/application/secret"
"github.com/elastic/elastic-agent/internal/pkg/agent/errors"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (d *EncryptedDiskStore) ensureKey() error {
if d.key == nil {
key, err := secret.GetAgentSecret(secret.WithVaultPath(d.vaultPath))
if err != nil {
return err
return fmt.Errorf("could not get agent key: %w", err)
}
d.key = key.Value
}
Expand Down
6 changes: 4 additions & 2 deletions internal/pkg/agent/vault/vault_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ type Vault struct {
}

// New initializes the vault store
// Call Close when done to release the resouces
// Call Close when done to release the resources
func New(name string) (*Vault, error) {
var keychain C.SecKeychainRef

err := statusToError(C.OpenKeychain(keychain))
if err != nil {
return nil, err
return nil, fmt.Errorf("could not open keychain: %w", err)
}

return &Vault{
name: name,
keychain: keychain,
Expand Down
9 changes: 5 additions & 4 deletions internal/pkg/agent/vault/vault_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
Expand All @@ -29,28 +30,28 @@ type Vault struct {
mx sync.Mutex
}

// Open initializes the vault store
// New opens initializes the vault store
func New(path string) (*Vault, error) {
dir := filepath.Dir(path)

// If there is no specific path then get the executable directory
if dir == "." {
exefp, err := os.Executable()
if err != nil {
return nil, err
return nil, fmt.Errorf("could not get executable path: %w", err)
}
dir = filepath.Dir(exefp)
path = filepath.Join(dir, path)
}

err := os.MkdirAll(path, 0750)
if err != nil {
return nil, err
return nil, fmt.Errorf("could not create folders %s: %w", path, err)
}

key, err := getSeed(path)
if err != nil {
return nil, err
return nil, fmt.Errorf("could not get seed to create new valt: %w", err)
}

return &Vault{
Expand Down

0 comments on commit e3dcded

Please sign in to comment.