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