13647: Merge branch 'master' into 13647-keepstore-config
[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.curoverse.com/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.MaxKeepBlockBuffers":                      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.ManagedProperties":                true,
96         "Collections.ManagedProperties.*":              true,
97         "Collections.ManagedProperties.*.*":            true,
98         "Collections.PreserveVersionIfIdle":            true,
99         "Collections.TrashSweepInterval":               false,
100         "Collections.TrustAllContent":                  false,
101         "Collections.WebDAVCache":                      false,
102         "Containers":                                   true,
103         "Containers.CloudVMs":                          false,
104         "Containers.CrunchRunCommand":                  false,
105         "Containers.CrunchRunArgumentsList":            false,
106         "Containers.DefaultKeepCacheRAM":               true,
107         "Containers.DispatchPrivateKey":                false,
108         "Containers.JobsAPI":                           true,
109         "Containers.JobsAPI.Enable":                    true,
110         "Containers.JobsAPI.GitInternalDir":            false,
111         "Containers.Logging":                           false,
112         "Containers.LogReuseDecisions":                 false,
113         "Containers.MaxComputeVMs":                     false,
114         "Containers.MaxDispatchAttempts":               false,
115         "Containers.MaxRetryAttempts":                  true,
116         "Containers.MinRetryPeriod":                    true,
117         "Containers.ReserveExtraRAM":                   true,
118         "Containers.SLURM":                             false,
119         "Containers.StaleLockTimeout":                  false,
120         "Containers.SupportedDockerImageFormats":       true,
121         "Containers.SupportedDockerImageFormats.*":     true,
122         "Containers.UsePreemptibleInstances":           true,
123         "EnableBetaController14287":                    false,
124         "Git":                                          false,
125         "InstanceTypes":                                true,
126         "InstanceTypes.*":                              true,
127         "InstanceTypes.*.*":                            true,
128         "Login":                                        true,
129         "Login.ProviderAppSecret":                      false,
130         "Login.ProviderAppID":                          false,
131         "Login.LoginCluster":                           true,
132         "Login.RemoteTokenRefresh":                     true,
133         "Mail":                                         false,
134         "ManagementToken":                              false,
135         "PostgreSQL":                                   false,
136         "RemoteClusters":                               true,
137         "RemoteClusters.*":                             true,
138         "RemoteClusters.*.ActivateUsers":               true,
139         "RemoteClusters.*.Host":                        true,
140         "RemoteClusters.*.Insecure":                    true,
141         "RemoteClusters.*.Proxy":                       true,
142         "RemoteClusters.*.Scheme":                      true,
143         "Services":                                     true,
144         "Services.*":                                   true,
145         "Services.*.ExternalURL":                       true,
146         "Services.*.InternalURLs":                      false,
147         "SystemLogs":                                   false,
148         "SystemRootToken":                              false,
149         "TLS":                                          false,
150         "Users":                                        true,
151         "Users.AnonymousUserToken":                     true,
152         "Users.AdminNotifierEmailFrom":                 false,
153         "Users.AutoAdminFirstUser":                     false,
154         "Users.AutoAdminUserWithEmail":                 false,
155         "Users.AutoSetupNewUsers":                      false,
156         "Users.AutoSetupNewUsersWithRepository":        false,
157         "Users.AutoSetupNewUsersWithVmUUID":            false,
158         "Users.AutoSetupUsernameBlacklist":             false,
159         "Users.EmailSubjectPrefix":                     false,
160         "Users.NewInactiveUserNotificationRecipients":  false,
161         "Users.NewUserNotificationRecipients":          false,
162         "Users.NewUsersAreActive":                      false,
163         "Users.UserNotifierEmailFrom":                  false,
164         "Users.UserProfileNotificationAddress":         false,
165         "Volumes":                                      true,
166         "Volumes.*":                                    true,
167         "Volumes.*.*":                                  false,
168         "Volumes.*.AccessViaHosts":                     true,
169         "Volumes.*.AccessViaHosts.*":                   true,
170         "Volumes.*.AccessViaHosts.*.ReadOnly":          true,
171         "Volumes.*.ReadOnly":                           true,
172         "Volumes.*.Replication":                        true,
173         "Volumes.*.StorageClasses":                     true,
174         "Volumes.*.StorageClasses.*":                   false,
175         "Workbench":                                    true,
176         "Workbench.ActivationContactLink":              false,
177         "Workbench.APIClientConnectTimeout":            true,
178         "Workbench.APIClientReceiveTimeout":            true,
179         "Workbench.APIResponseCompression":             true,
180         "Workbench.ApplicationMimetypesWithViewIcon":   true,
181         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
182         "Workbench.ArvadosDocsite":                     true,
183         "Workbench.ArvadosPublicDataDocURL":            true,
184         "Workbench.DefaultOpenIdPrefix":                false,
185         "Workbench.EnableGettingStartedPopup":          true,
186         "Workbench.EnablePublicProjectsPage":           true,
187         "Workbench.FileViewersConfigURL":               true,
188         "Workbench.LogViewerMaxBytes":                  true,
189         "Workbench.MultiSiteSearch":                    true,
190         "Workbench.ProfilingEnabled":                   true,
191         "Workbench.Repositories":                       false,
192         "Workbench.RepositoryCache":                    false,
193         "Workbench.RunningJobLogRecordsToFetch":        true,
194         "Workbench.SecretKeyBase":                      false,
195         "Workbench.ShowRecentCollectionsOnDashboard":   true,
196         "Workbench.ShowUserAgreementInline":            true,
197         "Workbench.ShowUserNotifications":              true,
198         "Workbench.SiteName":                           true,
199         "Workbench.Theme":                              true,
200         "Workbench.UserProfileFormFields":              true,
201         "Workbench.UserProfileFormFields.*":            true,
202         "Workbench.UserProfileFormFields.*.*":          true,
203         "Workbench.UserProfileFormFields.*.*.*":        true,
204         "Workbench.UserProfileFormMessage":             true,
205         "Workbench.VocabularyURL":                      true,
206 }
207
208 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
209         var errs []string
210         for k, v := range m {
211                 lookupKey := k
212                 safe, ok := whitelist[lookupPrefix+k]
213                 if !ok {
214                         lookupKey = "*"
215                         safe, ok = whitelist[lookupPrefix+"*"]
216                 }
217                 if !ok {
218                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
219                         continue
220                 }
221                 if !safe {
222                         delete(m, k)
223                         continue
224                 }
225                 if v, ok := v.(map[string]interface{}); ok {
226                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
227                         if err != nil {
228                                 errs = append(errs, err.Error())
229                         }
230                 }
231         }
232         if len(errs) > 0 {
233                 return errors.New(strings.Join(errs, "\n"))
234         }
235         return nil
236 }