From f5097b8dc75860861bbebad2a86912b2b4e1aa5f Mon Sep 17 00:00:00 2001 From: Zemuldo Date: Sun, 29 Oct 2023 10:19:31 +0300 Subject: [PATCH] set secret with options --- README.md | 11 +++++++++++ lib/ex_secrets.ex | 13 +++++++++---- lib/providers/google_secret_manager.ex | 13 +++++++++---- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a7ba0bc..e2b8b55 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ end ## Basic Usage +### Get a secret + Secrets are first fetched using system environment. If found thats the value that is used. For this, no configuration is required. ```elixir @@ -44,6 +46,15 @@ nil iex(7)> ``` +### Se Secret + +You can set a new secret version using: + +```elixir +iex(20)> ExSecrets.set("TEST", "test", provider: :azure_key_vault) +:ok +``` + ## Supported Providers You can configure: diff --git a/lib/ex_secrets.ex b/lib/ex_secrets.ex index ed284a1..1815944 100644 --- a/lib/ex_secrets.ex +++ b/lib/ex_secrets.ex @@ -134,13 +134,18 @@ defmodule ExSecrets do Calling this function requires the provider to be configured with credentials that allow create secrets like Secret Admionistrator in Azure Key Vault. """ - @spec set(atom(), String.t(), String.t()) :: :ok | :error - def set(provider, key, value) do - with provider when is_atom(provider) <- Resolver.call(provider), + @spec set(String.t(), String.t(), Keyword.t()) :: :ok | :error + def set(key, value, opts \\ []) + + def set(key, value, opts) do + with provider <- Keyword.get(opts, :provider), + provider when is_atom(provider) <- Resolver.call(provider), :ok <- Kernel.apply(provider, :set, [key, value]) do Cache.save(key, value) + :ok else - _ -> :error + _ -> + :error end end diff --git a/lib/providers/google_secret_manager.ex b/lib/providers/google_secret_manager.ex index 15d9937..7d2340d 100644 --- a/lib/providers/google_secret_manager.ex +++ b/lib/providers/google_secret_manager.ex @@ -212,10 +212,15 @@ defmodule ExSecrets.Providers.GoogleSecretManager do url = "https://secretmanager.googleapis.com/v1/projects/#{cred["project_id"]}/secrets/#{name}:addVersion" - client.post(url, Poison.encode!(payload), %{ - "Authorization" => "Bearer #{access_token}", - "content-type" => "application/json" - }) + client.post( + url, + Poison.encode!(payload), + %{ + "Authorization" => "Bearer #{access_token}", + "content-type" => "application/json" + }, + timeout: 30_000 + ) end defp get_access_token(cred) do