Programmatically provide NiFi InvokeHTTP different certificates

北城余情 提交于 2019-12-04 02:09:32

问题


I have a requirement in Nifi where I have cycle through different HTTPS REST Endpoints and provide different certificates for some endpoints and different username / password for some other endpoints.

I used InvokeHTTP processor to send the requests, although URL takes an expression language, I cannot setup SSLContextService with an expression.

Alternatively, I thought on using ExecuteScript to call those Endpoints, however as listed here in StackOverflow post; I still don't know how to programmatically call an external service through a script.

Any help appreciated.


回答1:


just for fun created the groovy script that calls http.

for sure you can avoid using it. and I believe InvokeHTTP processor covers almost all needs.

However.. going to call test rest service: /post at https://httpbin.org

the flow: GenerateFlowFile (generates body) -> EcecuteGroovyScript (call service)

The body generated by GenerateFlowFile : {"id":123, "txt":"aaabbbccc"}

In ExecuteGroovyScript 1.5.0 declare the CTL.ssl1 property and link it to StandardSSLContextService

and now the script:

@Grab(group='acme.groovy', module='acmehttp', version='20180301', transitive=false)
import groovyx.acme.net.AcmeHTTP
import org.apache.nifi.ssl.SSLContextService.ClientAuth

def ff=session.get()
if(!ff)return
def http
ff.write{ffIn, ffOut->
    http = AcmeHTTP.post(
        url:    "https://httpbin.org/post", //base url
        query: [aaa:"hello", bbb:"world!"], //query parameters
        // send flowfile content (stream) as a body
        body:   ffIn,
        headers:[
            //assign content-type from flowfile `mime.type` attribute
            "content-type":ff.'mime.type' 
        ],
        // you can declare `CTX.ssl1`, `CTX,.ssl2`,... processor properties and map them to SSLContextService
        // then depending on some condition create different SSLContext
        // in this case let's take `CTL.ssl1` service to create context
        ssl:  CTL["ssl"+1].createSSLContext(ClientAuth.WANT),
        // the next commented line creates trust all ssl context:
        //ssl:  AcmeHTTP.getNaiveSSLContext(), 

        // the receiver that transfers url response stream to flowfile stream
        receiver:{respStream, httpCtx-> ffOut << respStream }
    )
}
//set response hesders as flow file attributes with 'http.header.' prefix
http.response.headers.each{ k,v-> ff['http.header.'+k]=v }
//status code and message
ff.'http.status.code' = http.response.code
ff.'http.status.message' = http.response.message
if( http.response.code < 400){
    //transfer to success if response was ok
    REL_SUCCESS << ff
}else{
    //transfer to failure when response code is 400+
    REL_FAILURE << ff
}


来源:https://stackoverflow.com/questions/49578492/programmatically-provide-nifi-invokehttp-different-certificates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!