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