@cryptotaxi247 / kubo / commits / f3fbedf31

no longer worry about moving key out of config file

Jeromy committed Apr 8, 2015 at 23:16 UTC f3fbedf31279dac516eb6bbdec788f9515c793e3
3 files changed +52 -144
cmd/ipfs/daemon.go
+1 -1
@@ -119,7 +119,7 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
119 // sure we are permitted to access the resources (datastore, etc.)
120 repo, err := fsrepo.Open(req.Context().ConfigRoot)
121 if err != nil {
122 - res.SetError(fmt.Errorf("Couldn't obtain lock. Is another daemon already running?"), cmds.ErrNormal)
122 + res.SetError(err, cmds.ErrNormal)
123 return
124 }
125
repo/fsrepo/fsrepo.go
+26
@@ -4,6 +4,7 @@ import (
4 "errors"
5 "fmt"
6 "io"
7 + "io/ioutil"
8 "os"
9 "path"
10 "strconv"
@@ -18,6 +19,7 @@ import (
19 "github.com/ipfs/go-ipfs/repo/common"
20 config "github.com/ipfs/go-ipfs/repo/config"
21 lockfile "github.com/ipfs/go-ipfs/repo/fsrepo/lock"
22 + mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
23 serialize "github.com/ipfs/go-ipfs/repo/fsrepo/serialize"
24 dir "github.com/ipfs/go-ipfs/thirdparty/dir"
25 "github.com/ipfs/go-ipfs/thirdparty/eventlog"
@@ -26,6 +28,8 @@ import (
28 ds2 "github.com/ipfs/go-ipfs/util/datastore2"
29 )
30
31 +var RepoVersion = "2"
32 +
33 const (
34 leveldbDirectory = "datastore"
35 flatfsDirectory = "blocks"
@@ -107,6 +111,24 @@ func open(repoPath string) (repo.Repo, error) {
111 if !isInitializedUnsynced(r.path) {
112 return nil, errors.New("ipfs not initialized, please run 'ipfs init'")
113 }
114 +
115 + // Check version, and error out if not matching
116 + ver, err := ioutil.ReadFile(path.Join(expPath, "version"))
117 + if err != nil {
118 + if os.IsNotExist(err) {
119 + return nil, errors.New("version check failed, no version file found, please run 0-to-1 migration tool.")
120 + }
121 + return nil, err
122 + }
123 +
124 + vers := string(ver)[:1]
125 +
126 + if vers != RepoVersion {
127 + return nil, fmt.Errorf("Repo has incorrect version: '%s'\nProgram version is: '%s'\nPlease run the appropriate migration tool before continuing",
128 + vers, RepoVersion)
129 +
130 + }
131 +
132 // check repo path, then check all constituent parts.
133 // TODO acquire repo lock
134 // TODO if err := initCheckDir(logpath); err != nil { // }
@@ -208,6 +230,10 @@ func Init(repoPath string, conf *config.Config) error {
230 return err
231 }
232
233 + if err := mfsr.RepoPath(repoPath).WriteVersion(RepoVersion); err != nil {
234 + return err
235 + }
236 +
237 return nil
238 }
239
repo/fsrepo/migrations/1-to-2/main.go
+25 -143
@@ -2,9 +2,7 @@ package main
2
3 import (
4 "encoding/json"
5 - "errors"
5 "fmt"
7 - "io/ioutil"
6 "os"
7 "path"
8 "strings"
@@ -14,7 +12,6 @@ import (
12 leveldb "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
13 dsq "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/query"
14 migrate "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-migrate"
17 - fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
15 mfsr "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
16
17 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
@@ -41,8 +38,8 @@ func (m migration) Apply(opts migrate.Options) error {
38 return err
39 }
40
44 - // 1) move key out of config, into its own path
45 - err := moveKeyOutOfConfig(opts.Path)
41 + // 1) run some sanity checks to make sure we should even bother
42 + err := sanityChecks(opts)
43 if err != nil {
44 return err
45 }
@@ -87,16 +84,31 @@ func (m migration) Revert(opts migrate.Options) error {
84 return err
85 }
86
90 - // 3) move key back into config
91 - err = moveKeyIntoConfig(npath)
87 + // 3) change version number back down
88 + repo = mfsr.RepoPath(npath)
89 + err = repo.WriteVersion("1")
90 if err != nil {
91 return err
92 }
93
96 - // 4) change version number back down
97 - repo = mfsr.RepoPath(npath)
98 - err = repo.WriteVersion("1")
94 + return nil
95 +}
96 +
97 +// sanityChecks performs a set of tests to make sure the migration will go
98 +// smoothly
99 +func sanityChecks(opts migrate.Options) error {
100 + npath := strings.Replace(opts.Path, ".go-ipfs", ".ipfs", 1)
101 +
102 + // make sure we can move the repo from .go-ipfs to .ipfs
103 + err := os.Mkdir(npath, 0777)
104 + if err != nil {
105 + return err
106 + }
107 +
108 + // we can? good, remove it now
109 + err = os.Remove(npath)
110 if err != nil {
111 + // this is weird... not worth continuing
112 return err
113 }
114
@@ -104,7 +116,8 @@ func (m migration) Revert(opts migrate.Options) error {
116 }
117
118 func transferBlocksToFlatDB(repopath string) error {
107 - r, err := fsrepo.Open(repopath)
119 + ldbpath := path.Join(repopath, "datastore")
120 + ldb, err := leveldb.NewDatastore(ldbpath, nil)
121 if err != nil {
122 return err
123 }
@@ -120,7 +133,7 @@ func transferBlocksToFlatDB(repopath string) error {
133 return err
134 }
135
123 - return transferBlocks(r.Datastore(), fds, "/b/", "")
136 + return transferBlocks(ldb, fds, "/b/", "")
137 }
138
139 func transferBlocksFromFlatDB(repopath string) error {
@@ -183,98 +196,6 @@ func transferBlocks(from, to dstore.Datastore, fpref, tpref string) error {
196 return nil
197 }
198
186 -func moveKeyOutOfConfig(repopath string) error {
187 - // Make keys directory
188 - keypath := path.Join(repopath, "keys")
189 - err := os.Mkdir(keypath, 0777)
190 - if err != nil {
191 - return err
192 - }
193 -
194 - // Grab the config
195 - cfg, err := loadConfigJSON(repopath)
196 - if err != nil {
197 - return err
198 - }
199 -
200 - // get the private key from it
201 - privKey, err := getPrivateKeyFromConfig(cfg)
202 - if err != nil {
203 - return err
204 - }
205 -
206 - keyfilepath := path.Join(keypath, peerKeyName)
207 - fi, err := os.OpenFile(keyfilepath, os.O_CREATE|os.O_WRONLY, 0600)
208 - if err != nil {
209 - return err
210 - }
211 -
212 - // Write our b64-protobuf encoded key
213 - _, err = fi.WriteString(privKey)
214 - if err != nil {
215 - return err
216 - }
217 -
218 - err = fi.Close()
219 - if err != nil {
220 - return err
221 - }
222 -
223 - // Now that the key is safely in its own file, remove it from the config
224 - err = clearPrivateKeyFromConfig(cfg)
225 - if err != nil {
226 - return err
227 - }
228 -
229 - err = saveConfigJSON(repopath, cfg)
230 - if err != nil {
231 - return err
232 - }
233 -
234 - return nil
235 -}
236 -
237 -// Part of the 2-to-1 revert process
238 -func moveKeyIntoConfig(repopath string) error {
239 - // Make keys directory
240 - keypath := path.Join(repopath, "keys")
241 -
242 - // Grab the config
243 - cfg, err := loadConfigJSON(repopath)
244 - if err != nil {
245 - return err
246 - }
247 -
248 - keyfilepath := path.Join(keypath, peerKeyName)
249 - pkey, err := ioutil.ReadFile(keyfilepath)
250 - if err != nil {
251 - return err
252 - }
253 -
254 - id, ok := cfg["Identity"]
255 - if !ok {
256 - return errors.New("expected to find an identity object in config")
257 - }
258 - identity, ok := id.(map[string]interface{})
259 - if !ok {
260 - return errors.New("expected Identity in config to be an object")
261 - }
262 - identity["PrivKey"] = string(pkey)
263 -
264 - err = saveConfigJSON(repopath, cfg)
265 - if err != nil {
266 - return err
267 - }
268 -
269 - // Now that the key is safely in the config, delete the file
270 - err = os.RemoveAll(keypath)
271 - if err != nil {
272 - return err
273 - }
274 -
275 - return nil
276 -}
277 -
199 func moveIpfsDir(curpath string) (string, error) {
200 newpath := strings.Replace(curpath, ".go-ipfs", ".ipfs", 1)
201 return newpath, os.Rename(curpath, newpath)
@@ -321,45 +242,6 @@ func saveConfigJSON(repoPath string, cfg map[string]interface{}) error {
242 return nil
243 }
244
324 -func getPrivateKeyFromConfig(cfg map[string]interface{}) (string, error) {
325 - ident, ok := cfg["Identity"]
326 - if !ok {
327 - return "", errors.New("no identity found in config")
328 - }
329 -
330 - identMap, ok := ident.(map[string]interface{})
331 - if !ok {
332 - return "", errors.New("expected Identity to be object (map)")
333 - }
334 -
335 - privkey, ok := identMap["PrivKey"]
336 - if !ok {
337 - return "", errors.New("no PrivKey field found in Identity")
338 - }
339 -
340 - privkeyStr, ok := privkey.(string)
341 - if !ok {
342 - return "", errors.New("expected PrivKey to be a string")
343 - }
344 -
345 - return privkeyStr, nil
346 -}
347 -
348 -func clearPrivateKeyFromConfig(cfg map[string]interface{}) error {
349 - ident, ok := cfg["Identity"]
350 - if !ok {
351 - return errors.New("no identity found in config")
352 - }
353 -
354 - identMap, ok := ident.(map[string]interface{})
355 - if !ok {
356 - return errors.New("expected Identity to be object (map)")
357 - }
358 -
359 - delete(identMap, "PrivKey")
360 - return nil
361 -}
362 -
245 func main() {
246 m := migration{}
247 migrate.Main(&m)