Programmatically provide NiFi InvokeHTTP different certificates

前端 未结 1 1163
梦毁少年i
梦毁少年i 2021-01-16 16:04

I have a requirement in Nifi where I have cycle through different HTTPS REST Endpoints and provide different certificates for some endpoints and different u

相关标签:
1条回答
  • 2021-01-16 16:31

    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
    }
    
    0 讨论(0)
提交回复
热议问题