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