@@ -23,6 +23,11 @@ import (
23
23
"storj.io/uplink"
24
24
)
25
25
26
+ const (
27
+ existingProvider = "existing"
28
+ newProvider = "new"
29
+ )
30
+
26
31
var satMap = map [string ]string {
27
32
"us-central-1.tardigrade.io" : "12EayRS2V1kEsWESU9QMRseFhdxYxKicsiFmxrsLZHeLUtdps3S@us-central-1.tardigrade.io:7777" ,
28
33
"europe-west-1.tardigrade.io" : "12L9ZFwhzVpuEKMUNUqkaTLGzwY9G24tbiigLiXpmZWKwmcNDDs@europe-west-1.tardigrade.io:7777" ,
@@ -36,49 +41,55 @@ func init() {
36
41
Description : "Tardigrade Decentralized Cloud Storage" ,
37
42
NewFs : NewFs ,
38
43
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
+
47
46
config .FileDeleteKey (name , fs .ConfigProvider )
48
47
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" )
53
52
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
+ }
58
57
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
+ }
63
62
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
+ }
68
67
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
+ }
70
81
},
71
82
Options : []fs.Option {
72
83
{
73
84
Name : fs .ConfigProvider ,
74
85
Help : "Choose an authentication method." ,
75
86
Required : true ,
76
- Default : "existing" ,
87
+ Default : existingProvider ,
77
88
Examples : []fs.OptionExample {{
78
89
Value : "existing" ,
79
90
Help : "Use an existing access grant." ,
80
91
}, {
81
- Value : "new" ,
92
+ Value : newProvider ,
82
93
Help : "Create a new access grant from satellite address, API key, and passphrase." ,
83
94
},
84
95
}},
@@ -92,7 +103,7 @@ func init() {
92
103
Name : "satellite_address" ,
93
104
Help : "Satellite Address. Custom satellite address should match the format: <nodeid>@<address>:<port>." ,
94
105
Required : false ,
95
- Provider : "new" ,
106
+ Provider : newProvider ,
96
107
Default : "us-central-1.tardigrade.io" ,
97
108
Examples : []fs.OptionExample {{
98
109
Value : "us-central-1.tardigrade.io" ,
@@ -110,13 +121,13 @@ func init() {
110
121
Name : "api_key" ,
111
122
Help : "API Key." ,
112
123
Required : false ,
113
- Provider : "new" ,
124
+ Provider : newProvider ,
114
125
},
115
126
{
116
127
Name : "passphrase" ,
117
128
Help : "Encryption Passphrase. To access existing objects enter passphrase used for uploading." ,
118
129
Required : false ,
119
- Provider : "new" ,
130
+ Provider : newProvider ,
120
131
IsPassword : true ,
121
132
},
122
133
},
@@ -125,10 +136,10 @@ func init() {
125
136
126
137
// Options defines the configuration for this backend
127
138
type Options struct {
128
- Access string `config:"access "`
139
+ Access string `config:"access_grant "`
129
140
130
- SatelliteAddress string `config:"satellite-address "`
131
- APIKey string `config:"api-key "`
141
+ SatelliteAddress string `config:"satellite_address "`
142
+ APIKey string `config:"api_key "`
132
143
Passphrase string `config:"passphrase"`
133
144
}
134
145
@@ -183,6 +194,23 @@ func NewFs(name, root string, m configmap.Mapper) (_ fs.Fs, err error) {
183
194
}
184
195
}
185
196
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
+
186
214
if access == nil {
187
215
return nil , errors .New ("access not found" )
188
216
}
0 commit comments