15881: Add LDAP authentication option.
[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         "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.MaxKeepBlobBuffers":                       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.ForwardSlashNameSubstitution":     true,
96         "Collections.ManagedProperties":                true,
97         "Collections.ManagedProperties.*":              true,
98         "Collections.ManagedProperties.*.*":            true,
99         "Collections.PreserveVersionIfIdle":            true,
100         "Collections.TrashSweepInterval":               false,
101         "Collections.TrustAllContent":                  false,
102         "Collections.WebDAVCache":                      false,
103         "Collections.BalanceCollectionBatch":           false,
104         "Collections.BalancePeriod":                    false,
105         "Collections.BlobMissingReport":                false,
106         "Collections.BalanceCollectionBuffers":         false,
107         "Containers":                                   true,
108         "Containers.CloudVMs":                          false,
109         "Containers.CrunchRunCommand":                  false,
110         "Containers.CrunchRunArgumentsList":            false,
111         "Containers.DefaultKeepCacheRAM":               true,
112         "Containers.DispatchPrivateKey":                false,
113         "Containers.JobsAPI":                           true,
114         "Containers.JobsAPI.Enable":                    true,
115         "Containers.JobsAPI.GitInternalDir":            false,
116         "Containers.Logging":                           false,
117         "Containers.LogReuseDecisions":                 false,
118         "Containers.MaxComputeVMs":                     false,
119         "Containers.MaxDispatchAttempts":               false,
120         "Containers.MaxRetryAttempts":                  true,
121         "Containers.MinRetryPeriod":                    true,
122         "Containers.ReserveExtraRAM":                   true,
123         "Containers.SLURM":                             false,
124         "Containers.StaleLockTimeout":                  false,
125         "Containers.SupportedDockerImageFormats":       true,
126         "Containers.SupportedDockerImageFormats.*":     true,
127         "Containers.UsePreemptibleInstances":           true,
128         "ForceLegacyAPI14":                             false,
129         "Git":                                          false,
130         "InstanceTypes":                                true,
131         "InstanceTypes.*":                              true,
132         "InstanceTypes.*.*":                            true,
133         "Login":                                        true,
134         "Login.GoogleClientID":                         false,
135         "Login.GoogleClientSecret":                     false,
136         "Login.GoogleAlternateEmailAddresses":          false,
137         "Login.PAM":                                    true,
138         "Login.PAMService":                             false,
139         "Login.PAMDefaultEmailDomain":                  false,
140         "Login.ProviderAppID":                          false,
141         "Login.ProviderAppSecret":                      false,
142         "Login.LDAP":                                   true,
143         "Login.LDAP.AppendDomain":                      false,
144         "Login.LDAP.EmailAttribute":                    false,
145         "Login.LDAP.Enable":                            true,
146         "Login.LDAP.InsecureTLS":                       false,
147         "Login.LDAP.SearchAttribute":                   false,
148         "Login.LDAP.SearchBase":                        false,
149         "Login.LDAP.SearchBindUser":                    false,
150         "Login.LDAP.SearchBindPassword":                false,
151         "Login.LDAP.SearchFilters":                     false,
152         "Login.LDAP.StartTLS":                          false,
153         "Login.LDAP.StripDomain":                       false,
154         "Login.LDAP.URL":                               false,
155         "Login.LDAP.UsernameAttribute":                 false,
156         "Login.LoginCluster":                           true,
157         "Login.RemoteTokenRefresh":                     true,
158         "Mail":                                         true,
159         "Mail.MailchimpAPIKey":                         false,
160         "Mail.MailchimpListID":                         false,
161         "Mail.SendUserSetupNotificationEmail":          false,
162         "Mail.IssueReporterEmailFrom":                  false,
163         "Mail.IssueReporterEmailTo":                    false,
164         "Mail.SupportEmailAddress":                     true,
165         "Mail.EmailFrom":                               false,
166         "ManagementToken":                              false,
167         "PostgreSQL":                                   false,
168         "RemoteClusters":                               true,
169         "RemoteClusters.*":                             true,
170         "RemoteClusters.*.ActivateUsers":               true,
171         "RemoteClusters.*.Host":                        true,
172         "RemoteClusters.*.Insecure":                    true,
173         "RemoteClusters.*.Proxy":                       true,
174         "RemoteClusters.*.Scheme":                      true,
175         "Services":                                     true,
176         "Services.*":                                   true,
177         "Services.*.ExternalURL":                       true,
178         "Services.*.InternalURLs":                      false,
179         "SystemLogs":                                   false,
180         "SystemRootToken":                              false,
181         "TLS":                                          false,
182         "Users":                                        true,
183         "Users.AnonymousUserToken":                     true,
184         "Users.AdminNotifierEmailFrom":                 false,
185         "Users.AutoAdminFirstUser":                     false,
186         "Users.AutoAdminUserWithEmail":                 false,
187         "Users.AutoSetupNewUsers":                      false,
188         "Users.AutoSetupNewUsersWithRepository":        false,
189         "Users.AutoSetupNewUsersWithVmUUID":            false,
190         "Users.AutoSetupUsernameBlacklist":             false,
191         "Users.EmailSubjectPrefix":                     false,
192         "Users.NewInactiveUserNotificationRecipients":  false,
193         "Users.NewUserNotificationRecipients":          false,
194         "Users.NewUsersAreActive":                      false,
195         "Users.PreferDomainForUsername":                false,
196         "Users.UserNotifierEmailFrom":                  false,
197         "Users.UserProfileNotificationAddress":         false,
198         "Volumes":                                      true,
199         "Volumes.*":                                    true,
200         "Volumes.*.*":                                  false,
201         "Volumes.*.AccessViaHosts":                     true,
202         "Volumes.*.AccessViaHosts.*":                   true,
203         "Volumes.*.AccessViaHosts.*.ReadOnly":          true,
204         "Volumes.*.ReadOnly":                           true,
205         "Volumes.*.Replication":                        true,
206         "Volumes.*.StorageClasses":                     true,
207         "Volumes.*.StorageClasses.*":                   false,
208         "Workbench":                                    true,
209         "Workbench.ActivationContactLink":              false,
210         "Workbench.APIClientConnectTimeout":            true,
211         "Workbench.APIClientReceiveTimeout":            true,
212         "Workbench.APIResponseCompression":             true,
213         "Workbench.ApplicationMimetypesWithViewIcon":   true,
214         "Workbench.ApplicationMimetypesWithViewIcon.*": true,
215         "Workbench.ArvadosDocsite":                     true,
216         "Workbench.ArvadosPublicDataDocURL":            true,
217         "Workbench.DefaultOpenIdPrefix":                false,
218         "Workbench.EnableGettingStartedPopup":          true,
219         "Workbench.EnablePublicProjectsPage":           true,
220         "Workbench.FileViewersConfigURL":               true,
221         "Workbench.LogViewerMaxBytes":                  true,
222         "Workbench.MultiSiteSearch":                    true,
223         "Workbench.ProfilingEnabled":                   true,
224         "Workbench.Repositories":                       false,
225         "Workbench.RepositoryCache":                    false,
226         "Workbench.RunningJobLogRecordsToFetch":        true,
227         "Workbench.SecretKeyBase":                      false,
228         "Workbench.ShowRecentCollectionsOnDashboard":   true,
229         "Workbench.ShowUserAgreementInline":            true,
230         "Workbench.ShowUserNotifications":              true,
231         "Workbench.SiteName":                           true,
232         "Workbench.Theme":                              true,
233         "Workbench.UserProfileFormFields":              true,
234         "Workbench.UserProfileFormFields.*":            true,
235         "Workbench.UserProfileFormFields.*.*":          true,
236         "Workbench.UserProfileFormFields.*.*.*":        true,
237         "Workbench.UserProfileFormMessage":             true,
238         "Workbench.VocabularyURL":                      true,
239         "Workbench.WelcomePageHTML":                    true,
240         "Workbench.InactivePageHTML":                   true,
241         "Workbench.SSHHelpPageHTML":                    true,
242         "Workbench.SSHHelpHostSuffix":                  true,
243 }
244
245 func redactUnsafe(m map[string]interface{}, mPrefix, lookupPrefix string) error {
246         var errs []string
247         for k, v := range m {
248                 lookupKey := k
249                 safe, ok := whitelist[lookupPrefix+k]
250                 if !ok {
251                         lookupKey = "*"
252                         safe, ok = whitelist[lookupPrefix+"*"]
253                 }
254                 if !ok {
255                         errs = append(errs, fmt.Sprintf("config bug: key %q not in whitelist map", lookupPrefix+k))
256                         continue
257                 }
258                 if !safe {
259                         delete(m, k)
260                         continue
261                 }
262                 if v, ok := v.(map[string]interface{}); ok {
263                         err := redactUnsafe(v, mPrefix+k+".", lookupPrefix+lookupKey+".")
264                         if err != nil {
265                                 errs = append(errs, err.Error())
266                         }
267                 }
268         }
269         if len(errs) > 0 {
270                 return errors.New(strings.Join(errs, "\n"))
271         }
272         return nil
273 }