Question

I am able to change a build's description with the following program. This will change the build's description to "FOO FOO FOO". Unfortunately, my login doesn't work. Right now, this test Jenkins build server has no security on it. However, on our regular Jenkins server, you need to be logged in to change a build's description.:

#! /usr/bin/env perl

use 5.12.0;
use warnings;

use LWP::UserAgent;
use HTTP::Request::Common qw(POST);
use Data::Dumper;

use constant {
    JENKINS_BASE            => "http://build.vegibank.com/",
    USER_ID                 => "buildguy",
    PASSWORD                => "swordfish",
};

use constant {
    LOGIN_URL               => JENKINS_BASE . '/j_acegi_security_check',
    JOB_URL                 => JENKINS_BASE . '/job',
    SUBMIT_DESCRIPTION      => 'submitDescription',
};

my $job_number = 4;
my $job_name   = "proginator-2.0";
my $description = "FOO FOO FOO";

my $user_agent = LWP::UserAgent->new or die qq(Can't get User Agent);

#
# My Login Stuff (but it doesn't do anything w/ security off
#

my $response = $user_agent->request (
    POST LOGIN_URL,
    [
        j_username => USER_ID,
        j_password => PASSWORD,
    ],
);

$response = $user_agent->request (
    POST "@{[JOB_URL]}/$job_name/$job_number/@{[SUBMIT_DESCRIPTION]}",
    [
        description => "$description",
    ],
);

I'm trying to connect to the Jenkins login session, but I don't believe I'm doing it quite right. When I attempt to login, I get a 302 response and the following dump of my response object:

$VAR1 = bless( {
     '_protocol' => 'HTTP/1.1',
     '_content' => '',
     '_rc' => '302',
     '_headers' => bless( {
            'connection' => 'close',
            'client-response-num' => 1,
            'set-cookie' => 'JSESSIONID=1D5DF6FAF8714B2ACA4D496FBFE6E983; Path=/jenkins/; HttpOnly',
            'location' => 'http://build.vegicorp.com/;jsessionid=1D5DF6FAF8714B2ACA4D496FBFE6E983',
            'date' => 'Mon, 13 May 2013 20:02:35 GMT',
            'client-peer' => '10.10.21.96:80',
            'content-length' => '0',
            'client-date' => 'Mon, 13 May 2013 20:02:35 GMT',
            'content-type' => 'text/plain; charset=UTF-8',
            'server' => 'Apache-Coyote/1.1'
    }, 'HTTP::Headers' ),
     '_msg' => 'Moved Temporarily',
     '_request' => bless( {
            '_content' => 'j_username=buildguy&j_password=swordfish',
            '_uri' => bless( do{\(my $o = 'http://build.vegicorp.com/j_acegi_security_check')}, 'URI::http' ),
            '_headers' => bless( {
                   'user-agent' => 'libwww-perl/6.03',
                   'content-type' => 'application/x-www-form-urlencoded',
                   'content-length' => 42
     }, 'HTTP::Headers' ),
            '_method' => 'POST',
            '_uri_canonical' => $VAR1->{'_request'}{'_uri'}
     }, 'HTTP::Request' )
}, 'HTTP::Response' );

I figure I must be hitting a valid page since I'm getting a 302 code, but my fields might not be correct (or I'm going to the wrong page).

Can anyone help?

Était-ce utile?

La solution

My authorization is failing because ...what is the technical term? Oh yeah... "doing it all wrong."

After Googling and getting a lot of unhelpful junk, I, on a lark, decided to see if the Jenkins website might have something on this. And, it did right under a page called Authenticating scripted clients. In fact, they even give a Perl LWP example for a scripted client.

Ha ha, I was trying too hard. It seems that Jenkins will use the basic HTTP authentication mechanism, and I don't have to go through conniptions trying to figure out how their login form works. Apparently, Jenkins is simplifying the basic authentication mechanism for you even if your authentication mechanism is far from basic -- like a good program should do.

So, all I had to do was use the basic authentication mechanism.

my $browser = LWP::UserAgent->new or die qq(Cannot get User Agent);
my $request = HTTP::Request->new;
$request->authorization_basic(USER_ID, PASSWORD);
$request->method("GET");
$request->url("$jenkins_url");
my $response = $browser->request($request);
if ( not $response->is_success ) {
    die qq(Something went horribly wrong...);
}

Autres conseils

I've seen the redirect when the login is successful -- it sets the session cookie and redirects you to the main page.

Your post might be failing because the UA object isn't persisting the session cookie. Per the documentation, 'The default is to have no cookie_jar, i.e. never automatically add "Cookie" headers to the requests.' Try:

my $ua = LWP::UserAgent->new( cookie_jar => HTTP::Cookies->new() );

To store and reuse your session for the description change post.

(Also, credentials are visible in your header dump, may want to edit... Edit: I'm an idiot, they're in your constants too and're likely fake.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top