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