Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9bff607

Browse files
authoredApr 18, 2020
Merge pull request #4 from storj/wizard2
Wizard improvements
2 parents 1f8554b + 1b9e9ab commit 9bff607

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed
 

‎backend/storj/fs.go

+61-33
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ import (
2323
"storj.io/uplink"
2424
)
2525

26+
const (
27+
existingProvider = "existing"
28+
newProvider = "new"
29+
)
30+
2631
var satMap = map[string]string{
2732
"us-central-1.tardigrade.io": "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us-central-1.tardigrade.io:7777",
2833
"europe-west-1.tardigrade.io": "12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs@europe-west-1.tardigrade.io:7777",
@@ -36,49 +41,55 @@ func init() {
3641
Description: "Tardigrade Decentralized Cloud Storage",
3742
NewFs: NewFs,
3843
Config: func(name string, configMapper configmap.Mapper) {
39-
satelliteString, _ := configMapper.Get("satellite_address")
40-
apiKey, _ := configMapper.Get("api_key")
41-
passphrase, _ := configMapper.Get("passphrase")
42-
accessString, _ := configMapper.Get("access_grant")
43-
44-
config.FileDeleteKey(name, "satellite_address")
45-
config.FileDeleteKey(name, "api_key")
46-
config.FileDeleteKey(name, "passphrase")
44+
provider, _ := configMapper.Get(fs.ConfigProvider)
45+
4746
config.FileDeleteKey(name, fs.ConfigProvider)
4847

49-
// satelliteString contains always default and passphrase can be empty
50-
if accessString != "" || (accessString == "" && apiKey == "") {
51-
return
52-
}
48+
if provider == newProvider {
49+
satelliteString, _ := configMapper.Get("satellite_address")
50+
apiKey, _ := configMapper.Get("api_key")
51+
passphrase, _ := configMapper.Get("passphrase")
5352

54-
satellite, found := satMap[satelliteString]
55-
if !found {
56-
satellite = satelliteString
57-
}
53+
// satelliteString contains always default and passphrase can be empty
54+
if apiKey == "" {
55+
return
56+
}
5857

59-
access, err := uplink.RequestAccessWithPassphrase(context.TODO(), satellite, apiKey, passphrase)
60-
if err != nil {
61-
log.Fatalf("Couldn't create access grant: %v", err)
62-
}
58+
satellite, found := satMap[satelliteString]
59+
if !found {
60+
satellite = satelliteString
61+
}
6362

64-
serialziedAccess, err := access.Serialize()
65-
if err != nil {
66-
log.Fatalf("Couldn't serialize access grant: %v", err)
67-
}
63+
access, err := uplink.RequestAccessWithPassphrase(context.TODO(), satellite, apiKey, passphrase)
64+
if err != nil {
65+
log.Fatalf("Couldn't create access grant: %v", err)
66+
}
6867

69-
configMapper.Set("access_grant", serialziedAccess)
68+
serialziedAccess, err := access.Serialize()
69+
if err != nil {
70+
log.Fatalf("Couldn't serialize access grant: %v", err)
71+
}
72+
configMapper.Set("satellite_address", satellite)
73+
configMapper.Set("access_grant", serialziedAccess)
74+
} else if provider == existingProvider {
75+
config.FileDeleteKey(name, "satellite_address")
76+
config.FileDeleteKey(name, "api_key")
77+
config.FileDeleteKey(name, "passphrase")
78+
} else {
79+
log.Fatalf("Invalid provider type: %s", provider)
80+
}
7081
},
7182
Options: []fs.Option{
7283
{
7384
Name: fs.ConfigProvider,
7485
Help: "Choose an authentication method.",
7586
Required: true,
76-
Default: "existing",
87+
Default: existingProvider,
7788
Examples: []fs.OptionExample{{
7889
Value: "existing",
7990
Help: "Use an existing access grant.",
8091
}, {
81-
Value: "new",
92+
Value: newProvider,
8293
Help: "Create a new access grant from satellite address, API key, and passphrase.",
8394
},
8495
}},
@@ -92,7 +103,7 @@ func init() {
92103
Name: "satellite_address",
93104
Help: "Satellite Address. Custom satellite address should match the format: <nodeid>@<address>:<port>.",
94105
Required: false,
95-
Provider: "new",
106+
Provider: newProvider,
96107
Default: "us-central-1.tardigrade.io",
97108
Examples: []fs.OptionExample{{
98109
Value: "us-central-1.tardigrade.io",
@@ -110,13 +121,13 @@ func init() {
110121
Name: "api_key",
111122
Help: "API Key.",
112123
Required: false,
113-
Provider: "new",
124+
Provider: newProvider,
114125
},
115126
{
116127
Name: "passphrase",
117128
Help: "Encryption Passphrase. To access existing objects enter passphrase used for uploading.",
118129
Required: false,
119-
Provider: "new",
130+
Provider: newProvider,
120131
IsPassword: true,
121132
},
122133
},
@@ -125,10 +136,10 @@ func init() {
125136

126137
// Options defines the configuration for this backend
127138
type Options struct {
128-
Access string `config:"access"`
139+
Access string `config:"access_grant"`
129140

130-
SatelliteAddress string `config:"satellite-address"`
131-
APIKey string `config:"api-key"`
141+
SatelliteAddress string `config:"satellite_address"`
142+
APIKey string `config:"api_key"`
132143
Passphrase string `config:"passphrase"`
133144
}
134145

@@ -183,6 +194,23 @@ func NewFs(name, root string, m configmap.Mapper) (_ fs.Fs, err error) {
183194
}
184195
}
185196

197+
if access == nil && f.opts.SatelliteAddress != "" && f.opts.APIKey != "" && f.opts.Passphrase != "" {
198+
access, err = uplink.RequestAccessWithPassphrase(ctx, f.opts.SatelliteAddress, f.opts.APIKey, f.opts.Passphrase)
199+
if err != nil {
200+
return nil, errors.Wrap(err, "storj: access")
201+
}
202+
203+
serializedAccess, err := access.Serialize()
204+
if err != nil {
205+
return nil, errors.Wrap(err, "storj: access")
206+
}
207+
208+
err = config.SetValueAndSave(f.name, "access_grant", serializedAccess)
209+
if err != nil {
210+
return nil, errors.Wrap(err, "storj: access")
211+
}
212+
}
213+
186214
if access == nil {
187215
return nil, errors.New("access not found")
188216
}

0 commit comments

Comments
 (0)
Please sign in to comment.