背景:

1、近年来,微服务架构在大中型企业中的应用越来越广泛。在解决了单体架构所带来的负责性高、部署慢、创新性低的问题的同时,也带来了一些新的安全问题。在微服务的架构中,一个大的应用会被拆分成多个小的单一的服务提供出来,这些小的服务有自己的处理,有自己的数据库(也可以共用),业务系统开发的每个接口都需要独立实现和安全相关的认证、授权、限速,不但重复开发而且无法统一管理,容易产生遗漏。Kong是由Mashape开发的并且于2015年开源的一款API网关框架,使用kong以后,应用系统只需要专注自己的业务实现,通过的缓存、日志记录、认证、授权等均有kong来实现,同时kong也可以用于传统业务接口的保护,如短信轰炸、账户爆破、卡号证件号遍历等等。,笔者所在的单位也在积极拓展微服务架构,身为高级安全工程(背锅侠),如何解决api安全,是笔者的工作之一。

2、近日,某监管部门发文提示,某机构驻场人员通过伪造esb报文,向esb发送请求,esb未对报文的来源及有效性做校验,直接调用转账接口进行转账,造成资金损失。笔者思考,利用kong的api key认证模式能够解决该风险。

kong的工作模式

CLIENT
KONG SERVERS

当Kong运行时,每个对API的请求将先被Kong命中,然后这个请求将会被代理到最终的API。在requests和responses之间,Kong将会执行已经事先安装和配置好的任何插件,授权您的API。Kong是每个API请求的入口点(point)。

Kong的安装

环境:虚拟机centos7 硬盘20G以上

参考

1、安装依赖

安装pcre zlib openssl

sudo yum install -y pcre pcre-devel

sudo yum install -y zlib zlib-devel

sudo yum install -y openssl openssl-devel

安装postgresql(同样也可以使用cassandra)

sudo yum install -y https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-7-x86_64/pgdg-centos95-9.5-2.noarch.rpm

sudo yum install -y postgresql95-server postgresql95-contrib

sudo /usr/pgsql-9.5/bin/postgresql95-setup initdb

sudo systemctl enable postgresql-9.5.service

sudo systemctl start postgresql-9.5.service

suso systemctl status postgresql-9.5.service

配置postgresql sudo passwd postgres(修改密码为postgresql)

新建用户kon gsudo adduser kong

切换centos用户su postgres

进入控制台psql

修改管理员postgres密码\password postgres(这里也修改为postgresql)

创建数据库用户

create user kong with password ‘123456’;

// 为新用户建立数据库
create database kong owner kong;

// 把新建的数据库权限赋予 kong
grant all privileges on database kong to kong;

退出\q

登录 psql -U kong -d kong -h 127.0.0.1 -p 5432

这个时候会登录失败,修改

/var/lib/pgsql/9.5/data/pg_hba.conf

If you want to allow non-local connections, you need to add more
“host” records.
In that case you will also need to make PostgreSQL
listen on a non-local interface via the listen addresses
configuration parameter, or via the -i or -h command line switches.

TYPE DATABASE

USER
ADDRESS
“local” is
local

IPv4 local

host

IPv6 local

host
for Unix domain
connections:
connections:
socket

Allow replication connections from

replication privilege.

connectionsonly127.0.0.1/32localhost,by a127.0.0.1/32
: 1/128
user
with
local
host
iost
replication
replication
replication
postgres
postgres
postgres
METHOD
trust
trust
trust
the
trust
trust
trust

2、安装kong

下载

https://bintray.com/kong/kong-community-edition-rpm/download_file?file_path=centos/7/kong-community-edition-0.13.1.el7.noarch.rpm

安装

rpm -ivh kong-community-edition-0.13.1.el7.noarch.rpm

修改配置文件

sudo vi /etc/kong/kong.conf,添加自己开始设置的postgres的配置信息

database = postgres

pg_host =
127.0.0.1
pg_port —
- 5432
pg_user = kong
pg_password =
123456
pg_database = kong
pg_ssl = off
Determines which of PostgreSQL or Cassandra
this node will use as its datastore.
Accepted values are ‘postgres’ and
cassandra

The PostgreSQL host to connect to.

The port to connect to.

The username to authenticate if required.

The password to authenticate if required.

The database name to connect to.

Toggles client-server TLS connections

between Kong and PostgreSQL.

添加环境变量export KONG_SERF_PATH=”/usr/local/bin/serf”

启动kong kong start –c kong.conf

这个时候会报错,按照提示 kong migrations up

再次启动

kong的默认端口为8000 管理端口为8001

