Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQL-125] Redact sensitive information from traces #189

Merged
merged 10 commits into from
Dec 14, 2021
9 changes: 8 additions & 1 deletion src/main/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<root level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
<logger name="io.pedestal.http" level="warn" />
<!--IMPORTANT: We need to turn Pedestal logging off because on an
error, Pedestal will spew out the entire request/response map,
which may contain sensitive info.-->
<!--TODO: Perhaps we can create a custom error logger to replace
Pedestal's logger that applies redaction to the request/response
map before logging?-->
<logger name="io.pedestal.http.impl.servlet-interceptor" level="off" />
</configuration>
4 changes: 2 additions & 2 deletions src/main/lrsql/input/statement.clj
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@
(defn- warn-on-dupe-shas
[attachments shas]
(when (not= (count attachments) (count shas))
(log/warnf "%s\nAttachments: %s"
duplicate-sha-emsg
(log/warn duplicate-sha-emsg)
(log/debug "Attachments: %s"
(mapv #(dissoc % :content) attachments))))

(defn add-insert-attachment-inputs
Expand Down
4 changes: 2 additions & 2 deletions src/main/lrsql/system/database.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[com.stuartsierra.component :as component]
[lrsql.backend.protocol :as bp]
[lrsql.spec.config :as cs]
[lrsql.system.util :refer [assert-config make-jdbc-url]])
[lrsql.system.util :as cu :refer [assert-config make-jdbc-url]])
(:import [com.zaxxer.hikari HikariConfig HikariDataSource]
[com.codahale.metrics MetricRegistry]
[com.codahale.metrics.jmx JmxReporter]))
Expand Down Expand Up @@ -97,7 +97,7 @@
(if-not ?conn-pool
(let [conn-pool (make-conn-pool backend config)]
(log/infof "Starting new connection for %s database..." db-type)
(log/debugf "Config: %s" config)
(log/debugf "Config: %s" (cu/redact-config-vars config))
milt marked this conversation as resolved.
Show resolved Hide resolved
(assoc conn :conn-pool conn-pool))
(do
(log/info "Connection already started; do nothing.")
Expand Down
43 changes: 41 additions & 2 deletions src/main/lrsql/system/util.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
(ns lrsql.system.util
(:require [clojure.spec.alpha :as s]
[clojure.walk :as w]
[clojure.tools.logging :as log]
[next.jdbc.connection :as jdbc-conn]
[ring.util.codec :refer [form-encode form-decode]]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Macros
;; Helpers and Macros
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(def redactable-config-vars
#{:db-password
:admin-pass-default
:api-secret-default
:key-password})

(defn redact-config-vars
"Given a `config` map, replace redactable values with \"[REDACTED]\"
(if it is a string; keywords and symbols are treated similarly).
See `redactable-config-vars` for which vars are redactable."
[config]
(w/postwalk (fn [node]
(if (map-entry? node)
(let [[k v] node]
(if (contains? redactable-config-vars k)
;; We only consider strings and similar because
;; other vals should immediately cause an assertion
;; error and never be meaningful passwords.
(cond
(string? v) [k "[REDACTED]"]
(keyword? v) [k :redacted]
(symbol? v) [k 'redacted]
:else [k v])
[k v]))
node))
config))

(defmacro assert-config
[spec component-name config]
`(when-some [err# (s/explain-data ~spec ~config)]
`(when-some [err# (s/explain-data ~spec ~(redact-config-vars config))]
(do
(log/errorf "Configuration errors:\n%s"
(with-out-str (s/explain-out err#)))
Expand All @@ -19,6 +47,17 @@
{:type ::invalid-config
:error-data err#})))))

(comment ; TODO: Turn these into tests
(s/explain :lrsql.spec.config/database
(redact-config-vars {:db-type "h2:mem"
:db-name "foo"
:db-password 100}))
(assert-config :lrsql.spec.config/database
"database"
{:db-type "h2:mem"
:db-name "foo"
:db-password "swordfish"}))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; JDBC Config
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
6 changes: 3 additions & 3 deletions src/main/lrsql/system/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[com.yetanalytics.lrs.pedestal.interceptor :as i]
[lrsql.admin.routes :refer [add-admin-routes]]
[lrsql.spec.config :as cs]
[lrsql.system.util :refer [assert-config]]
[lrsql.system.util :refer [assert-config redact-config-vars]]
[lrsql.util.cert :as cu]))

(defn- service-map
Expand Down Expand Up @@ -70,7 +70,7 @@
(assert-config ::cs/webserver "webserver" config)
(if server
(do (log/info "Webserver already started; do nothing.")
(log/debugf "Server map: %s" server)
(log/debugf "Server map: %s" (redact-config-vars server))
this)
(if lrs
(let [service (or service ;; accept passed in
Expand All @@ -92,7 +92,7 @@
host
ssl-port)))
(log/info logo)
(log/debugf "Server map: %s" server)
(log/debugf "Server map: %s" (redact-config-vars server))
;; Return new webserver
(assoc this
:service service
Expand Down