> 为了让开发自己能够去配置nginx转发规则,因此搭建了kong网关,将权限分配出去 #### 1.创建一个Docker网络 需要创建一个自定义网络,以允许容器相互发现和通信。在此示例中kong-net是网络名称,您可以使用任何名称 ``` $ docker network create kong-net ``` #### 2.启动数据库 使用PostgreSQL容器 ``` $ docker run -d --name kong-database \ --network=kong-net \ -p 5432:5432 \ -e "POSTGRES_USER=kong" \ -e "POSTGRES_DB=kong" \ postgres:9.6 ``` #### 3.准备数据库 使用临时Kong容器运行迁移 ``` $ docker run --rm \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ kong:latest kong migrations bootstrap ``` #### 4. 启动kong 迁移运行并且数据库准备就绪后,启动一个将连接到数据库容器的Kong容器,就像临时迁移容器一样 ``` $ docker run -d --name kong \ --network=kong-net \ -e "KONG_DATABASE=postgres" \ -e "KONG_PG_HOST=kong-database" \ -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \ -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \ -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \ -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \ -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \ -p 8000:8000 \ -p 8443:8443 \ -p 8001:8001 \ -p 8444:8444 \ kong:latest ``` #### 5.测试kong是否正常运行 ``` $ curl -i http://localhost:8001/ ``` 获取服务列表 ``` $ curl -i http://localhost:8001/services ``` #### 6. 搭建UI kong的UI开源流行的有两个,一个是kong-dashboard,另一个是konga,konga的开发相比之下比较活跃,本次使用它搭建,依然使用docker搭建 [dockerhub链接](https://hub.docker.com/r/pantsel/konga) ``` $ docker pull pantsel/konga $ docker run -p 1337:1337 --network kong-net --name konga -e "NODE_ENV=production" pantsel/konga ``` #### 7.设置konga管理员信息 浏览器访问宿主机的1337端口,设置密码  #### konga的ldap登录功能 konga的ldap登录功能目前有些bug,在实际搭建过程中,我们修改了ldap模块的部分代码,现可以接入正确读取ldap信息 配置文件信息.env ``` PORT=1337 NODE_ENV=production #NODE_ENV=deployment KONGA_HOOK_TIMEOUT=120000 DB_ADAPTER=mysql DB_URI=mysql://192.168.9.11:3306/konga KONGA_LOG_LEVEL=silly #TOKEN_SECRET=some_secret_token DB_USER=konga DB_PASSWORD=123456789 KONGA_AUTH_PROVIDER=ldap KONGA_LDAP_HOST=ldap://ad.test.com:389 KONGA_LDAP_BIND_DN=cas@test.com KONGA_LDAP_BIND_PASSWORD=test@12345 KONGA_LDAP_USER_SEARCH_BASE=OU=测试公司(上海),DC=test,DC=com KONGA_LDAP_USER_SEARCH_FILTER=sAMAccountName={{username}} KONGA_LDAP_USER_ATTRS=sAMAccountName,uSNCreated,givenName,sn,userPrincipalName,distinguishedName,memberOf KONGA_LDAP_GROUP_SEARCH_BASE=OU=测试公司(上海),DC=test,DC=com KONGA_LDAP_GROUP_SEARCH_FILTER==(|(sAMAccountName={{sAMAccountName}})) KONGA_ADMIN_GROUP_REG=^(delopyTest|admin|konga)$ KONGA_LDAP_GROUP_ATTRS=sAMAccountName KONGA_LDAP_ATTR_USERNAME=sAMAccountName KONGA_LDAP_ATTR_FIRSTNAME=givenName KONGA_LDAP_ATTR_LASTNAME=sn KONGA_LDAP_ATTR_EMAIL=userPrincipalName ``` - KONGA_AUTH_PROVIDER 可以是local和ldap认证,local使用本地数据库认证 - KONGA_LDAP_USER_ATTRS 是从ldap中提取的字段,可以被后续配置文件引用 #### ldap模块的修改 konga根目录下api/services/protocols/ldap.js 第9行 ``` var commonName = /^cn=([^,]+),.*/; 修改为 var commonName = /^(CN|cn)=([^,]+),.*/; ``` 第22-44行 ``` if (data.id) { sails.models.user.update({id: data.id}, data).exec(function(err) { if (err) { console.error("Failed to update user from ldap", err); next(err); } else { next(null, data); } }); } else { sails.models.user.create(data).exec(function (err, user) { if (err) { console.error("Failed to create user from ldap", err); next(err); } else { next(null, data); } }); } 修改为 if (data.id) { sails.models.user.update({id: data.id}, data).exec(function(err) { if (err) { console.error("Failed to update user from ldap", err); next(err); } else { data["id"] = user.id next(null, data); } }); } else { sails.models.user.create(data).exec(function (err, user) { if (err) { console.error("Failed to create user from ldap", err); next(err); } else { data["id"] = user.id next(null, data); } }); } 添加了 data["id"] = user.id,不然在ldap登录之后会序列化失败返回401错误 ``` 第51-53行 ``` var member_test = function (group) { // return adminGroup.test(commonName.replace(group, "$1")); return adminGroup.test(group.replace(commonName, "$2")); } 注释了 return adminGroup.test(commonName.replace(group, "$1")); 添加了 return adminGroup.test(group.replace(commonName, "$2")); 这里group是字符串, commonName是正则表达式,位置弄反了 ``` 修改后就可正确通过ldap登录konga啦! 最后修改:2019 年 07 月 26 日 04 : 08 PM © 允许规范转载
nb