Skip to content

Commit

Permalink
Merge pull request #4 from ndrluis/fix-auth
Browse files Browse the repository at this point in the history
Use the java implementation key for oauth uri configuration
  • Loading branch information
ndrluis committed Jul 8, 2024
2 parents cfbb253 + 6f90938 commit c484d58
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/ex_iceberg/rest/catalog.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule ExIceberg.Rest.Catalog do

defp authenticate(%Catalog{config: config, client: client} = catalog)
when config.credential != nil do
base_url = config.authorization_uri || config.uri
base_url = config.oauth2_server_uri || config.uri

optional_params =
%{audience: config.audience, resource: config.resource}
Expand Down Expand Up @@ -75,8 +75,8 @@ defmodule ExIceberg.Rest.Catalog do

defp authenticate(catalog), do: catalog

defp get_config(%Catalog{config: config} = catalog) do
Client.request(:get_config, config: config)
defp get_config(%Catalog{config: config, client: client} = catalog) do
client.request(:get_config, config: config)

# TODO: Parse config and merge
# (default_config (from api) + user_config + override_config(from api))
Expand Down
6 changes: 3 additions & 3 deletions lib/ex_iceberg/rest/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule ExIceberg.Rest.Config do
* `scope` - `openid offline corpds:ds:profile`: Desired scope of the requested security token (default: `catalog`).
* `resource` - `rest_catalog.iceberg.com`: URI for the target resource or service.
* `audience` - `rest_catalog`: Logical name of the target resource or service.
* `authorization_uri` - `https://auth-service/cc`: Authentication URL to use for client credentials authentication (default: `uri` + 'v1/oauth/tokens').
* `oauth2_server_uri` - `https://auth-service/cc`: Authentication URL to use for client credentials authentication (default: `uri` + 'v1/oauth/tokens').
"""

defstruct uri: nil,
Expand All @@ -20,7 +20,7 @@ defmodule ExIceberg.Rest.Config do
scope: "catalog",
resource: nil,
audience: nil,
authorization_uri: nil
oauth2_server_uri: nil

@type t :: %__MODULE__{
uri: String.t(),
Expand All @@ -30,7 +30,7 @@ defmodule ExIceberg.Rest.Config do
scope: String.t(),
resource: String.t(),
audience: String.t(),
authorization_uri: String.t()
oauth2_server_uri: String.t()
}

def new(options) do
Expand Down
50 changes: 42 additions & 8 deletions test/unit/rest/catalog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ defmodule ExIceberg.Rest.CatalogTest do
alias ExIceberg.Rest.Catalog

describe "new/2" do
defmodule DefaultMockClient do
def request(:get_token, config: _, base_url: _, form: _) do
{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

test "does not authenticate when credential does not exist" do
defmodule MockClient do
def request(:get_token, config: _, base_url: _, form: params) do
def request(:get_token, config: _, base_url: _, form: _) do
send(self(), :get_token)
end

def request(:get_config, _), do: {:ok, %{}}
end

Catalog.new(
Expand All @@ -31,6 +41,8 @@ defmodule ExIceberg.Rest.CatalogTest do

{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

defmodule MockClientWithResource do
Expand All @@ -44,6 +56,8 @@ defmodule ExIceberg.Rest.CatalogTest do

{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

Catalog.new(
Expand All @@ -66,6 +80,8 @@ defmodule ExIceberg.Rest.CatalogTest do

{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

defmodule MockClientWithSingleCred do
Expand All @@ -74,6 +90,8 @@ defmodule ExIceberg.Rest.CatalogTest do

{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

Catalog.new(
Expand All @@ -90,20 +108,36 @@ defmodule ExIceberg.Rest.CatalogTest do
end

test "merge token into config when authenticated" do
defmodule MockClient do
def request(:get_token, config: _, base_url: _, form: params) do
{:ok, "some_token"}
end
end

%{config: config} =
Catalog.new(
"catalog",
%{uri: "http://localhost:8181", credential: "foo"},
MockClient
DefaultMockClient
)

assert config.token == "some_token"
end

test "change the auth base url when oauth2_server_uri exists" do
defmodule MockClientCustomOauth do
def request(:get_token, config: _, base_url: base_url, form: _) do
assert base_url == "http://other_host.io"

{:ok, "some_token"}
end

def request(:get_config, _), do: {:ok, %{}}
end

Catalog.new(
"catalog",
%{
uri: "http://localhost:8181",
credential: "foo",
oauth2_server_uri: "http://other_host.io"
},
MockClientCustomOauth
)
end
end
end

0 comments on commit c484d58

Please sign in to comment.