Merge branch '16314-testuserdb'
[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.arvados.org/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         "API":                                          true,
63         "API.AsyncPermissionsUpdateInterval":           false,
64         "API.DisabledAPIs":                             false,
65         "API.KeepServiceRequestTimeout":                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.SendTimeout":                              true,
75         "API.WebsocketClientEventQueue":                false,
76         "API.WebsocketServerEventQueue":                false,
77         "AuditLogs":                                    false,
78         "AuditLogs.MaxAge":                             false,
79         "AuditLogs.MaxDeleteBatch":                     false,
80         "AuditLogs.UnloggedAttributes":                 false,
81         "ClusterID":                                    true,
82         "Collections":                                  true,
83         "Collections.BalanceCollectionBatch":           false,
84         "Collections.BalanceCollectionBuffers":         false,
85         "Collections.BalancePeriod":                    false,
86         "Collections.BalanceTimeout":                   false,
87         "Collections.BlobDeleteConcurrency":            false,
88         "Collections.BlobMissingReport":                false,
89         "Collections.BlobReplicateConcurrency":         false,
90         "Collections.BlobSigning":                      true,
91         "Collections.BlobSigningKey":                   false,
92         "Collections.BlobSigningTTL":                   true,
93         "Collections.BlobTrash":                        false,
94         "Collections.BlobTrashCheckInterval":           false,
95         "Collections.BlobTrashConcurrency":             false,
96         "Collections.BlobTrashLifetime":                false,
97         "Collections.CollectionVersioning":             false,
98         "Collections.DefaultReplication":               true,
99         "Collections.DefaultTrashLifetime":             true,
100         "Collections.ForwardSlashNameSubstitution":     true,
101         "Collections.ManagedProperties":                true,
102         "Collections.ManagedProperties.*":              true,
103         "Collections.ManagedProperties.*.*":            true,
104         "Collections.PreserveVersionIfIdle":            true,
105         "Collections.S3FolderObjects":                  true,
106         "Collections.TrashSweepInterval":               false,
107         "Collections.TrustAllContent":                  false,
108         "Collections.WebDAVCache":                      false,
109         "Containers":                                   true,
110         "Containers.CloudVMs":                          false,
111         "Containers.CrunchRunArgumentsList":            false,
112         "Containers.CrunchRunCommand":                  false,
113         "Containers.DefaultKeepCacheRAM":               true,
114         "Containers.DispatchPrivateKey":                false,
115         "Containers.JobsAPI":                           true,
116         "Containers.JobsAPI.Enable":                    true,
117         "Containers.JobsAPI.GitInternalDir":            false,
118         "Containers.Logging":                           false,
119         "Containers.LogReuseDecisions":                 false,
120         "Containers.MaxComputeVMs":                     false,
121         "Containers.MaxDispatchAttempts":               false,
122         "Containers.MaxRetryAttempts":                  true,
123         "Containers.MinRetryPeriod":                    true,
124         "Containers.ReserveExtraRAM":                   true,
125         "Containers.SLURM":                             false,
126         "Containers.StaleLockTimeout":                  false,
127         "Containers.SupportedDockerImageFormats":       true,
128         "Containers.SupportedDockerImageFormats.*":     true,
129         "Containers.UsePreemptibleInstances":           true,
130         "ForceLegacyAPI14":                             false,
131         "Git":                                          false,
132         "InstanceTypes":                                true,
133         "InstanceTypes.*":                              true,
134         "InstanceTypes.*.*":                            true,
135         "Login":                                        true,
136         "Login.Google":                                 true,
137         "Login.Google.AlternateEmailAddresses":         false,
138         "Login.Google.ClientID":                        false,
139         "Login.Google.ClientSecret":                    false,
140         "Login.Google.Enable":                          true,
141         "Login.LDAP":                                   true,
142         "Login.LDAP.AppendDomain":                      false,
143         "Login.LDAP.EmailAttribute":                    false,
144         "Login.LDAP.Enable":                            true,
145         "Login.LDAP.InsecureTLS":                       false,
146         "Login.LDAP.SearchAttribute":                   false,
147         "Login.LDAP.SearchBase":                        false,
148         "Login.LDAP.SearchBindPassword":                false,
149         "Login.LDAP.SearchBindUser":                    false,
150         "Login.LDAP.SearchFilters":                     false,
151         "Login.LDAP.StartTLS":                          false,
152         "Login.LDAP.StripDomain":                       false,
153         "Login.LDAP.URL":                               false,
154         "Login.LDAP.UsernameAttribute":                 false,
155         "Login.LoginCluster":                           true,
156         "Login.OpenIDConnect":                          true,
157         "Login.OpenIDConnect.ClientID":                 false,
158         "Login.OpenIDConnect.ClientSecret":             false,
159         "Login.OpenIDConnect.EmailClaim":               false,
160         "Login.OpenIDConnect.EmailVerifiedClaim":       false,
161         "Login.OpenIDConnect.Enable":                   true,
162         "Login.OpenIDConnect.Issuer":                   false,
163         "Login.OpenIDConnect.UsernameClaim":            false,
164         "Login.PAM":                                    true,
165         "Login.PAM.DefaultEmailDomain":                 false,
166         "Login.PAM.Enable":                             true,
167         "Login.PAM.Service":                            false,
168         "Login.RemoteTokenRefresh":                     true,
169         "Login.SSO":                                    true,
170         "Login.SSO.Enable":                             true,
171         "Login.SSO.ProviderAppID":                      false,
172         "Login.SSO.ProviderAppSecret":                  false,
173         "Login.Test":                                   true,
174         "Login.Test.Enable":                            true,
175         "Login.Test.Users":                             false,
176         "Mail":                                         true,
177         "Mail.EmailFrom":                               false,
178         "Mail.IssueReporterEmailFrom":                  false,
179         "Mail.IssueReporterEmailTo":                    false,
180         "Mail.MailchimpAPIKey":                         false,
181         "Mail.MailchimpListID":                         false,
182         "Mail.SendUserSetupNotificationEmail":          false,
183         "Mail.SupportEmailAddress":                     true,
184         "ManagementToken":                              false,
185         "PostgreSQL":                                   false,
186         "RemoteClusters":                               true,
187         "RemoteClusters.*":                             true,
188         "RemoteClusters.*.ActivateUsers":               true,
189         "RemoteClusters.*.Host":                        true,
190         "RemoteClusters.*.Insecure":                    true,
191         "RemoteClusters.*.Proxy":                       true,
192         "RemoteClusters.*.Scheme":                      true,
193         "Services":                                     true,
194         "Services.*":                                   true,
195         "Services.*.ExternalURL":                       true,
196         "Services.*.InternalURLs":                      false,
197         "SystemLogs":                                   false,
198         "SystemRootToken":                              false,
199         "TLS":                                          false,
200         "Users":                                        true,
201         "Users.AdminNotifierEmailFrom":                 false,
202         "Users.AnonymousUserToken":                     true,
203         "Users.AutoAdminFirstUser":                     false,
204         "Users.AutoAdminUserWithEmail":                 false,
205         "Users.AutoSetupNewUsers":                      false,
206         "Users.AutoSetupNewUsersWithRepository":        false,
207         "Users.AutoSetupNewUsersWithVmUUID":            false,
208         "Users.AutoSetupUsernameBlacklist":             false,
209         "Users.EmailSubjectPrefix":                     false,
210         "Users.NewInactiveUserNotificationRecipients":  false,
211         "Users.NewUserNotificationRecipients":          false,
212         "Users.NewUsersAreActive":                      false,
213         "Users.PreferDomainForUsername":                false,
214         "Users.UserNotifierEmailFrom":                  false,
215         "Users.UserProfileNotificationAddress":         false,
216         "Volumes":                                      true,
217         "Volumes.*":                                    true,
218         "Volumes.*.*":                                  false,
219         "Volumes.*.AccessViaHosts":                     true,
220         "Volumes.*.AccessViaHosts.*":                   true,
221         "Volumes.*.AccessViaHosts.*.ReadOnly":          true,
222         "Volumes.*.ReadOnly":                           true,
223         "Volumes.*.Replication":                        true,
224         "Volumes.*.StorageClasses":                     true,
225         "Volumes.*.StorageClasses.*":                   false,
226         "Workbench":                                    true,
227         "Workbench.ActivationContactLink":              false,
228         "Workbench.APIClientConnectTimeout":            true,
229         "Workbench.APIClientReceiveTimeout":            true,
230         "Workbench.APIResponseCompression":             true,
231         "Workbench.ApplicationMimetypesWithViewIcon":   true,
232         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
233         "Workbench.ArvadosDocsite":                     true,
234         "Workbench.ArvadosPublicDataDocURL":            true,
235         "Workbench.DefaultOpenIdPrefix":                false,
236         "Workbench.EnableGettingStartedPopup":          true,
237         "Workbench.EnablePublicProjectsPage":           true,
238         "Workbench.FileViewersConfigURL":               true,
239         "Workbench.InactivePageHTML":                   true,
240         "Workbench.LogViewerMaxBytes":                  true,
241         "Workbench.MultiSiteSearch":                    true,
242         "Workbench.ProfilingEnabled":                   true,
243         "Workbench.Repositories":                       false,
244         "Workbench.RepositoryCache":                    false,
245         "Workbench.RunningJobLogRecordsToFetch":        true,
246         "Workbench.SecretKeyBase":                      false,
247         "Workbench.ShowRecentCollectionsOnDashboard":   true,
248         "Workbench.ShowUserAgreementInline":            true,
249         "Workbench.ShowUserNotifications":              true,
250         "Workbench.SiteName":                           true,
251         "Workbench.SSHHelpHostSuffix":                  true,
252         "Workbench.SSHHelpPageHTML":                    true,
253         "Workbench.Theme":                              true,
254         "Workbench.UserProfileFormFields":              true,
255         "Workbench.UserProfileFormFields.*":            true,
256         "Workbench.UserProfileFormFields.*.*":          true,
257         "Workbench.UserProfileFormFields.*.*.*":        true,
258         "Workbench.UserProfileFormMessage":             true,
259         "Workbench.VocabularyURL":                      true,
260         "Workbench.WelcomePageHTML":                    true,
261 }
262
263 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
264         var errs []string
265         for k, v := range m {
266                 lookupKey := k
267                 safe, ok := whitelist[lookupPrefix+k]
268                 if !ok {
269                         lookupKey = "*"
270                         safe, ok = whitelist[lookupPrefix+"*"]
271                 }
272                 if !ok {
273                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
274                         continue
275                 }
276                 if !safe {
277                         delete(m, k)
278                         continue
279                 }
280                 if v, ok := v.(map[string]interface{}); ok {
281                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
282                         if err != nil {
283                                 errs = append(errs, err.Error())
284                         }
285                 }
286         }
287         if len(errs) > 0 {
288                 return errors.New(strings.Join(errs, "\n"))
289         }
290         return nil
291 }