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
|
// Command atlas-import imports data from the old Northstar Master Server.
package main
import (
"bytes"
"context"
"crypto/sha1"
"encoding/hex"
"fmt"
"net/netip"
"os"
"time"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/r2northstar/atlas/db/atlasdb"
"github.com/r2northstar/atlas/db/pdatadb"
"github.com/r2northstar/atlas/pkg/api/api0"
"github.com/r2northstar/atlas/pkg/pdata"
"github.com/spf13/pflag"
)
var opt struct {
Progress bool
Help bool
}
func init() {
pflag.BoolVarP(&opt.Progress, "progress", "p", false, "Show progress")
pflag.BoolVarP(&opt.Help, "help", "h", false, "Show this help text")
}
func main() {
pflag.Parse()
if pflag.NArg() != 3 || opt.Help {
fmt.Printf("usage: %s [options] northstar_db atlas_db pdata_db\n\noptions:\n%s", os.Args[0], pflag.CommandLine.FlagUsages())
if opt.Help {
os.Exit(2)
}
os.Exit(0)
}
na, np, err := migrate(pflag.Arg(0), pflag.Arg(1), pflag.Arg(2))
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
fmt.Printf("imported %d accounts, %d pdata blobs\n", na, np)
}
type nsacct struct {
ID uint64 `db:"id"`
CurrentAuthToken string `db:"currentAuthToken"`
CurrentAuthTokenExpirationTime int64 `db:"currentAuthTokenExpirationTime"`
CurrentServerID *string `db:"currentServerId"`
PersistentDataBaseline []byte `db:"persistentDataBaseline"`
LastAuthIP *string `db:"lastAuthIp"`
Username string `db:"username"`
}
func migrate(nsfn, atlasfn, pdatafn string) (int, int, error) {
ctx := context.Background()
nsdb, err := sqlx.Connect("sqlite3", nsfn+"?mode=ro")
if err != nil {
return 0, 0, fmt.Errorf("open northstar db %q: %w", nsfn, err)
}
defer nsdb.Close()
if _, err := os.Stat(atlasfn); err == nil {
return 0, 0, fmt.Errorf("create atlas db: %q already exists", atlasfn)
}
if _, err := os.Stat(pdatafn); err == nil {
return 0, 0, fmt.Errorf("create pdata db: %q already exists", pdatafn)
}
adb, err := atlasdb.Open(atlasfn)
if err != nil {
return 0, 0, fmt.Errorf("create atlas db: %w", err)
}
defer adb.Close()
pdb, err := pdatadb.Open(pdatafn)
if err != nil {
return 0, 0, fmt.Errorf("create atlas db: %w", err)
}
defer pdb.Close()
if _, to, err := adb.Version(); err != nil {
return 0, 0, fmt.Errorf("migrate atlas db: %w", err)
} else if err = adb.MigrateUp(ctx, to); err != nil {
return 0, 0, fmt.Errorf("migrate atlas db: %w", err)
}
if _, to, err := pdb.Version(); err != nil {
return 0, 0, fmt.Errorf("migrate pdata db: %w", err)
} else if err = pdb.MigrateUp(ctx, to); err != nil {
return 0, 0, fmt.Errorf("migrate pdata db: %w", err)
}
rows, err := nsdb.Queryx(`SELECT * FROM accounts`)
if err != nil {
return 0, 0, fmt.Errorf("query northstar db: %w", err)
}
defer rows.Close()
var na, np int
for rows.Next() {
var n nsacct
if err := rows.StructScan(&n); err != nil {
return 0, 0, fmt.Errorf("query northstar db: scan row: %w", err)
}
if err := insertA(&n, adb); err != nil {
return 0, 0, fmt.Errorf("migrate uid %d (%s): %w", n.ID, n.Username, err)
} else {
na++
}
if done, err := insertP(&n, pdb); err != nil {
return 0, 0, fmt.Errorf("migrate uid %d (%s): %w", n.ID, n.Username, err)
} else if done {
np++
}
if opt.Progress && na%1000 == 0 {
fmt.Printf("done %d\n", na)
}
}
if err := rows.Err(); err != nil {
return 0, 0, fmt.Errorf("query northstar db: scan rows: %w", err)
}
return na, np, nil
}
func insertA(n *nsacct, a *atlasdb.DB) error {
var x api0.Account
if n.ID != 0 {
x.UID = n.ID
} else {
return fmt.Errorf("uid is zero")
}
if n.Username != "" {
x.Username = n.Username
}
if n.LastAuthIP != nil && *n.LastAuthIP != "" {
if v, err := netip.ParseAddr(*n.LastAuthIP); err == nil {
x.AuthIP = v
} else {
fmt.Fprintf(os.Stderr, "warning: uid %d (%s): failed to parse last auth ip %q (%v), ignoring\n", n.ID, n.Username, *n.LastAuthIP, err)
}
}
if v := time.Unix(n.CurrentAuthTokenExpirationTime/1000, 0); time.Until(v).Abs() < time.Hour*24 {
x.AuthToken = n.CurrentAuthToken
x.AuthTokenExpiry = v
}
if n.CurrentServerID != nil && *n.CurrentServerID != "" {
x.LastServerID = *n.CurrentServerID
}
if err := a.SaveAccount(&x); err != nil {
return err
}
return nil
}
func insertP(n *nsacct, p *pdatadb.DB) (bool, error) {
var pd pdata.Pdata
if err := pd.UnmarshalBinary(n.PersistentDataBaseline); err == nil {
if len(pd.ExtraData) < 140 {
if !bytes.Equal(n.PersistentDataBaseline, pdata.DefaultPdata) && isOldDefaultPdata(n.PersistentDataBaseline) {
if sz, err := p.SetPdata(n.ID, n.PersistentDataBaseline); err != nil {
return false, err
} else if sz > 2200 {
fmt.Fprintf(os.Stderr, "info: uid %d (%s): large compressed pdata size %d\n", n.ID, n.Username, sz)
}
return true, nil
}
} else {
fmt.Fprintf(os.Stderr, "warning: uid %d (%s): %d junk in pdata, discarding\n", n.ID, n.Username, len(pd.ExtraData))
}
} else {
fmt.Fprintf(os.Stderr, "warning: uid %d (%s): invalid pdata (%v), discarding\n", n.ID, n.Username, err)
}
return false, nil
}
func isOldDefaultPdata(b []byte) bool {
ss := sha1.Sum(b)
return hex.EncodeToString(ss[:]) == "9dab70c01c475bf976689d4af525aa39db6d73bc"
}
|