-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
keyring.go
122 lines (100 loc) · 3.37 KB
/
keyring.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
package age
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/gopasspw/gopass/internal/backend"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/pkg/appdir"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/gopasspw/gopass/pkg/debug"
"github.com/gopasspw/gopass/pkg/fsutil"
)
var (
// OldIDFile is the old file name for the recipients.
OldIDFile = ".age-ids"
// OldKeyring is the old file name for the keyring.
OldKeyring = filepath.Join(appdir.UserConfig(), "age-keyring.age")
)
func migrate(ctx context.Context, s backend.Storage) error {
out.Noticef(ctx, "Attempting to migrate age backend. You will need to unlock your identities keyring.")
oldIDPath := filepath.Join(s.Path(), OldIDFile)
newIDPath := filepath.Join(s.Path(), IDFile)
if fsutil.IsFile(oldIDPath) && fsutil.IsFile(newIDPath) {
out.Warningf(ctx, "Both %s and %s exist. Removing the old one (%s).", oldIDPath, newIDPath, oldIDPath)
if err := os.Remove(oldIDPath); err != nil {
out.Errorf(ctx, "Failed to remove %s: %s", oldIDPath, err)
}
}
if fsutil.IsFile(oldIDPath) {
out.Noticef(ctx, "Found %s. Migrating to %s.", oldIDPath, newIDPath)
if err := os.Rename(oldIDPath, newIDPath); err != nil {
out.Errorf(ctx, "Failed to rename %s to %s: %s", oldIDPath, newIDPath, err)
}
}
// create a new instance so we can use decryptFile.
a, err := New(ctx)
if err != nil {
return err
}
if !ctxutil.HasPasswordCallback(ctx) {
debug.Log("no password callback found, redirecting to askPass")
ctx = ctxutil.WithPasswordCallback(ctx, func(prompt string, _ bool) ([]byte, error) {
pw, err := a.askPass.Passphrase(prompt, fmt.Sprintf("to load the age keyring at %s", OldKeyring), false)
return []byte(pw), err
})
ctx = ctxutil.WithPasswordPurgeCallback(ctx, a.askPass.Remove)
}
if fsutil.IsFile(OldKeyring) && fsutil.IsFile(a.identity) {
out.Warningf(ctx, "Both %s and %s exist. Keeping both. Recover any identities from %s as needed.", OldKeyring, a.identity, OldKeyring)
return nil
}
if !fsutil.IsFile(OldKeyring) {
// nothing to do.
return nil
}
debug.Log("loading old identities from %s", OldKeyring)
ids, err := a.loadIdentitiesFromKeyring(ctx)
if err != nil {
return err
}
debug.Log("writing new identities to %s", a.identity)
if err := a.saveIdentities(ctx, ids, false); err != nil {
return err
}
return os.Remove(OldKeyring)
}
// Keyring is an age keyring.
// Deprecated: Only used for backwards compatibility. Will be removed soon.
type Keyring []Keypair
// Keypair is a public / private keypair.
// Deprecated: Only used for backwards compatibility. Will be removed soon.
type Keypair struct {
Name string `json:"name"`
Email string `json:"email"`
Identity string `json:"identity"`
}
func (a *Age) loadIdentitiesFromKeyring(ctx context.Context) ([]string, error) {
buf, err := a.decryptFile(ctx, OldKeyring)
if err != nil {
debug.Log("can't decrypt keyring at %s: %s", OldKeyring, err)
return nil, err
}
var kr Keyring
if err := json.Unmarshal(buf, &kr); err != nil {
debug.Log("can't parse keyring at %s: %s", OldKeyring, err)
return nil, err
}
// remove invalid IDs.
valid := make([]string, 0, len(kr))
for _, k := range kr {
if k.Identity == "" {
continue
}
valid = append(valid, k.Identity)
}
debug.Log("loaded keyring with %d valid entries from %s", len(kr), OldKeyring)
return valid, nil
}