-
Notifications
You must be signed in to change notification settings - Fork 64
/
migrate_account.go
152 lines (132 loc) Β· 3.26 KB
/
migrate_account.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
package cmd
import (
"fmt"
"log"
"github.com/charmbracelet/charm/client"
"github.com/charmbracelet/charm/proto"
"github.com/spf13/cobra"
)
// MigrateAccountCmd is a command to convert your legacy RSA SSH keys to the
// new Ed25519 standard keys.
var (
verbose bool
linkError bool
MigrateAccountCmd = &cobra.Command{
Use: "migrate-account",
Hidden: true,
Short: "",
Long: "",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Migrating account...")
rcfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
rcfg.KeyType = "rsa"
rsaClient, err := client.NewClient(rcfg)
if err != nil {
return err
}
ecfg, err := client.ConfigFromEnv()
if err != nil {
return err
}
ecfg.KeyType = "ed25519"
ed25519Client, err := client.NewClient(ecfg)
if err != nil {
return err
}
lc := make(chan string)
go func() {
lh := &linkHandler{desc: "link-gen", linkChan: lc}
rsaClient.LinkGen(lh)
}()
tok := <-lc
lh := &linkHandler{desc: "link-request", linkChan: lc}
ed25519Client.Link(lh, tok)
if verbose {
log.Printf("link-gen sync encrypt keys")
}
err = rsaClient.SyncEncryptKeys()
if err != nil {
if verbose {
log.Printf("link-gen sync encrypt keys failed")
} else {
printError()
}
return err
}
if verbose {
log.Printf("link-request sync encrypt keys")
}
err = ed25519Client.SyncEncryptKeys()
if err != nil {
if verbose {
log.Printf("link-request sync encrypt keys failed")
} else {
printError()
}
return err
}
if !linkError {
fmt.Println("Account migrated! You're good to go.")
} else {
printError()
}
return nil
},
}
)
type linkHandler struct {
desc string
linkChan chan string
}
func (lh *linkHandler) TokenCreated(l *proto.Link) {
lh.printDebug("token created", l)
lh.linkChan <- string(l.Token)
lh.printDebug("token created sent to chan", l)
}
func (lh *linkHandler) TokenSent(l *proto.Link) {
lh.printDebug("token sent", l)
}
func (lh *linkHandler) ValidToken(l *proto.Link) {
lh.printDebug("valid token", l)
}
func (lh *linkHandler) InvalidToken(l *proto.Link) {
lh.printDebug("invalid token", l)
}
func (lh *linkHandler) Request(l *proto.Link) bool {
lh.printDebug("request", l)
return true
}
func (lh *linkHandler) RequestDenied(l *proto.Link) {
lh.printDebug("request denied", l)
}
func (lh *linkHandler) SameUser(l *proto.Link) {
lh.printDebug("same user", l)
}
func (lh *linkHandler) Success(l *proto.Link) {
lh.printDebug("success", l)
}
func (lh *linkHandler) Timeout(l *proto.Link) {
lh.printDebug("timeout", l)
}
func (lh linkHandler) Error(l *proto.Link) {
linkError = true
lh.printDebug("error", l)
if !verbose {
printError()
}
}
func (lh *linkHandler) printDebug(msg string, l *proto.Link) {
if verbose {
log.Printf("%s %s:\t%v\n", lh.desc, msg, l)
}
}
func printError() {
fmt.Println("\nThere was an error migrating your account. Please re-run with the -v argument `charm migrate-account -v` and join our slack at https://charm.sh/slack to help debug the issue. Sorry about that, we'll try to figure it out!")
}
func init() {
MigrateAccountCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "print debug output")
}