Merge branch '15529-federated-user-accounts' refs #15529
[arvados.git] / lib / config / deprecated.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package config
6
7 import (
8         "fmt"
9         "io/ioutil"
10         "os"
11         "strings"
12
13         "git.curoverse.com/arvados.git/sdk/go/arvados"
14         "github.com/ghodss/yaml"
15 )
16
17 type deprRequestLimits struct {
18         MaxItemsPerResponse            *int
19         MultiClusterRequestConcurrency *int
20 }
21
22 type deprCluster struct {
23         RequestLimits deprRequestLimits
24         NodeProfiles  map[string]nodeProfile
25 }
26
27 type deprecatedConfig struct {
28         Clusters map[string]deprCluster
29 }
30
31 type nodeProfile struct {
32         Controller    systemServiceInstance `json:"arvados-controller"`
33         Health        systemServiceInstance `json:"arvados-health"`
34         Keepbalance   systemServiceInstance `json:"keep-balance"`
35         Keepproxy     systemServiceInstance `json:"keepproxy"`
36         Keepstore     systemServiceInstance `json:"keepstore"`
37         Keepweb       systemServiceInstance `json:"keep-web"`
38         Nodemanager   systemServiceInstance `json:"arvados-node-manager"`
39         DispatchCloud systemServiceInstance `json:"arvados-dispatch-cloud"`
40         RailsAPI      systemServiceInstance `json:"arvados-api-server"`
41         Websocket     systemServiceInstance `json:"arvados-ws"`
42         Workbench1    systemServiceInstance `json:"arvados-workbench"`
43 }
44
45 type systemServiceInstance struct {
46         Listen   string
47         TLS      bool
48         Insecure bool
49 }
50
51 func (ldr *Loader) applyDeprecatedConfig(cfg *arvados.Config) error {
52         var dc deprecatedConfig
53         err := yaml.Unmarshal(ldr.configdata, &dc)
54         if err != nil {
55                 return err
56         }
57         hostname, err := os.Hostname()
58         if err != nil {
59                 return err
60         }
61         for id, dcluster := range dc.Clusters {
62                 cluster, ok := cfg.Clusters[id]
63                 if !ok {
64                         return fmt.Errorf("can't load legacy config %q that is not present in current config", id)
65                 }
66                 for name, np := range dcluster.NodeProfiles {
67                         if name == "*" || name == os.Getenv("ARVADOS_NODE_PROFILE") || name == hostname {
68                                 name = "localhost"
69                         } else if ldr.Logger != nil {
70                                 ldr.Logger.Warnf("overriding Clusters.%s.Services using Clusters.%s.NodeProfiles.%s (guessing %q is a hostname)", id, id, name, name)
71                         }
72                         applyDeprecatedNodeProfile(name, np.RailsAPI, &cluster.Services.RailsAPI)
73                         applyDeprecatedNodeProfile(name, np.Controller, &cluster.Services.Controller)
74                         applyDeprecatedNodeProfile(name, np.DispatchCloud, &cluster.Services.DispatchCloud)
75                 }
76                 if dst, n := &cluster.API.MaxItemsPerResponse, dcluster.RequestLimits.MaxItemsPerResponse; n != nil && *n != *dst {
77                         *dst = *n
78                 }
79                 if dst, n := &cluster.API.MaxRequestAmplification, dcluster.RequestLimits.MultiClusterRequestConcurrency; n != nil && *n != *dst {
80                         *dst = *n
81                 }
82                 cfg.Clusters[id] = cluster
83         }
84         return nil
85 }
86
87 func applyDeprecatedNodeProfile(hostname string, ssi systemServiceInstance, svc *arvados.Service) {
88         scheme := "https"
89         if !ssi.TLS {
90                 scheme = "http"
91         }
92         if svc.InternalURLs == nil {
93                 svc.InternalURLs = map[arvados.URL]arvados.ServiceInstance{}
94         }
95         host := ssi.Listen
96         if host == "" {
97                 return
98         }
99         if strings.HasPrefix(host, ":") {
100                 host = hostname + host
101         }
102         svc.InternalURLs[arvados.URL{Scheme: scheme, Host: host}] = arvados.ServiceInstance{}
103 }
104
105 const defaultKeepstoreConfigPath = "/etc/arvados/keepstore/keepstore.yml"
106
107 type oldKeepstoreConfig struct {
108         Debug *bool
109 }
110
111 func (ldr *Loader) loadOldConfigHelper(component, path string, target interface{}) error {
112         if path == "" {
113                 return nil
114         }
115         buf, err := ioutil.ReadFile(path)
116         if err != nil {
117                 return err
118         }
119
120         ldr.Logger.Warnf("you should remove the legacy %v config file (%s) after migrating all config keys to the cluster configuration file (%s)", component, path, ldr.Path)
121
122         err = yaml.Unmarshal(buf, target)
123         if err != nil {
124                 return fmt.Errorf("%s: %s", path, err)
125         }
126         return nil
127 }
128
129 // update config using values from an old-style keepstore config file.
130 func (ldr *Loader) loadOldKeepstoreConfig(cfg *arvados.Config) error {
131         if ldr.KeepstorePath == "" {
132                 return nil
133         }
134         var oc oldKeepstoreConfig
135         err := ldr.loadOldConfigHelper("keepstore", ldr.KeepstorePath, &oc)
136         if os.IsNotExist(err) && (ldr.KeepstorePath == defaultKeepstoreConfigPath) {
137                 return nil
138         } else if err != nil {
139                 return err
140         }
141
142         cluster, err := cfg.GetCluster("")
143         if err != nil {
144                 return err
145         }
146
147         if v := oc.Debug; v == nil {
148         } else if *v && cluster.SystemLogs.LogLevel != "debug" {
149                 cluster.SystemLogs.LogLevel = "debug"
150         } else if !*v && cluster.SystemLogs.LogLevel != "info" {
151                 cluster.SystemLogs.LogLevel = "info"
152         }
153
154         cfg.Clusters[cluster.ClusterID] = *cluster
155         return nil
156 }
157
158 type oldCrunchDispatchSlurmConfig struct {
159         Client *arvados.Client
160
161         SbatchArguments *[]string
162         PollPeriod      *arvados.Duration
163         PrioritySpread  *int64
164
165         // crunch-run command to invoke. The container UUID will be
166         // appended. If nil, []string{"crunch-run"} will be used.
167         //
168         // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
169         CrunchRunCommand *[]string
170
171         // Extra RAM to reserve (in Bytes) for SLURM job, in addition
172         // to the amount specified in the container's RuntimeConstraints
173         ReserveExtraRAM *int64
174
175         // Minimum time between two attempts to run the same container
176         MinRetryPeriod *arvados.Duration
177
178         // Batch size for container queries
179         BatchSize *int64
180 }
181
182 const defaultCrunchDispatchSlurmConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
183
184 func loadOldClientConfig(cluster *arvados.Cluster, client *arvados.Client) {
185         if client == nil {
186                 return
187         }
188         if client.APIHost != "" {
189                 cluster.Services.Controller.ExternalURL.Host = client.APIHost
190         }
191         if client.Scheme != "" {
192                 cluster.Services.Controller.ExternalURL.Scheme = client.Scheme
193         } else {
194                 cluster.Services.Controller.ExternalURL.Scheme = "https"
195         }
196         if client.AuthToken != "" {
197                 cluster.SystemRootToken = client.AuthToken
198         }
199         cluster.TLS.Insecure = client.Insecure
200         ks := ""
201         for i, u := range client.KeepServiceURIs {
202                 if i > 0 {
203                         ks += " "
204                 }
205                 ks += u
206         }
207         cluster.Containers.SLURM.SbatchEnvironmentVariables = map[string]string{"ARVADOS_KEEP_SERVICES": ks}
208 }
209
210 // update config using values from an crunch-dispatch-slurm config file.
211 func (ldr *Loader) loadOldCrunchDispatchSlurmConfig(cfg *arvados.Config) error {
212         if ldr.CrunchDispatchSlurmPath == "" {
213                 return nil
214         }
215         var oc oldCrunchDispatchSlurmConfig
216         err := ldr.loadOldConfigHelper("crunch-dispatch-slurm", ldr.CrunchDispatchSlurmPath, &oc)
217         if os.IsNotExist(err) && (ldr.CrunchDispatchSlurmPath == defaultCrunchDispatchSlurmConfigPath) {
218                 return nil
219         } else if err != nil {
220                 return err
221         }
222
223         cluster, err := cfg.GetCluster("")
224         if err != nil {
225                 return err
226         }
227
228         loadOldClientConfig(cluster, oc.Client)
229
230         if oc.SbatchArguments != nil {
231                 cluster.Containers.SLURM.SbatchArgumentsList = *oc.SbatchArguments
232         }
233         if oc.PollPeriod != nil {
234                 cluster.Containers.CloudVMs.PollInterval = *oc.PollPeriod
235         }
236         if oc.PrioritySpread != nil {
237                 cluster.Containers.SLURM.PrioritySpread = *oc.PrioritySpread
238         }
239         if oc.CrunchRunCommand != nil {
240                 if len(*oc.CrunchRunCommand) >= 1 {
241                         cluster.Containers.CrunchRunCommand = (*oc.CrunchRunCommand)[0]
242                 }
243                 if len(*oc.CrunchRunCommand) >= 2 {
244                         cluster.Containers.CrunchRunArgumentsList = (*oc.CrunchRunCommand)[1:]
245                 }
246         }
247         if oc.ReserveExtraRAM != nil {
248                 cluster.Containers.ReserveExtraRAM = arvados.ByteSize(*oc.ReserveExtraRAM)
249         }
250         if oc.MinRetryPeriod != nil {
251                 cluster.Containers.MinRetryPeriod = *oc.MinRetryPeriod
252         }
253         if oc.BatchSize != nil {
254                 cluster.API.MaxItemsPerResponse = int(*oc.BatchSize)
255         }
256
257         cfg.Clusters[cluster.ClusterID] = *cluster
258         return nil
259 }
260
261 type oldWsConfig struct {
262         Client       *arvados.Client
263         Postgres     *arvados.PostgreSQLConnection
264         PostgresPool *int
265         Listen       *string
266         LogLevel     *string
267         LogFormat    *string
268
269         PingTimeout      *arvados.Duration
270         ClientEventQueue *int
271         ServerEventQueue *int
272
273         ManagementToken *string
274 }
275
276 const defaultWebsocketConfigPath = "/etc/arvados/ws/ws.yml"
277
278 // update config using values from an crunch-dispatch-slurm config file.
279 func (ldr *Loader) loadOldWebsocketConfig(cfg *arvados.Config) error {
280         if ldr.WebsocketPath == "" {
281                 return nil
282         }
283         var oc oldWsConfig
284         err := ldr.loadOldConfigHelper("arvados-ws", ldr.WebsocketPath, &oc)
285         if os.IsNotExist(err) && ldr.WebsocketPath == defaultWebsocketConfigPath {
286                 return nil
287         } else if err != nil {
288                 return err
289         }
290
291         cluster, err := cfg.GetCluster("")
292         if err != nil {
293                 return err
294         }
295
296         loadOldClientConfig(cluster, oc.Client)
297
298         if oc.Postgres != nil {
299                 cluster.PostgreSQL.Connection = *oc.Postgres
300         }
301         if oc.PostgresPool != nil {
302                 cluster.PostgreSQL.ConnectionPool = *oc.PostgresPool
303         }
304         if oc.Listen != nil {
305                 cluster.Services.Websocket.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
306         }
307         if oc.LogLevel != nil {
308                 cluster.SystemLogs.LogLevel = *oc.LogLevel
309         }
310         if oc.LogFormat != nil {
311                 cluster.SystemLogs.Format = *oc.LogFormat
312         }
313         if oc.PingTimeout != nil {
314                 cluster.API.SendTimeout = *oc.PingTimeout
315         }
316         if oc.ClientEventQueue != nil {
317                 cluster.API.WebsocketClientEventQueue = *oc.ClientEventQueue
318         }
319         if oc.ServerEventQueue != nil {
320                 cluster.API.WebsocketServerEventQueue = *oc.ServerEventQueue
321         }
322         if oc.ManagementToken != nil {
323                 cluster.ManagementToken = *oc.ManagementToken
324         }
325
326         cfg.Clusters[cluster.ClusterID] = *cluster
327         return nil
328 }
329
330 type oldKeepProxyConfig struct {
331         Client          *arvados.Client
332         Listen          *string
333         DisableGet      *bool
334         DisablePut      *bool
335         DefaultReplicas *int
336         Timeout         *arvados.Duration
337         PIDFile         *string
338         Debug           *bool
339         ManagementToken *string
340 }
341
342 const defaultKeepproxyConfigPath = "/etc/arvados/keepproxy/keepproxy.yml"
343
344 func (ldr *Loader) loadOldKeepproxyConfig(cfg *arvados.Config) error {
345         if ldr.KeepproxyPath == "" {
346                 return nil
347         }
348         var oc oldKeepProxyConfig
349         err := ldr.loadOldConfigHelper("keepproxy", ldr.KeepproxyPath, &oc)
350         if os.IsNotExist(err) && ldr.KeepproxyPath == defaultKeepproxyConfigPath {
351                 return nil
352         } else if err != nil {
353                 return err
354         }
355
356         cluster, err := cfg.GetCluster("")
357         if err != nil {
358                 return err
359         }
360
361         loadOldClientConfig(cluster, oc.Client)
362
363         if oc.Listen != nil {
364                 cluster.Services.Keepproxy.InternalURLs[arvados.URL{Host: *oc.Listen}] = arvados.ServiceInstance{}
365         }
366         if oc.DefaultReplicas != nil {
367                 cluster.Collections.DefaultReplication = *oc.DefaultReplicas
368         }
369         if oc.Timeout != nil {
370                 cluster.API.KeepServiceRequestTimeout = *oc.Timeout
371         }
372         if oc.Debug != nil {
373                 if *oc.Debug && cluster.SystemLogs.LogLevel != "debug" {
374                         cluster.SystemLogs.LogLevel = "debug"
375                 } else if !*oc.Debug && cluster.SystemLogs.LogLevel != "info" {
376                         cluster.SystemLogs.LogLevel = "info"
377                 }
378         }
379         if oc.ManagementToken != nil {
380                 cluster.ManagementToken = *oc.ManagementToken
381         }
382
383         // The following legacy options are no longer supported. If they are set to
384         // true or PIDFile has a value, error out and notify the user
385         unsupportedEntry := func(cfgEntry string) error {
386                 return fmt.Errorf("the keepproxy %s configuration option is no longer supported, please remove it from your configuration file", cfgEntry)
387         }
388         if oc.DisableGet != nil && *oc.DisableGet {
389                 return unsupportedEntry("DisableGet")
390         }
391         if oc.DisablePut != nil && *oc.DisablePut {
392                 return unsupportedEntry("DisablePut")
393         }
394         if oc.PIDFile != nil && *oc.PIDFile != "" {
395                 return unsupportedEntry("PIDFile")
396         }
397
398         cfg.Clusters[cluster.ClusterID] = *cluster
399         return nil
400 }
401
402 const defaultKeepWebConfigPath = "/etc/arvados/keep-web/keep-web.yml"
403
404 type oldKeepWebConfig struct {
405         Client *arvados.Client
406
407         Listen string
408
409         AnonymousTokens    []string
410         AttachmentOnlyHost string
411         TrustAllContent    bool
412
413         Cache struct {
414                 TTL                  arvados.Duration
415                 UUIDTTL              arvados.Duration
416                 MaxCollectionEntries int
417                 MaxCollectionBytes   int64
418                 MaxPermissionEntries int
419                 MaxUUIDEntries       int
420         }
421
422         // Hack to support old command line flag, which is a bool
423         // meaning "get actual token from environment".
424         deprecatedAllowAnonymous bool
425
426         // Authorization token to be included in all health check requests.
427         ManagementToken string
428 }
429
430 func (ldr *Loader) loadOldKeepWebConfig(cfg *arvados.Config) error {
431         if ldr.KeepWebPath == "" {
432                 return nil
433         }
434         var oc oldKeepWebConfig
435         err := ldr.loadOldConfigHelper("keep-web", ldr.KeepWebPath, &oc)
436         if os.IsNotExist(err) && ldr.KeepWebPath == defaultKeepWebConfigPath {
437                 return nil
438         } else if err != nil {
439                 return err
440         }
441
442         cluster, err := cfg.GetCluster("")
443         if err != nil {
444                 return err
445         }
446
447         loadOldClientConfig(cluster, oc.Client)
448
449         cluster.Services.WebDAV.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
450         cluster.Services.WebDAVDownload.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
451         cluster.Services.WebDAVDownload.ExternalURL = arvados.URL{Host: oc.AttachmentOnlyHost}
452         cluster.TLS.Insecure = oc.Client.Insecure
453         cluster.ManagementToken = oc.ManagementToken
454         cluster.Collections.TrustAllContent = oc.TrustAllContent
455         cluster.Collections.WebDAVCache.TTL = oc.Cache.TTL
456         cluster.Collections.WebDAVCache.UUIDTTL = oc.Cache.UUIDTTL
457         cluster.Collections.WebDAVCache.MaxCollectionEntries = oc.Cache.MaxCollectionEntries
458         cluster.Collections.WebDAVCache.MaxCollectionBytes = oc.Cache.MaxCollectionBytes
459         cluster.Collections.WebDAVCache.MaxPermissionEntries = oc.Cache.MaxPermissionEntries
460         cluster.Collections.WebDAVCache.MaxUUIDEntries = oc.Cache.MaxUUIDEntries
461         if len(oc.AnonymousTokens) > 0 {
462                 cluster.Users.AnonymousUserToken = oc.AnonymousTokens[0]
463                 if len(oc.AnonymousTokens) > 1 {
464                         ldr.Logger.Warn("More than 1 anonymous tokens configured, using only the first and discarding the rest.")
465                 }
466         }
467
468         cfg.Clusters[cluster.ClusterID] = *cluster
469         return nil
470 }
471
472 const defaultGitHttpdConfigPath = "/etc/arvados/git-httpd/git-httpd.yml"
473
474 type oldGitHttpdConfig struct {
475         Client          *arvados.Client
476         Listen          string
477         GitCommand      string
478         GitoliteHome    string
479         RepoRoot        string
480         ManagementToken string
481 }
482
483 func (ldr *Loader) loadOldGitHttpdConfig(cfg *arvados.Config) error {
484         if ldr.GitHttpdPath == "" {
485                 return nil
486         }
487         var oc oldGitHttpdConfig
488         err := ldr.loadOldConfigHelper("arv-git-httpd", ldr.GitHttpdPath, &oc)
489         if os.IsNotExist(err) && ldr.GitHttpdPath == defaultGitHttpdConfigPath {
490                 return nil
491         } else if err != nil {
492                 return err
493         }
494
495         cluster, err := cfg.GetCluster("")
496         if err != nil {
497                 return err
498         }
499
500         loadOldClientConfig(cluster, oc.Client)
501
502         cluster.Services.GitHTTP.InternalURLs[arvados.URL{Host: oc.Listen}] = arvados.ServiceInstance{}
503         cluster.TLS.Insecure = oc.Client.Insecure
504         cluster.ManagementToken = oc.ManagementToken
505         cluster.Git.GitCommand = oc.GitCommand
506         cluster.Git.GitoliteHome = oc.GitoliteHome
507         cluster.Git.Repositories = oc.RepoRoot
508
509         cfg.Clusters[cluster.ClusterID] = *cluster
510         return nil
511 }