<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sachin Kumar Shukla on Medium]]></title>
        <description><![CDATA[Stories by Sachin Kumar Shukla on Medium]]></description>
        <link>https://medium.com/@skshukla.0336?source=rss-d72acce7de1b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-5Uu0yjMlaGBNXcl</url>
            <title>Stories by Sachin Kumar Shukla on Medium</title>
            <link>https://medium.com/@skshukla.0336?source=rss-d72acce7de1b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 20:09:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@skshukla.0336/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kafka Step-by-Step Cluster Setup in KRaft Mode with Prometheus/Grafana Monitoring (Part-1, Theory)]]></title>
            <link>https://medium.com/@skshukla.0336/kafka-step-by-step-cluster-setup-in-kraft-mode-with-prometheus-grafana-monitoring-part-1-theory-f2185433fb4d?source=rss-d72acce7de1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2185433fb4d</guid>
            <dc:creator><![CDATA[Sachin Kumar Shukla]]></dc:creator>
            <pubDate>Mon, 05 Aug 2024 16:38:01 GMT</pubDate>
            <atom:updated>2024-08-05T16:38:01.025Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong><em>What is Kafka</em></strong></h3><p>Kafka is a realtime messages streaming product which is designed to handle millions of messages per second. It’s a defacto standard in the industry for communicating the information (messages) across systems. By design it’s distributed, highly available, messages queuing system based on commit log.</p><h3><strong>KRaft (Kafka Raft)</strong></h3><p>This was introduced to remove the dependency on zookeeper. This protocol ensures the metadata is accurately replicated across the quarom. Out of several benefits the one which is most important that controller failover is almost instantaneous. For further info go through <a href="https://developer.confluent.io/learn/kraft/?utm_medium=sem&amp;utm_source=google&amp;utm_campaign=ch.sem_br.nonbrand_tp.prs_tgt.dsa_mt.dsa_rgn.apac_lng.eng_dv.all_con.confluent-developer&amp;utm_term=&amp;creative=&amp;device=c&amp;placement=&amp;gad_source=1&amp;gclid=Cj0KCQjw8MG1BhCoARIsAHxSiQk3R4vujXDuK8zCLb_6R6f0cu3JJWAEzSLgOK8Pp7alux4YaLxcUD4aAiJQEALw_wcB#benefits-of-kafkas-new-quorum-controller"><em>Benefits of Kafka’s new quorum controller</em></a></p><h3>Deployment Architecture Setup</h3><p>For this article, the setup is done in AWS cloud in “<strong>us-east-1</strong>” region with 3 machines (1 master where <strong><em>cluster id</em></strong> is generated using <strong>t2-medium</strong> and 2 other nodes using <strong>t2-small </strong>which use the same <strong><em>cluster id</em></strong>). All three machines form a Kafka cluster in KRaft mode. The Brokers also expose the JMX matrix on port <strong>7071</strong> via jmx prometheus agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tKT_F1lVqF2YW9GzUMfu4Q.jpeg" /></figure><ul><li>Kafka Cluster setup in KRaft mode with 3 machines in one region in AWS cloud</li><li>The ports are exposed as PLAINTEXT on <strong>9092</strong> to the outside world (so it can be tested from anywhere from development perspective, <strong><em>Though it’s HA as cluster but not secure intentionally to showcase and test the capabilities</em></strong>)</li><li>Quaram voting port is <strong>9097</strong>.</li><li>JMX metric is exposed on each Broker at port <strong>7071</strong> which is pulled by the Prometheus application deployed on Node-1 and it expose it as HTTP on port <strong>9090</strong>.</li><li>Finally, Grafana can use Prometheus as backend to show visually these matrices.</li></ul><h3>Cluster Configurations</h3><p>The cluster contains three machines, At the time of setup the Machines IP addresses were as below (<em>for reference</em>).</p><pre>a) Node-1 : PublicIP - 54.225.43.194, Private IP - 172.31.25.87<br>b) Node-2 : PublicIP - 54.164.113.180, Private IP - 172.31.24.244<br>c) Node-3 : PublicIP - 52.200.227.6, Private IP - 172.31.25.29</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oz--hy5GUmzc_vwig_ZEWA.png" /></figure><p>Kafka Home directory: “<em>/home/kafka/softwares/kafka</em>”</p><ul><li>Kafka installation contains <em>server.properties</em> under the folder “<em>/home/kafka/softwares/kafka/config/kraft</em>”. Make a copy as <em>server_{1/2/3}.properties</em> respectively for each node.</li><li>Adjust the properties accordingly. e.g. each node should have it’s unique <strong><em>node.id</em></strong> value, <strong><em>advertised.listeners</em></strong> should be it’s public IP (as it’s easy to demonstrate testing the cluster from anywhere including your personal laptop) and also <strong><em>controller.quorum.voters</em></strong> should have all the machine’s private ips.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FrWxsM111Ezq2ycLaLaFcQ.png" /></figure><ul><li>Generate the Cluster ID on the first node by issuing the command</li></ul><pre>${KAFKA_HOME}/bin/kafka-storage.sh random-uuid</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ms5B0Dopvqwm1pYKVEwH8Q.png" /></figure><ul><li>Use this cluster id to format the log</li></ul><pre>${KAFKA_HOME}/bin/kafka-storage.sh format -t n65AfD6sTk6u4nzxZGuncQ -c ${KAFKA_HOME}/config/kraft/server_1.properties</pre><ul><li>And, finally start the broker by issuing the command</li></ul><pre>${KAFKA_HOME}/bin/kafka-server-start.sh ${KAFKA_HOME}/config/kraft/server_1.properties</pre><ul><li>Repeat the same process (by using the same clusterID) in other nodes as well to form the cluster.</li></ul><h3>Prometheus Setup</h3><p>To read the JMX stats there are two things required to do</p><ul><li>On each Brokers Expose the JMX matrix to an end point. This can be archived by a <a href="https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/1.0.1/jmx_prometheus_javaagent-1.0.1.jar"><strong><em>jmx exporter agent</em></strong></a><strong><em> and </em></strong><a href="https://github.com/sks336/terraform-kafka/blob/main/resources_00_tmp/config/kafka_kraft.yml"><strong><em>kafka_kraft.yml</em></strong></a><strong><em> </em></strong>file. Setup KAFKA_OPTS while starting the server to achieve this.</li></ul><pre>export KAFKA_OPTS=&#39;-javaagent:${KAFKA_HOME_DIR}/softwares/dist/jmx_prometheus_javaagent-1.0.1.jar=7071:${KAFKA_HOME_DIR}/resources_00_tmp/config/kafka_kraft.yml&#39;</pre><ul><li>As each broker would expose this metric to their machine at this (7071) port, Run the prometheus application to fetch the metrics from each broker.</li></ul><pre>/home/kafka/softwares/prometheus/prometheus \<br>    --config.file /home/kafka/resources_00_tmp/config/prometheus.yml \<br>    --storage.tsdb.path /var/lib/prometheus/ \<br>    --web.console.templates=/home/kafka/softwares/prometheus/consoles \<br>    --web.console.libraries=/home/kafka/softwares/prometheus/console_libraries</pre><p>here, the prometheus.yml file would contain the job name, and the target machines ip/port to fetch these matrices from each broker.</p><pre>global:<br>  scrape_interval: 10s<br><br>scrape_configs:<br>  - job_name: &#39;kafka_job&#39;<br>    scrape_interval: 5s<br>    static_configs:<br>      - targets: [&#39;172.31.25.87:7071&#39;,&#39;172.31.24.244:7071&#39;,&#39;172.31.25.29:7071&#39;]</pre><p>Access the Prometheus UI then in URL: <strong><em>http://&lt;Node-1 IP&gt;:9090/graph</em></strong></p><p>under <strong>Status-&gt;Targets</strong> it would display the status of all the Kafka brokers</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_qscmx0kYJoCLScB40e_iw.png" /></figure><p>Typing any metric initial would list down all the suggestions to chose from.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UGQQCKjBJ0L1heqUA9xyGw.png" /></figure><p>Choosing a metric would display the information. e.g. if all the brokers are working then active broker count displayed by each machine is 3 in this case.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SCG0S1_Bh4d5MdQnVZyjcg.png" /></figure><p>In case one of the broker is brought down, then under target it would show it DOWN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TUGbf1fc475JPx011L6eKw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mcyVppXEfrzB-B8GyLbjNw.png" /></figure><h3>Grafana for Visualisation</h3><p>Grafana can be started by issuing the command in master node.</p><pre>${KAFKA_HOME_DIR}/softwares/grafana/bin/grafana server --homepath ${KAFKA_HOME_DIR}/softwares/grafana cfg:default.paths.logs=/var/log/grafana/grafana.log</pre><p>Once started, it can be accessed at <a href="http://54.225.43.194:3000/"><strong><em>http://&lt;NODE-1 IP&gt;:3000</em></strong></a></p><p>The default credentials are admin/admin which at first login would promt to change. Once inside, chose the prometheus as Data Source and enter the prometheus URL as shown below and save. This would setup Prometheus as the backend data source.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5acLSZ3lKaIH6bTnOwqWA.png" /></figure><p>Finally, under dashboard, create a dashboard by importing this <a href="https://github.com/sks336/terraform-kafka/blob/main/resources_00_tmp/config/kafka-overview.json"><strong><em>kafka-overview.json</em></strong></a> file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iAimDFouBCX8sXIAtRY8rw.png" /></figure><p>PS: <em>There is no fun if it cannot be tried at your end. Wait for the second article where I would explain how anyone with just having terraform and aws-cli setup can deploy it in just a few minutes without any internal details</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2185433fb4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MTLS-AWS API Gateway (Part-II)]]></title>
            <link>https://medium.com/@skshukla.0336/mtls-aws-api-gateway-part-ii-15d338385c65?source=rss-d72acce7de1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/15d338385c65</guid>
            <category><![CDATA[api-gateway]]></category>
            <category><![CDATA[ssl]]></category>
            <category><![CDATA[tlm]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[tls]]></category>
            <dc:creator><![CDATA[Sachin Kumar Shukla]]></dc:creator>
            <pubDate>Sat, 25 Mar 2023 10:51:34 GMT</pubDate>
            <atom:updated>2023-03-25T11:01:12.565Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This document is the Part — II of implementing the MTLS (Mutual TLS).</em></p><p><em>For understanding in depth about the MTLS in general, do visit</em> <a href="https://medium.com/@skshukla.0336/mtls-everything-you-need-to-know-e03804b30804">https://medium.com/@skshukla.0336/mtls-everything-you-need-to-know-e03804b30804</a></p><h3><strong>1. Introduction</strong></h3><p>While the previous part explains in depth about <strong>Mutual TLS</strong>, the focus of this document is on achieving the same via <strong>Amazon’s API Gateway</strong>.</p><p>The concepts being covered here are,</p><ul><li>Acquire the Certs from a valid Certificate Authority.</li><li>Make secure API call to the backend application running on EC2 via API Gateway</li></ul><h3>2. Setup</h3><p><strong>2.1 Acquire Certificates</strong></p><p>Amazon API Gatway requires the valid certificates from proper Certificate Authorities instead of Self Signed Certificate from the allowed Authorities. For this demo <a href="https://letsencrypt.org/">https://letsencrypt.org/</a> is used to issue the certs</p><p>(<strong><em>fullchain.pem</em></strong><em> is the certificate and </em><strong><em>privkey.pem</em></strong><em> is the private key. Domain name used to issue the certificate is: </em><strong>api-gateway-server.skshukla.com, </strong><em>Once Route-53 entry is made for this subdomain the certs can be obtained while running the commands in EC2 from the link above</em>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2gwxM9HIX4_LcFb0mZOPOQ.png" /></figure><p><strong>2.2 Create Key Store and Trust Store</strong></p><p>Use below command to create Key Store and Trust Store.</p><pre># Use certificate and private key to create P12 file<br><br>openssl pkcs12 -export -out server.p12 -name &quot;api-gateway-server.skshukla.com&quot; \<br>  -inkey privkey.pem -in fullchain.pem -passin pass:12345678 \<br>  -password pass:12345678<br><br># Create keystore from P12 file<br><br>keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 \<br>-destkeystore myserver_keystore.jks -deststoretype JKS  <br><br># Single command to create the Trust Store and import Certificate<br><br>keytool -import -trustcacerts -noprompt -alias ca \<br>  -ext san=dns:api-gateway-server.skshukla.com,ip:127.0.0.1 -file fullchain.pem \<br>  -keystore myserver_truststore.jks</pre><p><strong>2.3 Import Certificate to Trust Store</strong></p><p>As the backend service should allow only the valid Clients (API Gateway in this case), for MTLS, <strong>the API Gateway Certificate needs to be imported into the Trust Store</strong>.</p><p>API Gateway Certificate can be obtained by <strong><em>Client Certificate</em></strong> -&gt; <strong><em>Generate Client Certificate</em></strong> and click copy. This would copy the Certificate content in the Clipboard. Create a file “<strong><em>api_gateway.crt</em></strong>” with the copied content.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Of7yWJwO0y8mSadDvlDdcA.png" /></figure><p>Import the API Gateway Certificate to the trust store.</p><pre>keytool -import -trustcacerts -noprompt -alias ca-api-gateway \<br>  -ext san=dns:api-gateway-server.skshukla.com,ip:127.0.0.1 -file api_gateway.crt \<br>  -keystore myserver_truststore.jks</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-6cybB2Dn7BYtpZVeXDxrg.png" /></figure><p>Finally, ensure that MTLS is enabled with this value in <em>application.yaml</em> file and start the application. (Refer part-I to run the application from <a href="https://github.com/skshukla/tlsdemo">github</a>)</p><pre>client-auth: need<br>key-alias: api-gateway-server.skshukla.com</pre><ul><li><strong><em>client-auth: need</em></strong> ensures that Server would check client (Two way)</li><li><strong><em>key-alias</em></strong> needs to be correct and should match the domain.</li></ul><h3>3. Testing</h3><p>As the only client allowed is API gateway and hence either by passing<em> — cacert</em> option or <em>insecure (-k)</em> option would not work. Server rejects the request until it has come from API Gateway for which certificate is added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lzowsvR2zq9EUfikkr_Z1A.png" /></figure><p>In the API gateway, chose the “Integration Type” as HTTP and <strong><em>check “Use HTTP Proxy Integration” option</em></strong> and give Endpoint url as this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNo0NmLb5FgbTYLeS-zXrg.png" /></figure><p>After creating the API, chose the same certificate and hitting Test button would display the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hPhPMms8fRVmnLQq63itQg.png" /></figure><h3>4. Conclusion</h3><p>We can see by using MTLS the Server can accept the request only from the known clients and rejecting others. This would drastically improve the security of the system overall.</p><p><strong>Linkedin:</strong></p><p><a href="https://www.linkedin.com/in/sks336/">https://www.linkedin.com/in/sks336/</a></p><p><strong>YouTube:</strong></p><p><a href="https://www.youtube.com/channel/UCHkwNRHcXizsBiPE0smkJAA">https://www.youtube.com/channel/UCHkwNRHcXizsBiPE0smkJAA</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15d338385c65" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MTLS-Everything You need to know (Part-I)]]></title>
            <link>https://medium.com/@skshukla.0336/mtls-everything-you-need-to-know-e03804b30804?source=rss-d72acce7de1b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e03804b30804</guid>
            <category><![CDATA[tls]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[mutual-tls]]></category>
            <category><![CDATA[example]]></category>
            <category><![CDATA[certificate-authority]]></category>
            <dc:creator><![CDATA[Sachin Kumar Shukla]]></dc:creator>
            <pubDate>Sat, 25 Mar 2023 05:18:06 GMT</pubDate>
            <atom:updated>2023-03-25T15:45:52.787Z</atom:updated>
            <content:encoded><![CDATA[<p><em>For Accessing the Backend Service from Amazon API Gateway using MTLS, refer Part-II </em><a href="https://medium.com/@skshukla.0336/mtls-aws-api-gateway-part-ii-15d338385c65">https://medium.com/@skshukla.0336/mtls-aws-api-gateway-part-ii-15d338385c65</a></p><h3><strong>1. Introduction</strong></h3><p>This article describes how Client and Server can communicate securely with each other over Transport Layer Security (TLS).</p><p>The concepts being covered here are,</p><ul><li>One-Way TLS Authentication</li><li>Two-Way TLS Authentication</li></ul><h3>2. Terminology</h3><p>Before going for the theory and demo, let’s understand some of the concepts and terminologies around Secure Communications in general.</p><h4>2.1 <strong>Certificate Request</strong></h4><p>A Client or Server in order to acquire a valid certificate first need to create a <strong><em>CSR</em></strong> (Certificate Request) file which they need to submit to the <strong><em>Certificate Authority</em></strong> which can sign and issue a valid certificate</p><h4>2.2 Certificate Authority</h4><p>A Certificate Authority is a <em>trusted organization</em> who confirm the authenticity of a website (Server). Their primarily responsibilities are,</p><ul><li>Issue the Certificates to the Entities (Servers, Clients, Websites)</li><li>Confirm whether it’s the same Website which it claims it is.</li></ul><p>Few of the Certificate Authorities are <a href="https://www.verisign.com/">VeriSign</a>, <a href="https://www.digicert.com/">DigiCert</a>.</p><h4>2.3 Certificate</h4><p>A (public) certificate is what is issued to the Entity (Server, or Client). As CA issues the certificates they can confirm also whether it’s a valid certificate signed by them or not.</p><h4>2.3 SSL vs TLS</h4><p>SSL, Stands for, Server Socket Layer however TLS, Stand for, Transport Layer Security. SSL is just the older name.</p><h4>2.4 Public/Private keys</h4><ul><li><strong>Public Key</strong> is used to <em>Encrypt</em> the data. (Think of certificates. No harm passing the public key, certificate, to anyone publicly)</li><li><strong>Private Key</strong> is used to <em>Decrypt</em> the data, encrypted by Public key.</li><li>Private Key is like a password which should <em>never cross the boundry</em> of a system who has generated it. Consider it very secure. Think of it a system password which you don’t even share with your manager or any other person in authority.</li></ul><h3>3. One-Way Authentication</h3><p>To understand MTLS better, lets start first with the one-way authentication. Clients and Servers can be geographically apart, as they are <em>programs, machines/robots</em> and hence they need to confirm the other side’s <strong><em>identity </em></strong>they are communicating with in an automated manner.</p><figure><img alt="One-Way Transport Layer Security" src="https://cdn-images-1.medium.com/max/1024/1*phzdidlNLRzcx6fw_IotyQ.jpeg" /><figcaption><strong>1.0 One Way Transport Layer Security (TLS)</strong></figcaption></figure><ol><li>Server generates the <em>private key</em> and <em>CSR</em>. The CSR is just a request which needs to be traded for the valid certificate. Only CAs can issue the certificates.</li><li>CSR is presented to the CA.</li><li>CAs may verify the identity of Website (Server), which sometime may include a physical visit by them to the business premises in case of enterprise, and issue the public certificate. (Till this point it’s a one time setup)</li><li>Client requests the server and the HandShake process starts.</li><li>Server in tern returns its Certificate (public key) to the client.</li><li>Client (a program, browsers) can confirm the CAs whether it’s a valid certificate issued by them or not. <em>This step is where Client is assured whether the party with which Client is talking is the same which it claims it is. (Client has verified the identity of Server)</em></li><li>If all looks good, Communication starts (I am skipping the further handshake process as the focus is on setup and MTLS)</li></ol><h3>3. Two-Way Authentication</h3><p>In Two-way authentication both the Server and Client needs to validate their identity in terms of communication to happen. The way it differs from above case is when server gets the request and it does not identify the client then it would reject the request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dCUUeBByu9ecjJQhEJ3QvQ.jpeg" /></figure><ol><li>Server generates the <em>private key</em> and <em>CSR </em>as above</li><li>CSR is presented to the CA.</li><li>(a) CAs may verify the identity of Website (Server) and issue the public certificate. (b) Server would import the public certificate or CA (root cert) to its <strong><em>Trust Store </em></strong>that way <strong><em>any client who is issued a cert by “this Authority“ can communicate with the Server</em></strong>.</li><li>Client requests the server and the HandShake process starts.</li><li>Server in tern returns its Certificate (public key) to the client.</li><li>Client (a program, browsers) can confirm the CAs whether it’s a valid certificate issued by them or not. <em>This step is where Client is assured whether the party with which Client is talking is the same which it claims it is.</em></li><li>While making a call to the server, Client uses its <strong><em>Certificate</em></strong> and <strong><em>Client</em></strong> <strong><em>Key</em></strong> for the communication which is validated at the Server side to ensure the identity of client. Server consults Trust Store whether it’s a valid client to be given access or not. Please note, <strong><em>Client</em></strong> <strong><em>Key</em></strong> does not travel over network, it’s a private key and used to decrypt the data over the TCP tunnel.</li></ol><h3>3. Commands</h3><p>For this demo, let’s work with <strong><em>Self Signed Certificate</em></strong>. It’s easier to generate and work with Self Signed Certificate. The only problem is if accessing via browser it would complain as it does not recognise the certs. They are equally secure as the one generated by the CAs.</p><pre># OpenSSL version being used is &quot;OpenSSL 3.1.0&quot; (Older version gave some issues)<br># All password used here is 12345678 for simplicity<br><br># Generating Root Cert<br><br>openssl req -x509 -sha256 -days 3650 -newkey rsa:4096 \<br>  -keyout rootCA.key -out rootCA.crt -passout pass:12345678<br><br># Generating Server key and CSR<br><br>openssl req -new -newkey rsa:4096 -keyout server.key -out server.csr \<br>  -passout pass:12345678<br><br># Content of Server EXT file (server.ext)<br><br>    authorityKeyIdentifier=keyid,issuer<br>    basicConstraints=CA:FALSE<br>    subjectAltName = @alt_names<br>    [alt_names]<br>    DNS.1 = api-server.skshukla.com<br><br># Recall Step-3, CA would take CSR, use it&#39;s key and password <br># and issue Certificate<br><br>openssl x509 -req -CA rootCA.crt -CAkey rootCA.key -in server.csr \<br>  -out server.crt -days 365 -CAcreateserial -extfile server.ext \<br>  -passin pass:12345678<br><br># Generate p12 file (Contains the key and cert)<br><br>openssl pkcs12 -export -out server.p12 -name &quot;api-server.skshukla.com&quot; \<br>  -inkey server.key -in server.crt -passin pass:12345678 \<br>  -password pass:12345678<br><br># Create KeyStore<br><br>keytool -importkeystore -srckeystore server.p12 -srcstoretype PKCS12 \<br>-destkeystore myserver_keystore.jks -deststoretype JKS<br><br># For One Way TLS communication, Steps till generating KeyStore is sufficient.<br><br># Generate Trust Store<br><br>keytool -import -trustcacerts -noprompt -alias ca \<br>  -ext san=dns:${SERVER_CN},ip:127.0.0.1 -file rootCA.crt \<br>  -keystore myserver_truststore.jks<br><br># So far, Server setup is fully done. Now for any client to communicate <br># to the server, we need to generate the &quot;CLIENT&quot; certs and get it signed <br># by the same Authority whose cert is stored in Trust Store.<br><br># Client generates it&#39;s key and CSR<br><br>openssl req -new -newkey rsa:4096 -keyout client.key -out client.csr \<br>  -passout pass:12345678<br><br># CA use it&#39;s key and credentials along with the CSR and issue Cert<br><br>openssl x509 -req -CA rootCA.crt -CAkey rootCA.key -in client.csr \<br>  -out client.crt -days 365 -CAcreateserial -passin pass:12345678<br><br>openssl pkcs12 -export -out client.p12 -name &quot;client&quot; -inkey client.key \<br>  -in client.crt -passin pass:12345678 -password pass:12345678</pre><h3>4a. Make it work for One-Way TLS Authentication</h3><ul><li>Clone the Spring Boot Sample application.</li></ul><pre># My current working directory is: /Users/sachin/tmp/t2<br><br>git clone https://github.com/skshukla/tlsdemo.git</pre><ul><li>Refer the script at <a href="https://github.com/skshukla/tlsdemo/blob/master/scripts/script.sh">script in github</a>, the only variable to change there is (PROJ_DIR) and give the path for the project checked out. e.g. in my case. (<em>If needed, change the Common names too, but not mandatory for the demo to work</em>)</li></ul><pre>PROJ_DIR=/Users/sachin/tmp/t2/tlsdemo</pre><ul><li>Copy the whole content of <a href="https://github.com/skshukla/tlsdemo/blob/master/scripts/script.sh">scripts.sh</a> file (after adjusting the above variable) and paste it to terminal and press enter. If all goes good, it would print the last few lines as below.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bB8-8b2RLISNm4hjBqbngQ.png" /></figure><p>By this time, work/cert directory would be created and it would contain all the certificates.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*otgdN23TGK-yGVzMst-y2A.png" /></figure><p>The script would also copy the final keystore and trust store files to the src/main/resources folder</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*s-94qU4p-1fTck8QcZitCg.png" /></figure><ul><li>Start the application by issuing the command</li></ul><pre>mvn clean spring-boot:run</pre><ul><li>As the certificate has been generated (in this demo) using <em>api-server.skshukla.com</em> as the COMMON NAME for server and hence access the application using this endpoint. Make entry in <em>hostfile</em> to point this name to your localhost.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GJo4OvkOWxhDX0SbVwyJQA.png" /></figure><ul><li>Once the application is up and host entry is made, access the application by using the curl command.</li></ul><pre><br># Below command would result into error, as now client <br># needs to verify the identity of server<br><br>curl -X GET \<br> https://api-server.skshukla.com:8443<br><br># Use unsecure option (-k), this would tell that client risks to access the server <br># as it does not identify it.<br><br>curl -k -X GET \<br> https://api-server.skshukla.com:8443<br><br># Finally, to access the application use the root cert file, <br># this tells that client would trust the server only if <br># it&#39;s trusted by this Authority. (Note, -k, option is not given)<br><br>curl -X GET \<br> --cacert /Users/sachin/tmp/t2/tlsdemo/work/certs/rootCA.crt  \<br> https://api-server.skshukla.com:8443</pre><h3>4b. Make it work for Two-Way TLS Authentication</h3><p>The good news is, if you have followed the steps so far, you would have to modify only one config under “<em>src/main/resources/application.yaml</em>”</p><p>As the TrustStore is also copied to resources folder and hence application would trust those client who have been verified by the same CA, All that is needed to uncomment the line below and bring up the application again.</p><pre>client-auth: need</pre><ul><li>now the above command would fail as this time Server does not identify the client and rejects the request.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hDPSBI8FA2Yn0zZlt2gOZA.png" /></figure><ul><li>To make it work use the curl command with right client details (key, crt, root cert and password). <em>Please note, </em><strong><em>— cacert </em></strong><em>is only needed in case of Self Signed Certificate, as curl needs to know that it’s the right server its going to access based on this certificate which client trusts. In case of certs issued by the actual CAs, this option can be ommitted.</em></li></ul><pre>curl -X GET --pass 12345678 \<br> --key /Users/sachin/tmp/t2/tlsdemo/work/certs/client.key \<br> --cert /Users/sachin/tmp/t2/tlsdemo/work/certs/client.crt  \<br> --cacert /Users/sachin/tmp/t2/tlsdemo/work/certs/rootCA.crt  \<br> https://api-server.skshukla.com:8443</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*iZr0PqJ5aKQIvOcEHtrSDA.png" /></figure><h3>5. Conclusion</h3><p>We learned how to implement One-Way and Two-Way Authentication over Transport Layer Security and securing the Backend Service which improves the security of the system. To know how to implement the same in Cloud using Amazon API Gateway, visit the Part-II <a href="https://medium.com/@skshukla.0336/mtls-aws-api-gateway-part-ii-15d338385c65">https://medium.com/@skshukla.0336/mtls-aws-api-gateway-part-ii-15d338385c65</a></p><p><strong>Linkedin:</strong></p><p><a href="https://www.linkedin.com/in/sks336/">https://www.linkedin.com/in/sks336/</a></p><p><strong>YouTube:</strong></p><p><a href="https://www.youtube.com/channel/UCHkwNRHcXizsBiPE0smkJAA">https://www.youtube.com/channel/UCHkwNRHcXizsBiPE0smkJAA</a></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F6yh9uphcxcs&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6yh9uphcxcs&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=google" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/df4a2a50dc6cf32d1c230a13061f9484/href">https://medium.com/media/df4a2a50dc6cf32d1c230a13061f9484/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e03804b30804" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>