b79dec4d9d1532b1f348965e5c657e71df21704e
[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         "AuditLogs":                                    false,
68         "AuditLogs.MaxAge":                             false,
69         "AuditLogs.MaxDeleteBatch":                     false,
70         "AuditLogs.UnloggedAttributes":                 false,
71         "Collections":                                  true,
72         "Collections.BlobSigning":                      true,
73         "Collections.BlobSigningKey":                   false,
74         "Collections.BlobSigningTTL":                   true,
75         "Collections.CollectionVersioning":             false,
76         "Collections.DefaultReplication":               true,
77         "Collections.DefaultTrashLifetime":             true,
78         "Collections.ManagedProperties":                true,
79         "Collections.ManagedProperties.*":              true,
80         "Collections.ManagedProperties.*.*":            true,
81         "Collections.PreserveVersionIfIdle":            true,
82         "Collections.TrashSweepInterval":               false,
83         "Collections.TrustAllContent":                  false,
84         "Containers":                                   true,
85         "Containers.CloudVMs":                          false,
86         "Containers.DefaultKeepCacheRAM":               true,
87         "Containers.DispatchPrivateKey":                false,
88         "Containers.JobsAPI":                           true,
89         "Containers.JobsAPI.CrunchJobUser":             false,
90         "Containers.JobsAPI.CrunchJobWrapper":          false,
91         "Containers.JobsAPI.CrunchRefreshTrigger":      false,
92         "Containers.JobsAPI.DefaultDockerImage":        false,
93         "Containers.JobsAPI.Enable":                    true,
94         "Containers.JobsAPI.GitInternalDir":            false,
95         "Containers.JobsAPI.ReuseJobIfOutputsDiffer":   false,
96         "Containers.Logging":                           false,
97         "Containers.LogReuseDecisions":                 false,
98         "Containers.MaxComputeVMs":                     false,
99         "Containers.MaxDispatchAttempts":               false,
100         "Containers.MaxRetryAttempts":                  true,
101         "Containers.SLURM":                             false,
102         "Containers.StaleLockTimeout":                  false,
103         "Containers.SupportedDockerImageFormats":       true,
104         "Containers.UsePreemptibleInstances":           true,
105         "EnableBetaController14287":                    false,
106         "Git":                                          false,
107         "InstanceTypes":                                true,
108         "InstanceTypes.*":                              true,
109         "InstanceTypes.*.*":                            true,
110         "Login":                                        false,
111         "Mail":                                         false,
112         "ManagementToken":                              false,
113         "PostgreSQL":                                   false,
114         "RemoteClusters":                               true,
115         "RemoteClusters.*":                             true,
116         "RemoteClusters.*.ActivateUsers":               true,
117         "RemoteClusters.*.Host":                        true,
118         "RemoteClusters.*.Insecure":                    true,
119         "RemoteClusters.*.Proxy":                       true,
120         "RemoteClusters.*.Scheme":                      true,
121         "Services":                                     true,
122         "Services.*":                                   true,
123         "Services.*.ExternalURL":                       true,
124         "Services.*.InternalURLs":                      false,
125         "SystemLogs":                                   false,
126         "SystemRootToken":                              false,
127         "TLS":                                          false,
128         "Users":                                        true,
129         "Users.AnonymousUserToken":                     true,
130         "Users.AdminNotifierEmailFrom":                 false,
131         "Users.AutoAdminFirstUser":                     false,
132         "Users.AutoAdminUserWithEmail":                 false,
133         "Users.AutoSetupNewUsers":                      false,
134         "Users.AutoSetupNewUsersWithRepository":        false,
135         "Users.AutoSetupNewUsersWithVmUUID":            false,
136         "Users.AutoSetupUsernameBlacklist":             false,
137         "Users.EmailSubjectPrefix":                     false,
138         "Users.NewInactiveUserNotificationRecipients":  false,
139         "Users.NewUserNotificationRecipients":          false,
140         "Users.NewUsersAreActive":                      false,
141         "Users.UserNotifierEmailFrom":                  false,
142         "Users.UserProfileNotificationAddress":         false,
143         "Workbench":                                    true,
144         "Workbench.ActivationContactLink":              false,
145         "Workbench.APIClientConnectTimeout":            true,
146         "Workbench.APIClientReceiveTimeout":            true,
147         "Workbench.APIResponseCompression":             true,
148         "Workbench.ApplicationMimetypesWithViewIcon":   true,
149         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
150         "Workbench.ArvadosDocsite":                     true,
151         "Workbench.ArvadosPublicDataDocURL":            true,
152         "Workbench.DefaultOpenIdPrefix":                false,
153         "Workbench.EnableGettingStartedPopup":          true,
154         "Workbench.EnablePublicProjectsPage":           true,
155         "Workbench.FileViewersConfigURL":               true,
156         "Workbench.LogViewerMaxBytes":                  true,
157         "Workbench.MultiSiteSearch":                    true,
158         "Workbench.ProfilingEnabled":                   true,
159         "Workbench.Repositories":                       false,
160         "Workbench.RepositoryCache":                    false,
161         "Workbench.RunningJobLogRecordsToFetch":        true,
162         "Workbench.SecretKeyBase":                      false,
163         "Workbench.ShowRecentCollectionsOnDashboard":   true,
164         "Workbench.ShowUserAgreementInline":            true,
165         "Workbench.ShowUserNotifications":              true,
166         "Workbench.SiteName":                           true,
167         "Workbench.Theme":                              true,
168         "Workbench.UserProfileFormFields":              true,
169         "Workbench.UserProfileFormFields.*":            true,
170         "Workbench.UserProfileFormFields.*.*":          true,
171         "Workbench.UserProfileFormFields.*.*.*":        true,
172         "Workbench.UserProfileFormMessage":             true,
173         "Workbench.VocabularyURL":                      true,
174 }
175
176 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
177         var errs []string
178         for k, v := range m {
179                 lookupKey := k
180                 safe, ok := whitelist[lookupPrefix+k]
181                 if !ok {
182                         lookupKey = "*"
183                         safe, ok = whitelist[lookupPrefix+"*"]
184                 }
185                 if !ok {
186                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
187                         continue
188                 }
189                 if !safe {
190                         delete(m, k)
191                         continue
192                 }
193                 if v, ok := v.(map[string]interface{}); ok {
194                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
195                         if err != nil {
196                                 errs = append(errs, err.Error())
197                         }
198                 }
199         }
200         if len(errs) > 0 {
201                 return errors.New(strings.Join(errs, "\n"))
202         }
203         return nil
204 }