Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize dependencies, HTTP and JSON libraries #2

Merged
merged 5 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.1.0
* Remove eliver, HTTPoison, and Poison as dependencies
* Add Req as the module to make HTTP requests
* Add Plug as a dev dependency for mocking requests
* Rewrite all internals to use Req and Jason; public interface remains unchanged
* Update all outdated mix packages

## 2.0.0

* fixed wrong header sent with AWS IAM scenarios
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0
2.1.0
2 changes: 0 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,4 @@ import Config
#
# import_config "#{Mix.env}.exs"

config :vaultex, httpoison: HTTPoison

import_config "#{Mix.env()}.exs"
6 changes: 3 additions & 3 deletions config/dev.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Config

config :vaultex, httpoison: HTTPoison
config :vaultex, app_id: "foo"
config :vaultex, user_id: "bar"
config :vaultex,
app_id: "foo",
user_id: "bar"
2 changes: 0 additions & 2 deletions config/prod.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import Config

config :vaultex, httpoison: HTTPoison
16 changes: 12 additions & 4 deletions config/test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import Config

config :vaultex, httpoison: Vaultex.Test.TestDoubles.MockHTTPoison
config :vaultex, app_id: "foo"
config :vaultex, user_id: "bar"
config :vaultex, vault_addr: "http://localhost:8200"
config :vaultex,
req_opts: [
plug: Vaultex.VaultStub,
retry: false
],
app_id: "foo",
user_id: "bar",
vault_addr: "http://localhost:8200"

config :ex_aws,
access_key_id: "",
secret_access_key: ""

# Print only warnings and errors during test
config :logger,
level: :warning
189 changes: 0 additions & 189 deletions lib/test_doubles/mock_httpoison.ex

This file was deleted.

31 changes: 17 additions & 14 deletions lib/vaultex/auth.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule Vaultex.Auth do
@moduledoc """
Handles initial authentication to the Vault server.
"""

def handle(:approle, {role_id, secret_id}, state) do
handle(:approle, %{role_id: role_id, secret_id: secret_id}, state)
end
Expand All @@ -24,31 +28,24 @@ defmodule Vaultex.Auth do
end

def handle(:token, {token}, state) do
request(:get, "#{state.url}auth/token/lookup-self", %{}, [
{"X-Vault-Token", token},
{"Content-Type", "application/json"}
])
request(:get, "#{state.url}auth/token/lookup-self", nil, [{"x-vault-token", token}])
|> handle_response(state)
end

# auth method with usernames are expected to call `POST auth/:method/login/:username`
def handle(method, %{username: username} = credentials, state) do
request(:post, "#{state.url}auth/#{method}/login/#{username}", credentials, [
{"Content-Type", "application/json"}
])
request(:post, "#{state.url}auth/#{method}/login/#{username}", credentials, [])
|> handle_response(state)
end

# Generic login behavior for most methods
def handle(method, credentials, state) when is_map(credentials) do
request(:post, "#{state.url}auth/#{method}/login", credentials, [
{"Content-Type", "application/json"}
])
request(:post, "#{state.url}auth/#{method}/login", credentials, [])
|> handle_response(state)
end

defp handle_response({:ok, response}, state) do
case response.body |> Poison.decode!() do
defp handle_response({:ok, %Req.Response{} = response}, state) do
case response.body do
%{"errors" => messages} ->
{:reply, {:error, messages}, state}

Expand All @@ -60,11 +57,17 @@ defmodule Vaultex.Auth do
end
end

defp handle_response({_, %HTTPoison.Error{reason: reason}}, state) do
defp handle_response({:error, exception}, state) do
reason =
case exception do
%{reason: reason} -> reason
_ -> Exception.message(exception)
end

{:reply, {:error, ["Bad response from vault [#{state.url}]", reason]}, state}
end

defp request(method, url, params = %{}, headers) do
defp request(method, url, params, headers) do
Vaultex.RedirectableRequests.request(method, url, params, headers)
end
end
2 changes: 1 addition & 1 deletion lib/vaultex/aws_iam.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defmodule Vaultex.Auth.AWSIAM do

headers
|> Enum.into(%{})
|> Poison.encode!()
|> Jason.encode!()
end

defp maybe_add_role(credentials, nil), do: credentials
Expand Down
2 changes: 1 addition & 1 deletion lib/vaultex/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ defmodule Vaultex.Client do
@moduledoc """
Provides a functionality to authenticate and read from a vault endpoint.
"""

use GenServer

alias Vaultex.Auth, as: Auth
alias Vaultex.Read, as: Read
alias Vaultex.Write, as: Write
Expand Down
12 changes: 9 additions & 3 deletions lib/vaultex/delete.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
defmodule Vaultex.Delete do
def handle(key, state = %{token: token}) do
request(:delete, "#{state.url}#{key}", %{}, [{"X-Vault-Token", token}])
request(:delete, "#{state.url}#{key}", %{}, [{"x-vault-token", token}])
|> handle_response(state)
end

Expand All @@ -9,13 +9,19 @@ defmodule Vaultex.Delete do
end

defp handle_response({:ok, response}, state) do
case response.status_code do
case response.status do
204 -> {:reply, :ok, state}
error_code -> {:reply, {:error, error_code}, state}
end
end

defp handle_response({_, %HTTPoison.Error{reason: reason}}, state) do
defp handle_response({:error, exception}, state) do
reason =
case exception do
%{reason: reason} -> reason
_ -> Exception.message(exception)
end

{:reply, {:error, ["Bad response from vault [#{state.url}]", reason]}, state}
end

Expand Down
15 changes: 9 additions & 6 deletions lib/vaultex/leases.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ defmodule Vaultex.Leases do
def handle(:renew, lease, increment, state = %{token: token}) do
body = %{"lease_id" => lease, "increment" => increment}

request(:put, "#{state.url}sys/leases/renew", body, [
{"Content-Type", "application/json"},
{"X-Vault-Token", token}
])
request(:put, "#{state.url}sys/leases/renew", body, [{"x-vault-token", token}])
|> handle_response(state)
end

Expand All @@ -14,13 +11,19 @@ defmodule Vaultex.Leases do
end

defp handle_response({:ok, response}, state) do
case response.body |> Poison.decode!() do
case response.body do
%{"errors" => messages} -> {:reply, {:error, messages}, state}
parsed_resp -> {:reply, {:ok, parsed_resp}, state}
end
end

defp handle_response({_, %HTTPoison.Error{reason: reason}}, state) do
defp handle_response({:error, exception}, state) do
reason =
case exception do
%{reason: reason} -> reason
_ -> Exception.message(exception)
end

{:reply, {:error, ["Bad response from vault [#{state.url}]", reason]}, state}
end

Expand Down
Loading