3、安装kong 图形化界面

yum install npm

npm install -g kong-dashboard

kong-dashboard start –kong-url http://127.0.0.1:8001

默认开放8080号端口,访问

192.168.1.101
Home
APIs
Consumers
Plugins
SNIs
Certificates
Upstreams
Search
Welcome to Kong Dashboard
Kong dashboard is a central hub for you to manage your Kong setup.
Learn Kong
This Dashboard will let you interact with
your Kong API to create or edit APIs,
Consumers and Plugins.
Wondering what all that means? Have a
look at the Kong documentation.
DOCUMENTATION
Get started
Not sure where to start?
How about listing your current APIs or
Consumers, creating a new API or a new
Consumer?
CREATE AN API
Contribute
Kong Dashboard is an open source
project hosted on github.
Special thanks to 5 top contributors:
marceldiass
balexandre
nkCoding
GITHUB PAGE

4、安装lnmp

wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz

解压 tar -xzvf lnmpl.5.tar.gz

进入解压目录执行./install.sh lnmp 自动化安装

创建数据库farmsec 表 student 在/home/wwwroot/default/douwaf-api

目录下创建文件下farsec,创建文件users.php内容为

username=””;username=””;username=” ” ; uid
conmysqlconnect(“1271′‘root′if(conmysqlconnect(“1271′‘root′if(con mysql connect( “127 1’ ‘root’ if (con)
mysql select db(“farmsec” ,con);′root”);con);′root”);con); ’ root”); sql
‘select from users where uid=uid”;uid”;uid”; result
mysql query(sql);while(sql);while(sql); while(row = my'ql fetch array(result))result))result)) username —
row[‘username′];row[‘username′];row[ ‘username’]; email = row[‘email′];row[‘email′];row[ ‘email’]; phone = $row[ ‘phone’];
echo ;

创建3个用户分别为柯南 小六 和19

farmsec
New
users
information schema
mysql
performance schema
Show all
+ Options
Number of mws:
email
kaoquanyang
wangba
363636@qq.com
25
phone
1366666
13888888
1382772
Filter
uid
3
username
kenan
xiaoliu
shijiu
正常情况下,这个场景就相当于我们说的,通过遍历uid越权查看其他用户信息

访问http://192.168.1.101/farmsec/users.php?uid=1

@ 192.168.1.101/farmsec/users.php?uid=1
uid=l, username=kenan, email=kaoquanyang, phone=1366666

4、配置kong

配置基础转发服务

curl -i -X POST –url http://localhost:8001/apis/ –data ‘name=farmsec’ –data ‘hosts=127.0.0.1’ –data ‘upstream_url=http://127.0.0.1:80’

第一个–data 指定目录为farmsec,第二个data制定为本机 第三个–data 是要转发的url地址

创建成功

[root@localhost farmsec]# curl -i -X POST
e=farmsec
-data ‘hosts=127.0.0.1’
-data
HTTP/I.1 201 Created
Date: sun, 01 Jul 2018 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-0rigin: *
Server: kong/0.13.1
upstream
{“created at” :1530447738574, “strip uri” :true, id
” , “hosts , “name
” : “farmsec” , “http if terminated” :false, “preserve host” :fal
se, “upstream url •
” . “http:\/\/127.O.O.1:80
” , “upstream connect timeout” :60000, “upstream se
nd timeout” :60000, “upstream read timeout” :60000, ” retries” “https only” :false}
–url http://localhost:8001/apis/
uri=http://127.O.O.1:80’
- -data
’ nam
这时可以通过curlf访问kong的api服务,通过执行http header的HOST字段来区分不同的api服务

curl “http://127.0.0.1:8000/farmsec/users.php?uid=2” –header ‘HOST:127.0.0.1’

Not Found[root@localhost fcurl -i -X POST –url http://localhost:8001/apis/
e=farmsec’ –data ‘hosts=127.0.0.1’
-data
HTTP/I.1 201 Created
Date: sun, 01 Jul 2018 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-0rigin: *
Server: kong/0.13.1
-data
‘nam
upstream
uri=http://127.O.O.1:80’
{“created at” :1530448218066, “strip uri” :true, “Id
” , “hosts , “name
” : “farmsec” , “http if terminated” :false, “preserve host” :fal
se, “upstream url •
” . “http:\/\/127.O.O.1:80
” , “upstream connect timeout” :60000, “upstream se
nd timeout” :60000, “upstream read timeout” :60000, ” retries” “https only” :false}
[root@localhost farmsec]# Is
p.php users.php
[root@localhost farmsec]# curl “http://127.0.0.1:8000/farmsec/users.php?uid=2”
‘HOST:127.O.O.1
farmsec]#
-header

图形化界面的管理后台的api栏目中也多了一个api

192.168.1.101
Home
APIs
Consumers
Plugins
SNIs
Certificates
Upstreams
APIs
Name
farmsec
Host(s)
127.0.0.1
Uri(s)
Method(s)
Search
Upstream url
http://127.O.O.1:80
Created
Jul 1, 2018
eoo

使用kong进行key认证

1、创建consumer

curl -i -X POST –url http://127.0.0.1:8001/consumers/ –data “username=xiaodong” –data “custom_id=5”

[root@localhost farmsec]# curl -i -X POST –url http://127.0.0.1:8001/consumers/
“username=xiaodong” - -data “custom id=5”
HTTP/I.1 201 Created
Date: sun, 01 Jul 2018 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-0rigin: *
” username ” : “xiaodong
-data
Server: kong/0.13.1
{“custom id” : “5” , “created
[root@localhost farmsec]#
at” : 1530419836000 ,
” , ” id” : “a64f7411-a9fO-4
记下id

2、创建API key

curl -i -X POST –url http://127.0.0.1:8001/consumers/xiaodong/key-auth –data “key=a64f7411-a9f0-4e77-b042-a3a280079783”(这一点兜哥书中给的为-d “” 实际中可能由于版本问题无法实现)

[root@localhost farmsec]# curl
-i -X POST –url http://127.0.0.1:8001/consumers/xiaodon
g/key-auth
- -data “key=a64f7411
-a9fO-4e77-b042-a3a280079783”
HTTP/I.1 201 Created
Date: sun, 01 Jul 2018 GMT
Content-Type: application/json;
charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-0rigin: *
Server: kong/0.13.1
id” : “9eeOc95c-f945-46gg-9da5-19860a928ed4” , “created at” : 1530420003000, “key” : “a64f7411
-a9fO-4e77-b042-a3a280079783
” , “consumer id

分配的apikey 为64f7411-a9f0-4e77-b042-a3a280079783

开启api key认证功能

curl -i -X POST –url http://127.0.0.1:8001/apis/farmsec/plugins –data “name=key-auth” –data “config.hide_credentials=true”

[root@localhost farmsec]# curl -i -X POST –url http://127.0.0.1:8001/apis/farmsec/plug
-data “name—key-auth”
-data “config.hide credentials=true”
HTTP/I.1 201 Created
Date: sun, 01 Jul 2018 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-0rigin: *
Server: kong/0.13.1
{“created at” :1530420169000, “config” in body” :false, “hide credentials” :true, “anon
, ” • ” :iiff34c30f-afd2-4a24-9814
ymous” : , ” run on preflight” :true, “key names” : [“apikey”]} Id
-Ocgc2f36b65e -
” , “name” : “key-auth” , “api Id
” , “enabl
ed” : true}

api key验证

开启认证后,不允许直接访问原j接口

[root@localhost farmsec]# curl
‘HOST:127.O.O.1’
{“message” : “No API key found in
[root@localhost farmsecl#
“http : //127.0.0.1 : 8000/farmsec/users .
request”}
-header

Url也不允许访问

192.168.1.101
:8000/farmsec/users.php?uid=2
“message
. no route and no API found with those values
使用api认证访问

[root@localhost farmsec]# curl “http://127.0.0.1:8000/farmsec/users.php?uid=2”
‘HOST:127.O.O.1
-H “apikey:a64f7411-a9fO-4e77-b042-a3a280079783”
farmsec]#
-header

再看consumer

192.168.1.101
:8080/#!/consumers
Jul 1, 2018
Home
APIs
Consumers
Plugins
SNIs
Certificates
Upstreams
Consumers
Usemame
Mac
farmsec
kongtest —data customer_id=2
xiaodong
Custom id
2
(none)
5
Search
Created
Jun 30, 2018
Jun 30, 2018
Jun 30, 2018
eoo
eoo
eoo
eoo

此外kong还可以用于api访问限速、Bot检测等等。当然了,这只是一个简单的尝试,如果要应用于实际的业务场景还需要更多的探索,写出适合业务的插件。

参考:

https://github.com/xuxiangwork/Sharing/wiki/centos7-%E5%AE%89%E8%A3%85-kong-%E8%AF%A6%E8%A7%A3

《企业安全建设—基于开源软件打造企业网络安全》-刘焱

企业安全建设之API网关kong的搭建相关推荐

  1. 开源API网关Kong基本介绍和安装验证

    本文将介绍开源API网关Kong.在GitHub搜索API网关类的开源产品,可以看到Kong网关常年都是排第一的位置,而且当前很多都有一定研发能力的企业在API网关产品选项的时候基本也会选择Kong网 ...

  2. 开源syslog服务器_开源API网关Kong基本介绍和安装验证

    今天准备介绍下开源API网关Kong,在Gtihub搜索API网关类的开源产品,可以看到Kong网关常年都是排第一的位置,而且当前很多都有一定研发能力的企业在API网关产品选型的时候基本也会选择Kon ...

  3. API网关Kong(二):Kong与Kubernetes集成的方法

    作者: 李佶澳   转载请保留:原文地址   发布时间:2018-09-30 16:07:13 +0800 说明 先说组成 控制平面与数据平面 CustomResourceDefinitions 开始 ...

  4. API网关Kong(三):功能梳理和插件使用-基本使用过程

    作者: 李佶澳   转载请保留:原文地址   发布时间:2018-10-10 14:37:53 +0800 说明 Kong的Admin API Kong定义的资源之间的关联关系 使用过程了解 先了解下 ...

  5. 开源API网关Kong

    开源API网关Kong Kong 是一个在 Nginx 运行的 Lua 应用程序,由 lua-nginx-module 实现.Kong 和 OpenResty 一起打包发行,其中已经包含了 lua-n ...

  6. API 网关 Kong 实战

    1.Kong 介绍 Kong 是一款基于 OpenResty(Nginx + Lua 模块)编写的高可用.易扩展的,由 Mashape 公司开源的 API Gateway 项目.Kong 是基于 NG ...

  7. 微服务API网关-kong初探

    一 概述 Kong是一个clould-native.快速的.可扩展的.分布式的微服务抽象层(也称为API网关.API中间件或在某些情况下称为服务网格)框架.更确切地说,Kong是一个在Nginx中运行 ...

  8. Api网关Kong集成Consul做服务发现及在Asp.Net Core中的使用

     1622219047536 写在前面   Api网关我们之前是用 .netcore写的 Ocelot的,使用后并没有完全达到我们的预期,花了些时间了解后觉得kong可能是个更合适的选择. 简单说下 ...

  9. 原 docker(22):使用docker 搭建网关,Mashape API 网关 Kong,安装dashboard,和pgadmin4

    [其他docker相关分类]: https://blog.csdn.net/freewebsys/article/category/3103827 本文的原文连接是: http://blog.csdn ...

最新文章

  1. MATLAB【九】————ICP算法实现
  2. 在单链表和双链表中删除倒数第K个节点
  3. android theme 错误,为什么修改android:theme就崩溃,求助
  4. 优化-浏览器缓存和压缩优化
  5. python应用程序实例_python中一个非常简单的异步应用程序
  6. 【手写数字识别】基于matlab CNN网络手写数字识别分类【含Matlab源码 1286期】
  7. Adobe Flash Player v26.0.0.126发布:请尽快更新
  8. 办公室计算机收不到主机打印机,图文详解电脑怎么连接办公室打印机 一招教你搞定!...
  9. python DataFrame的shift()方法
  10. 如何提高网页的加载速度 ——优化网页图片加载
  11. 自定义gii生成模块
  12. JavaScript实战操作(DOM,BOM)
  13. 基于Yocto的qmake编译环境默认QMAKE_SPEC不是linux-oe-g++的问题
  14. English Learning NetSource
  15. JVM线程本地分配缓冲区(Thread Local Allocation Buffer)TLAB详解
  16. strip()函数使用方法
  17. 在暑期实习时需要注意些什么
  18. java程序自动重启_让Java程序自动重启的实现方法(推荐)
  19. Unity3D案例太空射击(Space Shooter)流程介绍与代码分析(上)
  20. Angular NG-ZORRO 解决 nz-table 加纵向滚动条表头和单元格不对齐的问题

热门文章

  1. java中判断字符串实际长度(中文2个字符,英文1个字符)的方法,包含中英文符号
  2. Photoshop设计icon
  3. 百度站长平台使用教程:快速收录
  4. 新版标准日本语初级_第十三课
  5. PySide2/PyQt5 设置关闭提示窗口
  6. Barycentric coordinate system(重心坐标系)
  7. 【蓝桥杯算法训练】无聊的逗
  8. matlab 互信息计算公式,matlab几种计算互信息的方法
  9. 思科模拟器 --- 路由器综合路由配置
  10. 【Nodejs】Too many levels of symbolic links