Skip to content

:before_env_set hook should be more flexible #58

@halostatue

Description

@halostatue

Two suggestions, but they sort of conflict with one another, so it might be better to have two different keys/hooks (only one should be configured):

  1. Allow mfa(): before_env_set: {MySecretManager, :resolve_secrets, []} which would be called as apply(mod, fun, [pair | args]).
  2. Allow a /2 function, which is not used by Map.new/2 directly, but is used with Enum.reduce/3. I'd love to specify this with mfa() as well, but there's some oddities involved that would need to be resolved.

Both of these are centred around the same issue, using an Elixir implementation of telia-oss/aws-env.

Using an MFA (entry 1), one could write a function that would parse values with sm:// or ssm:// or whatever and make an ExAws call to resolve the value. This would be inefficient, as ssm://foo/bar/baz showing up more than once would (of necessity) retrieve the same value. This could be mitigated with the process dictionary, but one would have no way of clearing that.

A different approach might be to do something like:

before_env_set: [collect: {MySecretManager, :collect, [%{}]}]

# elsewhere
defmodule MySecretManager do
  def collect(env, cache) do
    {vars, _cache} = Enum.reduce(env, {%{}, cache}, &resolve/2)
    vars
  end

  defp resolve({key, "sm://" <> lookup}, {acc, %{lookup => value} = cache}) do
    {Map.put(acc, key, value), cache}
  end

  defp resolve({key, "sm://" <> lookup}, {acc, cache}) 
    value = resolve_sm(lookup)
    {Map.put(acc, key, value), Map.put(cache, lookup, value)}
end

 defp resolve({key, value}, {acc, cache}), do: {Map.put(acc, key, value), cache}

I'm sure there's a better way to write this, but having an efficient way of resolving this without performing duplicate lookups would be very good. (There's more to what we have than what I've shown, but it's not really that different.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions