<?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 Niran Peiris on Medium]]></title>
        <description><![CDATA[Stories by Niran Peiris on Medium]]></description>
        <link>https://medium.com/@niranpeiris?source=rss-4e253b3b6696------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GRJiBUkEmrQjabFysJGoJg.jpeg</url>
            <title>Stories by Niran Peiris on Medium</title>
            <link>https://medium.com/@niranpeiris?source=rss-4e253b3b6696------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 19:51:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@niranpeiris/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[️ Troubleshooting WSO2 API Manager 4.x]]></title>
            <link>https://medium.com/@niranpeiris/%EF%B8%8F-troubleshooting-wso2-api-manager-4-x-08500c23305a?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/08500c23305a</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[jvm-diagnostics]]></category>
            <category><![CDATA[wso2-api-manager]]></category>
            <category><![CDATA[troubleshooting]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Fri, 23 May 2025 13:18:06 GMT</pubDate>
            <atom:updated>2025-05-23T13:18:06.926Z</atom:updated>
            <content:encoded><![CDATA[<h3>🛠️ Troubleshooting WSO2 API Manager 4.x in Kubernetes: JVM Heap, JFR, Threads &amp; Native Memory Diagnostics</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z7nEloj5HR3ZLWQtFgK_6g.png" /></figure><p>When running Java-based workloads like WSO2 API Manager 4.x in Kubernetes, issues like high CPU usage, memory leaks, and native memory growth are common and hard to debug without proper observability. This guide explains <strong>how to collect and analyze JVM internals (heap, threads, native memory, JFR)</strong> even inside stripped-down container environments using practical tools and best practices.</p><h3>How to Enable JVM Flags in Kubernetes</h3><p>Before using advanced diagnostics like JFR, NMT, or heap dump tools, the JVM must be started with the right flags. In Kubernetes, the cleanest way to inject these is through environment variables.</p><p>Add the parameters such as below under your container’s env section:</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: wso2test<br>spec:<br>    - name: wso2test<br>      image: docker.wso2.com/wso2am:4.3.0.50-alpine<br>      env:<br>        - name: JVM_MEM_OPTS<br>          value: &gt;<br>            -XX:+UnlockDiagnosticVMOptions<br>            -XX:+DebugNonSafepoints<br>            -XX:+FlightRecorder<br>            -XX:NativeMemoryTracking=detai</pre><h3>How to Log into a Container and Find the JVM PID</h3><p>To begin any diagnostic, you need to connect to the pod and locate the Java process. We can use the below command to log in to any running container.</p><pre>kubectl exec -it &lt;pod-name&gt; -c &lt;container-name&gt; - /bin/bash</pre><p>Then we can use the below command to identify the process ID.</p><pre>ps -ef | grep wso2</pre><p>This gives you the PID (e.g., 49) of the running JVM process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PFkchUcjaWWG3IPZcvyzfg.png" /></figure><h3>Heap Dump: What It Is and How to Collect It</h3><h4>Why heap dumps?</h4><p>A heap dump is a snapshot of all Java objects in memory at a given time. It’s essential for:</p><ul><li>Analyzing memory leaks</li><li>Investigating OutOfMemoryErrors</li><li>Understanding object allocation trends</li></ul><h4>How to collect</h4><p>The below command tells the JVM to generate a binary-format heap dump of live objects only, and write it to /logs/heap_dump.hprof.</p><pre>jmap -dump:live,format=b,file=/logs/heap_dump.hprof &lt;pid&gt;</pre><ul><li>Jmap (Java Memory Map tool): part of the JDK. Used to inspect the memory of a running JVM process.</li><li>dump: Initiates a heap dump</li><li>live: Only include live objects (i.e., objects that are reachable and not garbage). Skips garbage.</li><li>format=b: Binary format — required for tools like Eclipse MAT, VisualVM, or Java Mission Control</li><li>&lt;pid&gt;: The process ID of the running Java application (e.g., 49)</li></ul><p>This file can be analyzed using tools like Eclipse MAT or VisualVM.</p><h3>Java Flight Recorder (JFR): Why and How</h3><h4>Why use JFR?</h4><p>JFR provides a continuous profiling stream from the JVM that includes:</p><ul><li>Method-level CPU usage</li><li>GC pauses</li><li>Lock contention</li><li>Thread activity</li></ul><p>It’s ideal for low-overhead production profiling.</p><h4>How to start and stop</h4><p>Start:</p><pre>jcmd &lt;pid&gt; JFR.start delay=5s duration=30m name=Profiling filename=profiling.jfr compress=true settings=profile</pre><ul><li>jcmd: Java diagnostic command tool (part of the JDK)</li><li>&lt;pid&gt;: JVM process ID (e.g., 49)</li><li>JFR.start: Start a Flight Recording session</li><li>delay=5s: Wait 5 seconds before recording begins</li><li>duration=30m: Automatically stop recording after 30 minutes</li><li>name=Profiling: Give the session a friendly name</li><li>filename=profiling.jfr: Where to write the JFR recording file</li><li>compress=true: Compress output (useful for large files)</li><li>settings=profile: Use built-in “profile” settings (more detailed than default)</li></ul><p>Stop:</p><pre>jcmd &lt;pid&gt; JFR.stop name=Profiling</pre><p>Before running the above commands, you need to enable these JVM flags while starting the Java process. You can find how to start the process with these flags in the above section. (How to Enable JVM Flags in Kubernetes)</p><ul><li>-XX:+UnlockDiagnosticVMOptions</li><li>-XX:+DebugNonSafepoints</li><li>-XX:+FlightRecorder</li></ul><h3>Native Memory Tracking (NMT): What It Is and Why It Matters</h3><h4>Why NMT?</h4><p>NMT is a JVM feature that lets you monitor and compare these non-heap memory regions, which are invisible in regular heap dumps. NMT lets you:</p><ul><li>Track memory outside the heap</li><li>Detect class metadata growth</li><li>Profile memory usage over time</li></ul><h4>Enable via JVM flag:</h4><p>To enable it, add this flag to your JVM startup. You can find how to start the process with these flags in the above section. (How to Enable JVM Flags in Kubernetes)</p><pre>-XX:NativeMemoryTracking=detail</pre><h4>Command 01</h4><pre>jcmd &lt;pid&gt; VM.native_memory baseline scale=MB &gt; baseline-`date +%d-%m-%Y-%H:%M:%S`</pre><ul><li>baseline: Save a memory usage snapshot for later comparison</li><li>scale=MB: Output sizes in megabytes</li></ul><p>Creates a baseline snapshot of current native memory usage.</p><p><strong>Why it matters:</strong></p><p>It acts like a checkpoint, so you can later run a detail.diff to see what changed (i.e., if native memory has grown over time).</p><h4>Command 02</h4><pre>jcmd &lt;pid&gt; VM.native_memory detail scale=MB &gt; native_memory_detail-`date +%d-%m-%Y-%H:%M:%S`</pre><ul><li>detail: Show full memory breakdown</li><li>scale=MB: Output sizes in megabytes</li></ul><p>Shows a detailed breakdown of current native memory usage.</p><p><strong>Use it when:</strong></p><p>You want to see how much memory is used by each subsystem, like threads, class metadata, compiler, etc. Output categories include:</p><ul><li>Thread</li><li>Class</li><li>Code</li><li>GC</li><li>Internal</li></ul><h4>Command 03</h4><pre>jcmd &lt;pid&gt; VM.native_memory detail.diff scale=MB &gt; native_memory_diff-`date +%d-%m-%Y-%H:%M:%S`</pre><ul><li>detail.diff: Compares memory usage now vs. baseline</li><li>scale=MB: Output sizes in megabytes</li></ul><p>Compares current memory usage to the previous baseline and shows what changed.</p><p><strong>Use it when:</strong></p><p>You’re troubleshooting memory leaks or growth and want to see which subsystem is responsible.</p><h4>Example Flow:</h4><pre># Step 1: Baseline before heavy load or issue<br>jcmd 49 VM.native_memory baseline scale=MB &gt; /logs/nmt-baseline.txt<br><br># Step 2: Let app run, then capture detailed current state<br>jcmd 49 VM.native_memory detail scale=MB &gt; /logs/nmt-detail.txt<br><br># Step 3: Compare now vs. baseline<br>jcmd 49 VM.native_memory detail.diff scale=MB &gt; /logs/nmt-diff.txt</pre><h3>Thread Dumps and CPU Usage</h3><p>Thread dumps show what each thread is doing and are key to diagnosing:</p><ul><li>Deadlocks</li><li>Thread pool exhaustion</li><li>Stuck or spinning threads</li></ul><h4>Collecting multiple dumps over time</h4><p>We can use this type of script to collect thread dumps and usage. [<a href="https://github.com/JanakaPushpakumara/troubleshoot-kit/blob/master/scripts-and-commands/thread-dump/thread-analyze.sh">1</a>]</p><pre>#!/bin/bash<br>if [ &quot;$#&quot; -ne 3 ]; then<br>        echo &quot;usage: sh thread-analyze.sh &lt;pid&gt; &lt;number-of-dumps&gt; &lt;interval&gt;&quot;<br>        exit<br>fi<br><br>count=$2<br>for i in `seq 1 $count`;<br>do<br>        jstack -l $1 &gt; thread_dump_`date &quot;+%F-%T&quot;`.txt &amp;<br>        ps --pid $1 -Lo pid,tid,%cpu,time,nlwp,c &gt; thread_usage_`date &quot;+%F-%T&quot;`.txt &amp;<br>if [ $i -ne $count ]; then<br>        echo &quot;sleeping for $3 [$i]&quot;<br>        sleep $3<br>fi<br>done</pre><p><strong>Note</strong>:</p><p>This often fails in Alpine containers. In such scenarios, we can use a sidecart container to collect thread usages.</p><pre>apiVersion: v1<br>kind: Pod<br>metadata:<br>  name: wso2test<br>spec:<br>  securityContext:<br>    fsGroup: 0<br>  shareProcessNamespace: true<br>  containers:<br>    - name: debug-sidecar<br>      image: eclipse-temurin:17<br>      command: [&quot;sleep&quot;, &quot;infinity&quot;]<br>      securityContext:<br>        runAsUser: 10001<br>        runAsGroup: 10001<br>      volumeMounts:<br>        - name: logs-dir<br>          mountPath: /logs<br>    - name: wso2test<br>      image: docker.wso2.com/wso2am:4.3.0.50-alpine<br>      env:<br>        - name: JVM_MEM_OPTS<br>          value: &gt;<br>            -XX:+UnlockDiagnosticVMOptions<br>            -XX:+DebugNonSafepoints<br>            -XX:+FlightRecorder<br>            -XX:NativeMemoryTracking=detail<br>      volumeMounts:<br>        - name: logs-dir<br>          mountPath: /logs<br>  volumes:<br>    - name: logs-dir<br>      emptyDir: {}</pre><h4>What’s Added in the Sidecar-Based Debug Pod</h4><ol><li><strong>shareProcessNamespace: true<br></strong>Allows all containers in the pod (including the sidecar) to see each other’s processes. The sidecar must access the PID of the WSO2 JVM to run jcmd, jmap, and jstack. Without this, the sidecar cannot see or interact with the WSO2 process tree.</li><li><strong>securityContext with runAsUser and runAsGroup<br></strong>Ensures the sidecar runs with the same UID/GID as the WSO2 process. The JVM Attach API (used by jcmd, jmap, etc.) requires that the attaching user has the same UID as the target JVM. Otherwise, you’ll see:<strong><em> AttachNotSupportedException: target process … doesn’t respond or HotSpot VM not loaded</em></strong></li><li><strong>fsGroup: 0 at the pod level<br></strong>Ensures that volumes mounted into the pod are group-writable by GID 0 (root), which both containers can access. If you mount a shared volume like /logs, both containers must be able to write to it — this ensures write permissions are correct without needing root access.</li><li><strong>Shared emptyDir volume for /logs<br></strong>Creates a temporary in-memory or disk-backed volume shared between containers. Allows both WSO2 and the debug-sidecar to write and read diagnostic files, such as: heap_dump.hprof, profiling.jfr, thread_usage.txt, nmt-diff.txt.</li><li><strong>command: [“sleep”, “infinity”] in the sidecar<br></strong>Keeps the debug container running without exiting.</li></ol><h4>Why use a sidecar?</h4><p>Alpine containers (like WSO2’s) lack `top`, `jmap`, `jcmd`, and may prevent heap/attach tools from working. Sidecars provide:</p><ul><li>Full JDK tools (`jcmd`, `jmap`, `jstack`, `top`)</li><li>Visibility into the WSO2 process via `shareProcessNamespace`</li><li>Writable shared volume for exporting dumps</li></ul><h3>Copying Files In and Out</h3><p>To upload a script:</p><pre>kubectl cp &lt;local-file-path&gt; &lt;pod&gt;:/logs/ -c debug-sidecar</pre><p>To download logs:</p><pre>kubectl cp &lt;pod&gt;:/logs/heap_dump.hprof &lt;local-file-path&gt;</pre><h3>Final Notes</h3><p>For a deeper dive into how to visualize and analyze Java thread behavior using thread dumps, check out this companion article:</p><p><a href="https://medium.com/@niranpeiris/visualizing-and-troubleshooting-java-thread-behavior-ef318439d9dc">Visualizing and Troubleshooting Java Thread Behavior</a></p><p>Collecting advanced diagnostics like JFR, NMT, and heap/thread dumps is completely possible in Kubernetes, even for production containers — if you’re using the right setup:</p><ul><li>Use a sidecar for tooling</li><li>Enable JVM flags via JVM_MEM_OPTS</li><li>Always align UID/GID and filesystem permissions</li><li>Mount a shared /logs directory to keep files accessible</li></ul><p>This approach turns any WSO2 API Manager 4.x container into a fully observable and debuggable JVM.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=08500c23305a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automating WSO2 API Manager (4.x.x) Deployments with a Go-Based CLI Tool]]></title>
            <link>https://medium.com/@niranpeiris/automating-wso2-api-manager-4-x-x-deployments-with-a-go-based-cli-tool-08d6d6deb490?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/08d6d6deb490</guid>
            <category><![CDATA[wso2-api-manager]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <category><![CDATA[api-gateway-deployment]]></category>
            <category><![CDATA[devops-automation]]></category>
            <category><![CDATA[go-cli-tools]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Tue, 13 May 2025 14:45:42 GMT</pubDate>
            <atom:updated>2025-05-13T14:46:22.147Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k6sIOCGld92zGjldOV-E7w.png" /></figure><p>Tired of wrestling with deployment.toml files every time you spin up a WSO2 API Manager cluster? Whether you’re deploying one Gateway or scaling out HA-ready Traffic Managers and Control Planes, the manual setup is slow, repetitive, and error-prone.</p><p>I built a Go-powered CLI tool that takes a single JSON config and automatically generates fully patched, profile-specific APIM packs — complete with offsets, HA blocks, profiling, and MySQL drivers. Just plug in your config and go.</p><h3>Why Go?</h3><ul><li>Cross-platform builds: Easily build for macOS, Linux, or Windows</li><li>Fast and lightweight: Great for scripting large deployments</li><li>Rich standard library: Makes file I/O, parsing, and templating clean</li></ul><h3>What the Tool Does</h3><p>This Go-based deployment tool automates WSO2 API Manager setup using a single, declarative JSON file.</p><h4>1. Reads the Input JSON Configuration</h4><p>You provide a config.json that defines:</p><ul><li>Where the APIM binary ZIP is located</li><li>What API Manager version and update level to apply</li><li>Which profiles to deploy (Gateway, Traffic Manager, Control Plane)</li><li>HA and profiling preferences</li><li>Database credentials</li><li>MySQL driver path</li></ul><p>This makes it easy to reproduce deployments, switch versions, or scale nodes, all from one config file.</p><h4>2. Extracts the Provided Binary</h4><p>The tool unzips the WSO2 API Manager distribution from the given path, preparing it for customization.</p><h4>3. Updates the Pack to a Specific Update Level</h4><p>Based on your update_level (e.g., “2”), it applies the latest patch or a specific update level using WSO2’s Update Tool (U2).</p><h4>4. Copies the MySQL Driver</h4><p>It automatically copies your specified MySQL connector JAR into the lib/ directory of the pack, ensuring database connectivity out of the box.</p><h4>5. Creates Node Packs per Profile</h4><p>Using the updated binary:</p><ul><li>Separate directories are created for each profile (e.g., gateway-1/, gateway-2/, tm-1/,etc)</li><li>Nodes are cloned based on the count value in the config</li><li>Profile-specific naming and folder structures are applied</li></ul><h4>6. Applies Profiling Configurations</h4><p>If profiling is enabled (enable_profiling: true), the tool runs the profiling script and removes unwanted files from each profile.</p><h4>7. Dynamically updates deployment.toml</h4><p>The tool reads version-specific base TOML templates and:</p><ul><li>Applies offsets automatically (offset=1, offset=2, etc.)</li><li>Injects HA blocks conditionally</li><li>Populates DB credentials and other environment-specific fields</li><li>Ensures only relevant sections (e.g., [apim.throttling] for TMs) are applied per node type</li></ul><h4>8. Creates the Databases Automatically</h4><p>Using credentials and DB names from the config, the tool:</p><ul><li>Connects to the target MySQL server</li><li>Creates the APIM DB and Shared DB if they don’t already exist</li></ul><h4>9. Packs Are Ready to Start</h4><p>After all updates, you get clean, runnable directories per node. No manual edits required. Each node is ready to run with just a ./bin/api-manager.sh or via Docker/Kubernetes wrapper scripts.</p><h4>Example JSON Input</h4><p>Here’s an example config.json to generate a 2-node HA Gateway, 2-node HA Control Plane, and skip Traffic Manager:</p><pre>{<br>  &quot;apim_zip_path&quot;: &quot;/path/to/wso2am-4.4.0.zip&quot;,<br>  &quot;version&quot;: &quot;4.4.0&quot;,<br>  &quot;update_level&quot;: &quot;2&quot;,<br>  &quot;gateway&quot;: {<br>    &quot;enabled&quot;: true,<br>    &quot;count&quot;: 2,<br>    &quot;enable_ha&quot;: true,<br>    &quot;enable_profiling&quot;: true<br>  },<br>  &quot;traffic_manager&quot;: {<br>    &quot;enabled&quot;: false,<br>    &quot;count&quot;: 1,<br>    &quot;enable_ha&quot;: true,<br>    &quot;enable_profiling&quot;: true<br>  },<br>  &quot;control_plane&quot;: {<br>    &quot;enabled&quot;: true,<br>    &quot;count&quot;: 1,<br>    &quot;enable_ha&quot;: true,<br>    &quot;enable_profiling&quot;: true<br>  },<br>  &quot;database&quot;: {<br>    &quot;host&quot;: &quot;localhost&quot;,<br>    &quot;port&quot;: 3306,<br>    &quot;user&quot;: &quot;root&quot;,<br>    &quot;password&quot;: &quot;Toor&quot;,<br>    &quot;apim_db_name&quot;: &quot;TEST_APIM_DB1&quot;,<br>    &quot;shared_db_name&quot;: &quot;TEST_SHARED_DB1&quot;<br>  },<br>  &quot;db_driver_path&quot;: &quot;/path/to/mysql-connector-j-8.0.31/mysql-connector-j-8.0.31.jar&quot;<br>}</pre><p>This config will:</p><ul><li>Deploy a 2-node Gateway cluster with profiling enabled</li><li>Deploy a two-node Control Plane with HA-related configurations</li><li>Skip TM completely</li><li>Use MySQL for both APIM and Shared DBs</li></ul><h3>How to Use</h3><p>1. Clone the Repo</p><pre>git clone https://github.com/niranRameshPeiris/apim-deployctl.git<br>cd apim-deployctl</pre><p>2. Edit your Config</p><ul><li>You can edit the existing config.json file in the root directory</li></ul><p>3. Run the Tool</p><ul><li>On macOS Apple</li></ul><pre>./apim-deployer-mac-apple config.json</pre><ul><li>On macOS Intel</li></ul><pre>./apim-deployer-mac-intel config.json</pre><ul><li>On Linux</li></ul><pre>./apim-deployer-linux config.json</pre><ul><li>On Windows</li></ul><pre>./apim-deployer-win.exe config.json</pre><h3>Output</h3><p>Once the tool runs, it creates a fully structured deployment layout inside a deployment/ directory. This includes a dedicated folder for each node instance based on the roles (Gateway, Control Plane, etc.) and the configured node count.</p><h4>Example Folder Structure</h4><pre>deployment/<br>├── gateway-1/<br>│   ├── bin/<br>│   ├── repository/<br>│   └── ...<br>├── gateway-2/<br>│   ├── bin/<br>│   ├── repository/<br>│   └── ...<br>├── control-plane-1/<br>│   ├── bin/<br>│   ├── repository/<br>│   └── ...</pre><h4>What’s Inside?</h4><p>Each node directory is a complete APIM runtime pack, pre-configured</p><ul><li>Fully extracted and patched (U2 applied if specified)</li><li>MySQL driver injected</li><li>deployment.toml tailored to node type, offset, HA settings, and DB config</li><li>Profiling is enabled if requested</li></ul><p>You’re ready to run each node individually using:</p><pre>sh api-manager.sh -Dprofile=control-plane<br>sh api-manager.sh -Dprofile=traffic-manager<br>sh api-manager.sh -Dprofile=gateway-worker</pre><h3>Try It Yourself</h3><p>Whether you’re managing a dev cluster or preparing production infrastructure, this tool saves hours of manual editing and reduces misconfigurations dramatically.</p><p>Check it out on GitHub:</p><p><a href="https://github.com/niranRameshPeiris/apim-deployctl">GitHub - niranRameshPeiris/apim-deployctl</a></p><h3>Conclusion</h3><p>Manual configuration of WSO2 API Manager deployments, especially across multiple profiles and high availability setups, can be tedious and error-prone. This Go-based deployment tool eliminates that complexity by automating the entire setup process from a single JSON config. It provides a repeatable, scalable, and production-ready deployment foundation.</p><blockquote><em>This tool is still under active development and hasn’t been extensively tested in all scenarios. It currently supports only </em><strong><em>MySQL</em></strong><em> as the database backend.</em></blockquote><blockquote><em>Please test it in your own environment and feel free to open an issue or PR if you encounter any bugs or want to contribute improvements.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=08d6d6deb490" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Analyzing Latency in WSO2 APIs with Correlation Logs and a Custom Java Tool]]></title>
            <link>https://medium.com/@niranpeiris/analyzing-latency-in-wso2-apis-with-correlation-logs-and-a-custom-java-tool-d1a5a7a9dfe4?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1a5a7a9dfe4</guid>
            <category><![CDATA[wso2-api-manager]]></category>
            <category><![CDATA[api-latency]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[correlation-log]]></category>
            <category><![CDATA[automation]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Mon, 12 May 2025 14:20:03 GMT</pubDate>
            <atom:updated>2025-05-12T14:20:03.230Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XLYFXyuUn0R9qUyw1KI3bA.png" /></figure><p>Modern APIs power critical business transactions, and when performance dips, users notice. For engineers managing APIs with WSO2 API Manager, one of the most powerful yet underutilized tools is correlation logging. But analyzing large volumes of log files manually can quickly become overwhelming.</p><p>In this post, we’ll explore what correlation logs are, the challenges of working with them at scale, and how I built a lightweight Java tool to analyze them efficiently, even across thousands of files.</p><h3>What Are Correlation Logs?</h3><p>WSO2 API Manager provides <a href="https://apim.docs.wso2.com/en/4.1.0/observe/api-manager/monitoring-correlation-logs/">correlation logs</a> to trace a request as it flows through the gateway, backend, and response path. Every request is tagged with a unique <strong>correlation ID</strong>, which appears in all relevant log entries.</p><p>These logs are extremely useful to measure:</p><ul><li><strong>Backend latency</strong> (time spent calling the backend)</li><li><strong>Round-trip latency</strong> (total time to process the request end-to-end)</li></ul><p>However, extracting these metrics across multiple distributed logs is not straightforward.</p><h3>Why Analyzing Logs Is Challenging</h3><p>In real-world deployments, WSO2 log directories may contain thousands of log files, each with millions of lines. Here’s what makes correlation log analysis difficult:</p><ul><li>Log entries are <strong>distributed across files</strong></li><li>Tools like grep or awk are inefficient for structured correlation tracking</li><li>For example, if a REQUEST_HEAD entry appears in file1.log, and the corresponding ROUND-TRIP LATENCY is in file2.log, traditional approaches fail</li></ul><p>To solve this, I built a simple but efficient Java tool that automates the entire process. It reduces manual effort, improves visibility, and saves hours of log inspection.</p><h3>How It Works</h3><p>The tool runs as a CLI-based .jar file and follows a two-pass workflow.</p><h4>Step 1: Inputs</h4><p>When you execute the JAR, the program prompts for:</p><ul><li>Path to the logs directory</li><li>HTTP method (e.g., POST, or * for all)</li><li>Resource path (e.g., /test/v1/token or /test/**)</li></ul><h4>Step 2: Collect Matching Correlation IDs</h4><p>The tool scans all files to find log lines that match:</p><ul><li>The specified HTTP method</li><li>The specified resource path (with wildcard support)</li><li>Log lines that contain REQUEST_HEAD</li></ul><p>These lines are used to extract all matching correlation IDs.</p><h4>Step 3: Collect Latencies</h4><p>It then scans again to find BACKEND LATENCY and ROUND-TRIP LATENCY log lines for those correlation IDs, even if they appear in other files.</p><p>The tool:</p><ul><li>Normalizes whitespace</li><li>Supports wildcard path patterns</li><li>Efficiently processes large directories using buffered readers</li></ul><h3>How to Use It</h3><ul><li>Download the JAR from <a href="https://github.com/niranRameshPeiris/correlation-log-analyzer/blob/main/target/CorrelationLogAnalyzer-1.0-jar-with-dependencies.jar">here</a></li><li>Run it from your terminal using the command below.</li></ul><pre>java -jar CorrelationLogAnalyzer-1.0-jar-with-dependencies.jar</pre><ul><li>You’ll be prompted to enter:<br>- Path to the logs directory (e.g., /Niran/Documents/logs)<br>- HTTP method (e.g., POST, or * for all)<br>- API resource path pattern (e.g., /test/v1/*)</li><li>Output file:<br>- A file called latency-report.csv will be generated:</li><li>Sample Output:</li></ul><pre>CorrelationID|ResourcePath|BACKEND_LATENCY|ROUND_TRIP_LATENCY<br>395dcb7a-b599–4752-ad9a-914bebecca72|/test/v1/token|6008|6021<br>90a85bcb-ce36–42b8-bbbf-0bb06b94d93e|/test/v1/token|4009|4018<br>94527064–4c9b-4386–872e-64c9ee38d2a0|/test/v1/token|2007|2017<br>e3a028f2–90ae-4739-b44d-7d57326b949a|/test/v1/token|1011|1021<br>cec12450-b717–4032–8a08–4f90b145fc78|/test/v1/timeout|6|27<br>1cb7a2f2-be9a-411d-bdbc-4c371714d374|/test/v1/token|8|18</pre><h3>Want to Customize It?</h3><p>The code is open source and written in clean, modular Java 11.</p><p><a href="https://github.com/niranRameshPeiris/correlation-log-analyzer">GitHub - niranRameshPeiris/correlation-log-analyzer</a></p><p>Clone this repository:</p><pre>git clone https://github.com/niranRameshPeiris/correlation-log-analyzer.git<br>cd correlation-log-analyzer</pre><p>Customize the behavior (filters, output format, etc.) in the CorrelationLogAnalyzer.java file and build the JAR:</p><pre>mvn clean package</pre><p>Run it:</p><pre>java -jar target/CorrelationLogAnalyzer-1.0-jar-with-dependencies.jar</pre><h3>Final Thoughts</h3><p>If you’re working with API latencies, correlation logs are your friend, but only if you can make sense of them. This tool is my way of making structured latency analysis accessible and fast.</p><p>If this helps you, feel free to ⭐️ the GitHub repo or suggest improvements!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1a5a7a9dfe4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubernetes for Absolute Beginners: Build and Run Your First Two-Pod App from Scratch]]></title>
            <link>https://medium.com/@niranpeiris/kubernetes-for-absolute-beginners-build-and-run-your-first-two-pod-app-from-scratch-4f11d42f84b9?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f11d42f84b9</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[hands-on-guide]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[demo]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Sun, 11 May 2025 10:32:09 GMT</pubDate>
            <atom:updated>2025-05-11T10:32:09.142Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever felt like Kubernetes is too complex to learn? Let’s change that.</p><p>In this tutorial, I won’t dive deep into theory or cloud setup. Instead, you’ll get a feel for how Kubernetes works by playing around with it. Deploying a real full-stack app (frontend + backend), seeing how parts connect, and interacting with it step by step.</p><p>No cloud billing. No complex configs. Just a simple hands-on learning experience that makes Kubernetes concepts click.</p><h3>What You’ll Learn</h3><ul><li>What is a pod, service, and deployment by actually running them</li><li>How two apps talk to each other inside Kubernetes</li><li>How to create and access your app using kubectl</li><li>Why Kubernetes uses service names instead of IPs</li><li>The difference between ClusterIP, NodePort, and LoadBalancer (explained simply)</li></ul><h3>What We’re Building</h3><ul><li>A backend app: responds with “Hello from Backend”</li><li>A frontend app: fetches that message and displays it</li><li>Two pods running in a cluster</li><li>One Kubernetes Service for each, so they can talk</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*mcGb9qMC5S2Bj8dtNuy0SA.jpeg" /></figure><h3>Step 1: Understanding the Apps</h3><h4>Backend (BackendAPI.js)</h4><pre>var express = require(&#39;express&#39;);<br>var app = express();<br><br>app.get(&#39;/gethomepage&#39;, function (req, res) {<br>    res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>    res.setHeader(&#39;Content-Type&#39;, &#39;application/json&#39;);<br>    res.end(JSON.stringify({&quot;Content&quot;:&quot;Welcome to the Home Page.&quot;}));<br>})<br><br>app.get(&#39;/getofficepage&#39;, function (req, res) {<br>    res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>    res.setHeader(&#39;Content-Type&#39;, &#39;application/json&#39;);<br>    res.end(JSON.stringify({&quot;Content&quot;:&quot;Welcome to the Office Page.&quot;}));<br>})<br><br>app.listen(3000, function () {<br>  console.log(&quot;App listening on port 3000!&quot;);<br>});</pre><p><strong>👇 What it does:</strong></p><ul><li>Uses Express, a NodeJS library to create web servers.</li><li>Sets up two route: GET /gethomepage and /getofficepage</li><li>When someone calls the /gethomepage, it replies with:<br><em>{“Content”:”Welcome to the Home Page.”}</em></li><li>When someone calls the /getofficepage, it replies with:<br><em>{“Content”:”Welcome to the Office Page.”}</em></li><li>Listens for requests on port 3000.</li></ul><p>You can think of this as a basic API server that just says hello.</p><h4>Frontend (ClientApplication.js)</h4><pre>var express = require(&#39;express&#39;);<br>const axios = require(&#39;axios&#39;)<br><br>var app = express();<br><br>app.get(&#39;/home&#39;, function (req, res) {<br>    axios.get(&#39;http://backend-service:3000/gethomepage&#39;, {})<br>    .then(response =&gt; {<br>      res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>      res.setHeader(&#39;Content-Type&#39;, &#39;text/plain&#39;);<br>      res.end(response.data.Content);<br>    })<br>    .catch(error =&gt; {<br>      res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>      res.setHeader(&#39;Content-Type&#39;, &#39;text/plain&#39;);<br>      res.end(&quot;Error : &quot; + error);<br>    })   <br>})<br><br>app.get(&#39;/office&#39;, function (req, res) {<br>  axios.get(&#39;http://backend-service:3000/getofficepage&#39;, {})<br>  .then(response =&gt; {<br>    res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>    res.setHeader(&#39;Content-Type&#39;, &#39;text/plain&#39;);<br>    res.end(response.data.Content);<br>  })<br>  .catch(error =&gt; {<br>    res.setHeader(&#39;Access-Control-Allow-Origin&#39;, &#39;*&#39;);<br>    res.setHeader(&#39;Content-Type&#39;, &#39;text/plain&#39;);<br>    res.end(&quot;Error : &quot; + error);<br>  })   <br>})<br><br>app.listen(7000, function () {<br>  console.log(&quot;App listening on port 7000!&quot;);<br>});</pre><p><strong>👇 What it does:</strong></p><ul><li>Also uses <strong>Express</strong> to create a server.</li><li>When someone calls the /home or /office, it uses <strong>Axios</strong> (a library to make HTTP calls) to <strong>call the backend</strong></li><li>Then return only the reponse to the client.</li><li>If the backend isn’t working, it shows an error message instead.</li></ul><blockquote>Notice: the frontend makes a request to http://backend-service:3000. This is not magic. It&#39;s a <strong>Kubernetes Service name</strong>!</blockquote><p>Now that you have a clear understanding of how both the frontend and backend applications work individually, let’s take it a step further. We’ll deploy them in Kubernetes and see how these components interact within a real cluster environment.</p><h3>Step 2: Building and Pushing Docker Images</h3><p>Before we can deploy our applications to Kubernetes, we need to package them into Docker images. A Docker image is like a snapshot of your application and everything it needs to run, including the code, runtime (NodeJS in our case), libraries, and dependencies. Kubernetes runs containers from these images.</p><p>So, we first build Docker images locally, and then push them to a Docker registry (like Docker Hub). This registry acts like a cloud storage for your images, so Kubernetes can pull them down when needed.</p><h4>What is in a Dockerfile?</h4><p>A Dockerfile is a simple text file with instructions for how to build your app image. Here is an example for the backend:</p><pre>FROM node:18-alpine<br># Set working directory<br>WORKDIR /app<br># Copy application source code<br>COPY . .<br># Install dependencies<br>RUN npm install<br># Define the command to run the application<br>CMD [&quot;npm&quot;, &quot;start&quot;]</pre><h4><strong>Explanation</strong>:</h4><ul><li><strong>FROM node:18-alpine</strong>: Uses a lightweight NodeJS base image.</li><li><strong>WORKDIR /app</strong>: Sets the working directory inside the image to /app.</li><li><strong>COPY . .</strong>: Copies the rest of the application code into the /app directory inside the image.</li><li><strong>RUN npm install</strong>: Installs only the dependencies needed to run.</li><li><strong>CMD […]</strong>: Specifies the command to run when the container starts.</li></ul><p>To get started quickly, you can use the code from this GitHub repository:</p><p><a href="https://github.com/niranRameshPeiris/kubernetes-demo">GitHub - niranRameshPeiris/kubernetes-demo</a></p><p>It contains the full source code and Dockerfiles for both the backend and frontend applications we’ll deploy.</p><p>Let’s go through the steps to build and push Docker images, one by one, with explanations for each command.</p><h4>Backend: Build and Push Docker Image</h4><p>Navigate into the backend folder containing the backend source code and Dockerfile.</p><pre>cd backend</pre><p>Then execute the belwo command to build the image.</p><p><strong>Image name format</strong>: [docker-registry-name]:[image-name]:[version]</p><pre>docker build -t yourname/backend:1.0.0 .</pre><p>Then push the image to Docker Hub (or your preferred registry) so Kubernetes can download and run it later.</p><pre>docker push yourname/backend:1.0.0</pre><h4>Frontend: Build and Push Docker Image</h4><p>Navigate to the webapp folder:</p><pre>cd ../webapp</pre><p>Build the frontend image:</p><pre>docker build -t yourname/webapp:1.0.0 .</pre><p>Push it to Docker Hub:</p><pre>docker push yourname/webapp:1.0.0</pre><p>Once both images are pushed, they’re ready to be used in your Kubernetes YAML files for deployment.</p><h3>Step 3: Deploying to Kubernetes</h3><p>Now that your Docker images are pushed to a registry, you can deploy them to your Kubernetes cluster. To do this, we use YAML configuration files. one for the backend and one for the frontend.</p><p>These YAML files describe what Kubernetes should create: deployments (to manage the pods) and services (to expose those pods).</p><p>Let’s break it down step by step.</p><h4>What’s Inside the YAML Files?</h4><p>Here’s a simplified version of the backend deployment YAML (backend.yaml):</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: backend-deployment<br>spec:<br>  replicas: 1<br>  template:<br>    metadata:<br>      labels:<br>        app: backend<br>    spec:<br>      containers:<br>      - name: backend<br>        image: yourname/backend:1.0.0<br>        ports:<br>        - containerPort: 3000<br>---<br>apiVersion: v1<br>kind: Service<br>metadata:<br>  name: backend-service<br>spec:<br>  selector:<br>    app: backend<br>  ports:<br>    - port: 3000<br>      targetPort: 3000<br>  type: ClusterIP</pre><h4>Explanation:</h4><p><strong>Deployment</strong></p><ul><li>replicas: 1: Runs one copy of the backend pod.</li><li>image: Pulls your previously built Docker image.</li><li>containerPort: Tells Kubernetes the port your app uses internally.</li></ul><p><strong>Service</strong></p><ul><li>Connects to pods with the label app: backend</li><li>Exposes port 3000 internally.</li><li>type: ClusterIP: Makes the service accessible only inside the cluster.</li></ul><p>The frontend YAML is similar, but:</p><ul><li>Uses type: NodePort so you can access it from your browser</li></ul><h4>How Does a Service Map to a Pod?</h4><p>Kubernetes uses labels and selectors to connect services to the right pods. In the pod template inside the deployment, we set a label:</p><pre>labels:<br> app: backend</pre><p>And in the service definition, we specify:</p><pre>selector:<br> app: backend</pre><p>This means, send traffic to any pod that has the label app=backend. So even if the pod restarts, scales up to more replicas, or gets a new IP, the service will always route traffic to it as long as the label matches.</p><p>This is how your frontend is able to reliably call <a href="http://backend-service:3000/">http://backend-service:3000</a> and reach the backend pod.</p><p>It’s like Kubernetes saying, I don’t care what the pod’s IP is, just find all the pods labeled app=backend and forward requests to them.</p><h4>Deploy with kubectl</h4><p>Since now we know what is a YAML file, we can use the below comamnd to deploy our backend and frontend to the kubernetes.</p><p>Use the following commands to create the deployments and services:</p><pre>kubectl apply -f backend.yaml<br>kubectl apply -f webapp.yaml</pre><p>We can use the below commands to check What’s Running</p><pre>kubectl get pods<br>kubectl get services</pre><p>You should see:</p><ul><li>One pod for the backend</li><li>One pod for the frontend</li><li>Two services (backend-service and webapp-service)</li></ul><figure><img alt="Check What’s Running" src="https://cdn-images-1.medium.com/max/1024/1*bJmRTSZWdZGlTZwCdQapzQ.png" /><figcaption>Check What’s Running</figcaption></figure><p>Then access the app at <a href="http://localhost:30500/">http://localhost:30500</a></p><ul><li>30500 is the NodePort defined in the YAML file</li></ul><h3>What Did You Just Build?</h3><p>Let’s break down what just happened using real concepts. Before that, take a look at the diagram below. It illustrates the high-level architecture of a Kubernetes cluster and its major components:</p><figure><img alt="Most Common Kubernetes Components" src="https://cdn-images-1.medium.com/max/960/1*uFuXZCgr5l3E6ULlFifppQ.jpeg" /><figcaption>Most Common Kubernetes Components</figcaption></figure><h4>Pod</h4><p>A pod is the smallest unit in Kubernetes. It wraps a container (like your Docker image).</p><ul><li>You created 1 pod for the backend.</li><li>1 pod for the frontend.</li></ul><h4>Deployment</h4><p>A deployment manages pods for you:</p><ul><li>It ensures your pod is always running.</li><li>If it crashes, it restarts it.</li><li>If you change the image, it updates smoothly.</li><li>You defined this in backend.yaml and webapp.yaml.</li></ul><h4>Service</h4><p>A service gives your pod a stable name &amp; port to be accessed by others.</p><ul><li>Your frontend calls <a href="http://backend-service:3000/">http://backend-service:3000</a> because of the service.</li><li>Even if backend restarts, the name backend-service always points to the right pod.</li></ul><p><strong>Service Types</strong></p><ul><li>ClusterIP: Internal only (Backend, DB)</li><li>NodePort: Exposes app on a node IP and port (Local dev/testing)</li><li>LoadBalancer: Uses cloud LB, public IP (Production)</li></ul><blockquote>Here we have used ClusterIP to access backend from the frontend. And we have used NodePort to test the app from your browser.</blockquote><h3>Conclusion</h3><p>You just deployed a real multi-component application to Kubernetes from source code to running pods. You now understand how containers, pods, services, and deployments work together to bring your app to life.</p><p>This is just the beginning. From here, you can:</p><ul><li>Add environment variables</li><li>Use ConfigMaps and Secrets</li><li>Try Ingress for routing</li><li>Learn about persistent storage</li></ul><p>Kubernetes is powerful, and it gets easier the more you build with it. Keep experimenting and you’ll master it faster than you think!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f11d42f84b9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Visualizing and Troubleshooting Java Thread Behavior]]></title>
            <link>https://medium.com/@niranpeiris/visualizing-and-troubleshooting-java-thread-behavior-ef318439d9dc?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/ef318439d9dc</guid>
            <category><![CDATA[java-performance]]></category>
            <category><![CDATA[troubleshooting]]></category>
            <category><![CDATA[cpu-profiling]]></category>
            <category><![CDATA[devops-tool]]></category>
            <category><![CDATA[thread-dump-analysis]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Sun, 11 May 2025 04:21:50 GMT</pubDate>
            <atom:updated>2025-05-11T04:21:50.532Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y4Ssu3GE4W0f9G1YbR14Sg.png" /></figure><h3><strong>Introduction</strong></h3><p>Java is one of the most powerful platforms for building high-performance, scalable systems. But like any runtime, it has its complexities, particularly when things go wrong. When your Java service is experiencing latency spikes, high CPU usage, or unexpected crashes, the go-to diagnostics often include thread dumps, heap analysis, and CPU usage reports.</p><p>These diagnostic tools provide raw data, but interpreting that data quickly and accurately is not always easy. In this post, I’ll explore the common challenges developers face when troubleshooting Java applications and introduce a set of practical tools I’ve developed to make that process faster and more insightful.</p><h3><strong>Java Troubleshooting Essentials</strong></h3><p>Here are essential commands you should know when gathering data to analyze with these tools:</p><ul><li><strong>View per-thread CPU usage</strong></li></ul><pre>top -H -p &lt;pid&gt;</pre><ul><li><strong>Capture a thread dump</strong></li></ul><pre>jstack &lt;pid&gt; &gt; thread_dump.txt</pre><ul><li><strong>Capture a heap dump</strong></li></ul><pre>jmap -dump:live,format=b,file=heap.hprof &lt;pid&gt;</pre><ul><li><strong>Monitor native memory usage</strong></li></ul><pre>jcmd &lt;pid&gt; VM.native_memory summary</pre><h3><strong>Challenges in Analyzing Java Thread Dumps</strong></h3><p>Analyzing raw thread dumps has several limitations:</p><ul><li><strong>Thread identification is manual and error-prone:</strong> Threads appear in unstructured formats and are not grouped meaningfully.</li><li><strong>No CPU context:</strong> You cannot directly tell which threads are consuming the most CPU.</li><li><strong>Time comparison is difficult:</strong> Analyzing multiple dumps for thread state transitions requires side-by-side diffing, which is inefficient.</li><li>These issues slow down root cause analysis during critical debugging sessions, especially in production environments.</li></ul><h3>A Better Way to Debug Java Threads</h3><p>To solve these common pain points, I built a pair of lightweight, visual tools to help engineers better understand Java thread behavior and resource usage. These tools are designed to work together but can also be used independently, depending on the use case.</p><p>In performance troubleshooting, CPU usage and memory allocation patterns often go hand-in-hand. This toolkit facilitates correlation by allowing engineers to:</p><ul><li>Match high-CPU threads from usage reports with their JVM stack traces.</li><li>Identify garbage collection threads consuming excessive CPU.</li></ul><p>Cross-reference heap dumps captured using <strong><em>jmap</em></strong> for memory allocation insights.</p><p>This combined visibility is crucial when investigating memory leaks, inefficient object creation, or CPU spikes tied to garbage collection.</p><h3><strong>Introducing the Java Thread Analysis Toolkit</strong></h3><p>This solution includes two focused web-based tools, both of which are bundled into a single Docker image for convenience.</p><ol><li><strong>Thread CPU Analyzer</strong></li></ol><blockquote>This tool is useful when CPU usage is unexpectedly high and you want to pinpoint which application threads are responsible.</blockquote><ul><li>Uploads a thread usage report and a matching Java thread dump.</li><li>Correlates thread IDs (`TID`) from the OS-level report with `nid` values in the thread dump.</li><li>Displays a sortable table including:<br>- CPU usage percentage<br>- Thread ID (decimal and hex)<br>- JVM thread ID<br>- Thread name</li></ul><figure><img alt="CPU Analyzer Table Showing Thread Name, CPU %, and Stack Info" src="https://cdn-images-1.medium.com/max/1024/1*95LeSkitjC2wCaDtbHVSQw.png" /><figcaption>CPU Analyzer Table Showing Thread Name, CPU %, and Stack Info</figcaption></figure><p><strong>2. Thread Dump Visualizer</strong></p><blockquote>This is especially useful for detecting deadlocks, idle threads, or unexpected blocking behavior.</blockquote><ul><li>Accepts multiple thread dumps taken over time.</li><li>Group threads by name or ID.</li><li>Displays a visual timeline of thread states and stack traces.</li></ul><figure><img alt="Thread Visualizer Showing State Timeline Across Multiple Dumps" src="https://cdn-images-1.medium.com/max/1024/1*xJAIBia44YUERvSm_yvM1Q.png" /><figcaption>Thread Visualizer Showing State Timeline Across Multiple Dumps</figcaption></figure><ul><li>Highlights long-running or unchanging threads.</li></ul><figure><img alt="Same State for Too Long" src="https://cdn-images-1.medium.com/max/1024/1*Nm_a2hZW8sEuGg_mHOBxMQ.png" /><figcaption>Same State for Too Long</figcaption></figure><figure><img alt="Same Stack for Too Long" src="https://cdn-images-1.medium.com/max/1024/1*UqIHpBlr7A3Ek-OXkgDQJQ.png" /><figcaption>Same Stack for Too Long</figcaption></figure><h4><strong>How These Tools Improve Debugging</strong></h4><ul><li><strong>Identify high CPU threads immediately</strong>: No need to cross-reference text files manually.</li><li><strong>Visualize thread state changes over time</strong>: Quickly detect stuck threads or looping behavior.</li><li><strong>Correlate CPU and memory usage</strong>: Provides insight into whether CPU-intensive threads are tied to GC or object allocation.</li><li><strong>Save time in RCA</strong>: Cut down hours spent during incident analysis by visually pinpointing problem threads.</li></ul><h4><strong>Deployment and Usage</strong></h4><p>All tools are bundled in one Docker image.</p><p>To get started, pull the image:</p><pre>docker pull niranpeiris/thread-analyzer-tools:1.0.0</pre><p>Run the image:</p><pre>docker run -p 3000:3000 -p 4000:4000 niranpeiris/thread-analyzer-tools:1.0.0</pre><p>Access in your browser:</p><ul><li>Thread Dump Visualizer: <a href="http://localhost:3000/">http://localhost:3000</a></li><li>Thread CPU Usage Analyzer: <a href="http://localhost:4000/">http://localhost:4000</a></li></ul><p><strong>Want to customize or extend the Docker image?</strong> You can clone the full toolkit source and build the image yourself:</p><pre>git clone https://github.com/niranRameshPeiris/thread-analyzer-tools.git<br>cd thread-analyzer-tools<br>docker build -t thread-analyzer-tools .</pre><blockquote><strong>Prefer to run each tool separately instead of using Docker?</strong> You can clone and run the NodeJS apps individually:</blockquote><ol><li><strong>Run Thread CPU Usage Analyzer Manually:</strong></li></ol><pre>git clone https://github.com/niranRameshPeiris/thread-cpu-analyzer.git<br>cd thread-cpu-analyzer<br>npm install<br>npm start</pre><p>Then open your browser at: <a href="http://localhost:3000/">http://localhost:3000</a></p><p>2. <strong>Run</strong> <strong>Thread Dump Visualizer</strong></p><pre>git clone https://github.com/niranRameshPeiris/thread-viz-by-peiris.git<br>cd thread-viz-by-peiris<br>npm install<br>npm start</pre><p>Then open your browser at: <a href="http://localhost:3000/">http://localhost:3000</a></p><h4>Available Repositories</h4><ul><li>Thread Dump Visualizer</li></ul><p><a href="https://github.com/niranRameshPeiris/thread-viz-by-peiris">GitHub - niranRameshPeiris/thread-viz-by-peiris</a></p><ul><li>Thread CPU Usage Analyzer</li></ul><p><a href="https://github.com/niranRameshPeiris/thread-cpu-analyzer">GitHub - niranRameshPeiris/thread-cpu-analyzer</a></p><ul><li>Combined Toolkit</li></ul><p><a href="https://github.com/niranRameshPeiris/thread-analyzer-tools">GitHub - niranRameshPeiris/thread-analyzer-tools</a></p><h3><strong>Disclaimer</strong></h3><p>While these tools offer a streamlined and structured way to analyze Java thread and CPU behavior, <strong>they are not guaranteed to be 100% accurate in every environment</strong>. Variations in thread naming, JVM behavior, or OS-level mappings can affect precision.</p><p>Feel free to download the source code, experiment with it, and adapt it to fit your environment and JVM usage patterns.</p><h3>Conclusion</h3><p>Modern Java applications demand more than basic logs and thread dumps for performance debugging. With these tools, engineers can correlate thread activity, CPU usage, and heap behavior with clarity and speed. Whether responding to an outage or tuning a system under load, having this toolkit can make the difference between guesswork and insight.</p><p>For teams handling large-scale JVM systems, these tools provide a structured and visual approach to understanding what your threads are doing and why it matters.</p><blockquote>If you found this helpful or have suggestions, feel free to explore the source code, try the tools in your environment, or contribute via GitHub. Feedback and improvements are always welcome.</blockquote><blockquote>Tools like these evolve best through shared learning. If you’ve built something similar or face unique thread-related issues in your Java stack, I’d be happy to connect and discuss.</blockquote><blockquote>I plan to continue refining these tools based on real-world feedback. If you use them or build something inspired by them, let me know, I’d love to hear about your experience.</blockquote><blockquote>Thanks for reading!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ef318439d9dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Custom Rate Limiting Policy to limit requests allowed per IP for a defined time period]]></title>
            <link>https://medium.com/@niranpeiris/custom-rate-limiting-policy-to-limit-requests-allowed-per-ip-for-a-defined-time-period-3da95540ac08?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/3da95540ac08</guid>
            <category><![CDATA[wso2-api-manager]]></category>
            <category><![CDATA[custom-throttling]]></category>
            <category><![CDATA[throttling]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Thu, 30 Mar 2023 06:35:48 GMT</pubDate>
            <atom:updated>2023-03-30T06:35:48.979Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*xIUC64mQOcBqU0jgOksQSg.png" /></figure><p>Assume you have exposed multiple APIs to the internet with the use of WSO2 API Manager. All the APIs have been subscribed to a single Application and clients use Client Credential grant to invoke the APIs. In this scenario, since everyone has the same credentials it is hard to limit the number of requests each user is allowed to make.</p><p>In a scenario like this if we want to limit the number of requests one user is allowed to make for a defined time. One possible solution is to limit requests based on IP address.</p><blockquote>Q: How can we achieve this??? 🤔</blockquote><blockquote>A: We can achieve this using a “Custom Rate Limiting” policy.</blockquote><h4>Define a Custom Rate Limiting Policy</h4><p>There are three mandatory fields we have to fill when creating a custom rate-limiting policy.</p><ol><li>Name</li></ol><p>2. Key Template</p><p>3. Siddhi query</p><h4>Siddhi query</h4><p>The below query will only allow 5 requests per IP address for 1 minute.</p><pre>FROM RequestStream<br>SELECT ( cast(map:get(propertiesMap,&#39;ip&#39;),&#39;Long&#39;) == cast(map:get(propertiesMap,&#39;ip&#39;),&#39;Long&#39;) ) AS isEligible ,<br>str:concat(cast(map:get(propertiesMap,&#39;ip&#39;),&#39;String&#39;),&#39;&#39;) as throttleKey<br>INSERT INTO EligibilityStream;<br>FROM EligibilityStream [isEligible==true] #throttler:timeBatch(1 min)<br>SELECT throttleKey, (count(throttleKey) &gt;= 5) as isThrottled, expiryTimeStamp group by throttleKey<br>INSERT ALL EVENTS into ResultStream;</pre><p><strong>RequestStream</strong> consists of many parameters such as userId, apiContext, apiVersion, etc. Also, there is a parameter called <strong>propertiesMap. </strong>Form <strong>propertiesMap</strong> we can get the IP address of the client.</p><blockquote>map:get(propertiesMap,’ip’)</blockquote><h4>Key Template</h4><pre>Key Template: $clientIp</pre><p>When defining the “<strong>Key Template</strong>” we have to make sure Key Template matches the <strong>throttleKey</strong> defined in the Siddhi query.</p><p>In the above query, the <strong>string value of the IP address </strong>has been defined as throttleKey. Hence we have to define the same value in the Key Template.</p><p>As per the documentation,[1] there are a few keys that are allowed to be mentioned under <strong>Key Template</strong>.</p><blockquote>resourceKey, userId, apiContext, apiVersion, appTenant, apiTenant, appId, clientIp</blockquote><p>From this list, <strong>clientIp</strong> represents the IP address of the client.</p><p>Once we save this <strong>Custom Rate limiting policy, </strong>only 5 requests will be allowed for each IP for 1 minute.</p><p>Hope the provided information helps you to understand how to write a Custom Rate Limiting Policy to limit requests allowed per IP for a defined time period…</p><p>[1] — <a href="https://apim.docs.wso2.com/en/4.0.0/design/rate-limiting/advanced-topics/custom-throttling/">https://apim.docs.wso2.com/en/4.0.0/design/rate-limiting/advanced-topics/custom-throttling/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3da95540ac08" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to create your own wso2 docker image?]]></title>
            <link>https://medium.com/@niranpeiris/how-to-create-your-own-wso2-docker-image-95ce0c2335e8?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/95ce0c2335e8</guid>
            <category><![CDATA[apim]]></category>
            <category><![CDATA[dockerfiles]]></category>
            <category><![CDATA[wso2]]></category>
            <category><![CDATA[wso2-api-manager]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Thu, 17 Feb 2022 08:05:58 GMT</pubDate>
            <atom:updated>2022-02-17T12:09:50.489Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>Greetings Everyone !!! 😁</em></strong></p><p>In this medium, I will explain how to build your own WSO2 API Manager image. Before trying out the examples please make sure to install Docker. Also, make sure to download the APIM-3.2.0 binary version from <a href="https://wso2.com/api-management/previous-releases/">here</a></p><p><strong>NOTE: Please note that WSO2 provides a set of API Manager docker images for us. Anyone with an active subscription will be able to pull the latest images from </strong><a href="https://docker.wso2.com/tags.php?repo=wso2am"><strong>here</strong></a><strong>.</strong></p><p>Let&#39;s start creating the image…</p><p>When creating your own docker image mainly there are two steps.</p><p><strong>Step 01: Creating a Dockerfile.</strong></p><p>Dockerfile is a file that contains a set of instructions. Docker uses these instructions to build images automatically. Hence these instructions should be written in a format docker can understand. You can think of this file as the blueprint of the image you are going to build.</p><p><strong>Step 02: Building the image.</strong></p><p>After successfully creating the Dockerfile you have to run the “docker build” command to create the docker image.</p><h3><strong>Step 01: Creating a Dockerfile.</strong></h3><p>When creating a Dockerfile, generally we have to follow a set of guidelines.</p><ol><li><strong>Choose the base Operating System for your image. <br>- </strong>Every docker image must be based on another image. In this example, I have chosen Ubuntu as the base image. <strong>You can also use the WSO2 API Manager docker image if want to.</strong></li><li><strong>Then install all the dependencies.<br></strong>- This step includes tasks such as updating the OS, Installing packages (JAVA), Granting permissions, Setting environmental variables, etc. As a summary, you have to make the base image ready to install and run API Manager.</li><li><strong>Copy the source code. <br></strong>- This step includes copying the files from the local system to the docker image. In this example, we will copy the downloaded API Manager binary to the image. (If you have used the WSO2 API Manager docker image as the base image, you do not need to copy the source code. It will be available inside the base image)</li><li><strong>Specify the Entrypoint.<br></strong>Entry point allows us to specify a command that will be run when the image is running as a container.</li></ol><p>When building docker images it should be done in a layered architecture. Each line of instruction creates a new layer in the docker image. This set of instructions should be written inside a file called Dockerfile.</p><p><strong>Dockerfile</strong></p><blockquote>FROM ubuntu <br>ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64<br>RUN apt-get update <br>RUN apt -y install default-jre<br>RUN apt -y install unzip<br>COPY wso2am-3.2.0.zip /home/wso2am-3.2.0.zip<br>RUN unzip /home/wso2am-3.2.0.zip -d /home/<br>RUN chmod +x /home/wso2am-3.2.0/bin/wso2server.sh<br>ENTRYPOINT sh /home/wso2am-3.2.0/bin/wso2server.sh</blockquote><p>Let’s go through line by line…</p><blockquote>Line1: FROM ubuntu</blockquote><p>Every Dockerfile should start from the “FROM ..” instruction. This is where we have to mention the base image name. In this example, I have used Ubuntu as the base image.</p><blockquote>ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64</blockquote><p>“ENV” instruction can be used to set environmental variables inside an image. Here I have set the JAVA_HOME required to run the API Manager as an environmental variable.</p><blockquote>RUN apt-get update <br>RUN apt -y install default-jre<br>RUN apt -y install unzip</blockquote><p>“RUN” instruction allows us the run commands inside the base image. Here I have used “RUN” instruction to update the OS, install JAVA and UNZIP packages.</p><blockquote>COPY wso2am-3.2.0.zip /home/wso2am-3.2.0.zip</blockquote><p>“COPY” instruction allows us to copy files from the local system to the base image. In this example, I am copying the downloaded API Manager-3.2.0 binary from my local machine to the base image.</p><blockquote>RUN unzip /home/wso2am-3.2.0.zip -d /home/<br>RUN chmod +x /home/wso2am-3.2.0/bin/wso2server.sh</blockquote><p>Here I have extracted the copied binary to the home directory. After that, I have given the executable permission to the “ wso2server.sh” script.</p><blockquote>ENTRYPOINT sh /home/wso2am-3.2.0/bin/wso2server.sh</blockquote><p>“ENTRYPOINT” instruction allows us to specify a command to be run at the time we run the image as a container. In this example, I am running the startup script at the time the container starts.</p><p>After successfully creating a Dockerfile all we have to do is run a few commands to build the image. Let’s see what are the command required for this.</p><h3>Step 02: <strong>Building the image.</strong></h3><p>When you have the Dockerfile with you, building the image is easy. All. you have to do is use a set of docker commands.</p><p>First of all, make sure you have the <strong>API Manager binary </strong>and the <strong>Dockerfile</strong> in the same location where you going to execute the “docker build” command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*TK9NNrUAt9yBTtlnV1CCJQ.png" /></figure><blockquote>docker build -t [image-name] [location-of-the-Dockerfile]</blockquote><blockquote><strong>Example</strong>: docker build -t my-apim .</blockquote><p>As you can see below, docker will create your image step by step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*ZKrQED5ZPmFxCH-hkla1sw.png" /></figure><p><strong><em>Please note that when you are building the image for the first time there will be lots of log lines.</em></strong></p><p>Once you are done building your image, you can go ahead and run it.</p><blockquote>docker run -it -p 8243:8243 -p 9443:944 3 [image-name]</blockquote><blockquote><strong>Example</strong>: docker run -it -p 8243:8243 -p 9443:9443 my-apim</blockquote><ul><li>-i = Keep STDIN open even if not attached[1]</li><li>-t = Allocate a pseudo-TTY[1]</li><li>-p = Ports 8243 and 9443 have been mapped to the corresponding Docker host ports. (host machine on top of which containers are spawned)</li></ul><p>-it runs Docker interactively (you get a pseudo-TTY with STDIN).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aQJXJieqwgWUriRw21Hw0w.png" /></figure><p>Here I have only explained simple steps that you can follow to build your own API Manager image. When running API Manager inside a docker container, there are a few additional areas that you have to consider as well.</p><ol><li>A method to change the configurations.</li><li>How to persist the synapse artifacts.</li></ol><h3>A method to change the configurations.</h3><p>When it comes to the docker environment, we just cannot exec into the docker image and then restart the API Manager after changing the configurations. Once we destroy the container all the changes will be lost. Hence we should find a way to persist these configurations.</p><ol><li><strong>Copying the configurations while building the image.<br></strong><em>You can simply achieve this by adding an additional instruction to the Dockerfile. The downside of this is, we have to rebuild the image each time we make changes to the configuration file.</em></li><li><strong>Using docker volume. (Change without rebuilding)<br></strong>We can mount a configuration file or a directory while starting the container. When using this approach we do not have to rebuild the image again and again.</li></ol><blockquote>docker run -it — mount type=bind,source=[LOCAL_PATH]/deployment.toml,target=[CONTAINER_PATH]/deployment.toml [image-name]</blockquote><blockquote>docker run -it — mount type=bind,source=/home/niran/docker-images/apim-image/deployment.toml,target=/home/wso2am-3.2.0/repository/conf/deployment.toml my-apim</blockquote><h3>How to persist the synapse artifacts.</h3><p>When it comes to docker, once you stop the container all the synapse artifacts related to APIs will be lost. Hence in order to persist this data, you have to mount the <strong>synapse-configs</strong> and <strong>executionplans</strong> directories while starting the containers.</p><p>First, you have to create two folders to be mounted in your local machine. then you can run the image with some additional parameters.</p><blockquote>docker run -it <br>— mount type=bind,source=[LOCAL_PATH]/synapse-configs,target=[CONTAINER_PATH]/synapse-configs <br> — mount type=bind,source=[LOCAL_PATH]/executionplans,target=[CONTAINER_PATH]/executionplans<br> [image-name]</blockquote><blockquote><strong>Example</strong>:</blockquote><blockquote>docker run -it<br>— mount type=bind,source=/home/niran/docker-images/apim-image/synapse-configs,target=/home/wso2am-3.2.0/repository/deployment/server/synapse-configs <br> — mount type=bind,source=/home/niran/docker-images/apim-image/executionplans,target=/home/wso2am-3.2.0/repository/deployment/server/executionplans<br>my-apim</blockquote><p>Hope the provided information helps you to understand how to build your own WSO2 API Manager docker image from the scratch…</p><p>[1] <a href="https://docs.docker.com/engine/reference/commandline/run/">https://docs.docker.com/engine/reference/commandline/run/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95ce0c2335e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Hacking Search Terms]]></title>
            <link>https://medium.com/@niranpeiris/google-hacking-search-terms-5791d80ff75e?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/5791d80ff75e</guid>
            <category><![CDATA[google-hacking]]></category>
            <category><![CDATA[penetration-testing]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[search-term]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Sat, 21 Mar 2020 09:34:43 GMT</pubDate>
            <atom:updated>2020-03-21T09:34:43.202Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*qwquyLhvUIkjvQbq" /></figure><h4>Link</h4><pre>link:www.google.com</pre><p>List down the web-pages that have links pointing to the Google homepage.</p><h4>Related</h4><pre>related:www.google.com</pre><p>List down the web pages that are similar to the Google homepage.</p><h4>Info</h4><pre>info:www.google.com</pre><p>Shows information about the Google homepage.</p><h4>site</h4><pre>site:www.google.com</pre><p>Google will restrict the results to those websites in the given domain</p><h4>allintitle</h4><pre>allintitle: google search</pre><p>Return only documents that have both “google” and “search” in the title.</p><h4>intitle</h4><pre>intitle:google search</pre><p>Return documents that mention the word “google” in their title, and mention the word “search” anywhere in the document (title or body).</p><h4>allinurl</h4><pre>allinurl: google search</pre><p>Return only documents that have both “google” and “search” in the URL.</p><pre>allinurl: foo/bar</pre><p>Restrict the results to pages with the words “foo” and “bar” in the URL</p><h4>inurl</h4><pre>inurl:google search</pre><p>Return documents that mention the word “google” in their URL, and mention the word “search” anywhere in the document (URL or no).</p><p>Examples</p><ol><li>( filetype:flv | filetype:mp4 ) intext:keyword inurl:youtube</li><li>index of:dan brown</li><li>( . ) a single-character wildcard and ( * ) any words</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5791d80ff75e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cross-site Request Forgery protection (CSRF) via Double Submit Cookies Patterns]]></title>
            <link>https://medium.com/@niranpeiris/cross-site-request-forgery-protection-csrf-via-double-submit-cookies-patterns-8366d7d4549f?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/8366d7d4549f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[cookies]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Fri, 20 Mar 2020 19:27:26 GMT</pubDate>
            <atom:updated>2020-03-21T05:27:51.506Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Click <a href="https://medium.com/@niranpeiris/cross-site-request-forgery-protection-csrf-via-synchronizer-token-patterns-4f9a530871b0">here</a> to find out what is CSRF and what are the primary two methods that can be used to protect web applications from CSRF.</p><h3>How do Double Submit Cookies Patterns work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/803/1*nNQ1tkSyhC8E5HaR8I592A.png" /><figcaption>workflow</figcaption></figure><h3>How to implement Cross-site Request Forgery protection via Double Submit Cookies Patterns?</h3><p>Here I am going to explain how to implement Cross-site Request Forgery protection (CSRF) via Double Submit Cookies Patterns in a PHP web application.</p><p>Click <a href="https://github.com/niranRameshPeiris/Cross-site-Request-Forgery-protection-in-web-applications-via-Double-Submit-Cookies-Patterns">here</a> to download the PHP web application which will be used below.</p><p>Remember since we set secure flags in the cookies you have to enable https in your localhost.</p><blockquote><em>Step 01:Creating a web application</em></blockquote><blockquote><em>Step 02:Edit the login function</em></blockquote><blockquote><em>Edit the login function to generate a random string and store it inside a cookie as the CSRF token while you validating user credentials and creating the session attributes as below and then </em><strong><em>set the secure flag of the CSRF cookie so that it only can be sent through https (Encrypted).</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*KArcXo9j4ZAqSetAdgRB7g.png" /><figcaption>login function</figcaption></figure><blockquote><em>Step 03:When loading a page request CSRF token from the cokkie.</em></blockquote><blockquote><em>Client-side code</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*FQcJLlnC1BlVDPdEdsKvnA.png" /><figcaption>client-side</figcaption></figure><blockquote><em>In the above code by usingn JavaScript CSRF token will be derived from the cokkie and then set the derived value to a hidden input field called ‘csrf_token’ to be used when submittiing a form.</em></blockquote><blockquote><em>Step 04: When submitting a form send the CSRF token which was set by using JavaScript when loading the page for validation.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/711/1*2UFudmxu0US-V3pVUQi0rA.png" /><figcaption>form submission</figcaption></figure><blockquote><em>Step 05: Validate the CSRF token before performing the action requested by the client-side.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/1*i5S2uRBxCXQol-183mtfSg.png" /><figcaption>validation</figcaption></figure><blockquote>In the above code, the server read the CSRF cookie and checks whether the value taken from the cookie is the same as the value retrieved from the POST request.</blockquote><h3>Lets test whether above implementation works</h3><p><strong><em>Valid form submission by a legitimate user</em></strong></p><p>When a legitimate user submits the form backend should respond the way it supposes to since the CSRF token send to the backend is valid.</p><p><strong><em>Invalid form submission by an attacker</em></strong></p><p>Assume an attacker trick the user to submit the same form through a link or something since the attacker doesn’t have access to the CSRF token the request should fail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*_A6ukIo6qMlk4nve_fGEcw.png" /><figcaption>warning</figcaption></figure><h3>Conclusion</h3><blockquote><em>Important thing is that an attacker cannot gain the same access token since JavaScript cannot add values to the head when the domain is different, also since the secure flag of the CSRF cookie is set it can only send through https. In that scenario, no one will be able to eavesdrop on your communication channel.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8366d7d4549f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cross-site Request Forgery protection (CSRF) via Synchronizer Token Patterns]]></title>
            <link>https://medium.com/@niranpeiris/cross-site-request-forgery-protection-csrf-via-synchronizer-token-patterns-4f9a530871b0?source=rss-4e253b3b6696------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f9a530871b0</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[csrf]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[synchronizertokenpattern]]></category>
            <dc:creator><![CDATA[Niran Peiris]]></dc:creator>
            <pubDate>Fri, 20 Mar 2020 18:50:48 GMT</pubDate>
            <atom:updated>2020-03-20T19:27:53.839Z</atom:updated>
            <content:encoded><![CDATA[<h3>What is CSRF</h3><p>Cross-site request forgery is an attack where an attacker trick authenticated user of a web application to perform an action without their consent. (you do it without knowing you are doing it)</p><p><em>Ex: Assume that you have a web application and you can perform actions like shutting down a website, also assume that this action will be handled by a GET request shown as below (CSRF also can be applied for the POST request as well)</em></p><pre>Request : - https://ww.exampleCSRF.com/action?action=shutdown</pre><p>What an attacker is sending you is an image like below to trick you.</p><pre>Link : - <br>&lt;a href=&quot;https://ww.exampleCSRF.com/action?action=shutdown&quot;&gt;<br> &lt;img src=&quot;img url&quot;&gt;<br> &lt;/a&gt;</pre><p>When you click on it without you knowing you will shut down your web application if you are logged in already.</p><h3>How to protect your web application from CSRF?</h3><ol><li>Synchronizer Token Patterns</li><li>Double Submit Cookies Patterns</li></ol><h3>How do Synchronizer Token Patterns work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*aiBmESC5M7epfmV1obNxzw.png" /><figcaption>Workflow</figcaption></figure><h3>How to implement Cross-site Request Forgery protection via Synchronizer Token Patterns?</h3><p>Here I am going to explain how to implement Cross-site Request Forgery protection (CSRF) via Synchronizer Token Patterns in a PHP web application.</p><p>Click <a href="https://github.com/niranRameshPeiris/Cross-site-Request-Forgery-protection-in-web-applications-via-Synchronizer-Token-Pattern">here</a> to download the PHP web application which will be used below.</p><blockquote>Step 01:Creating a web application</blockquote><blockquote>Step 02:Edit the login function</blockquote><blockquote>Edit the login function to generate a random string and store it in the memory as the CSRF token while you validating user credentials and creating the session attributes as below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*XPdfHZ4U29wtCwW9h9eOGA.png" /><figcaption>login function</figcaption></figure><blockquote>Step 03:When loading a page request CSRF token from the server</blockquote><blockquote>Client-side code</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*i6mEeInoCWt5z3_lBSzpLQ.png" /><figcaption>client-side</figcaption></figure><blockquote>server-side code</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*uQZmQeweqOna4A5jMiXYcQ.png" /><figcaption>server-side</figcaption></figure><blockquote>Step 04: <em>When submitting a form send the CSRF token which was set by using JavaScript when loading the page for validation.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*JVdLJjoJiZkTJhkWOrZbdQ.png" /><figcaption>form submission</figcaption></figure><blockquote>Step 05: <em>Validate the CSRF token before performing the action requested by the client-side.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*KritBPB_eUyL0ymoJEZcuA.png" /><figcaption>validation</figcaption></figure><h3>Lets test whether above implementation works</h3><p><strong><em>Valid form submission by a legitimate user</em></strong></p><p>When a legitimate user submits the form backend should respond the way it supposes to since the CSRF token send to the backend is valid.</p><p><strong><em>Invalid form submission by an attacker</em></strong></p><p>Assume an attacker trick the user to submit the same form through a link or something since the attacker doesn&#39;t have access to the CSRF token the request should fail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*_A6ukIo6qMlk4nve_fGEcw.png" /><figcaption>CSRF detection</figcaption></figure><h3><strong>Conclusion</strong></h3><blockquote>Important thing is when we have used Synchronizer Token Patterns to defend against CSRF attack, an attacker cannot gain the same access token since <strong><em>cross domains ajax requests aren’t possible</em></strong>.</blockquote><p>Click <a href="https://medium.com/@niranpeiris/cross-site-request-forgery-protection-csrf-via-double-submit-cookies-patterns-8366d7d4549f">here</a> to find out How to implement Cross-site Request Forgery protection (CSRF) via Double Submit Cookies Patterns.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f9a530871b0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>