Skip to content

Commit

Permalink
replace insecure_skip_tls_verify with auto set sni
Browse files Browse the repository at this point in the history
  • Loading branch information
mruoss committed Dec 16, 2023
1 parent e797b9a commit 8893f2e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
3 changes: 2 additions & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
erlang 26.2
elixir 1.15.7
elixir 1.15.7
kind 0.20.0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Configure the flame backend in our configuration or application setup:
children = [
{FLAME.Pool,
name: MyApp.SamplePool,
backend: {FLAMEK8sBackend, insecure_skip_tls_verify: true},
backend: FLAMEK8sBackend,
min: 0,
max: 10,
max_concurrency: 5,
Expand Down
9 changes: 3 additions & 6 deletions lib/flame_k8s_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ defmodule FLAMEK8sBackend do
* `:log` - The log level to use for verbose logging. Defaults to `false`.
* `:insecure_skip_tls_verify` - Skip TLS hostname verification. This is
required if you connect to your cluster via IP address.
### Prerequisites
In order for this to work, your application needs to meet some requirements.
Expand Down Expand Up @@ -172,7 +169,7 @@ defmodule FLAMEK8sBackend do
log: false,
req: nil

@valid_opts ~w(app_container_name insecure_skip_tls_verify runner_pod_tpl terminator_sup log)a
@valid_opts ~w(app_container_name runner_pod_tpl terminator_sup log)a
@required_config ~w()a

@impl true
Expand Down Expand Up @@ -205,7 +202,7 @@ defmodule FLAMEK8sBackend do
|> FLAME.Parent.new(self(), __MODULE__)
|> FLAME.Parent.encode()

{:ok, req} = K8sClient.connect(Keyword.take(provided_opts, [:insecure_skip_tls_verify]))
{:ok, req} = K8sClient.connect()

case K8sClient.get_pod(req, System.get_env("POD_NAMESPACE"), System.get_env("POD_NAME")) do
{:ok, base_pod} ->
Expand Down Expand Up @@ -250,7 +247,7 @@ defmodule FLAMEK8sBackend do
@impl true
def system_shutdown do
# This is not very nice but I don't have the opts on the runner
{:ok, req} = K8sClient.connect(insecure_skip_tls_verify: true)
{:ok, req} = K8sClient.connect()
namespace = System.get_env("POD_NAMESPACE")
name = System.get_env("POD_NAME")
K8sClient.delete_pod!(req, namespace, name)
Expand Down
19 changes: 10 additions & 9 deletions lib/flame_k8s_backend/k8s_client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ defmodule FLAMEK8sBackend.K8sClient do
@pod_tpl "/api/v1/namespaces/:namespace/pods/:name"
@pod_list_tpl "/api/v1/namespaces/:namespace/pods"

def connect(opts) do
def connect() do
ca_cert_path = Path.join(@sa_token_path, "ca.crt")
token_path = Path.join(@sa_token_path, "token")

verify =
if Keyword.get(opts, :insecure_skip_tls_verify, false),
do: :verify_none,
else: :verify_peer

apiserver_host = System.get_env("KUBERNETES_SERVICE_HOST")
apiserver_port = System.get_env("KUBERNETES_SERVICE_PORT")
apiserver_host_charlist = String.to_charlist(apiserver_host)
apiserver_port = System.get_env("KUBERNETES_SERVICE_PORT_HTTPS")

sni =
case :inet.parse_address(apiserver_host_charlist) do
{:ok, _} -> :disable
{:error, _} -> apiserver_host_charlist
end

with {:ok, token} <- File.read(token_path),
{:ok, ca_cert_raw} <- File.read(ca_cert_path),
Expand All @@ -24,7 +25,7 @@ defmodule FLAMEK8sBackend.K8sClient do
Req.new(
base_url: "https://#{apiserver_host}:#{apiserver_port}",
headers: [{:Authorization, "Bearer #{token}"}],
connect_options: [transport_opts: [cacerts: [ca_cert], verify: verify]]
connect_options: [transport_opts: [cacerts: [ca_cert], server_name_indication: sni]]
)
|> Req.Request.append_response_steps(verify_2xs: &verify_2xs/1)

Expand Down
2 changes: 1 addition & 1 deletion test_support/integration_test_runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule FlameK8sBackend.IntegrationTestRunner do
min: 0,
max: 2,
idle_shutdown_after: 1_000,
backend: {FLAMEK8sBackend, insecure_skip_tls_verify: true},
backend: {FLAMEK8sBackend},
log: :debug
}]

Expand Down

0 comments on commit 8893f2e

Please sign in to comment.