forked from gopasspw/gopass
/
action.go
112 lines (98 loc) · 2.72 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package action
import (
"context"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/blang/semver"
"github.com/justwatchcom/gopass/backend/crypto/gpg"
gpgcli "github.com/justwatchcom/gopass/backend/crypto/gpg/cli"
"github.com/justwatchcom/gopass/config"
"github.com/justwatchcom/gopass/store/root"
"github.com/justwatchcom/gopass/utils/out"
)
var (
stdin io.Reader = os.Stdin
stdout io.Writer = os.Stdout
stderr io.Writer = os.Stderr
)
type gpger interface {
Binary() string
ListPublicKeys(context.Context) (gpg.KeyList, error)
FindPublicKeys(context.Context, ...string) (gpg.KeyList, error)
ListPrivateKeys(context.Context) (gpg.KeyList, error)
CreatePrivateKeyBatch(context.Context, string, string, string) error
CreatePrivateKey(context.Context) error
FindPrivateKeys(context.Context, ...string) (gpg.KeyList, error)
GetRecipients(context.Context, string) ([]string, error)
Encrypt(context.Context, string, []byte, []string) error
Decrypt(context.Context, string) ([]byte, error)
ExportPublicKey(context.Context, string, string) error
ImportPublicKey(context.Context, string) error
Version(context.Context) semver.Version
}
// Action knows everything to run gopass CLI actions
type Action struct {
Name string
Store *root.Store
cfg *config.Config
gpg gpger
version semver.Version
}
// New returns a new Action wrapper
func New(ctx context.Context, cfg *config.Config, sv semver.Version) (*Action, error) {
gpg, err := gpgcli.New(ctx, gpgcli.Config{
Umask: umask(),
Args: gpgOpts(),
})
if err != nil {
out.Red(ctx, "Warning: GPG not found: %s", err)
}
return newAction(ctx, cfg, sv, gpg)
}
func newAction(ctx context.Context, cfg *config.Config, sv semver.Version, gpg gpger) (*Action, error) {
name := "gopass"
if len(os.Args) > 0 {
name = filepath.Base(os.Args[0])
}
act := &Action{
Name: name,
cfg: cfg,
version: sv,
gpg: gpg,
}
store, err := root.New(ctx, cfg, act.gpg)
if err != nil {
return nil, exitError(ctx, ExitUnknown, err, "failed to init root store: %s", err)
}
act.Store = store
return act, nil
}
func umask() int {
for _, en := range []string{"GOPASS_UMASK", "PASSWORD_STORE_UMASK"} {
if um := os.Getenv(en); um != "" {
if iv, err := strconv.ParseInt(um, 8, 32); err == nil && iv >= 0 && iv <= 0777 {
return int(iv)
}
}
}
return 077
}
func gpgOpts() []string {
for _, en := range []string{"GOPASS_GPG_OPTS", "PASSWORD_STORE_GPG_OPTS"} {
if opts := os.Getenv(en); opts != "" {
return strings.Fields(opts)
}
}
return nil
}
// String implement fmt.Stringer
func (s *Action) String() string {
return s.Store.String()
}
// HasGPG returns true if the GPG wrapper is initialized
func (s *Action) HasGPG() bool {
return s.gpg != nil
}