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

add auto-copy-enabled #57

Merged
merged 1 commit into from
Oct 25, 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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes

### v1.13.0

- Add AutoCopyEnabled field to profile.

### v1.12.5

- Security upgrades from Dependabot
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
46 changes: 28 additions & 18 deletions cmd/kiya/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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.")
}
}
Loading