18562: Rename config to AlwaysUsePreemptibleInstances.
[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.AlwaysUsePreemptibleInstances": true,
142         "Git":                                  false,
143         "InstanceTypes":                        true,
144         "InstanceTypes.*":                      true,
145         "InstanceTypes.*.*":                    true,
146         "Login":                                true,
147         "Login.Google":                         true,
148         "Login.Google.AlternateEmailAddresses": false,
149         "Login.Google.AuthenticationRequestParameters":        false,
150         "Login.Google.ClientID":                               false,
151         "Login.Google.ClientSecret":                           false,
152         "Login.Google.Enable":                                 true,
153         "Login.LDAP":                                          true,
154         "Login.LDAP.AppendDomain":                             false,
155         "Login.LDAP.EmailAttribute":                           false,
156         "Login.LDAP.Enable":                                   true,
157         "Login.LDAP.InsecureTLS":                              false,
158         "Login.LDAP.SearchAttribute":                          false,
159         "Login.LDAP.SearchBase":                               false,
160         "Login.LDAP.SearchBindPassword":                       false,
161         "Login.LDAP.SearchBindUser":                           false,
162         "Login.LDAP.SearchFilters":                            false,
163         "Login.LDAP.StartTLS":                                 false,
164         "Login.LDAP.StripDomain":                              false,
165         "Login.LDAP.URL":                                      false,
166         "Login.LDAP.UsernameAttribute":                        false,
167         "Login.LoginCluster":                                  true,
168         "Login.OpenIDConnect":                                 true,
169         "Login.OpenIDConnect.AcceptAccessToken":               false,
170         "Login.OpenIDConnect.AcceptAccessTokenScope":          false,
171         "Login.OpenIDConnect.AuthenticationRequestParameters": false,
172         "Login.OpenIDConnect.ClientID":                        false,
173         "Login.OpenIDConnect.ClientSecret":                    false,
174         "Login.OpenIDConnect.EmailClaim":                      false,
175         "Login.OpenIDConnect.EmailVerifiedClaim":              false,
176         "Login.OpenIDConnect.Enable":                          true,
177         "Login.OpenIDConnect.Issuer":                          false,
178         "Login.OpenIDConnect.UsernameClaim":                   false,
179         "Login.PAM":                                           true,
180         "Login.PAM.DefaultEmailDomain":                        false,
181         "Login.PAM.Enable":                                    true,
182         "Login.PAM.Service":                                   false,
183         "Login.RemoteTokenRefresh":                            true,
184         "Login.Test":                                          true,
185         "Login.Test.Enable":                                   true,
186         "Login.Test.Users":                                    false,
187         "Login.TokenLifetime":                                 false,
188         "Login.IssueTrustedTokens":                            false,
189         "Login.TrustedClients":                                false,
190         "Mail":                                                true,
191         "Mail.EmailFrom":                                      false,
192         "Mail.IssueReporterEmailFrom":                         false,
193         "Mail.IssueReporterEmailTo":                           false,
194         "Mail.MailchimpAPIKey":                                false,
195         "Mail.MailchimpListID":                                false,
196         "Mail.SendUserSetupNotificationEmail":                 false,
197         "Mail.SupportEmailAddress":                            true,
198         "ManagementToken":                                     false,
199         "PostgreSQL":                                          false,
200         "RemoteClusters":                                      true,
201         "RemoteClusters.*":                                    true,
202         "RemoteClusters.*.ActivateUsers":                      true,
203         "RemoteClusters.*.Host":                               true,
204         "RemoteClusters.*.Insecure":                           true,
205         "RemoteClusters.*.Proxy":                              true,
206         "RemoteClusters.*.Scheme":                             true,
207         "Services":                                            true,
208         "Services.*":                                          true,
209         "Services.*.ExternalURL":                              true,
210         "Services.*.InternalURLs":                             false,
211         "StorageClasses":                                      true,
212         "StorageClasses.*":                                    true,
213         "StorageClasses.*.Default":                            true,
214         "StorageClasses.*.Priority":                           true,
215         "SystemLogs":                                          false,
216         "SystemRootToken":                                     false,
217         "TLS":                                                 false,
218         "Users":                                               true,
219         "Users.ActivatedUsersAreVisibleToOthers":              false,
220         "Users.AdminNotifierEmailFrom":                        false,
221         "Users.AnonymousUserToken":                            true,
222         "Users.AutoAdminFirstUser":                            false,
223         "Users.AutoAdminUserWithEmail":                        false,
224         "Users.AutoSetupNewUsers":                             false,
225         "Users.AutoSetupNewUsersWithRepository":               false,
226         "Users.AutoSetupNewUsersWithVmUUID":                   false,
227         "Users.AutoSetupUsernameBlacklist":                    false,
228         "Users.EmailSubjectPrefix":                            false,
229         "Users.NewInactiveUserNotificationRecipients":         false,
230         "Users.NewUserNotificationRecipients":                 false,
231         "Users.NewUsersAreActive":                             false,
232         "Users.PreferDomainForUsername":                       false,
233         "Users.UserNotifierEmailFrom":                         false,
234         "Users.UserNotifierEmailBcc":                          false,
235         "Users.UserProfileNotificationAddress":                false,
236         "Users.UserSetupMailText":                             false,
237         "Users.RoleGroupsVisibleToAll":                        false,
238         "Volumes":                                             true,
239         "Volumes.*":                                           true,
240         "Volumes.*.*":                                         false,
241         "Volumes.*.AccessViaHosts":                            true,
242         "Volumes.*.AccessViaHosts.*":                          true,
243         "Volumes.*.AccessViaHosts.*.ReadOnly":                 true,
244         "Volumes.*.ReadOnly":                                  true,
245         "Volumes.*.Replication":                               true,
246         "Volumes.*.StorageClasses":                            true,
247         "Volumes.*.StorageClasses.*":                          true,
248         "Workbench":                                           true,
249         "Workbench.ActivationContactLink":                     false,
250         "Workbench.APIClientConnectTimeout":                   true,
251         "Workbench.APIClientReceiveTimeout":                   true,
252         "Workbench.APIResponseCompression":                    true,
253         "Workbench.ApplicationMimetypesWithViewIcon":          true,
254         "Workbench.ApplicationMimetypesWithViewIcon.*":        true,
255         "Workbench.ArvadosDocsite":                            true,
256         "Workbench.ArvadosPublicDataDocURL":                   true,
257         "Workbench.DefaultOpenIdPrefix":                       false,
258         "Workbench.EnableGettingStartedPopup":                 true,
259         "Workbench.EnablePublicProjectsPage":                  true,
260         "Workbench.FileViewersConfigURL":                      true,
261         "Workbench.IdleTimeout":                               true,
262         "Workbench.InactivePageHTML":                          true,
263         "Workbench.LogViewerMaxBytes":                         true,
264         "Workbench.MultiSiteSearch":                           true,
265         "Workbench.ProfilingEnabled":                          true,
266         "Workbench.Repositories":                              false,
267         "Workbench.RepositoryCache":                           false,
268         "Workbench.RunningJobLogRecordsToFetch":               true,
269         "Workbench.SecretKeyBase":                             false,
270         "Workbench.ShowRecentCollectionsOnDashboard":          true,
271         "Workbench.ShowUserAgreementInline":                   true,
272         "Workbench.ShowUserNotifications":                     true,
273         "Workbench.SiteName":                                  true,
274         "Workbench.SSHHelpHostSuffix":                         true,
275         "Workbench.SSHHelpPageHTML":                           true,
276         "Workbench.Theme":                                     true,
277         "Workbench.UserProfileFormFields":                     true,
278         "Workbench.UserProfileFormFields.*":                   true,
279         "Workbench.UserProfileFormFields.*.*":                 true,
280         "Workbench.UserProfileFormFields.*.*.*":               true,
281         "Workbench.UserProfileFormMessage":                    true,
282         "Workbench.WelcomePageHTML":                           true,
283 }
284
285 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
286         var errs []string
287         for k, v := range m {
288                 lookupKey := k
289                 safe, ok := whitelist[lookupPrefix+k]
290                 if !ok {
291                         lookupKey = "*"
292                         safe, ok = whitelist[lookupPrefix+"*"]
293                 }
294                 if !ok {
295                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
296                         continue
297                 }
298                 if !safe {
299                         delete(m, k)
300                         continue
301                 }
302                 if v, ok := v.(map[string]interface{}); ok {
303                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
304                         if err != nil {
305                                 errs = append(errs, err.Error())
306                         }
307                 }
308         }
309         if len(errs) > 0 {
310                 return errors.New(strings.Join(errs, "\n"))
311         }
312         return nil
313 }