jhthorsen / openapi-client

A client for talking to an Open API powered server
11 stars 17 forks source link

Mojo::UserAgent attribute in constructor ignored for the constructor #40

Open yoe opened 1 year ago

yoe commented 1 year ago

I'm trying to connect to a Kubernetes API server. This requires me to do two things:

This is even true when trying to access the OpenAPI definition (at $apiserver/openapi/v2).

In order to do that, I prepared a Mojo::UserAgent like so:

my $token;
{
  local $/="";
  open my $token_f, "</run/secrets/kubernetes.io/serviceaccount/token";
  $token=<$token_f>
}
my $ua = Mojo::UserAgent->new;
$ua->on(prepare => sub($ua, $tx) {
  $tx->req->headers->header("Authorization: Bearer $token");
});
$ua = $ua->ca("/run/secrets/kubernetes.io/serviceaccount/ca.crt");
my $client = OpenAPI::Client->new("https://" . $ENV{KUBERNETES_SERVICE_HOST} . ":" . $ENV{KUBERNETES_SERVICE_PORT} . "/openapi/v2", ua => $ua);

However, OpenAPI::Client does not seem to use the Mojo::UserAgent at this point:

root@perl:~# perl ./test 
GET https://10.152.183.1:443/openapi/v2: SSL connect attempt failed error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed

One can bypass that by setting MOJO_CA_FILE, but that misses the header:

root@perl:~# MOJO_CA_FILE=/run/secrets/kubernetes.io/serviceaccount/ca.crt perl ./test 
GET https://10.152.183.1:443/openapi/v2: Forbidden at /usr/local/lib/perl5/site_perl/5.36.0/JSON/Validator/Store.pm line 190.
jhthorsen commented 1 year ago

I just rewrote a test to pass in $ua, which works, so I'm not sure how to help you 😞

yoe commented 1 year ago

It works, because the test doesn't actually test the problem described in this bug :)

It's true that the constructor correctly assigns the user agent to the object by the time the constructor exits.

However, it does not use the passed user agent to download the openapi definition that is also passed to it. This is because the definition is downloaded by the _url_to_class call on line 41 of lib/OpenAPI/Client.pm, but the assignment of the attributes is only done on the next line.

jhthorsen commented 1 year ago

Aah! Now I get it :) Thanks for explaining. Shouldn’t be too hard to fix…