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