From 9dc3b46b526076d20c4b88216d983b6901d76825 Mon Sep 17 00:00:00 2001 From: ernest micklei Date: Fri, 25 Oct 2024 10:07:00 +0200 Subject: [PATCH] add auto-copy-enabled --- CHANGES.md | 4 ++++ README.md | 4 ++++ backend/backend.go | 19 ++++++++++--------- cmd/kiya/main.go | 46 ++++++++++++++++++++++++++++------------------ 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b774aa0..603771f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changes +### v1.13.0 + +- Add AutoCopyEnabled field to profile. + ### v1.12.5 - Security upgrades from Dependabot diff --git a/README.md b/README.md index 8c17600..5687142 100644 --- a/README.md +++ b/README.md @@ -268,6 +268,10 @@ Public key copied to clipboard The public key has been copied to the clipboard, but you must put the private key in a safe place ( e.g. print it out on paper and put it in a physical safe :)). +#### Auto copy to clipboard + +The `list` (which is the default command if no command is given) command will show a table of matches based on the given command line argument. If that list contains a single match and your profile has `autocopyenabled` set to true then the secret will be copied to the clipboard automatically. + ### Limitations - In this version, the private key can only be retrieved from the file system diff --git a/backend/backend.go b/backend/backend.go index 21fb945..ebac9cc 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -24,13 +24,14 @@ type Key struct { // Profile describes a single profile in a .kiya configuration type Profile struct { - Backend string - Label string - ProjectID string - Location string - Keyring string - CryptoKey string - Bucket string - VaultUrl string - SecretRunes []rune + Backend string + Label string + ProjectID string + Location string + Keyring string + CryptoKey string + Bucket string + VaultUrl string + SecretRunes []rune + AutoCopyEnabled bool // if true then the secret of a single list result will be copied to clipboard } diff --git a/cmd/kiya/main.go b/cmd/kiya/main.go index 21be2e1..2335147 100644 --- a/cmd/kiya/main.go +++ b/cmd/kiya/main.go @@ -134,19 +134,7 @@ func main() { case "copy": key := flag.Arg(2) - - if shouldPromptForPassword(b) { - pass := promptForPassword() - b.SetParameter("masterPassword", pass) - } - - value, err := b.Get(ctx, &target, key) - if err != nil { - log.Fatal(tre.New(err, "get failed", "key", key, "err", err)) - } - if err := clipboard.WriteAll(string(value)); err != nil { - log.Fatal(tre.New(err, "copy failed", "key", key, "err", err)) - } + copySecretToClipboard(ctx, b, target, key) case "get": key := flag.Arg(2) @@ -175,10 +163,8 @@ func main() { commandDelete(ctx, b, &target, key) case "list": // kiya [profile] list [|filter-term] - filter := flag.Arg(2) + listMatchingKeys(ctx, b, target, flag.Arg(2)) - keys := commandList(ctx, b, &target, filter) - writeTable(keys, &target, filter) case "template": commandTemplate(ctx, b, &target, *oOutputFilename) case "move": @@ -338,8 +324,7 @@ func main() { fmt.Println("Public key copied to clipboard") default: - keys := commandList(ctx, b, &target, flag.Arg(1)) - writeTable(keys, &target, flag.Arg(1)) + listMatchingKeys(ctx, b, target, flag.Arg(1)) } } @@ -385,3 +370,28 @@ func getBackend(ctx context.Context, p *backend.Profile) (backend.Backend, error return backend.NewKMS(kmsService, storageService), nil } } + +func copySecretToClipboard(ctx context.Context, be backend.Backend, target backend.Profile, key string) { + if shouldPromptForPassword(be) { + pass := promptForPassword() + be.SetParameter("masterPassword", pass) + } + value, err := be.Get(ctx, &target, key) + if err != nil { + log.Fatal(tre.New(err, "get failed", "key", key, "err", err)) + } + if err := clipboard.WriteAll(string(value)); err != nil { + log.Fatal(tre.New(err, "copy failed", "key", key, "err", err)) + } +} + +func listMatchingKeys(ctx context.Context, be backend.Backend, target backend.Profile, filter string) { + keys := commandList(ctx, be, &target, filter) + writeTable(keys, &target, filter) + // if there is only one match and AutoCopy is enabled + // then copy the secret to clipboard + if len(keys) == 1 && target.AutoCopyEnabled { + copySecretToClipboard(ctx, be, target, keys[0].Name) + fmt.Println("... copied secret to clipboard.") + } +}