<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arvind Sharma</title>
    <description>The latest articles on DEV Community by Arvind Sharma (@arvindsharma18).</description>
    <link>https://dev.to/arvindsharma18</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F878970%2Fed4f9156-a9d7-4722-88bd-65b4fee2f0ac.png</url>
      <title>DEV Community: Arvind Sharma</title>
      <link>https://dev.to/arvindsharma18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arvindsharma18"/>
    <language>en</language>
    <item>
      <title>Istio IP based Whitelisting</title>
      <dc:creator>Arvind Sharma</dc:creator>
      <pubDate>Sat, 22 Oct 2022 09:51:02 +0000</pubDate>
      <link>https://dev.to/arvindsharma18/istio-ip-based-whitelisting-18c7</link>
      <guid>https://dev.to/arvindsharma18/istio-ip-based-whitelisting-18c7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;You might have seen &lt;strong&gt;&lt;em&gt;Staff Only&lt;/em&gt;&lt;/strong&gt; signboards in malls, restaurant and other public places. In general, while you are free to use or roam around public places, there are some place which only authorized entities are allowed, like kitchen in an restaurant.&lt;/p&gt;

&lt;p&gt;Similarly while most of the services on the internet can be accessed by the public, there are some services which can only be accessed by &lt;em&gt;Authorized users&lt;/em&gt;. Usually such services have sensitive information, breach in which might lead to application compromise or other devastating consequences.&lt;br&gt;
It is therefore important to make sure that such services are secured to only authorized entity. One such measure is Whitelisting.&lt;/p&gt;
&lt;h2&gt;
  
  
  What is Istio
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;&lt;a href="https://istio.io/" rel="noopener noreferrer"&gt;Istio&lt;/a&gt; extends Kubernetes to establish a programmable, application-aware network using the powerful Envoy service proxy. Working with both Kubernetes and traditional workloads, Istio brings standard, universal traffic management, telemetry, and security to complex deployments&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Whitelisting in Istio
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: It is assumed that the reader has already setup Istio within the cluster.&lt;br&gt;
Istio provides &lt;a href="https://istio.io/latest/docs/tasks/security/authorization/authz-ingress/#ip-based-allow-list-and-deny-list" rel="noopener noreferrer"&gt;capability&lt;/a&gt; of allowing/denying access based on IP list. It achieves it through &lt;strong&gt;AuthorizationPolicy&lt;/strong&gt;. &lt;/p&gt;
&lt;h3&gt;
  
  
  Authorization Policy
&lt;/h3&gt;

&lt;p&gt;Istio has provided a wonderful &lt;a href="https://istio.io/latest/docs/reference/config/security/authorization-policy/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; on Authorization Policy. I would highly recommend the reader to go through them for finer nuances. For our scenario, we will work with &lt;strong&gt;&lt;em&gt;selector, rules and action&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8sqnr4i0154bdowttt2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8sqnr4i0154bdowttt2.png" alt="Istio IP based Whitelisting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At server side Envoy proxy, Istio through AuthorizationPolicy enforces allowing/denying access of inbound traffic. When a request comes to proxy, authorization engines, checks the incoming context against enforced policy and returns appropriate access as mentioned in the policy.&lt;/p&gt;
&lt;h3&gt;
  
  
  Implementing IP based Whitelisting using Authorization Policy
&lt;/h3&gt;

&lt;p&gt;We can enforce multiple authorization checks, but in this example, we will restrict our policy to Whitelist only.&lt;/p&gt;

&lt;p&gt;Suppose, you want to enforce Whitelist to path &lt;em&gt;&lt;strong&gt;/my_critical_service&lt;/strong&gt;&lt;/em&gt; to only a set of trusted IP (say &lt;em&gt;&lt;strong&gt;xxx.xxx.xxx.xxx/y,aaa.aaa.aaa.aaa/b&lt;/strong&gt;&lt;/em&gt;), we can achieve it by applying the below manifest. The below manifest will DENY all request to paths (/my_critical_service) which are not mentioned in the IP blocks list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: &amp;lt;INSERT APPROPRIATE NAME&amp;gt;
 namespace: &amp;lt;INSERT NAMESPACE&amp;gt;
