Skip to content

Commit

Permalink
Merge branch 'master' into feature/official-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
zivkovicmilos committed Nov 9, 2023
2 parents 90922f5 + 9c8f248 commit 34de86c
Show file tree
Hide file tree
Showing 220 changed files with 2,844 additions and 598 deletions.
16 changes: 16 additions & 0 deletions contribs/gnokeykc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `gnokeykc`

`gnokeykc` is a Go-based CLI tool that enhances [`gnokey`](../../gno.land/cmd/gnokey) by integrating with your system's keychain. It adds `gnokey kc ...` subcommands to set and unset passwords in the keychain, allowing Gnokey to fetch passwords directly from the keychain instead of prompting for terminal input.

## Usage

gnokey kc -h

## Terminal Alias

For ease of use, set up a terminal alias to replace `gnokey` with `gnokeykc`:

echo "alias gnokey='gnokeykc'" >> ~/.bashrc && source ~/.bashrc

Now, `gnokey` commands will use `gnokeykc`, fetching passwords from the keychain.

54 changes: 54 additions & 0 deletions contribs/gnokeykc/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module github.com/gnolang/gno/contribs/gnokeykc

go 1.20

replace github.com/gnolang/gno => ../..

require (
github.com/gnolang/gno v0.0.0-00010101000000-000000000000
github.com/zalando/go-keyring v0.2.3
)

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c // indirect
github.com/btcsuite/btcd/btcutil v1.0.0 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/cockroachdb/apd v1.1.0 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v3 v3.2103.4 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/gnolang/goleveldb v0.0.9 // indirect
github.com/gnolang/overflow v0.0.0-20170615021017-4d914c927216 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.4 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/peterbourgon/ff/v3 v3.4.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
237 changes: 237 additions & 0 deletions contribs/gnokeykc/go.sum

Large diffs are not rendered by default.

94 changes: 94 additions & 0 deletions contribs/gnokeykc/kc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"context"
"flag"
"fmt"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/zalando/go-keyring"
)

const (
kcService = "gnokey"
kcName = "encryption"
)

func newKcCmd(io commands.IO) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Name: "kc",
ShortUsage: "kc <command>",
ShortHelp: "Manage OS keychain",
},
commands.NewEmptyConfig(),
commands.HelpExec,
)
cmd.AddSubCommands(
newKcSetCmd(io),
newKcUnsetCmd(io),
)
return cmd
}

func newKcSetCmd(io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "set",
ShortUsage: "set",
ShortHelp: "set encryption password in OS keychain",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execKcSet(args, io)
},
)
}

func execKcSet(args []string, io commands.IO) error {
if len(args) != 0 {
return flag.ErrHelp
}

insecurePasswordStdin := false // XXX: cfg.rootCfg.InsecurePasswordStdin
password, err := io.GetPassword("Enter password.", insecurePasswordStdin)
if err != nil {
return fmt.Errorf("cannot read password: %w", err)
}

err = keyring.Set(kcService, kcName, password)
if err != nil {
return fmt.Errorf("cannot set password is OS keychain")
}

io.Printfln("Successfully added password for key.")
return nil
}

func newKcUnsetCmd(io commands.IO) *commands.Command {
return commands.NewCommand(
commands.Metadata{
Name: "unset",
ShortUsage: "unset",
ShortHelp: "unset password in OS keychain",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execKcUnset(args, io)
},
)
}

func execKcUnset(args []string, io commands.IO) error {
if len(args) != 0 {
return flag.ErrHelp
}

err := keyring.Delete(kcService, kcName)
if err != nil {
return fmt.Errorf("cannot unset password from OS keychain")
}

io.Printfln("Successfully unset password")
return nil
}
32 changes: 32 additions & 0 deletions contribs/gnokeykc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"context"
"fmt"
"os"

"github.com/gnolang/gno/tm2/pkg/commands"
"github.com/gnolang/gno/tm2/pkg/crypto/keys/client"
"github.com/zalando/go-keyring"
)

func main() {
stdio := commands.NewDefaultIO()
wrappedio := &wrappedIO{IO: stdio}
cmd := client.NewRootCmd(wrappedio)
cmd.AddSubCommands(newKcCmd(stdio))

if err := cmd.ParseAndRun(context.Background(), os.Args[1:]); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%+v\n", err)

os.Exit(1)
}
}

type wrappedIO struct {
commands.IO
}

func (io *wrappedIO) GetPassword(prompt string, insecure bool) (string, error) {
return keyring.Get(kcService, kcName)
}
Loading

0 comments on commit 34de86c

Please sign in to comment.