Skip to content

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

@yoe

Description

@yoe

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

  • Pass a token in a header
  • Set the API server's CA certificate

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.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions