Skip to content

Commit 40917c0

Browse files
authored
Merge pull request #96 from ipfs/migration/8-base32_encoded_keys
Migration 8-9 - Encoding key filenames with base32
2 parents 45b8090 + 31f3081 commit 40917c0

File tree

9 files changed

+267
-14
lines changed

9 files changed

+267
-14
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ ipfs repo version | go-ipfs versions
4444
4 | 0.4.3 - 0.4.5
4545
5 | 0.4.6 - 0.4.10
4646
6 | 0.4.11 - 0.4.15
47-
7 | 0.4.16 - current
47+
7 | 0.4.16 - 0.4.23
48+
8 | 0.5.0 - current
49+
9 | 0.5.0 - current
4850

4951
### How to Run Migrations
5052

ipfs-8-to-9/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
migrate "github.com/ipfs/fs-repo-migrations/go-migrate"
5+
mg8 "github.com/ipfs/fs-repo-migrations/ipfs-8-to-9/migration"
6+
)
7+
8+
func main() {
9+
m := mg8.Migration{}
10+
migrate.Main(&m)
11+
}

ipfs-8-to-9/migration/migration.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package mg8
2+
3+
import (
4+
base32 "encoding/base32"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
migrate "github.com/ipfs/fs-repo-migrations/go-migrate"
12+
mfsr "github.com/ipfs/fs-repo-migrations/mfsr"
13+
log "github.com/ipfs/fs-repo-migrations/stump"
14+
)
15+
16+
type Migration struct{}
17+
18+
func (m Migration) Versions() string {
19+
return "8-to-9"
20+
}
21+
22+
func (m Migration) Reversible() bool {
23+
return true
24+
}
25+
26+
const keyFilenamePrefix = "key_"
27+
28+
const keystoreRoot = "keystore"
29+
30+
func isEncoded(name string) bool {
31+
_, err := decode(name)
32+
return err == nil
33+
}
34+
35+
func encode(name string) (string, error) {
36+
if name == "" {
37+
return "", fmt.Errorf("key name must be at least one character")
38+
}
39+
40+
encoder := base32.StdEncoding.WithPadding(base32.NoPadding)
41+
encodedName := encoder.EncodeToString([]byte(name))
42+
return keyFilenamePrefix + strings.ToLower(encodedName), nil
43+
}
44+
45+
func decode(name string) (string, error) {
46+
if !strings.HasPrefix(name, keyFilenamePrefix) {
47+
return "", fmt.Errorf("key's filename has unexpected format")
48+
}
49+
50+
nameWithoutPrefix := strings.ToUpper(name[len(keyFilenamePrefix):])
51+
decoder := base32.StdEncoding.WithPadding(base32.NoPadding)
52+
data, err := decoder.DecodeString(nameWithoutPrefix)
53+
54+
return string(data), err
55+
}
56+
57+
func (m Migration) Apply(opts migrate.Options) error {
58+
log.Verbose = opts.Verbose
59+
log.Log("applying %s repo migration", m.Versions())
60+
61+
err := m.encodeDecode(
62+
opts,
63+
isEncoded, // skip if already encoded
64+
encode,
65+
)
66+
67+
if err != nil {
68+
return err
69+
}
70+
71+
err = mfsr.RepoPath(opts.Path).WriteVersion("9")
72+
if err != nil {
73+
log.Error("failed to update version file to 9")
74+
return err
75+
}
76+
77+
log.Log("updated version file")
78+
79+
return nil
80+
}
81+
82+
func (m Migration) encodeDecode(opts migrate.Options, shouldApplyCodec func(string) bool, codec func(string) (string, error)) error {
83+
keystoreRoot := filepath.Join(opts.Path, keystoreRoot)
84+
fileInfos, err := ioutil.ReadDir(keystoreRoot)
85+
86+
if err != nil {
87+
return err
88+
}
89+
90+
for _, info := range fileInfos {
91+
if info.IsDir() {
92+
log.Log("skipping ", info.Name(), " as it is directory!")
93+
continue
94+
}
95+
96+
if shouldApplyCodec(info.Name()) {
97+
log.Log("skipping ", info.Name(), ". Already in expected format!")
98+
continue
99+
}
100+
101+
log.VLog("Renaming key's filename: ", info.Name())
102+
encodedName, err := codec(info.Name())
103+
if err != nil {
104+
return err
105+
}
106+
107+
src := filepath.Join(keystoreRoot, info.Name())
108+
dest := filepath.Join(keystoreRoot, encodedName)
109+
110+
if err := os.Rename(src, dest); err != nil {
111+
return err
112+
}
113+
}
114+
return nil
115+
}
116+
117+
func (m Migration) Revert(opts migrate.Options) error {
118+
log.Verbose = opts.Verbose
119+
log.Log("reverting migration")
120+
121+
err := m.encodeDecode(
122+
opts,
123+
func(name string) bool {
124+
return !isEncoded(name) // skip if not encoded
125+
},
126+
decode,
127+
)
128+
129+
if err != nil {
130+
return err
131+
}
132+
133+
err = mfsr.RepoPath(opts.Path).WriteVersion("8")
134+
if err != nil {
135+
log.Error("failed to update version file to 8")
136+
return err
137+
}
138+
139+
log.Log("updated version file")
140+
141+
return nil
142+
}

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import (
1616
mg4 "github.com/ipfs/fs-repo-migrations/ipfs-4-to-5/migration"
1717
mg5 "github.com/ipfs/fs-repo-migrations/ipfs-5-to-6/migration"
1818
mg6 "github.com/ipfs/fs-repo-migrations/ipfs-6-to-7/migration"
19+
mg7 "github.com/ipfs/fs-repo-migrations/ipfs-7-to-8/migration"
20+
mg8 "github.com/ipfs/fs-repo-migrations/ipfs-8-to-9/migration"
1921
mfsr "github.com/ipfs/fs-repo-migrations/mfsr"
2022
)
2123

