diff --git a/lib/ex_iceberg/rest/catalog.ex b/lib/ex_iceberg/rest/catalog.ex index 400d897..8608812 100644 --- a/lib/ex_iceberg/rest/catalog.ex +++ b/lib/ex_iceberg/rest/catalog.ex @@ -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} @@ -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)) diff --git a/lib/ex_iceberg/rest/config.ex b/lib/ex_iceberg/rest/config.ex index 1669f62..6eb6aa1 100644 --- a/lib/ex_iceberg/rest/config.ex +++ b/lib/ex_iceberg/rest/config.ex @@ -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, @@ -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(), @@ -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 diff --git a/test/unit/rest/catalog_test.exs b/test/unit/rest/catalog_test.exs index 7fce06e..b8a4954 100644 --- a/test/unit/rest/catalog_test.exs +++ b/test/unit/rest/catalog_test.exs @@ -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( @@ -31,6 +41,8 @@ defmodule ExIceberg.Rest.CatalogTest do {:ok, "some_token"} end + + def request(:get_config, _), do: {:ok, %{}} end defmodule MockClientWithResource do @@ -44,6 +56,8 @@ defmodule ExIceberg.Rest.CatalogTest do {:ok, "some_token"} end + + def request(:get_config, _), do: {:ok, %{}} end Catalog.new( @@ -66,6 +80,8 @@ defmodule ExIceberg.Rest.CatalogTest do {:ok, "some_token"} end + + def request(:get_config, _), do: {:ok, %{}} end defmodule MockClientWithSingleCred do @@ -74,6 +90,8 @@ defmodule ExIceberg.Rest.CatalogTest do {:ok, "some_token"} end + + def request(:get_config, _), do: {:ok, %{}} end Catalog.new( @@ -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