-
-
Notifications
You must be signed in to change notification settings - Fork 501
/
Copy pathwrite.go
124 lines (95 loc) · 3.27 KB
/
write.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
113
114
115
116
117
118
119
120
121
122
123
124
package leaf
import (
"context"
"errors"
"fmt"
"strings"
"github.com/gopasspw/gopass/internal/config"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/queue"
"github.com/gopasspw/gopass/internal/store"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/gopass"
)
// Set encodes and writes the cipertext of one entry to disk.
func (s *Store) Set(ctx context.Context, name string, sec gopass.Byter) error {
if strings.Contains(name, "//") {
return fmt.Errorf("invalid secret name: %s", name)
}
if config.FromContext(ctx).GetM(s.alias, "core.readonly") == "true" {
return fmt.Errorf("writing to %s is disabled by `core.readonly`.", s.alias)
}
p := s.Passfile(name)
recipients, err := s.useableKeys(ctx, name)
if err != nil {
return fmt.Errorf("failed to list useable keys for %q: %w", p, err)
}
// make sure the encryptor can decrypt later
recipients = s.ensureOurKeyID(ctx, recipients)
ciphertext, err := s.crypto.Encrypt(ctx, sec.Bytes(), recipients)
if err != nil {
debug.Log("Failed encrypt secret: %s", err)
return store.ErrEncrypt
}
if err := s.storage.Set(ctx, p, ciphertext); err != nil {
return fmt.Errorf("failed to write secret: %w", err)
}
// It is not possible to perform concurrent git add and git commit commands
// so we need to skip this step when using concurrency and perform them
// at the end of the batch processing.
if IsNoGitOps(ctx) {
debug.Log("sub.Set(%s) - skipping git ops (disabled)")
return nil
}
if err := s.storage.Add(ctx, p); err != nil {
if errors.Is(err, store.ErrGitNotInit) {
return nil
}
return fmt.Errorf("failed to add %q to git: %w", p, err)
}
if !ctxutil.IsGitCommit(ctx) {
return nil
}
// try to enqueue this task, if the queue is not available
// it will return the task and we will execute it inline
t := queue.GetQueue(ctx).Add(func(_ context.Context) (context.Context, error) {
return nil, s.gitCommitAndPush(ctx, name)
})
ctx, err = t(ctx)
return err
}
func (s *Store) gitCommitAndPush(ctx context.Context, name string) error {
if err := s.storage.Commit(ctx, fmt.Sprintf("Save secret to %s: %s", name, ctxutil.GetCommitMessage(ctx))); err != nil {
switch {
case errors.Is(err, store.ErrGitNotInit):
debug.Log("commitAndPush - skipping git commit - git not initialized")
case errors.Is(err, store.ErrGitNothingToCommit):
debug.Log("commitAndPush - skipping git commit - nothing to commit")
default:
return fmt.Errorf("failed to commit changes to git: %w", err)
}
}
if !config.Bool(ctx, "core.autosync") {
debug.Log("not pushing to git remote, core.autosync is false")
return nil
}
debug.Log("syncing with remote ...")
if err := s.storage.Push(ctx, "", ""); err != nil {
if errors.Is(err, store.ErrGitNotInit) {
msg := "Warning: git is not initialized for this.storage. Ignoring auto-push option\n" +
"Run: gopass git init"
out.Errorf(ctx, msg)
return nil
}
if errors.Is(err, store.ErrGitNoRemote) {
msg := "Warning: git has no remote. Ignoring auto-push option\n" +
"Run: gopass git remote add origin ..."
debug.Log(msg)
return nil
}
return fmt.Errorf("failed to push to git remote: %w", err)
}
debug.Log("synced with remote")
return nil
}