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