Merge branch '14813-config-cors'
[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         err = redactUnsafe(m, "", "")
30         if err != nil {
31                 return err
32         }
33         return json.NewEncoder(w).Encode(m)
34 }
35
36 // whitelist classifies configs as safe/unsafe to reveal to
37 // unauthenticated clients.
38 //
39 // Every config entry must either be listed explicitly here along with
40 // all of its parent keys (e.g., "API" + "API.RequestTimeout"), or
41 // have an ancestor listed as false (e.g.,
42 // "PostgreSQL.Connection.password" has an ancestor
43 // "PostgreSQL.Connection" with a false value). Otherwise, it is a bug
44 // which should be caught by tests.
45 //
46 // Example: API.RequestTimeout is safe because whitelist["API"] == and
47 // whitelist["API.RequestTimeout"] == true.
48 //
49 // Example: PostgreSQL.Connection.password is not safe because
50 // whitelist["PostgreSQL.Connection"] == false.
51 //
52 // Example: PostgreSQL.BadKey would cause an error because
53 // whitelist["PostgreSQL"] isn't false, and neither
54 // whitelist["PostgreSQL.BadKey"] nor whitelist["PostgreSQL.*"]
55 // exists.
56 var whitelist = map[string]bool{
57         // | sort -t'"' -k2,2
58         "API":                                          true,
59         "API.AsyncPermissionsUpdateInterval":           false,
60         "API.DisabledAPIs":                             false,
61         "API.MaxIndexDatabaseRead":                     false,
62         "API.MaxItemsPerResponse":                      true,
63         "API.MaxRequestAmplification":                  false,
64         "API.MaxRequestSize":                           true,
65         "API.RailsSessionSecretToken":                  false,
66         "API.RequestTimeout":                           true,
67         "API.WebsocketClientEventQueue":                false,
68         "API.SendTimeout":                              true,
69         "API.WebsocketServerEventQueue":                false,
70         "AuditLogs":                                    false,
71         "AuditLogs.MaxAge":                             false,
72         "AuditLogs.MaxDeleteBatch":                     false,
73         "AuditLogs.UnloggedAttributes":                 false,
74         "Collections":                                  true,
75         "Collections.BlobSigning":                      true,
76         "Collections.BlobSigningKey":                   false,
77         "Collections.BlobSigningTTL":                   true,
78         "Collections.CollectionVersioning":             false,
79         "Collections.DefaultReplication":               true,
80         "Collections.DefaultTrashLifetime":             true,
81         "Collections.ManagedProperties":                true,
82         "Collections.ManagedProperties.*":              true,
83         "Collections.ManagedProperties.*.*":            true,
84         "Collections.PreserveVersionIfIdle":            true,
85         "Collections.TrashSweepInterval":               false,
86         "Collections.TrustAllContent":                  false,
87         "Containers":                                   true,
88         "Containers.CloudVMs":                          false,
89         "Containers.CrunchRunCommand":                  false,
90         "Containers.CrunchRunArgumentsList":            false,
91         "Containers.DefaultKeepCacheRAM":               true,
92         "Containers.DispatchPrivateKey":                false,
93         "Containers.JobsAPI":                           true,
94         "Containers.JobsAPI.CrunchJobUser":             false,
95         "Containers.JobsAPI.CrunchJobWrapper":          false,
96         "Containers.JobsAPI.CrunchRefreshTrigger":      false,
97         "Containers.JobsAPI.DefaultDockerImage":        false,
98         "Containers.JobsAPI.Enable":                    true,
99         "Containers.JobsAPI.GitInternalDir":            false,
100         "Containers.JobsAPI.ReuseJobIfOutputsDiffer":   false,
101         "Containers.Logging":                           false,
102         "Containers.LogReuseDecisions":                 false,
103         "Containers.MaxComputeVMs":                     false,
104         "Containers.MaxDispatchAttempts":               false,
105         "Containers.MaxRetryAttempts":                  true,
106         "Containers.MinRetryPeriod":                    true,
107         "Containers.ReserveExtraRAM":                   true,
108         "Containers.SLURM":                             false,
109         "Containers.StaleLockTimeout":                  false,
110         "Containers.SupportedDockerImageFormats":       true,
111         "Containers.SupportedDockerImageFormats.*":     true,
112         "Containers.UsePreemptibleInstances":           true,
113         "EnableBetaController14287":                    false,
114         "Git":                                          false,
115         "InstanceTypes":                                true,
116         "InstanceTypes.*":                              true,
117         "InstanceTypes.*.*":                            true,
118         "Login":                                        false,
119         "Mail":                                         false,
120         "ManagementToken":                              false,
121         "PostgreSQL":                                   false,
122         "RemoteClusters":                               true,
123         "RemoteClusters.*":                             true,
124         "RemoteClusters.*.ActivateUsers":               true,
125         "RemoteClusters.*.Host":                        true,
126         "RemoteClusters.*.Insecure":                    true,
127         "RemoteClusters.*.Proxy":                       true,
128         "RemoteClusters.*.Scheme":                      true,
129         "Services":                                     true,
130         "Services.*":                                   true,
131         "Services.*.ExternalURL":                       true,
132         "Services.*.InternalURLs":                      false,
133         "SystemLogs":                                   false,
134         "SystemRootToken":                              false,
135         "TLS":                                          false,
136         "Users":                                        true,
137         "Users.AnonymousUserToken":                     true,
138         "Users.AdminNotifierEmailFrom":                 false,
139         "Users.AutoAdminFirstUser":                     false,
140         "Users.AutoAdminUserWithEmail":                 false,
141         "Users.AutoSetupNewUsers":                      false,
142         "Users.AutoSetupNewUsersWithRepository":        false,
143         "Users.AutoSetupNewUsersWithVmUUID":            false,
144         "Users.AutoSetupUsernameBlacklist":             false,
145         "Users.EmailSubjectPrefix":                     false,
146         "Users.NewInactiveUserNotificationRecipients":  false,
147         "Users.NewUserNotificationRecipients":          false,
148         "Users.NewUsersAreActive":                      false,
149         "Users.UserNotifierEmailFrom":                  false,
150         "Users.UserProfileNotificationAddress":         false,
151         "Workbench":                                    true,
152         "Workbench.ActivationContactLink":              false,
153         "Workbench.APIClientConnectTimeout":            true,
154         "Workbench.APIClientReceiveTimeout":            true,
155         "Workbench.APIResponseCompression":             true,
156         "Workbench.ApplicationMimetypesWithViewIcon":   true,
157         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
158         "Workbench.ArvadosDocsite":                     true,
159         "Workbench.ArvadosPublicDataDocURL":            true,
160         "Workbench.DefaultOpenIdPrefix":                false,
161         "Workbench.EnableGettingStartedPopup":          true,
162         "Workbench.EnablePublicProjectsPage":           true,
163         "Workbench.FileViewersConfigURL":               true,
164         "Workbench.LogViewerMaxBytes":                  true,
165         "Workbench.MultiSiteSearch":                    true,
166         "Workbench.ProfilingEnabled":                   true,
167         "Workbench.Repositories":                       false,
168         "Workbench.RepositoryCache":                    false,
169         "Workbench.RunningJobLogRecordsToFetch":        true,
170         "Workbench.SecretKeyBase":                      false,
171         "Workbench.ShowRecentCollectionsOnDashboard":   true,
172         "Workbench.ShowUserAgreementInline":            true,
173         "Workbench.ShowUserNotifications":              true,
174         "Workbench.SiteName":                           true,
175         "Workbench.Theme":                              true,
176         "Workbench.UserProfileFormFields":              true,
177         "Workbench.UserProfileFormFields.*":            true,
178         "Workbench.UserProfileFormFields.*.*":          true,
179         "Workbench.UserProfileFormFields.*.*.*":        true,
180         "Workbench.UserProfileFormMessage":             true,
181         "Workbench.VocabularyURL":                      true,
182 }
183
184 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
185         var errs []string
186         for k, v := range m {
187                 lookupKey := k
188                 safe, ok := whitelist[lookupPrefix+k]
189                 if !ok {
190                         lookupKey = "*"
191                         safe, ok = whitelist[lookupPrefix+"*"]
192                 }
193                 if !ok {
194                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
195                         continue
196                 }
197                 if !safe {
198                         delete(m, k)
199                         continue
200                 }
201                 if v, ok := v.(map[string]interface{}); ok {
202                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
203                         if err != nil {
204                                 errs = append(errs, err.Error())
205                         }
206                 }
207         }
208         if len(errs) > 0 {
209                 return errors.New(strings.Join(errs, "\n"))
210         }
211         return nil
212 }