/
export_config.go
137 lines (117 loc) 路 3.32 KB
/
export_config.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
package app
import (
"encoding/base64"
"fmt"
"io"
"os"
"path"
"path/filepath"
"time"
"github.com/taubyte/tau/config"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
)
func exportConfig(ctx *cli.Context) error {
root := ctx.Path("root")
if root == "" {
root = config.DefaultRoot
}
if !filepath.IsAbs(root) {
return fmt.Errorf("root folder `%s` is not absolute", root)
}
shape := ctx.String("shape")
configRoot := root + "/config"
configPath := ctx.Path("path")
if configPath == "" {
configPath = path.Join(configRoot, shape+".yaml")
}
data, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("shape %s does not exist", shape)
}
host, err := os.Hostname()
if err != nil {
return fmt.Errorf("faile to fetch hostname with %w", err)
}
var version *string
if v := ctx.String("version"); v != "" {
version = &v
}
bundle := &config.Bundle{
Origin: config.BundleOrigin{
Shape: shape,
Host: host,
Creation: time.Now(),
Version: version,
},
}
if err = yaml.Unmarshal(data, &(bundle.Source)); err != nil {
return fmt.Errorf("yaml unmarshal failed with: %w", err)
}
pkey := bundle.Privatekey
if !ctx.Bool("unsafe") {
pkey = ""
}
skfilename := path.Join(configRoot, bundle.Swarmkey)
skdata, err := os.ReadFile(skfilename)
if err != nil {
return fmt.Errorf("reading %s failed with: %w %#v", skfilename, err, bundle)
}
dvsfilename := path.Join(configRoot, bundle.Domains.Key.Private)
dvsdata, err := os.ReadFile(dvsfilename)
if err != nil {
return fmt.Errorf("reading %s failed with: %w", dvsfilename, err)
}
var dvpdata []byte
if bundle.Domains.Key.Public != "" {
dvpfilename := path.Join(configRoot, bundle.Domains.Key.Public)
dvpdata, err = os.ReadFile(dvpfilename)
if err != nil {
return fmt.Errorf("reading %s failed with: %w", dvsfilename, err)
}
}
if ctx.Bool("protect") {
bundle.Origin.Protected = true
if passwd, err := promptPassword("Password?"); err != nil {
return fmt.Errorf("faild to read password with %w", err)
} else {
if skdata, err = encrypt(skdata, passwd); err != nil {
return fmt.Errorf("faild to encrypt swarm key with %w", err)
}
if dvsdata, err = encrypt(dvsdata, passwd); err != nil {
return fmt.Errorf("faild to encrypt domain's private key key with %w", err)
}
if dvpdata != nil {
if dvpdata, err = encrypt(dvpdata, passwd); err != nil {
return fmt.Errorf("faild to encrypt domain's public key key with %w", err)
}
}
if pkey != "" {
pkdata, err := encrypt([]byte(pkey), passwd)
if err != nil {
return fmt.Errorf("faild to encrypt private key key with %w", err)
}
pkey = base64.StdEncoding.EncodeToString(pkdata)
}
}
}
bundle.Privatekey = pkey
bundle.Swarmkey = base64.StdEncoding.EncodeToString(skdata)
bundle.Domains.Key.Private = base64.StdEncoding.EncodeToString(dvsdata)
bundle.Domains.Key.Public = base64.StdEncoding.EncodeToString(dvpdata)
var out io.Writer = os.Stdout
if ctx.Args().Present() {
filename := ctx.Args().First()
f, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0460)
if err != nil {
return fmt.Errorf("fail to open %s with %w", filename, err)
}
defer f.Close()
out = f
}
err = yaml.NewEncoder(out).Encode(bundle)
if err != nil {
return fmt.Errorf("fail to marshal configuration with %w", err)
}
return nil
}