用于解析需要身份验证的Jenkins作业(config.xml)文件的Perl脚本

时间:2014-07-25 15:39:29

标签: xml perl parsing authentication jenkins

我试图解析Jenkins的工作" config.xml"文件。

这是我尝试使用 HTTP :: Tiny 的一种方法:

use HTTP::Tiny;

my $page = "http://localhost:8080/job/Job_One_Name/config.xml";
system("start $page");  # this loads the xml file in browser successfully

my $wsdlResponse = HTTP::Tiny->new->get($page);
$wsdlResponse->{success} or die print $logger->error_die($!); 
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});

print $wsdlXML;

它因错误而死,"错误的文件描述符"

如果我注释掉该行,那么脚本会在parse_file行中断开 错误讯息:

Could not create file parser context for file "<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjo
b%2FJob_One_Name/config.xml'/><script>window.location.replace('/login?from=%2Fjob%2FJob_One_Name/config.xml');</script></head><body style='background-color:white; color:white;'>                                                                                                                                                    

Authentication required                                                                                               
<!--                                                                                                                  
You are authenticated as: anonymous                                                                                   
Groups that you are in:                                                                                               

Permission you need to have (but didn't): hudson.model.Hudson.Read                                                    
 ... which is implied by: hudson.security.Permission.GenericRead                                                      
 ... which is implied by: hudson.model.Hudson.Administer                                                              
-->                                                                                                                   

</body></html>                                                                                                      
                ": Result too large at Job.pm line 67.  

我还尝试过在线不同的示例中的 XML :: Twig LibXML-&gt; load_xml load_html ,但他们所有人都死于类似&#34的错误;无法为文件创建文件解析器上下文&#34; &#34;错误的文件描述符&#34; 或< em>&#34;指定位置,字符串或IO&#34; 。

关于可能出现什么问题的任何想法?提前谢谢。

更新

关注来自Jenkins&#39;的脚本客户端的&#39; Perl LWP示例&#39; Authenticating scripted clients,我已成功通过身份验证:

my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my %options = {'myUserName' => 'myPassword'};
$req = HTTP::Request->new( GET => $serverHost );
$req->authorization_basic( 'myUserName', 'myPassword' );
my $res = $uagent->request($req);

# Check the outcome of the response
print "Result: " . $res->status_line . "\n";
print $res->headers->as_string;
print "\n";
if ( !$res->is_success ) {
    print "Failed\n";
} else {
    print "Success!\n";

    # Parse XML
    my $wsdlResponse = HTTP::Tiny->new->get($page, \%options);
    #$dom = XML::LibXML->load_xml(location=>$page);     
    #print $dom . "\n";
    my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
    print $wsdlXML;
    #print $res->content, "\n";
}

它仍然给我相同的&#39;需要验证&#39; 错误。我不知道如何使用成功授权的请求来解析文件。

3 个答案:

答案 0 :(得分:1)

您似乎需要在请求中提供身份验证。

它在浏览器中工作的原因很可能是您已经登录到Jenkins并且凭据在浏览器中缓存。当您从Perl代码中获取URL时,这些凭证不会出现。

您需要检查Jenkins如何维护会话状态(令牌/ cookie)并在Perl代码中执行相同的过程。

答案 1 :(得分:1)

我通过尝试使用&firefox&#39;做了类似的事情。在&#39; icognito模式&#39; - 所以我可以很容易地看到相关的cookie。 然后使用&#39; EditThisCookie&#39;或者&#39; cookie.txt export&#39;插件导出它。 然后我使用该导出中的值在perl中创建了一个cookie。

最终我发现不使用该界面更容易,而是使用SSH接口代替该服务器。

答案 2 :(得分:0)

我想出了一种使用经过身份验证的请求来输出结果的方法。由于请求输出xml内容以及标题响应,即:

HTTP/1.1 200 OK
Connection: close
Server: Jetty(8.y.z-SNAPSHOT)
Content-Type: application/xml
Client-Date: Wed, 30 Jul 2014 14:49:12 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1

<?xml version... 
<project>
  ....
  ....
</project>

我使用正则表达式提取输出的xml部分并将其保存到文件中。从那里我使用LibXML的 parse_file 方法来解析新写入的文件。 (我有一个单独的子例程,它使用 findnodes 方法来提取我想要提取的特定节点。我没有发布它,因为此解决方案处理我遇到问题的身份验证。)< / p>

<强>解决方案:

use strict;
use warnings;
use XML::LibXML;
use Path::Class;
use LWP;
use HTTP::Cookies;

my $serverXmlUrl = "http\://localhost:8080/job/jobName/config.xml";
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my $request = HTTP::Request->new( GET => $serverXmlUrl );
$request->authorization_basic( 'UserName', 'Password' );
my $result = $uagent->request($request);

if ( !$result->is_success ) {
    print "Failed\n";
} else {
    print "Success!\n";

    # Create new file to write to
    my $dir         = dir(".");
    my $file        = $dir->file("job.xml");
    my $file_handle = $file->openw();
    my $xml_content = $result->as_string; # full result output

    # Regex to store only xml content
    $xml_content =~ s/.*(<\?xml.*)/$1/s; 
    # Save xml_content to file
    $file_handle->print($xml_content);
    $file_handle->close();
    # Parse newly written file
    print "Parsing file... \n";
    my $parser = XML::LibXML->new();
    my $doc    = $parser->parse_file($file);
    print $doc;
}