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

update data store, allow checking if keyring exists #10

Merged
merged 1 commit into from
May 27, 2024
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
17 changes: 17 additions & 0 deletions keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type DataStore interface {
// CleanStorage cleanups storage (credentials or key-value).
// Error is returned if the vault couldn't be unlocked.
CleanStorage(item SecretItem) error
// Exists checks if keyring exists in persistent storage.
Exists() bool
// Save saves the keyring to the persistent storage.
Save() error
// Destroy removes the keyring from the persistent storage.
Expand All @@ -77,6 +79,7 @@ type DataStore interface {
type Keyring interface {
launchr.Service
DataStore
ResetStorage()
}

type keyringService struct {
Expand All @@ -97,6 +100,11 @@ func (k *keyringService) ServiceInfo() launchr.ServiceInfo {
return launchr.ServiceInfo{}
}

// ResetStorage cleans store for subsequent reload.
func (k *keyringService) ResetStorage() {
k.store = nil
}

func (k *keyringService) defaultStore() (DataStore, error) {
if k.store != nil {
return k.store, nil
Expand Down Expand Up @@ -167,6 +175,15 @@ func (k *keyringService) CleanStorage(item SecretItem) error {
return s.CleanStorage(item)
}

// Exists checks if keyring exists in persistent storage.
func (k *keyringService) Exists() bool {
s, err := k.defaultStore()
if err != nil {
return false
}
return s.Exists()
}

// Save implements DataStore interface. Uses service default store.
func (k *keyringService) Save() error {
s, err := k.defaultStore()
Expand Down
14 changes: 14 additions & 0 deletions yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ func (s *dataStoreYaml) CleanStorage(item SecretItem) error {
return nil
}

// Exists checks if keyring exists in persistent storage.
func (s *dataStoreYaml) Exists() bool {
ageStorage, ok := s.file.(*ageFile)
if !ok {
panic("impossible type assertion")
}

info, err := os.Stat(ageStorage.file.fname)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

// Save implements DataStore interface.
func (s *dataStoreYaml) Save() error {
err := s.file.Open(os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
Expand Down
Loading