Search code examples
jenkinsvbscriptasp-classic

How to run a Jenkins job from classic ASP?


I have searched all over but can't find VBscript examples and all my attempts have failed. I need to run a Jenkins job from a classic ASP web page (VBScript). I can submit the job with the code below, but it returns a 403 crumb error. What I need to do is provide the user/password (which I have) for this job but I don't know how to setup the authentication for Jenkins in VBScript. I know the crumb error is due to CSRF (I read that much and can't turn that off) and hope that authentication will resolve that. Any help is much appreciated. Thanks in advance.

Dim strJenkinsURL, HttpReq
strJenkinsURL = "http://<jenkinsmaster>/job/<myjob>/buildWithParameters?token=test&Description="& strDesc &"&TestEnv="& testEnv

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "POST", strJenkinsURL, False
HttpReq.Send
Response.Write "<br>Status: "& HttpReq.Status & vbNewline
Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

EDIT: Based on comments, I attempted to add the Jenkins login information but I am still getting the 403 crumb error from Jenkins. I've tried searching for solution to getting the crumb, but haven't found any VBScript examples. Here is the code and response I am trying now but I have no idea if the setRequestHeaders are correct for Jenkins and the Jenkins documentation hasn't been any help:

Dim strJenkinsURL
strJenkinsURL = "http://<jenkins master>/job/testjob/buildWithParameters?token=test&Description="& strDesc &"&TestEnv="& testEnv

Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
HttpReq.Open "POST", strJenkinsURL, False
HttpReq.setRequestHeader "UserName", "jenkinsuser"
HttpReq.setRequestHeader "Password", "userpassword"
HttpReq.setRequestHeader "Jenkins-Crumb", "<crumbvalue>"
HttpReq.Send
Response.Write "<br>Status: "& HttpReq.Status & vbNewline
Response.Write "<br>Response: "& HttpReq.responseText & vbNewline

Status: 403 Response: HTTP ERROR 403 Problem accessing /job/testjob/buildWithParameters. Reason: No valid crumb was included in the request


Solution

  • After piecing together many suggestions with some trial and error, I was finally able to get the Jenkins job to run remotely with the VBScript code below (with variables set to proper values such as strCrumb). The returned Status will be 201 for success. The Base64 functions were taken from Base64 Encode String in VBScript

    Dim strJenkinsURL, strParam1, strParam2, strCrumb, strUsername, strPassword
    strJenkinsURL = "https://<jenkinsURL>/job/testjob/buildWithParameters?token=test&param1="& strParam1 &"&param2="& strParam2
    
    Set HttpReq = Server.CreateObject("MSXML2.ServerXMLHTTP")
    HttpReq.Open "POST", strJenkinsURL, False
    HttpReq.setOption 2, 13056
    HttpReq.setRequestHeader "Jenkins-Crumb", strCrumb
    HttpReq.setRequestHeader "Authorization", "Basic "& Base64Encode(strUsername &":"& strPassword)
    HttpReq.Send
    Response.Write "<br>Status: "& HttpReq.Status & vbNewline
    Response.Write "<br>Response: "& HttpReq.responseText & vbNewline
    
    Function Base64Encode(sText)
        Dim oXML, oNode
        Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
        Set oNode = oXML.CreateElement("base64")
        oNode.dataType = "bin.base64"
        oNode.nodeTypedValue = Stream_StringToBinary(sText)
        Base64Encode = oNode.text
        Set oNode = Nothing
        Set oXML = Nothing
    End Function
    
    Private Function Stream_StringToBinary(Text)
        Const adTypeText = 2
        Const adTypeBinary = 1
        Dim BinaryStream 'As New Stream
        Set BinaryStream = CreateObject("ADODB.Stream")
        BinaryStream.Type = adTypeText
        BinaryStream.CharSet = "us-ascii"
        BinaryStream.Open
        BinaryStream.WriteText Text
        BinaryStream.Position = 0
        BinaryStream.Type = adTypeBinary
        BinaryStream.Position = 0
        Stream_StringToBinary = BinaryStream.Read
        Set BinaryStream = Nothing
    End Function