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