forked from oleiade/trousseau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade.go
194 lines (158 loc) · 4.43 KB
/
upgrade.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package trousseau
import (
"encoding/json"
"fmt"
"sort"
"github.com/oleiade/serrure/openpgp"
)
type VersionMatcher func([]byte) bool
type UpgradeClosure func([]byte) ([]byte, error)
var UpgradeClosures map[string]UpgradeClosure = map[string]UpgradeClosure{
"0.3.0": upgradeZeroDotThreeToNext,
}
var VersionDiscoverClosures map[string]VersionMatcher = map[string]VersionMatcher{
"0.3.0": isVersionZeroDotThreeDotZero,
"0.3.1": isVersionZeroDotThreeDotOne,
}
func UpgradeFrom(startVersion string, d []byte, mapping map[string]UpgradeClosure) ([]byte, error) {
var versions []string
var out []byte = d
var err error
for version, _ := range mapping {
if version >= startVersion {
versions = append(versions, version)
}
}
sort.Strings(versions)
for idx, version := range versions {
var versionRepr string
if idx == (len(versions) - 1) {
versionRepr = TROUSSEAU_VERSION
} else {
versionRepr = version
}
upgradeClosure := mapping[version]
out, err = upgradeClosure(out)
if err != nil {
return nil, fmt.Errorf("Upgrading trousseau data store to version %s: failure\nReason: %s", versionRepr, err.Error())
}
}
return out, nil
}
func DiscoverVersion(d []byte, mapping map[string]VersionMatcher) string {
var versions []string
for version, _ := range mapping {
versions = append(versions, version)
}
sort.Strings(versions)
for _, version := range versions {
if mapping[version](d) == true {
return version
}
}
return ""
}
func isVersionZeroDotThreeDotZero(d []byte) bool {
if len(d) >= len(openpgp.PGP_MESSAGE_HEADER) &&
string(d[0:len(openpgp.PGP_MESSAGE_HEADER)]) == openpgp.PGP_MESSAGE_HEADER {
return true
}
return false
}
func isVersionZeroDotThreeDotOne(d []byte) bool {
var zeroDotFourStore map[string]interface{} = make(map[string]interface{})
var zeroDotFourKeys []string = []string{"crypto_algorithm", "crypto_type", "_data"}
err := json.Unmarshal(d, &zeroDotFourStore)
if err != nil {
return false
}
for _, key := range zeroDotFourKeys {
_, in := zeroDotFourStore[key]
if !in {
return false
}
}
return true
}
func upgradeZeroDotThreeToNext(d []byte) ([]byte, error) {
var err error
// Assert input data are in the expected version format
validVersion := isVersionZeroDotThreeDotZero(d)
if !validVersion {
return nil, fmt.Errorf("Provided input data not matching version 0.3 format")
}
// Declaring and instanciating a type matching
// the 0.3 version store format
legacyStore := struct {
Meta map[string]interface{} `json:"_meta"`
Data map[string]interface{} `json:"data"`
}{
Meta: make(map[string]interface{}),
Data: make(map[string]interface{}),
}
// Decrypt store version 0.3 (aka legacy)
passphrase, err := GetPassphrase()
if err != nil {
ErrorLogger.Fatal(err)
}
decrypter, err := openpgp.NewOpenPGPDecrypter(GnupgSecring(), passphrase)
if err != nil {
return nil, err
}
pd, err := decrypter.Decrypt(d)
if err != nil {
return nil, err
}
// Unmarshal it's content into the legacyStore
err = json.Unmarshal(pd, &legacyStore)
if err != nil {
return nil, err
}
// Declaring and instanciating a type matching
// the 0.4 version store format so we can inject the
// legacy data into it
newStore := struct {
Meta map[string]interface{} `json:"meta"`
Data map[string]interface{} `json:"store"`
}{
Meta: legacyStore.Meta,
Data: legacyStore.Data,
}
// Encode it in json
newStoreData, err := json.Marshal(newStore)
if err != nil {
return nil, err
}
// Retrieve legacyStore recipients
var recipients []string
for _, r := range legacyStore.Meta["recipients"].([]interface{}) {
recipients = append(recipients, r.(string))
}
encrypter, err := openpgp.NewOpenPGPEncrypter(GnupgPubring(), recipients)
if err != nil {
return nil, err
}
// Encrypt the encoded newStore content
ed, err := encrypter.Encrypt(newStoreData)
if err != nil {
return nil, err
}
// Declaring and instanciating a type matching
// the 0.4 version trousseau data store format
// so we can inject the encrypted store into it
newTrousseau := struct {
CryptoAlgorithm CryptoAlgorithm `json:"crypto_algorithm"`
CryptoType CryptoType `json:"crypto_type"`
Data []byte `json:"_data"`
}{
CryptoAlgorithm: GPG_ENCRYPTION,
CryptoType: ASYMMETRIC_ENCRYPTION,
Data: ed,
}
// Encode the new trousseau data store
trousseau, err := json.Marshal(newTrousseau)
if err != nil {
return nil, err
}
return trousseau, nil
}