spec:
 selector:
   matchLabels:
     key_for_istio_metdata: value_for_istio_metadata
 action: DENY
 rules:
 - from:
   - source:
       notIpBlocks: ["xxx.xxx.xxx.xxx/y","aaa.aaa.aaa.aaa/b"]
   to:
   - operation:
       paths: ["/my_critical_service/*"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's bisect to understand the above manifest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: &amp;lt;INSERT APPROPRIATE NAME&amp;gt;
 namespace: &amp;lt;INSERT NAMESPACE&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once Istio is setup, we can then use its CR, Authorization Policy. Assign appropriate metadata based on Istio installation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spec:
 selector:
   matchLabels:
     key_for_istio_metdata: value_for_istio_metadata
 action: DENY
 rules:
 - from:
   - source:
       notIpBlocks: ["xxx.xxx.xxx.xxx/y","aaa.aaa.aaa.aaa/b"]
   to:
   - operation:
       paths: ["/my_critical_service/*"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned earlier, Authorization Policy provides many options for enforcing policy checks, but for our task we have used &lt;strong&gt;&lt;em&gt;selector, action and rules&lt;/em&gt;&lt;/strong&gt; appropriately for IP based whitelisting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;selector&lt;/strong&gt; field is optional, which is used for selecting scope of our policy, i.e Target Workload where we want to enforce policy checking. 
Since we will be using Istio for Ingress, assign appropriate label key and values for identifying it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;action&lt;/strong&gt; field is used for enforcing appropriate action for set of rules. We can ALLOW or DENY request based on rules.
For our task, we will &lt;strong&gt;DENY&lt;/strong&gt; any request which is not matching our rules.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;rules&lt;/strong&gt; field specifies list of criteria for actions to be enforced. We will be using &lt;em&gt;from&lt;/em&gt; and &lt;em&gt;to&lt;/em&gt; rules for our example.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;from rule&lt;/strong&gt;, specifies the source of the request. We add trusted sources in notIpBlocks, so that we can DENY all request which are not trusted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;to rule&lt;/strong&gt;, specifies the operation of a request. In our scenario, we want to enforce whitelist for our &lt;em&gt;&lt;strong&gt;my_critical_service&lt;/strong&gt;&lt;/em&gt; path and so we provide appropriate path of our service within the rule.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that's all. Now you can replace or add some of your own criteria to make your network secure!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We have seen in this blog, on how to enforce IP based whitelisting using Authorization Policy in Istio. Hope you found this blog helpful.&lt;/p&gt;

&lt;p&gt;P.S: Cover Image Credit: &lt;a href="Image%20by%20&amp;lt;a%20href="&gt;S. Hermann &amp;amp;amp; F. Richter&lt;/a&gt; from &lt;a href="https://pixabay.com//?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=4031973" rel="noopener noreferrer"&gt;Pixabay&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>istio</category>
      <category>devops</category>
      <category>operations</category>
    </item>
    <item>
      <title>Monitoring Keycloak using Prometheus Operator - Kubernetes &amp; Helm Charts</title>
      <dc:creator>Arvind Sharma</dc:creator>
      <pubDate>Sat, 25 Jun 2022 09:55:30 +0000</pubDate>
      <link>https://dev.to/arvindsharma18/monitoring-keycloak-using-prometheus-operator-kubernetes-helm-charts-14f6</link>
      <guid>https://dev.to/arvindsharma18/monitoring-keycloak-using-prometheus-operator-kubernetes-helm-charts-14f6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Just like how a surveillance system over a compound wall enhances the security of our homes, Keycloak too requires continuous surveillance to better safeguard its application.&lt;/p&gt;

&lt;p&gt;This tutorial assumes that the reader has basic understanding on &lt;a href="https://www.keycloak.org/" rel="noopener noreferrer"&gt;Keycloak&lt;/a&gt; and it helps the readers to set up &lt;a href="https://prometheus-operator.dev/" rel="noopener noreferrer"&gt;Prometheus Operator&lt;/a&gt; to monitor their Keycloak. &lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;Before we begin, let us first, visualize what we will be doing to achieve our target, to monitor Keycloak using Prometheus Operator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcm8esmmbjtuxfnnju3b4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcm8esmmbjtuxfnnju3b4.png" alt="Solution Setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, let us assume that Keycloak is already installed on Keycloak Namepace.&lt;/p&gt;

&lt;p&gt;We will then: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Install Prometheus Operator on Monitoring Namespace which will automatically deploy Prometheus pod.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable Keycloak to publish its metrics at &lt;em&gt;keycloak-metrics&lt;/em&gt; Service.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Service Monitor (CRD provided by Operator) which points to the &lt;em&gt;keycloak-metrics&lt;/em&gt; Service and attach it to our Prometheus Pod.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For this tutorial, I have used Keycloak &lt;a href="https://github.com/bitnami/charts/tree/master/bitnami/keycloak" rel="noopener noreferrer"&gt;helm chart&lt;/a&gt; from Bitnami. Feel free to use other helm charts and adjust the parameters accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing Prometheus Operator
&lt;/h2&gt;

&lt;p&gt;We will install the Prometheus Operator using &lt;a href="https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack" rel="noopener noreferrer"&gt;Helm charts&lt;/a&gt; from prometheus-community. Feel free to use any other helm chart and change the parameters accordingly.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding Helm Repository
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Install Prometheus-Operator
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Adjust other parameters accordingly based on requirements.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;custom-values.yaml&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

      ...
      prometheus:
        prometheusSpec:
          serviceMonitorSelector:
            matchLabels:
              &amp;lt;Service Monitor Label Key&amp;gt;: &amp;lt;Service Monitor Label Value&amp;gt;
      ...



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are intimating Prometheus to look out for a &lt;strong&gt;Service Monitor&lt;/strong&gt; by adding labels into serviceMonitorSelector. &lt;br&gt;
Remember the labels, as we will have to use the same labels while creating Service Monitor later in this tutorial.&lt;/p&gt;

&lt;p&gt;Once &lt;em&gt;custom-values.yaml&lt;/em&gt; are modified according to the requirement, install Prometheus-Operator using below command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

helm install -f custom-values.yaml --namespace=[MONITORING_NAMESPACE] [RELEASE_NAME] prometheus-community/kube-prometheus-stack


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Exposing Keycloak Metrics
&lt;/h2&gt;

&lt;p&gt;Ultimately, our aim is to monitor Keycloak's metrics. Keycloak provides options to make it's metrics available for scraping and monitoring. To enable it, make sure to adjust this parameter to  Keycloak helm's &lt;em&gt;values.yaml&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;values.yaml&lt;/em&gt;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

      ...
      metrics:
        enabled: true
      ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
By default, &lt;a href="https://github.com/bitnami/charts/blob/master/bitnami/keycloak/templates/metrics-service.yaml" rel="noopener noreferrer"&gt;keycloak-metrics&lt;/a&gt; Service will include http-management port, but the metrics for scraping are enabled at &lt;strong&gt;http port&lt;/strong&gt; (80). So, make sure you &lt;strong&gt;patch&lt;/strong&gt; the &lt;em&gt;keycloak-metrics&lt;/em&gt; Service to include &lt;strong&gt;http port&lt;/strong&gt; in its ports specifications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Service Monitors
&lt;/h2&gt;

&lt;p&gt;Now that we have Prometheus-Operator running and Keycloak making it's metrics available at http port, how do we point the Prometheus to scrape at the http port where Keycloak furnishes it's metrics?&lt;/p&gt;

&lt;p&gt;The answer is Service Monitors. Service Monitors are used by Prometheus to automatically detect it's target service to scrape data. Refer &lt;a href="https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/user-guides/getting-started.md" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;Since during our installation, we had notified the Prometheus to look out for a Service Monitor by providing labels in serviceMonitorSelector, we will now create that service monitor with the information of our &lt;em&gt;keycloak-metrics&lt;/em&gt; service.&lt;/p&gt;

&lt;p&gt;Deploy the below manifest.&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt;: This manifest should be deployed only after the Prometheus-Operator is installed. As we are attaching this Service Monitor to Prometheus, therefore it must be there in the namespace where this Service Monitor is deployed.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Service Monitor Manifest&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
  name: &amp;lt;Insert Appropriate Name for Service Monitor&amp;gt;
  Namespace: &amp;lt;MONITORING_NAMESPACE&amp;gt;
  labels:
    &amp;lt;Service Monitor Label Key&amp;gt;: &amp;lt;Service Monitor Label Value&amp;gt;
spec:
  endpoints:
    - ports: http
      path: /auth/realms/master/metrics
      interval: &amp;lt;Select Appropriate Intervals at which the Metrics will be scraped automatically&amp;gt;
  selector:
    matchLabels:
      app.kubernetes.io/component: metrics
  namespaceSelector:
    matchNames:
      - &amp;lt;Insert Namespace where Keycloak is installed&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Make sure that the &lt;strong&gt;metadata.labels&lt;/strong&gt; have the same key/values as the ones we have provided in serviceMonitorSelector during Prometheus-Operator installation.&lt;/p&gt;

&lt;p&gt;I will now breakdown the manifest for better understanding.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  endpoints:
    - ports: http
      path: /auth/realms/master/metrics
      interval: &amp;lt;Select Appropriate Intervals at which the Metrics will be scraped automatically&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In &lt;em&gt;endpoints&lt;/em&gt; of Service Monitor specification, we mention at &lt;em&gt;&lt;strong&gt;which port&lt;/strong&gt;&lt;/em&gt; to scrape, &lt;em&gt;&lt;strong&gt;what path&lt;/strong&gt;&lt;/em&gt; to scrape, and &lt;em&gt;&lt;strong&gt;when (interval)&lt;/strong&gt;&lt;/em&gt; to scrape. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: &lt;em&gt;/auth/realms/master/metrics&lt;/em&gt; will publish the data for all the realms.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

  selector:
    matchLabels:
      app.kubernetes.io/component: metrics
  namespaceSelector:
    matchNames:
      - &amp;lt;Insert Namespace where Keycloak is installed&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above two selectors provide the service monitor with information about the service which will publish the data. So make sure you provide appropriate &lt;em&gt;selector labels&lt;/em&gt; for &lt;em&gt;keycloak-metrics service&lt;/em&gt; and &lt;em&gt;namespace&lt;/em&gt; where &lt;em&gt;Keycloak is installed&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;And that's it! Prometheus will now start scraping the Keycloak metrics automatically at given intervals. To visualize them, you now can open the dashboard and view the metrics!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;By going through this tutorial, we will now be able to monitor and analyze Keycloak metrics and view them on customizable Dashboards of Prometheus/Grafana.&lt;/p&gt;

&lt;p&gt;Thanks for checking out this post!&lt;/p&gt;

&lt;p&gt;Cover Image credits: Image by &lt;a href="https://pixabay.com/users/3844328-3844328/?utm_source=link-attribution&amp;amp;utm_medium=referral&amp;amp;utm_campaign=image&amp;amp;utm_content=1863880" rel="noopener noreferrer"&gt;Lorenzo Cafaro&lt;/a&gt; from Pixabay &lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
