-
Notifications
You must be signed in to change notification settings - Fork 8
/
objfs.go
executable file
·409 lines (363 loc) · 9.46 KB
/
objfs.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
///usr/bin/env go run objfs.go registry.go commands.go "$@"; exit
/*
* objfs.go
*
* Copyright 2018 Bill Zissimopoulos
*/
/*
* This file is part of Objfs.
*
* You can redistribute it and/or modify it under the terms of the GNU
* Affero General Public License version 3 as published by the Free
* Software Foundation.
*
* Licensees holding a valid commercial license may use this file in
* accordance with the commercial license agreement provided with the
* software.
*/
package main
import (
"crypto/rand"
"crypto/tls"
"flag"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/billziss-gh/golib/appdata"
"github.com/billziss-gh/golib/cmd"
"github.com/billziss-gh/golib/config"
cflag "github.com/billziss-gh/golib/config/flag"
"github.com/billziss-gh/golib/errors"
"github.com/billziss-gh/golib/keyring"
"github.com/billziss-gh/golib/trace"
"github.com/billziss-gh/golib/util"
"github.com/billziss-gh/objfs/auth"
"github.com/billziss-gh/objfs/errno"
"github.com/billziss-gh/objfs/httputil"
"github.com/billziss-gh/objfs/objio"
)
// Product variables. These variables can be overriden using the go build
// -ldflags switch. For example:
//
// go build -ldflags "-X main.MyVersion=0.9"
var (
MyProductName = "objfs"
MyDescription = "Object Storage File System"
MyCopyright = "2018 Bill Zissimopoulos"
MyRepository = "https://github.com/billziss-gh/objfs"
MyVersion = "DEVEL"
)
// Configuration variables. These variables control the overall operation of objfs.
//
// The logic of initializing these variables is rather complicated:
//
// - The configuration is determined by a combination of command-line parameters
// and a configuration file. When there is a conflict between the two, the
// command-line parameters take precendence.
//
// - The configuration file is named objfs.conf and placed in the appropriate
// directory for the underlying system, unless the -config command-line parameter
// is specified. The configuration file (if it exists) stores key/value pairs and
// may also have [sections].
//
// - The process starts by creating an empty "flag map" and proceeds by merging
// key/value pairs from the different sources.
//
// - If the configuration file exists it is read and the unnamed empty section ("")
// is merged into the flag map. Then any "-storage" command line parameter
// is merged into the flag map. Then if there is a configuration section with the
// name specified by "storage" that section is merged into the flag map.
//
// - The remaining command-line options (other than -storage) are merged
// into the flag map.
//
// - Finally the flag map is used to initialize the configuration variables.
//
// For the full logic see needvar.
var (
configPath string
dataDir string
programConfig config.TypedConfig
acceptTlsCert bool
authName string
authSession auth.Session
cachePath string
credentialPath string
credentials auth.CredentialMap
keyringKind string
storage objio.ObjectStorage
storageName string
storageUri string
)
func init() {
flag.CommandLine.Init(flag.CommandLine.Name(), flag.PanicOnError)
flag.Usage = cmd.UsageFunc()
flag.StringVar(&configPath, "config", "",
"`path` to configuration file")
flag.String("datadir", "",
"`path` to supporting data and caches")
flag.BoolVar(&trace.Verbose, "v", false,
"verbose")
flag.Bool("accept-tls-cert", false,
"accept any TLS certificate presented by the server (insecure)")
flag.String("auth", "",
"auth `name` to use")
flag.String("keyring", "user",
"keyring type to use: system, user, userplain")
flag.String("credentials", "",
"auth credentials `path` (keyring:service/user or /file/path)")
flag.String("storage", defaultStorageName,
"storage `name` to access")
flag.String("storage-uri", "",
"storage `uri` to access")
}
func usage(cmd *cmd.Cmd) {
if nil == cmd {
flag.Usage()
} else {
cmd.Flag.Usage()
}
exit(2)
}
func usageWithError(err error) {
flag.Usage()
warn(err)
exit(2)
}
func initKeyring(kind string, path string) {
var key []byte
switch kind {
case "system":
case "user":
pass, err := keyring.Get("objfs", "keyring")
if nil != err {
key = make([]byte, 16)
_, err = rand.Read(key)
if nil != err {
fail(err)
}
err = keyring.Set("objfs", "keyring", string(key))
if nil != err {
fail(err)
}
} else {
key = []byte(pass)
}
fallthrough
case "userplain":
keyring.DefaultKeyring = &keyring.OverlayKeyring{
Keyrings: []keyring.Keyring{
&keyring.FileKeyring{
Path: filepath.Join(path, "keyring"),
Key: key,
},
keyring.DefaultKeyring,
},
}
default:
usageWithError(errors.New("unknown keyring type; specify -keyring in the command line"))
}
}
var needvarOnce sync.Once
func needvar(args ...interface{}) {
needvarOnce.Do(func() {
if "" == configPath {
dir, err := appdata.ConfigDir()
if nil != err {
fail(err)
}
configPath = filepath.Join(dir, "objfs.conf")
}
flagMap := config.TypedSection{}
cflag.VisitAll(nil, flagMap,
"accept-tls-cert",
"auth",
"credentials",
"datadir",
"keyring",
"storage",
"storage-uri")
c, err := util.ReadFunc(configPath, func(file *os.File) (interface{}, error) {
return config.ReadTyped(file)
})
if nil == err {
programConfig = c.(config.TypedConfig)
for k, v := range programConfig[""] {
flagMap[k] = v
}
cflag.Visit(nil, flagMap, "storage")
for k, v := range programConfig[flagMap["storage"].(string)] {
flagMap[k] = v
}
cflag.Visit(nil, flagMap,
"accept-tls-cert",
"auth",
"credentials",
"datadir",
"keyring",
"storage-uri")
} else {
programConfig = config.TypedConfig{}
}
acceptTlsCert = flagMap["accept-tls-cert"].(bool)
authName = flagMap["auth"].(string)
credentialPath = flagMap["credentials"].(string)
dataDir = flagMap["datadir"].(string)
keyringKind = flagMap["keyring"].(string)
storageName = flagMap["storage"].(string)
storageUri = flagMap["storage-uri"].(string)
if "" == dataDir {
dir, err := appdata.DataDir()
if nil != err {
fail(err)
}
dataDir = filepath.Join(dir, "objfs")
}
initKeyring(keyringKind, dataDir)
if false {
fmt.Printf("configPath=%#v\n", configPath)
fmt.Printf("dataDir=%#v\n", dataDir)
fmt.Println()
fmt.Printf("acceptTlsCert=%#v\n", acceptTlsCert)
fmt.Printf("authName=%#v\n", authName)
fmt.Printf("credentialPath=%#v\n", credentialPath)
fmt.Printf("keyringKind=%#v\n", keyringKind)
fmt.Printf("storageName=%#v\n", storageName)
fmt.Printf("storageUri=%#v\n", storageUri)
}
if acceptTlsCert {
httputil.DefaultTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
})
for _, a := range args {
switch a {
case &authName:
if "" != authName {
continue
}
needvar(&storageName)
authName = storageName
case &authSession:
if nil != authSession {
continue
}
needvar(&authName, &credentials)
a, err := auth.Registry.NewObject(authName)
if nil != err {
usageWithError(errors.New("unknown auth; specify -auth in the command line"))
}
s, err := a.(auth.Auth).Session(credentials)
if nil != err {
fail(err)
}
authSession = s
case &cachePath:
if "" != cachePath {
continue
}
needvar(&storageName)
cachePath = filepath.Join(dataDir, storageName)
case &credentialPath:
if "" != credentialPath {
continue
}
needvar(&storageName)
credentialPath = "keyring:objfs/" + storageName
case &credentials:
if nil != credentials {
continue
}
needvar(&credentialPath)
credentials, _ = auth.ReadCredentials(credentialPath)
if nil == credentials {
usageWithError(
errors.New("unknown credentials; specify -credentials in the command line"))
}
case &storageName:
if "" != storageName {
continue
}
usageWithError(errors.New("unknown storage; specify -storage in the command line"))
case &storage:
if nil != storage {
continue
}
var creds interface{}
if "" != authName {
needvar(&authSession, &storageName)
creds = authSession
} else {
needvar(&credentials, &storageName)
creds = credentials
}
s, err := objio.Registry.NewObject(storageName, storageUri, creds)
if nil != err {
fail(err)
}
storage = s.(objio.ObjectStorage)
if trace.Verbose {
storage = &objio.TraceObjectStorage{ObjectStorage: storage}
}
}
}
}
func warn(err error) {
fmt.Fprintf(os.Stderr, "error: %v (%v)\n", err, errno.ErrnoFromErr(err))
}
func fail(err error) {
warn(err)
exit(1)
}
type exitcode int
func exit(c int) {
panic(exitcode(c))
}
func run(self *cmd.CmdMap, flagSet *flag.FlagSet, args []string) (ec int) {
defer func() {
if r := recover(); nil != r {
if c, ok := r.(exitcode); ok {
ec = int(c)
} else if _, ok := r.(error); ok {
ec = 2
} else {
panic(r)
}
}
}()
flagSet.Parse(args)
arg := flagSet.Arg(0)
cmd := self.Get(arg)
if nil == cmd {
if "help" == arg {
args = flagSet.Args()[1:]
if 0 == len(args) {
flagSet.Usage()
} else {
for _, name := range args {
cmd := self.Get(name)
if nil == cmd {
continue
}
cmd.Flag.Usage()
}
}
} else {
flagSet.Usage()
}
exit(2)
}
cmd.Main(cmd, flagSet.Args()[1:])
return
}
func addcmd(self *cmd.CmdMap, name string, main func(*cmd.Cmd, []string)) (cmd *cmd.Cmd) {
c := self.Add(name, main)
c.Flag.Init(c.Flag.Name(), flag.PanicOnError)
return c
}
func main() {
ec := run(cmd.DefaultCmdMap, flag.CommandLine, os.Args[1:])
if 0 != ec {
os.Exit(ec)
}
}