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