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