1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "github.com/sirupsen/logrus"
30 BlobSignatureTTL arvados.Duration
31 BlobSigningKeyFile string
32 RequireSignatures bool
33 SystemAuthTokenFile string
35 TrashLifetime arvados.Duration
36 TrashCheckInterval arvados.Duration
40 TLSCertificateFile string
46 systemAuthToken string
47 debugLogf func(string, ...interface{})
49 ManagementToken string
53 theConfig = DefaultConfig()
54 formatter = map[string]logrus.Formatter{
55 "text": &logrus.TextFormatter{
57 TimestampFormat: rfc3339NanoFixed,
59 "json": &logrus.JSONFormatter{
60 TimestampFormat: rfc3339NanoFixed,
63 log = logrus.StandardLogger()
66 const rfc3339NanoFixed = "2006-01-02T15:04:05.000000000Z07:00"
68 // DefaultConfig returns the default configuration.
69 func DefaultConfig() *Config {
74 RequireSignatures: true,
75 BlobSignatureTTL: arvados.Duration(14 * 24 * time.Hour),
76 TrashLifetime: arvados.Duration(14 * 24 * time.Hour),
77 TrashCheckInterval: arvados.Duration(24 * time.Hour),
82 // Start should be called exactly once: after setting all public
83 // fields, and before using the config.
84 func (cfg *Config) Start() error {
86 log.Level = logrus.DebugLevel
87 cfg.debugLogf = log.Printf
88 cfg.debugLogf("debugging enabled")
90 log.Level = logrus.InfoLevel
91 cfg.debugLogf = func(string, ...interface{}) {}
94 f := formatter[strings.ToLower(cfg.LogFormat)]
96 return fmt.Errorf(`unsupported log format %q (try "text" or "json")`, cfg.LogFormat)
100 if cfg.MaxBuffers < 0 {
101 return fmt.Errorf("MaxBuffers must be greater than zero")
103 bufs = newBufferPool(cfg.MaxBuffers, BlockSize)
105 if cfg.MaxRequests < 1 {
106 cfg.MaxRequests = cfg.MaxBuffers * 2
107 log.Printf("MaxRequests <1 or not specified; defaulting to MaxBuffers * 2 == %d", cfg.MaxRequests)
110 if cfg.BlobSigningKeyFile != "" {
111 buf, err := ioutil.ReadFile(cfg.BlobSigningKeyFile)
113 return fmt.Errorf("reading blob signing key file: %s", err)
115 cfg.blobSigningKey = bytes.TrimSpace(buf)
116 if len(cfg.blobSigningKey) == 0 {
117 return fmt.Errorf("blob signing key file %q is empty", cfg.BlobSigningKeyFile)
119 } else if cfg.RequireSignatures {
120 return fmt.Errorf("cannot enable RequireSignatures (-enforce-permissions) without a blob signing key")
122 log.Println("Running without a blob signing key. Block locators " +
123 "returned by this server will not be signed, and will be rejected " +
124 "by a server that enforces permissions.")
125 log.Println("To fix this, use the BlobSigningKeyFile config entry.")
128 if fn := cfg.SystemAuthTokenFile; fn != "" {
129 buf, err := ioutil.ReadFile(fn)
131 return fmt.Errorf("cannot read system auth token file %q: %s", fn, err)
133 cfg.systemAuthToken = strings.TrimSpace(string(buf))
136 if cfg.EnableDelete {
137 log.Print("Trash/delete features are enabled. WARNING: this has not " +
138 "been extensively tested. You should disable this unless you can afford to lose data.")
141 if len(cfg.Volumes) == 0 {
142 if (&unixVolumeAdder{cfg}).Discover() == 0 {
143 return fmt.Errorf("no volumes found")
146 for _, v := range cfg.Volumes {
147 if err := v.Start(); err != nil {
148 return fmt.Errorf("volume %s: %s", v, err)
150 log.Printf("Using volume %v (writable=%v)", v, v.Writable())
155 // VolumeTypes is built up by init() funcs in the source files that
156 // define the volume types.
157 var VolumeTypes = []func() VolumeWithExamples{}
159 type VolumeList []Volume
161 // UnmarshalJSON -- given an array of objects -- deserializes each
162 // object as the volume type indicated by the object's Type field.
163 func (vl *VolumeList) UnmarshalJSON(data []byte) error {
164 typeMap := map[string]func() VolumeWithExamples{}
165 for _, factory := range VolumeTypes {
166 t := factory().Type()
167 if _, ok := typeMap[t]; ok {
168 log.Fatal("volume type %+q is claimed by multiple VolumeTypes")
173 var mapList []map[string]interface{}
174 err := json.Unmarshal(data, &mapList)
178 for _, mapIn := range mapList {
179 typeIn, ok := mapIn["Type"].(string)
181 return fmt.Errorf("invalid volume type %+v", mapIn["Type"])
183 factory, ok := typeMap[typeIn]
185 return fmt.Errorf("unsupported volume type %+q", typeIn)
187 data, err := json.Marshal(mapIn)
192 err = json.Unmarshal(data, vol)
196 *vl = append(*vl, vol)
201 // MarshalJSON adds a "Type" field to each volume corresponding to its
203 func (vl *VolumeList) MarshalJSON() ([]byte, error) {
205 for _, vs := range *vl {
206 j, err := json.Marshal(vs)
211 data = append(data, byte(','))
213 t, err := json.Marshal(vs.Type())
217 data = append(data, j[0])
218 data = append(data, []byte(`"Type":`)...)
219 data = append(data, t...)
220 data = append(data, byte(','))
221 data = append(data, j[1:]...)
223 return append(data, byte(']')), nil