Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Authenticate via REST::Client against Rundeck API

by hdb (Monsignor)
on Mar 14, 2019 at 11:57 UTC ( [id://1231253]=note: print w/replies, xml ) Need Help??


in reply to Authenticate via REST::Client against Rundeck API

According to the API Reference you must retain a session cookie. I cannot see an option to specify a cookie jar in Rest::Client but there is an option to provide a LWP::UserAgent. So you might have to set up a UserAgent with a cookie jar and pass that to Rest::Client. Setting up an agent with cookie support could work like this:

my $myagent = LWP::UserAgent->new(); my $cookie_jar = HTTP::Cookies->new( autosave => 1, ignore_discard => 1, ); $myagent->cookie_jar( $cookie_jar );

Update: I cannot test this but it should look like

%options = ( "url" => "j_security_check", "j_username" => $username, "j_password" => $password, "useragent" => $myagent );

Update 2: The useragent that Rest::Client sets up if you do not provide one can be accessed as well. So it could also work like

my $cookie_jar = HTTP::Cookies->new( autosave => 1, ignore_discard => 1, ); $client->getUseragent()->cookie_jar( $cookie_jar );

Replies are listed 'Best First'.
Re^2: Authenticate via REST::Client against Rundeck API
by XavierFR (Novice) on Mar 14, 2019 at 12:36 UTC
    Thanks for your answer. Unfortunately this is not sufficient, as the library already provides a cookie jar :
    '_msg' => 'Forbidden', '_rc' => '403', '_headers' => bless( { 'connection' => 'close', 'client-ssl-cipher' => 'ECDHE-RSA-AES128-GCM-SHA256', 'set-cookie' => 'JSESSIONID=node0k6eor4wib9o5cbccpa3lkm5l220939.no +de0;Path=/;HttpOnly', 'date' => 'Thu, 14 Mar 2019 12:28:03 GMT', '::std_case' => { 'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer', 'x-application-context' => 'X-Application-Context', 'client-ssl-socket-class' => 'Client-SSL-Socket-Class', 'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject', 'client-peer' => 'Client-Peer', 'set-cookie' => 'Set-Cookie', 'client-ssl-cipher' => 'Client-SSL-Cipher', 'client-date' => 'Client-Date', 'client-response-num' => 'Client-Response-Num' }, 'client-response-num' => 1, 'client-date' => 'Thu, 14 Mar 2019 12:29:19 GMT', 'client-ssl-socket-class' => 'IO::Socket::SSL', 'client-peer' => 'XXSnipped at boss requestXX', 'client-ssl-cert-subject' => 'XXSnipped at boss requestXX', 'expires' => 'Thu, 01 Jan 1970 00:00:00 GMT', 'x-application-context' => 'application:production:4440', 'client-ssl-cert-issuer' => '/C=NL/ST=Noord-Holland/L=Amsterdam/O= +TERENA/CN=TERENA SSL CA 3', 'content-type' => 'text/xml;charset=utf-8' }, 'HTTP::Headers' )
    Xavier

      Sorry if it did not work. I have no access to a Rundeck instance so I cannot test. Just for my personal education: did you try my proposed code or did you conclude from the $res that I won't work?

        Hi, hdb,

        I tried your code, too. It actually looks like that :

        my $ua = LWP::UserAgent->new( 'agent' => basename($0), 'timeout' => $TIMEOUT, ); my $cookie_jar = HTTP::Cookies->new( file => "perlcookie.txt", autosave => 1, ignore_discard => 1, ); $ua->cookie_jar( $cookie_jar ); my $client = REST::Client->new( { host => $url, timeout => 10, useragent => $ua, follow => 1, } ); $client->addHeader ("content-type", 'application/x-www-form-urlencoded +'); $res = $client->POST( "j_security_check", "j_username=$username j_password=$password", ); print Dumper ($res); $res = $client->GET("api/14/system/info"); $rc = $client->responseCode (); print Dumper ($res); exit ( $rc);

        And cookie file contains that

        #LWP-Cookies-1.0 Set-Cookie3: JSESSIONID=node0npoji3aifa9r1c2xuxr6xvppe221132.node0; pa +th="/"; domain=[SNIP]; path_spec; discard; HttpOnly; version=0

        Unlike previous attempts, all HTTP transactions look good with this version of the code

        Xavier

        SOLVED (almost) !

        By default, LWP::UserAgent forwards only GET and HEAD requests. Adding POST gives me the login page.

        Xavier

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1231253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-16 05:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found