使用APACHE KNOX作为proxy访问web,同时开启SSO功能

创建topologies

cd /usr/hdp/current/knox-server
touch conf/topologies/haha.xml

haha.xml

<?xml version="1.0" encoding="utf-8"?>
<topology>
    <gateway>
        <provider>
            <role>authentication</role>
            <name>ShiroProvider</name>
            <enabled>false</enabled>

            <param>
                <name>main.ldapRealm</name>
                <value>org.apache.hadoop.gateway.shirorealm.KnoxLdapRealm</value>
            </param>
            <param>
                <name>main.ldapContextFactory</name>
                <value>org.apache.hadoop.gateway.shirorealm.KnoxLdapContextFactory</value>
            </param>
            <param>
                <name>main.ldapRealm.contextFactory</name>
                <value>$ldapContextFactory</value>
            </param>
            <param>
                <name>main.ldapRealm.contextFactory.url</name>
                <value>ldap://fsmanager</value>
            </param>
            <param>
                <name>main.ldapRealm.contextFactory.authenticationMechanism</name>
                <value>simple</value>
            </param>
            <param>
                <name>main.ldapRealm.userDnTemplate</name>
                <value>uid={0},ou=people,dc=hadoop,dc=apache,dc=org</value>
            </param>
            <param>
                <name>urls./**</name>
                <value>authcBasic</value>
            </param>
        </provider>
        <provider>
            <role>identity-assertion</role>
            <name>Default</name>
            <enabled>true</enabled>
        </provider>
        <provider>
            <role>hostmap</role>
            <name>static</name>
            <enabled>true</enabled>
            <param>
                <name>localhost</name>
                <value>sandbox,sandbox.hortonworks.com,fsmanager</value>
            </param>
        </provider>
        <provider>
            <role>federation</role>
            <name>SSOCookieProvider</name>
            <enabled>true</enabled>
            <param>
                <name>sso.authentication.provider.url</name>
                <value>https://fsmanager:8443/gateway/knoxsso/api/v1/websso</value>
            </param>
        </provider>
    </gateway>

    <service>
        <role>HAHAUIIII</role>
        <url>http://fsmanager:5000</url>
    </service>
</topology>

创建Server

mkdir -p /data/services/hahauiiii/2.4.0
touch {service, rewrite}.xml

service.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<service role="HAHAUIIII" name="hahauiiii" version="2.4.0">
    <routes>
        <!-- https://fsmanager:8443/gateway/haha/hahauiiii -->
        <route path="/hahauiiii/?**">
            <rewrite apply="HAHAUIIII/hahauiiii/inbound" to="request.url"/>
        </route>
        <!-- https://fsmanager:8443/gateway/haha/hahauiiii/v1/?op=LISTSTATUS -->
        <route path="/hahauiiii/v1/?**">
            <rewrite apply="HAHAUIIII/hahauiiii/inbound/version" to="request.url"/>
        </route>
    </routes>
<service>

rewrite.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rules>
    <rule dir="IN" name="HAHAUIIII/hahauiiii/inbound" pattern="*://*:*/**/hahauiiii">
        <rewrite template="{$serviceUrl[HAHAUIIII]}"/>
    </rule>

    <rule dir="IN" name="HAHAUIIII/hahauiiii/inbound/version" pattern="*://*:*/**/hahauiiii/{version}/?{**}">
        <rewrite template="{$serviceUrl[HAHAUIIII]}/{version}/?{**}"/>
    </rule>
</rules>

重新部署cluster

bin/knoxcli.sh redeploy --cluster haha

重新启动KNOX

bin/gataway.sh stop
bin/gateway.sh start

Server文件内容简介

Server 目录结构

data
└── services
    └── hahauiiii
        └── 2.4.0
            ├── rewrite.xml
            └── service.xml

service.xml

<service role="HAHAUIIII" name="hahauiiii" version="2.4.0">
    <routes>
        <route path="/hahauiiii/?**"></route>
    </routes>
</service>

<service role="HAHAUIIII">

  • 这里的role需要匹配 topology 文件中的 <topology><service><role> 属性

<service name="hahauiiii">

  • 这里的name需要匹配 <GATEWAY_HOME>/data/services 中相应的目录名称,这里是指hahauiiii这个目录名称

<service version="2.4.0">

  • 假如存在多个版本的server实现,version必须对应于相应的版本,及 <GATEWAY_HOME>/data/services, 这里是指2.4.0这个目录名称

<route path="/hahauiiii/?**"></route>

rewrite.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rules>
    <rule dir="IN" name="HAHAUIIII/hahauiiii/inbound" pattern="*://*:*/**/hahauiiii">
        <rewrite template="{$serviceUrl[HAHAUIIII]}"/>
    </rule>

<rule dir="IN">
<rule name="HAHAUIIII/hahauiiii/inbound">

  • 表明该条规则是应用于来自客户端的requests还是应用于gateway于对客户端response

<rule pattern="*://*:*/**/hahauiiii">

  • 匹配指定的URL,类似于正则表达式

<rewrite template="{$serviceUrl[HAHAUIIII]}"/>

{$serviceUrl[HAHAUIIII]} 会去寻找topologies中的指定ROLE中定义的URL, 这里指的是haha.xmlROLEHAHAUIIII中定义的URL<url>http://fsmanager:5000</url>


常见错误

500: rewrite.xml内容可能有问题,特别是partern可能有误
404:service.xml内容有问题,特别是path可能有误


Refenerce

Understanding Rewrite Rules for Apache Knox
Adding a service to Apache Knox
Knox SSO Integration for UIs


Appendix

创建临时可用的web服务代码,基于flask

from flask import Flask, request
app = Flask(__name__)


@app.route("/")
def hello():
    return "<h1>Hello World!</h1>"


@app.route("/v1/")
def version():
    return "<h1>Hello Stranger -> {0}</h1>".format(request.args.get("op"))


if __name__ == "__main__":
    app.run(host="0.0.0.0")
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值