以编程方式提供NiFi InvokeHTTP不同的证书 [英] Programmatically provide NiFi InvokeHTTP different certificates

查看:166
本文介绍了以编程方式提供NiFi InvokeHTTP不同的证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Nifi中有一个要求,我需要在不同的HTTP S REST端点之间循环,并为某些端点提供不同的证书,为其他端点提供不同的用户名/密码.

我使用InvokeHTTP处理器发送请求,尽管URL采用一种表达语言,但是我无法使用表达式设置SSLContextService.

或者,我考虑过使用ExecuteScript调用那些端点,但是作为解决方案

只是为了好玩而创建了一个名为http的groovy脚本.

确保可以避免使用它.而且我相信InvokeHTTP处理器可以满足几乎所有需求.

但是,..要致电测试休息服务:/post,位于 https://httpbin.org

流程:GenerateFlowFile(生成主体)-> EcecuteGroovyScript(呼叫服务)

GenerateFlowFile生成的主体:{"id":123, "txt":"aaabbbccc"}

ExecuteGroovyScript 1.5.0中声明CTL.ssl1属性并将其链接到StandardSSLContextService

现在是脚本:

 @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
}
 

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.

解决方案

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
}

这篇关于以编程方式提供NiFi InvokeHTTP不同的证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