22-
var CurrentVersion = 7
24+
var CurrentVersion = 9
2325

2426
var migrations = []gomigrate.Migration{
2527
&mg0.Migration{},
@@ -29,6 +31,8 @@ var migrations = []gomigrate.Migration{
2931
&mg4.Migration{},
3032
&mg5.Migration{},
3133
&mg6.Migration{},
34+
&mg7.Migration{},
35+
&mg8.Migration{},
3236
}
3337

3438
func GetIpfsDir() (string, error) {

sharness/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ BINS += bin/ipfs-4-to-5
2222
BINS += bin/ipfs-5-to-6
2323
BINS += bin/ipfs-6-to-7
2424
BINS += bin/ipfs-7-to-8
25+
BINS += bin/ipfs-8-to-9
2526
BINS += bin/ipfs-update
2627
BINS += bin/random-files
2728
BINS += bin/go-sleep
@@ -38,6 +39,7 @@ IPFS_4_TO_5_SRC = ../ipfs-4-to-5
3839
IPFS_5_TO_6_SRC = ../ipfs-5-to-6
3940
IPFS_6_TO_7_SRC = ../ipfs-6-to-7
4041
IPFS_7_TO_8_SRC = ../ipfs-7-to-8
42+
IPFS_8_TO_9_SRC = ../ipfs-8-to-9
4143

4244
# User might want to override those on the command line
4345
GOFLAGS =
@@ -125,6 +127,11 @@ bin/ipfs-7-to-8: $(call find_go_files, $(IPFS_7_TO_8_SRC)) BUILD-OPTIONS
125127
@echo "*** installing $@ ***"
126128
go build $(GOFLAGS) -o $@ $(IPFS_7_TO_8_SRC)
127129

130+
bin/ipfs-8-to-9: $(call find_go_files, $(IPFS_8_TO_9_SRC)) BUILD-OPTIONS
131+
@echo "*** installing $@ ***"
132+
go build $(GOFLAGS) -o $@ $(IPFS_8_TO_9_SRC)
133+
134+
128135
BUILD-OPTIONS: FORCE
129136
@bin/checkflags '$@' '$(GOFLAGS)' '*** new Go flags ***'
130137

sharness/lib/test-lib.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ test_init_ipfs_nd() {
263263

264264
test_expect_success "ipfs init succeeds" '
265265
export IPFS_PATH="$(pwd)/.ipfs" &&
266-
ipfs init -b=1024 > /dev/null
266+
ipfs init -b=2048 > /dev/null
267267
'
268268

269269
test_expect_success "prepare config -- mounting and bootstrap rm" '

sharness/t0030-simple-migration.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test_expect_success "'fs-repo-migrations -v' works" '
1313
'
1414

1515
test_expect_success "'fs-repo-migrations -v' output looks good" '
16-
echo "7" >expected &&
16+
echo "9" >expected &&
1717
test_cmp expected actual
1818
'
1919

@@ -30,7 +30,7 @@ test_expect_success "'fs-repo-migrations -v' works" '
3030
'
3131

3232
test_expect_success "'fs-repo-migrations -v' output looks good" '
33-
echo "7" >expected &&
33+
echo "9" >expected &&
3434
test_cmp expected actual
3535
'
3636

sharness/t0120-migration-7-to-8.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

sharness/t0120-migration-7-to-9.sh

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/bin/sh
2+
3+
test_description="Test migration 7 to 9"
4+
5+
. lib/test-lib.sh
6+
7+
# Dist specially built with a v0.5.0-dev-8to9pre1 release
8+
export IPFS_DIST_PATH="/ipfs/QmaaN2kipZfUpRSzwvUeG4Xi3yp1JJB294Vj8pnZ24hesF"
9+
export GOPATH="$(pwd)/gopath"
10+
mkdir -p gopath/bin
11+
export PATH="$(pwd)/../bin:$GOPATH/bin:$PATH"
12+
echo $IPFS_PATH
13+
14+
test_install_ipfs_nd "v0.4.23"
15+
16+
test_init_ipfs_nd
17+
18+
# We need bootstrap addresses to migrate. These are defaults from v0.4.23.
19+
test_expect_success "add bootstrap addresses" '
20+
test_config_set --json Bootstrap "[
21+
\"/dnsaddr/bootstrap.libp2p.io/ipfs/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN\",
22+
\"/dnsaddr/bootstrap.libp2p.io/ipfs/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa\",
23+
\"/dnsaddr/bootstrap.libp2p.io/ipfs/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb\",
24+
\"/dnsaddr/bootstrap.libp2p.io/ipfs/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt\",
25+
\"/ip4/104.131.131.82/tcp/4001/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ\",
26+
\"/ip4/104.236.179.241/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM\",
27+
\"/ip4/128.199.219.111/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu\",
28+
\"/ip4/104.236.76.40/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64\",
29+
\"/ip4/178.62.158.247/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd\",
30+
\"/ip6/2604:a880:1:20::203:d001/tcp/4001/ipfs/QmSoLPppuBtQSGwKDZT2M73ULpjvfd3aZ6ha4oFGL1KrGM\",
31+
\"/ip6/2400:6180:0:d0::151:6001/tcp/4001/ipfs/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu\",
32+
\"/ip6/2604:a880:800:10::4a:5001/tcp/4001/ipfs/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64\",
33+
\"/ip6/2a03:b0c0:0:1010::23:1001/tcp/4001/ipfs/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd\"
34+
]"
35+
'
36+
37+
test_expect_success "remember old bootstrap" '
38+
ipfs config Bootstrap | grep -E -o "Qm[a-zA-Z0-9]+" | sort > bootstrap_old
39+
'
40+
41+
# no need to launch daemon as this works offline
42+
43+
test_expect_success "add some keys to the keystore" '
44+
ipfs key gen -t rsa thisISKey1 && ipfs key gen -t ed25519 key2
45+
'
46+
47+
test_expect_success "ipfs key list" '
48+
ipfs key list > key_list
49+
'
50+
51+
test_expect_success "run migration 7 to 8" '
52+
ipfs-7-to-8 -verbose -path="$IPFS_PATH" > migration_output8
53+
'
54+
55+
test_expect_success "run migration 8 to 9" '
56+
ipfs-8-to-9 -verbose -path="$IPFS_PATH" > migration_output9
57+
'
58+
59+
test_expect_success "migration processed keys" '
60+
grep "thisISKey1" migration_output9 &&
61+
grep "key2" migration_output9
62+
'
63+
64+
test_expect_success "migrated files exist" '
65+
[ -f "${IPFS_PATH}/keystore/key_orugs42jknfwk6jr" ] &&
66+
[ -f "${IPFS_PATH}/keystore/key_nnsxsmq" ]
67+
'
68+
69+
test_install_ipfs_nd "v0.5.0-dev-8to9pre2"
70+
71+
test_expect_success "ipfs key list is the same" '
72+
ipfs key list > new_key_list
73+
test_cmp key_list new_key_list
74+
'
75+
76+
test_expect_success "migration revert to 8 succeeds" '
77+
ipfs-8-to-9 -revert -verbose -path="$IPFS_PATH" > revert_output8
78+
'
79+
80+
test_expect_success "migration revert to 7 succeeds" '
81+
ipfs-7-to-8 -revert -verbose -path="$IPFS_PATH" > revert_output7
82+
'
83+
84+
test_install_ipfs_nd "v0.4.23"
85+
86+
test_expect_success "bootstrap addresses were reverted" '
87+
ipfs config Bootstrap | grep -E -o "Qm[a-zA-Z0-9]+" | sort > bootstrap_revert
88+
test_cmp bootstrap_old bootstrap_revert
89+
'
90+
91+
test_expect_success "ipfs key list is the same after revert" '
92+
ipfs key list > revert_key_list
93+
test_cmp key_list revert_key_list
94+
'
95+
96+
test_done

0 commit comments

Comments
 (0)