kura_redact (kura v2.31.3)

View Source

Keeps the values of sensitive fields out of logs and telemetry.

A schema names its sensitive fields with the optional redact/0 callback. Encrypted fields are always among them:

-module(account).
-behaviour(kura_schema).
-export([table/0, fields/0, redact/0]).

redact() -> [password_hash, reset_token].

An embedded schema names its own the same way. An embed is stored and logged as one value, so an embed whose schema lists a field, or embeds a schema that does, is replaced whole wherever its parent is redacted.

kura_schema:redacted_fields/1 returns the full list. Their values are replaced by ~"[redacted]":

  • in the params of the [kura, repo, query] telemetry event and of the query log, for a statement kura builds from a schema. Where the position of each value is known (an insert, an update or delete by key, an insert_all) only the redacted fields' values are replaced. For a query that filters on a redacted field, an update_all that sets one and an upsert, every value of the statement is replaced. A statement that touches no redacted field is shown as it is. A query is judged by every schema it reads: the one it is from, the ones it joins, and those of its subqueries, CTEs and combined queries. A condition anywhere in it counts as filtering on a redacted field when it names one of any of those schemas, so a plain column with the name of a redacted field of another source in the same query has its statement's values replaced too. A distance from a redacted vector field counts as a filter on it;
  • in what record/2 and changeset/1 return, for your own logging.

The same event's error_reason keeps only the error's code and the names it carries (constraint, table, column) for every statement, since a database error message can quote a value, or the whole failing row. With {telemetry_error_detail, true} in the kura application environment the whole error is reported for a statement that reads or writes no schema with redacted fields.

An insert or update of a changeset that fails with an error kura maps to no field reports it on base: as ~"database error 22P02", the error's code, for a schema with redacted fields, and with the database's message for any other. What a query, insert_all, update_all or delete_all returns as {error, Reason} is the driver's error as it is, message and detail included: log its code, not the term.

Raw SQL through kura_repo_worker:query/3 and {fragment, SQL, Params} values inside a query that names no redacted field are not redacted: kura cannot tell what they hold. Run raw SQL whose parameters are sensitive with kura_repo_worker:query/4 and #{sensitive => true}.

Summary

Functions

Return Changeset safe to log; not for writing.

Return Record, a map of Schema's fields, with the value of each redacted field replaced by ~"[redacted]". Safe to log.

Functions

changeset(Changeset)

-spec changeset(#kura_changeset{valid :: boolean(),
                                schema :: module() | undefined,
                                data :: map(),
                                params :: map(),
                                changes :: map(),
                                errors :: [{atom(), binary()}],
                                types :: #{atom() => kura_types:kura_type()},
                                required :: [atom()],
                                action :: atom() | undefined,
                                constraints ::
                                    [#kura_constraint{type :: unique | foreign_key | check | exclusion,
                                                      constraint :: binary(),
                                                      field :: atom(),
                                                      message :: binary()}],
                                assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]},
                                prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})],
                                optimistic_lock :: atom() | undefined}) ->
                   #kura_changeset{valid :: boolean(),
                                   schema :: module() | undefined,
                                   data :: map(),
                                   params :: map(),
                                   changes :: map(),
                                   errors :: [{atom(), binary()}],
                                   types :: #{atom() => kura_types:kura_type()},
                                   required :: [atom()],
                                   action :: atom() | undefined,
                                   constraints ::
                                       [#kura_constraint{type ::
                                                             unique | foreign_key | check | exclusion,
                                                         constraint :: binary(),
                                                         field :: atom(),
                                                         message :: binary()}],
                                   assoc_changes :: #{atom() => #kura_changeset{} | [#kura_changeset{}]},
                                   prepare :: [fun((#kura_changeset{}) -> #kura_changeset{})],
                                   optimistic_lock :: atom() | undefined}.

Return Changeset safe to log; not for writing.

The redacted fields' values are replaced in its data, changes and params, as record/2 replaces them, and so is every error message that holds one of those values: a message that quotes a redacted field's text or integer, as a database's invalid input syntax does, or what a replaced key of the params holds. The changesets in assoc_changes are redacted the same way.

[{base, ~"invalid input syntax for type uuid: \"tok-SECRET\""}] = CS#kura_changeset.errors,
[{base, ~"[redacted]"}] = (kura_redact:changeset(CS))#kura_changeset.errors.

params are what the caller was sent, under the keys it was sent with. For a schema with redacted fields or with associations, a value in params is shown only under a key that names a field, an embed or an association of the schema, as an atom, a binary or a string. The value under any other key is replaced and the key is kept, since a confirmation such as ~"password_confirmation" or a mistyped name can hold what a redacted field or an association's record holds. A confirmation declared as a virtual field is a field: list it in redact/0 beside the field it confirms. The params of an association are read the same way, by its schema.

#{~"email" := ~"a@b.c", ~"password" := ~"[redacted]", ~"password_confirmation" := ~"[redacted]"} =
    (kura_redact:changeset(CS))#kura_changeset.params.

Never raises, whatever the changeset holds. What kura cannot read is replaced, never passed through:

  • an error message that is not text (a binary, a string or an atom), and every message when data, changes or params is not a map, becomes ~"[redacted]". A message that is text is returned as a binary;
  • an error that is not a {Field, Message} tuple with an atom for a field becomes {Field, ~"[redacted]"} when it starts with one, and {base, ~"[redacted]"} when it does not;
  • a data, changes or params that is not a map becomes #{base => ~"[redacted]"};
  • an entry of assoc_changes that is not a changeset, a term that is not a changeset at all, and a changeset whose schema is not a schema or has a redact/0 that does not return a list of fields, become an invalid changeset whose only error is {base, ~"[redacted]"}.

That is for a schema with redacted fields or with associations. A changeset without a schema, or of a schema that has neither, has nothing redacted or replaced but its assoc_changes.

record(Schema, Record)

-spec record(module(), map()) -> map().

Return Record, a map of Schema's fields, with the value of each redacted field replaced by ~"[redacted]". Safe to log.

The records an association of Schema holds, such as preloaded ones, are redacted by the association's schema. A key is a field's name as an atom, a binary or a string; the value under any other key is replaced, since kura cannot tell which field it is.

Never raises. Where Record is not a map, Schema is not a schema, or its redact/0 does not return a list of fields, the result is #{base => ~"[redacted]"}. A schema with no redacted field and no association has nothing to hide, so its record is returned as it is, whatever its keys.