14714: Adds keep-balance to cluster config loading
[arvados.git] / lib / config / export.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         "encoding/json"
9         "errors"
10         "fmt"
11         "io"
12         "strings"
13
14         "git.curoverse.com/arvados.git/sdk/go/arvados"
15 )
16
17 // ExportJSON writes a JSON object with the safe (non-secret) portions
18 // of the cluster config to w.
19 func ExportJSON(w io.Writer, cluster *arvados.Cluster) error {
20         buf, err := json.Marshal(cluster)
21         if err != nil {
22                 return err
23         }
24         var m map[string]interface{}
25         err = json.Unmarshal(buf, &m)
26         if err != nil {
27                 return err
28         }
29
30         // ClusterID is not marshalled by default (see `json:"-"`).
31         // Add it back here so it is included in the exported config.
32         m["ClusterID"] = cluster.ClusterID
33         err = redactUnsafe(m, "", "")
34         if err != nil {
35                 return err
36         }
37         return json.NewEncoder(w).Encode(m)
38 }
39
40 // whitelist classifies configs as safe/unsafe to reveal to
41 // unauthenticated clients.
42 //
43 // Every config entry must either be listed explicitly here along with
44 // all of its parent keys (e.g., "API" + "API.RequestTimeout"), or
45 // have an ancestor listed as false (e.g.,
46 // "PostgreSQL.Connection.password" has an ancestor
47 // "PostgreSQL.Connection" with a false value). Otherwise, it is a bug
48 // which should be caught by tests.
49 //
50 // Example: API.RequestTimeout is safe because whitelist["API"] == and
51 // whitelist["API.RequestTimeout"] == true.
52 //
53 // Example: PostgreSQL.Connection.password is not safe because
54 // whitelist["PostgreSQL.Connection"] == false.
55 //
56 // Example: PostgreSQL.BadKey would cause an error because
57 // whitelist["PostgreSQL"] isn't false, and neither
58 // whitelist["PostgreSQL.BadKey"] nor whitelist["PostgreSQL.*"]
59 // exists.
60 var whitelist = map[string]bool{
61         // | sort -t'"' -k2,2
62         "ClusterID":                                    true,
63         "API":                                          true,
64         "API.AsyncPermissionsUpdateInterval":           false,
65         "API.DisabledAPIs":                             false,
66         "API.MaxConcurrentRequests":                    false,
67         "API.MaxIndexDatabaseRead":                     false,
68         "API.MaxItemsPerResponse":                      true,
69         "API.MaxKeepBlobBuffers":                       false,
70         "API.MaxRequestAmplification":                  false,
71         "API.MaxRequestSize":                           true,
72         "API.RailsSessionSecretToken":                  false,
73         "API.RequestTimeout":                           true,
74         "API.WebsocketClientEventQueue":                false,
75         "API.SendTimeout":                              true,
76         "API.WebsocketServerEventQueue":                false,
77         "API.KeepServiceRequestTimeout":                false,
78         "AuditLogs":                                    false,
79         "AuditLogs.MaxAge":                             false,
80         "AuditLogs.MaxDeleteBatch":                     false,
81         "AuditLogs.UnloggedAttributes":                 false,
82         "Collections":                                  true,
83         "Collections.BlobSigning":                      true,
84         "Collections.BlobSigningKey":                   false,
85         "Collections.BlobSigningTTL":                   true,
86         "Collections.BlobTrash":                        false,
87         "Collections.BlobTrashLifetime":                false,
88         "Collections.BlobTrashConcurrency":             false,
89         "Collections.BlobTrashCheckInterval":           false,
90         "Collections.BlobDeleteConcurrency":            false,
91         "Collections.BlobReplicateConcurrency":         false,
92         "Collections.CollectionVersioning":             false,
93         "Collections.DefaultReplication":               true,
94         "Collections.DefaultTrashLifetime":             true,
95         "Collections.ManagedProperties":                true,
96         "Collections.ManagedProperties.*":              true,
97         "Collections.ManagedProperties.*.*":            true,
98         "Collections.PreserveVersionIfIdle":            true,
99         "Collections.TrashSweepInterval":               false,
100         "Collections.TrustAllContent":                  false,
101         "Collections.WebDAVCache":                      false,
102         "Collections.BalanceCollectionBatch":           false,
103         "Collections.BalancePeriod":                    false,
104         "Collections.BlobMissingReport":                false,
105         "Collections.BalanceCollectionBuffers":         false,
106         "Containers":                                   true,
107         "Containers.CloudVMs":                          false,
108         "Containers.CrunchRunCommand":                  false,
109         "Containers.CrunchRunArgumentsList":            false,
110         "Containers.DefaultKeepCacheRAM":               true,
111         "Containers.DispatchPrivateKey":                false,
112         "Containers.JobsAPI":                           true,
113         "Containers.JobsAPI.Enable":                    true,
114         "Containers.JobsAPI.GitInternalDir":            false,
115         "Containers.Logging":                           false,
116         "Containers.LogReuseDecisions":                 false,
117         "Containers.MaxComputeVMs":                     false,
118         "Containers.MaxDispatchAttempts":               false,
119         "Containers.MaxRetryAttempts":                  true,
120         "Containers.MinRetryPeriod":                    true,
121         "Containers.ReserveExtraRAM":                   true,
122         "Containers.SLURM":                             false,
123         "Containers.StaleLockTimeout":                  false,
124         "Containers.SupportedDockerImageFormats":       true,
125         "Containers.SupportedDockerImageFormats.*":     true,
126         "Containers.UsePreemptibleInstances":           true,
127         "EnableBetaController14287":                    false,
128         "Git":                                          false,
129         "InstanceTypes":                                true,
130         "InstanceTypes.*":                              true,
131         "InstanceTypes.*.*":                            true,
132         "Login":                                        true,
133         "Login.ProviderAppSecret":                      false,
134         "Login.ProviderAppID":                          false,
135         "Login.LoginCluster":                           true,
136         "Login.RemoteTokenRefresh":                     true,
137         "Mail":                                         false,
138         "ManagementToken":                              false,
139         "PostgreSQL":                                   false,
140         "RemoteClusters":                               true,
141         "RemoteClusters.*":                             true,
142         "RemoteClusters.*.ActivateUsers":               true,
143         "RemoteClusters.*.Host":                        true,
144         "RemoteClusters.*.Insecure":                    true,
145         "RemoteClusters.*.Proxy":                       true,
146         "RemoteClusters.*.Scheme":                      true,
147         "Services":                                     true,
148         "Services.*":                                   true,
149         "Services.*.ExternalURL":                       true,
150         "Services.*.InternalURLs":                      false,
151         "SystemLogs":                                   false,
152         "SystemRootToken":                              false,
153         "TLS":                                          false,
154         "Users":                                        true,
155         "Users.AnonymousUserToken":                     true,
156         "Users.AdminNotifierEmailFrom":                 false,
157         "Users.AutoAdminFirstUser":                     false,
158         "Users.AutoAdminUserWithEmail":                 false,
159         "Users.AutoSetupNewUsers":                      false,
160         "Users.AutoSetupNewUsersWithRepository":        false,
161         "Users.AutoSetupNewUsersWithVmUUID":            false,
162         "Users.AutoSetupUsernameBlacklist":             false,
163         "Users.EmailSubjectPrefix":                     false,
164         "Users.NewInactiveUserNotificationRecipients":  false,
165         "Users.NewUserNotificationRecipients":          false,
166         "Users.NewUsersAreActive":                      false,
167         "Users.UserNotifierEmailFrom":                  false,
168         "Users.UserProfileNotificationAddress":         false,
169         "Volumes":                                      true,
170         "Volumes.*":                                    true,
171         "Volumes.*.*":                                  false,
172         "Volumes.*.AccessViaHosts":                     true,
173         "Volumes.*.AccessViaHosts.*":                   true,
174         "Volumes.*.AccessViaHosts.*.ReadOnly":          true,
175         "Volumes.*.ReadOnly":                           true,
176         "Volumes.*.Replication":                        true,
177         "Volumes.*.StorageClasses":                     true,
178         "Volumes.*.StorageClasses.*":                   false,
179         "Workbench":                                    true,
180         "Workbench.ActivationContactLink":              false,
181         "Workbench.APIClientConnectTimeout":            true,
182         "Workbench.APIClientReceiveTimeout":            true,
183         "Workbench.APIResponseCompression":             true,
184         "Workbench.ApplicationMimetypesWithViewIcon":   true,
185         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
186         "Workbench.ArvadosDocsite":                     true,
187         "Workbench.ArvadosPublicDataDocURL":            true,
188         "Workbench.DefaultOpenIdPrefix":                false,
189         "Workbench.EnableGettingStartedPopup":          true,
190         "Workbench.EnablePublicProjectsPage":           true,
191         "Workbench.FileViewersConfigURL":               true,
192         "Workbench.LogViewerMaxBytes":                  true,
193         "Workbench.MultiSiteSearch":                    true,
194         "Workbench.ProfilingEnabled":                   true,
195         "Workbench.Repositories":                       false,
196         "Workbench.RepositoryCache":                    false,
197         "Workbench.RunningJobLogRecordsToFetch":        true,
198         "Workbench.SecretKeyBase":                      false,
199         "Workbench.ShowRecentCollectionsOnDashboard":   true,
200         "Workbench.ShowUserAgreementInline":            true,
201         "Workbench.ShowUserNotifications":              true,
202         "Workbench.SiteName":                           true,
203         "Workbench.Theme":                              true,
204         "Workbench.UserProfileFormFields":              true,
205         "Workbench.UserProfileFormFields.*":            true,
206         "Workbench.UserProfileFormFields.*.*":          true,
207         "Workbench.UserProfileFormFields.*.*.*":        true,
208         "Workbench.UserProfileFormMessage":             true,
209         "Workbench.VocabularyURL":                      true,
210 }
211
212 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
213         var errs []string
214         for k, v := range m {
215                 lookupKey := k
216                 safe, ok := whitelist[lookupPrefix+k]
217                 if !ok {
218                         lookupKey = "*"
219                         safe, ok = whitelist[lookupPrefix+"*"]
220                 }
221                 if !ok {
222                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
223                         continue
224                 }
225                 if !safe {
226                         delete(m, k)
227                         continue
228                 }
229                 if v, ok := v.(map[string]interface{}); ok {
230                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
231                         if err != nil {
232                                 errs = append(errs, err.Error())
233                         }
234                 }
235         }
236         if len(errs) > 0 {
237                 return errors.New(strings.Join(errs, "\n"))
238         }
239         return nil
240 }