Skip to content

Commit

Permalink
set secret with options
Browse files Browse the repository at this point in the history
  • Loading branch information
zemuldo committed Oct 29, 2023
1 parent 10e3055 commit f5097b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 9 additions & 4 deletions lib/ex_secrets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 9 additions & 4 deletions lib/providers/google_secret_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f5097b8

Please sign in to comment.