how to pass authentication in http:put method at run time?

174 views Asked by At

I am running the below xqy code using a schedule task and in this xqy code, I am passing manually the value of authentication node with method="digest" username = admin password = admin in the put call, Is there any way so that I can pass it at runtime using
1. Encrpyted token for username and password
2. getting the value username and password of the server at runtime , and passing it to the http-put function to access the rest service extension (/v1/resources/example) at 8000 port.

xquery version "1.0-ml";
    
    declare namespace hst = 'http://marklogic.com/xdmp/status/host';   
    declare namespace c = 'http://example.com/abc';  
    declare variable $Collection := 'collection';
    
    let $query :=    
         cts:and-query((   
            cts:collection-query($Collection),  
            cts:element-range-query(xs:QName('c:element'), '<=', fn:current-dateTime())  
        ))
          
    let $uris := cts:uris('',(), $query)   
    let $total-uri := count($uris)  
    return   
        if ($total-uri) then  
        let $PORT := '8000'   
        let $database := xdmp:database()  
        let $host-list := xdmp:hosts()    
        let $hosts :=     
                for $hosts-online in $host-list      
                where xdmp:host-status($hosts-online)/hst:hosts/hst:host[.//hst:online/fn:string() = 'true']    
                return $hosts-online                        
        let $cluster-size := fn:count($hosts)     
        return
            for $host at $index in $hosts       
            let $page-size := fn:ceiling($total-uri div $cluster-size)       
            let $start-index := ($index - 1) * $page-size + 1       
            let $end-index :=       
                if ($cluster-size eq $index)  then      
                    $total-uri       
                else         
                    ($index - 1) * $page-size +$page-size        
            let $urisforHost := string-join($uris[$start-index to $end-index],',')        
    
            let $url  := fn:concat('http://',xdmp:host-name($host),':',$PORT,'/v1/resources/example?rs:uris=',$urisforHost,'&amp;rs:db=',$database)
            return
                xdmp:spawn-function(function()
                    {xdmp:http-put($url,
                        <options xmlns="xdmp:http">
                           <authentication method="digest">
                             <username>admin</username>
                             <password>admin</password>
                           </authentication>
                     </options>
                    )
                    }
                )
    
1

There are 1 answers

1
M.Warnes On

This can be achieved using Secure Credentials by creating a credential

You then reference the credential id in your HTTP request, for example

xdmp:http-get("http://ml-node-1:8002/manage",
  <options xmlns="xdmp:http">
    <credential-id>{xdmp:credential-id("my-credential-name)}</credential-id>
  </options>)