View Source Getting Started

This short guide will help you get up to speed with using this library. It will use API credentials and interactation as a guiding example, but the details don't matter.

For context, the relevant things to know about this example are:

  • API username and password are available in environment variables and are considered to be highly sensitive
  • API authentication happens via basich auth
  • fetch_data_from_api makes a call to the authenticated API, for which it requires access credentials already in the basic auth format

Create a Wrapper Module

First we need to define a wrapper module to hold sensitive data:

defmodule MyApp.SecretData do
  use SensitiveData.Wrapper
end

This MyApp.SecretData module implements the SensitiveData.Wrapper behaviour, so feel free to read more about it in the documentation.

Wrap Sensitive Data

Let's fetch the sensitive credentials from the environment, and convert them into a more convenient form:

alias MyApp.SecretData

api_credentials =
  SecretData.from(fn ->
    user = System.fetch_env!("API_USER")
    password = System.fetch_env!("API_PASSWORD")
    Base.encode64("#{user}:#{password}")
  end)

Interact with Wrapped Data

With our credentials available in a wrapper, we can now make use of this sensitive information safely via SensitiveData.Wrapper.exec/3:

alias MyApp.SecretData

{:ok, _api_call_result} =
  SecretData.exec(api_credentials, fn basic_auth ->
    fetch_data_from_api(basic_auth: basic_auth)
  end)

This way if there's any issue, we can be sure that no sensitive information (e.g., API authentication credentials) will leak through stack traces, crash dumps, runtime state inspection, and so on.