{"id":38484,"date":"2016-07-29T09:42:49","date_gmt":"2016-07-29T04:12:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=38484"},"modified":"2016-07-29T11:58:51","modified_gmt":"2016-07-29T06:28:51","slug":"getting-started-with-application-authentication-via-kong-api-gateway","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/getting-started-with-application-authentication-via-kong-api-gateway\/","title":{"rendered":"Getting Started With Application Authentication Via Kong API Gateway"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-38485\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/kong2.png\" alt=\"kong2\" width=\"1448\" height=\"492\" \/><\/p>\n<p><span style=\"font-weight: 400\">Kong is an open-source, customizable, Nginx-based and scalable API middleware (API Gateway).Kong can be configured in front of any RESTful API and let the developers concentrate more on implementing business logic without caring about functionalities like authentication mechanism, rate limiting, logging, \u00a0internal communications between APIs, carrying out communication with public entities and other organizations.\u00a0It&#8217;s like a security layer \u00a0which sits in front of your application and enhances it&#8217;s performance.Kong provides full control over architecture and it&#8217;s currently used by many organizations including small and large ones.We can add many functionalities to Kong via plugins and it is easily customizable.<\/span><\/p>\n<p><span style=\"font-weight: 400\">Once you integrate Kong within your architecture, you will have full control over Kong\u2019s data.It&#8217;s built on Nginx and uses the robust database like Apache Cassandra and PostgreSQL.It also provides Admin interface to manage your APIs.You can make Kong scale as per your requirements in which stateless Kong servers talks to the single Cassandra or PostgreSQL database and act in the same manner.The client applications talks to Kong and then Kong acts as a\u00a0reverse proxy and routes the requests to the applications on the basis of managed plugins in Kong.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-38486\" src=\"\/blog\/wp-ttn-blog\/uploads\/2016\/07\/kong1.png\" alt=\"kong1\" width=\"565\" height=\"103\" \/><\/p>\n<p>Below is an illustration of integrating a RESTful API with Kong:<br \/>\n<strong>Requirements:<br \/>\n<\/strong>OS: ubuntu 14.04<br \/>\nkong version: 0.8.3<br \/>\nSingle Node Cassandra Database version: 2.2.7<\/p>\n<p><strong>Note:\u00a0<\/strong>Kong, by default, listens to API Requests on port 8000 and it&#8217;s RESTful admin interface runs on port 8001.<\/p>\n<p><b>Step 1: Adding an API to Kong:<\/b><\/p>\n<p>[js]curl -i -X POST \\<br \/>\n  &#8211;url http:\/\/localhost:8001\/apis\/ \\<br \/>\n  &#8211;data &#8216;name=app&#8217; \\<br \/>\n  &#8211;data &#8216;upstream_url=http:\/\/xxxxxxxxxx.com\/&#8217; \\<br \/>\n  &#8211;data &#8216;request_host=xxxxxxxxxx.com&#8217;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 201 Created<br \/>\nDate: Thu, 28 Jul 2016 03:33:39 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;upstream_url&#8221;:&#8221;http:\\\/\\\/xxxxxxxxxx.com\\\/&#8221;,&#8221;strip_request_path&#8221;:false,&#8221;id&#8221;:&#8221;f4cf8a4e-88fd-49f1-85d2-1dbaae256547&#8243;,&#8221;created_at&#8221;:1469676819000,&#8221;preserve_host&#8221;:false,&#8221;name&#8221;:&#8221;app&#8221;,&#8221;request_host&#8221;:&#8221;xxxxxxxxxx.com&#8221;}<\/p>\n<p><b>Step 2: Accessing API via Kong:<\/b><\/p>\n<p>[js]curl -i -X GET   &#8211;url http:\/\/localhost:8000\/heartbeat   &#8211;header &#8216;Host: xxxxxxxxxx.com&#8217; [\/js]<\/p>\n<p>HTTP\/1.1 200 OK<br \/>\nDate: Thu, 28 Jul 2016 03:36:21 GMT<br \/>\nContent-Type: application\/json;charset=UTF-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nServer: nginx\/1.4.6 (Ubuntu)<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nAccess-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE<br \/>\nAccess-Control-Max-Age: 3600<br \/>\nAccess-Control-Allow-Headers: Content-Type, x-requested-with, Content-Country , Content-Region, X-Auth-Token, Region, content-country, content-region, x-auth-token, region<br \/>\nX-Kong-Upstream-Latency: 9<br \/>\nX-Kong-Proxy-Latency: 0<br \/>\nVia: kong\/0.8.3<\/p>\n<p>{&#8220;CommitMessage&#8221;:&#8221;hooks dependency removed&#8221;,&#8221;CommitId&#8221;:&#8221;c003fe573436d3cee33c5753fd8ca7b12e08f1b8&#8243;,&#8221;mysql_status&#8221;:200,&#8221;status&#8221;:200,&#8221;home_hazelcast_status&#8221;:200,&#8221;CommitAuthor&#8221;:&#8221;yyyy &#8220;,&#8221;Version&#8221;:&#8221;qa-12.3.4&#8243;,&#8221;CommitMerge&#8221;:&#8221;&#8221;,&#8221;CommitDate&#8221;:&#8221;Fri Jul 15 14:43:04 2016 +0530&#8243;}<\/p>\n<p><b>Step 3: Enabling authentication plugin in Kong:<\/b><\/p>\n<p>[js]curl -i -X POST \\<br \/>\n  &#8211;url http:\/\/localhost:8001\/apis\/app\/plugins\/ \\<br \/>\n  &#8211;data &#8216;name=key-auth&#8217;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 201 Created<br \/>\nDate: Thu, 28 Jul 2016 03:37:58 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;api_id&#8221;:&#8221;f4cf8a4e-88fd-49f1-85d2-1dbaae256547&#8243;,&#8221;id&#8221;:&#8221;4e951c2e-3c24-4b23-95bb-13c96769ef6f&#8221;,&#8221;created_at&#8221;:1469677078000,&#8221;enabled&#8221;:true,&#8221;name&#8221;:&#8221;key-auth&#8221;,&#8221;config&#8221;:{&#8220;key_names&#8221;:[&#8220;apikey&#8221;],&#8221;hide_credentials&#8221;:false}}<\/p>\n<p><b>Step 4: Accessing API via Kong after enabling key-auth plugin:<\/b><\/p>\n<p>[js]curl -i -X GET \\<br \/>\n  &#8211;url http:\/\/localhost:8000\/ \\<br \/>\n  &#8211;header &#8216;Host: xxxxxxxxxx.com&#8217;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 401 Unauthorized<br \/>\nDate: Thu, 28 Jul 2016 03:39:11 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nWWW-Authenticate: Key realm=&#8221;kong&#8221;<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;message&#8221;:&#8221;No API Key found in headers, body or querystring&#8221;}<\/p>\n<p>As a result, Kong is blocking all requests without the authentication. Now, we have to add an authorized consumer to Kong to access the application.<\/p>\n<p><b>Step 5: Creating an authorized Consumer to access the API via Kong:<\/b><\/p>\n<p>[js]<br \/>\ncurl -i -X POST \\<br \/>\n  &#8211;url http:\/\/localhost:8001\/consumers\/ \\<br \/>\n  &#8211;data &quot;username=myuser&quot;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 201 Created<br \/>\nDate: Thu, 28 Jul 2016 03:41:40 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;username&#8221;:&#8221;myuser&#8221;,&#8221;created_at&#8221;:1469677300000,&#8221;id&#8221;:&#8221;364ae246-2965-43f5-b424-68d6b1dfe681&#8243;}<\/p>\n<p><b>Step 6: Creating an api-key for authorized Consumer:<\/b><\/p>\n<p>[js]curl -i -X POST \\<br \/>\n  &#8211;url http:\/\/localhost:8001\/consumers\/myuser\/key-auth\/ \\<br \/>\n  &#8211;data &#8216;key=mykey&#8217;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 201 Created<br \/>\nDate: Thu, 28 Jul 2016 03:42:52 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;key&#8221;:&#8221;mykey&#8221;,&#8221;consumer_id&#8221;:&#8221;364ae246-2965-43f5-b424-68d6b1dfe681&#8243;,&#8221;created_at&#8221;:1469677372000,&#8221;id&#8221;:&#8221;5c682d7c-8a62-473b-92a4-1127eb3a2d09&#8243;}<\/p>\n<p><b>Step 7: Validating the above set credentials for authorized Consumer:<\/b><\/p>\n<p>[js]curl -i -X GET \\<br \/>\n  &#8211;url http:\/\/localhost:8000\/heartbeat \\<br \/>\n  &#8211;header &quot;Host: xxxxxxxxxx.com&quot; \\<br \/>\n  &#8211;header &quot;apikey: mykey&quot;<br \/>\n[\/js]<\/p>\n<p>HTTP\/1.1 200 OK<br \/>\nDate: Thu, 28 Jul 2016 03:45:07 GMT<br \/>\nContent-Type: application\/json;charset=UTF-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nServer: nginx\/1.4.6 (Ubuntu)<br \/>\nAccess-Control-Allow-Origin: *<br \/>\nAccess-Control-Allow-Methods: POST, PUT, GET, OPTIONS, DELETE<br \/>\nAccess-Control-Max-Age: 3600<br \/>\nAccess-Control-Allow-Headers: Content-Type, x-requested-with, Content-Country , Content-Region, X-Auth-Token, Region, content-country, content-region, x-auth-token, region<br \/>\nX-Kong-Upstream-Latency: 9<br \/>\nX-Kong-Proxy-Latency: 0<br \/>\nVia: kong\/0.8.3<\/p>\n<p>{&#8220;CommitMessage&#8221;:&#8221;hooks dependency removed&#8221;,&#8221;CommitId&#8221;:&#8221;c003fe573436d3cee33c5753fd8ca7b12e08f1b8&#8243;,&#8221;mysql_status&#8221;:200,&#8221;status&#8221;:200,&#8221;home_hazelcast_status&#8221;:200,&#8221;CommitAuthor&#8221;:&#8221;yyyy &#8220;,&#8221;Version&#8221;:&#8221;qa-12.3.4&#8243;,&#8221;CommitMerge&#8221;:&#8221;&#8221;,&#8221;CommitDate&#8221;:&#8221;Fri Jul 15 14:43:04 2016 +0530&#8243;}<\/p>\n<p><b>Step 8: Kong blocking requests for unauthorized Consumer:<\/b><\/p>\n<p>[js]curl -i -X GET   &#8211;url http:\/\/localhost:8000\/heartbeat   &#8211;header &quot;Host: xxxxxxxxxx.com&quot; &#8211;header &quot;apikey: mykeynew&quot;[\/js]<\/p>\n<p>HTTP\/1.1 403 Forbidden<br \/>\nDate: Thu, 28 Jul 2016 03:51:14 GMT<br \/>\nContent-Type: application\/json; charset=utf-8<br \/>\nTransfer-Encoding: chunked<br \/>\nConnection: keep-alive<br \/>\nServer: kong\/0.8.3<\/p>\n<p>{&#8220;message&#8221;:&#8221;Invalid authentication credentials&#8221;}<\/p>\n<p><span style=\"font-weight: 400\">In this \u00a0way, we can do the API authentication via Kong keeping the backend application lightweight and focusing only on the product and Kong handling all the other services around the application.<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\"><br \/>\n<\/span><span style=\"font-weight: 400\">In my next blog, I\u2019ll be demonstrating some advanced use-cases with Kong API Gateway.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Kong is an open-source, customizable, Nginx-based and scalable API middleware (API Gateway).Kong can be configured in front of any RESTful API and let the developers concentrate more on implementing business logic without caring about functionalities like authentication mechanism, rate limiting, logging, \u00a0internal communications between APIs, carrying out communication with public entities and other organizations.\u00a0It&#8217;s [&hellip;]<\/p>\n","protected":false},"author":170,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":90},"categories":[1174,2348,7,1],"tags":[3817,3820,3824,3819,3816,3822,3821,3823,3818],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38484"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/170"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=38484"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/38484\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=38484"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=38484"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=38484"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}