<?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 Alper Kurtoglu on Medium]]></title>
        <description><![CDATA[Stories by Alper Kurtoglu on Medium]]></description>
        <link>https://medium.com/@alperkrtglu?source=rss-ca043f888bad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*C-Hg7iBnree-C7xU</url>
            <title>Stories by Alper Kurtoglu on Medium</title>
            <link>https://medium.com/@alperkrtglu?source=rss-ca043f888bad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:09:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alperkrtglu/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[Setting Up Jenkins on EC2 and Running Pipeline in Docker]]></title>
            <link>https://medium.com/@alperkrtglu/setting-up-jenkins-on-ec2-and-running-pipeline-in-docker-f7807c435e03?source=rss-ca043f888bad------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7807c435e03</guid>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[pipeline]]></category>
            <category><![CDATA[jenkins]]></category>
            <dc:creator><![CDATA[Alper Kurtoglu]]></dc:creator>
            <pubDate>Sat, 23 Sep 2023 20:30:28 GMT</pubDate>
            <atom:updated>2023-09-23T20:31:33.383Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*yH0CEJSIHHTdflYG" /></figure><p>In this tutorial, we will talk about setting up Jenkins on an EC2 instance and why we need to use Docker as an agent.</p><h4>What are the advantages of running a Jenkins pipeline in Docker?</h4><p>First, let’s answer what an <em>agent</em> is. An agent can be a physical machine, a virtual machine, or even a container. Simply put, anything that can run Java can serve as an agent for Jenkins.</p><p>We want our pipeline to not be tied to a single machine and to be portable across different machines if needed. Using a container as a pipeline agent gives us this flexibility. An image contains everything we need, independent of the server — like the operating system, tools according to our needs, dependencies, etc. Containerization makes it easier to move Jenkins instances to different machines. It eliminates many complex issues related to Jenkins setup and management.</p><p>It’s useful for version management. We can easily manage different versions of a specific tool. We can quickly update the special versions needed for different projects or stages.</p><p>From a security perspective, containers run in their own isolated environment. This adds an extra layer of security for us.</p><p>For testing different tools and technologies, we can use Docker containers. If someone has to install these tools onto agents for you, it can take a long time. However, by using containers, we can quickly test different tools and, if they are useful, consider installing them permanently on the agent.</p><p>First, we need to complete the Docker installation. For this, we will connect to an EC2 (Amazon Linux 2) instance and follow the steps below.</p><pre>sudo yum update<br>sudo yum install docker</pre><p>To run Docker commands without using sudo each time, we should execute the commands below.</p><pre>sudo groupadd docker<br>sudo usermod -aG docker ${USER}<br>newgrp docker</pre><p>To check Docker, let’s run the command below.</p><pre>docker info</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Eko0LGnc2kzRTeJHagM2FQ.png" /><figcaption>Connection could not be established because the Docker daemon is not running.</figcaption></figure><p>With these commands, the Docker service is manually started. Then, the Docker daemon is configured to run automatically when the system starts.</p><pre>sudo service docker start <br>sudo systemctl enable docker.service</pre><p>Next, we can start the Jenkins installation.</p><pre>docker run \<br>  -u root \<br>  -d \<br>  -p 80:8080 \<br>  -p 50000:50000 \<br>  -v jenkins_home:/var/jenkins_home \<br>  -v /var/run/docker.sock:/var/run/docker.sock \<br>  --restart=always \<br>  --name jenkins \<br>  jenkins/jenkins</pre><p>When we try to access Jenkins, as seen in the image, the page is unreachable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GqV6ti2oLRKvupR7KTLwEg.png" /></figure><p>For this, we need to add inbound rules to the Security Group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r2-6V9b0gq2pLrIUGy7E5g.png" /><figcaption>It accepts HTTP requests coming from port 80.</figcaption></figure><p>Now we can finally access Jenkins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fVk2ZYKLJZ-CstXZ0VX4Fw.png" /></figure><p>To obtain the password, we first need to connect to the Jenkins container and print the initialAdminPassword to the console.</p><pre>docker exec -it jenkins bash<br>cat /var/jenkins_home/secrets/initialAdminPassword</pre><p>We click on ‘Create a job’ and create a pipeline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ozQTXdVNfFRsnatmkNCKTg.png" /></figure><p>We set up the repository settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tBZL2JwPSgzST5JItqRVsw.png" /><figcaption>We filled in the Jenkinsfile path according to the path in the project. Jenkins will look for the Jenkinsfile at this path and run the pipeline accordingly.</figcaption></figure><p>Next, we need to go to the Plugin section and install the “Docker Pipeline” plugin.</p><p>Additionally, to run Docker commands in Jenkins, we need to install Docker CLI in the Jenkins container.</p><pre>apt-get update &amp;&amp; apt-get install -y docker.io</pre><p>Below is an example Jenkinsfile that we used.</p><pre>pipeline {<br>    agent any<br>    stages {<br>        stage(&#39;Prepare build agent&#39;) {<br>            agent {<br>                docker {<br>                    image &#39;openjdk:17.0.1-jdk-slim&#39;<br>                    args &#39;-v /var/run/docker.sock:/var/run/docker.sock -v /home/ec2-user/.m2:/root/.m2 -u root:root&#39;<br>                    reuseNode true<br>                }<br>            }<br>            stages {<br>                stage(&#39;[Started]&#39;) {<br>                    steps {<br>                        sh &#39;echo Pipeline Started!&#39;<br>                    }<br>                }<br>                stage(&#39;[Clean]&#39;) {<br>                    steps {<br>                        sh &#39;./mvnw clean&#39;<br>                    }<br>                }<br>            }<br>        }<br>    }<br>}</pre><ul><li><strong>agent any</strong> : Indicates that the pipeline can run on any agent (node).</li><li><strong>agent docker</strong> : Specifies that the pipeline will run on a container. In other words, either the Jenkins master or any suitable agent will pull this Docker image, start the container, and the pipeline will run on this container.</li><li><strong>-v /var/run/docker.sock:/var/run/docker.sock</strong> : Mounts the docker.sock on the container to the docker.sock on the host machine. This allows applications running on the container to communicate with the Docker daemon on the host machine.</li><li><strong>-v /home/ec2-user/.m2:/root/.m2 </strong>: Mounting the .m2 file ensures that Maven dependencies will not be downloaded again each time. In short, Maven dependencies will be cached.</li><li>-<strong>u root:root</strong> : Allows the container to run as the root user.</li><li><strong>reuseNode true</strong> : Run the container on the node specified at the top-level of the Pipeline, in the same workspace, rather than on a new node entirely.</li></ul><p>When we run the build and check the console, we see that a new container has been created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hwlSpwoj7MfPGD2JU-OpVA.png" /><figcaption>As seen in the picture, the pipeline is running on the container.</figcaption></figure><p>When we come to the Output section, we see that the build has been successfully completed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DR9G5icl2jnMTCROxMPVGQ.png" /></figure><h3>References</h3><p><a href="https://www.jenkins.io/doc/book/pipeline/docker/">https://www.jenkins.io/doc/book/pipeline/docker/</a></p><p><a href="https://www.jenkins.io/blog/2021/12/08/containers-as-build-agents/">https://www.jenkins.io/blog/2021/12/08/containers-as-build-agents/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7807c435e03" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spring OAuth2 with OIDC: Moving from ‘Scope’ to ‘Roles’]]></title>
            <link>https://medium.com/@alperkrtglu/spring-oauth2-with-keycloak-moving-from-scope-to-roles-34247f3ff78e?source=rss-ca043f888bad------2</link>
            <guid isPermaLink="false">https://medium.com/p/34247f3ff78e</guid>
            <category><![CDATA[spring-security]]></category>
            <category><![CDATA[keycloak]]></category>
            <category><![CDATA[oauth2]]></category>
            <category><![CDATA[spring]]></category>
            <dc:creator><![CDATA[Alper Kurtoglu]]></dc:creator>
            <pubDate>Wed, 16 Aug 2023 19:53:39 GMT</pubDate>
            <atom:updated>2023-08-17T11:45:29.832Z</atom:updated>
            <content:encoded><![CDATA[<p>In this tutorial, I will discuss how to use authorities instead of <em>scopes</em> as roles in Spring OAuth2.</p><p>By default when Spring OAuth2 decodes JWT, it uses the <em>scope</em> claim instead of user <em>roles</em> as authorities.</p><p>First, let’s discuss the difference between scope and authority;</p><h4><strong>Scope</strong></h4><p>Scopes define the permissions that determine what data of a user an application can access. For instance, if a 3rd party application wants to recommend movies to a user, it requires access to the movies the user has watched (e.g., “watched_movies”). This is where scopes come into play. This 3rd party application can access user information only to the extent the user has permitted.</p><p>This process ensures the safety of user information. Instead of accessing all of a user’s data, the 3rd party can access user data within the permissions granted.</p><h4>Authorities</h4><p>Authorities represent the actions (that one has permission for) a user can perform within an application. Compared to scopes, they are usually more detailed and specify which actions can be carried out within a specific application.</p><p>For example, a user can add a movie to their favorites (e.g., “<em>user</em>”). The permissions to perform this action are called authorities.</p><h4>How does the default process work?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*LUK4lZXbUqhmPrJ-x0h16g.png" /></figure><p>As we see here, Spring directs the incoming request to the right Authentication Provider (JwtAuthenticationProvider) through the AuthenticationManager. This provider is in charge of decoding and verifying the JWT access token. The provider then uses JwtAuthenticationConverter to turn the raw JWT into an AbstractAuthenticationToken. By default, it uses JwtGrantedAuthoritiesConverter at this step. The job of JwtGrantedAuthoritiesConverter is to change the incoming JWT into granted authorities.</p><p>By default, JwtGrantedAuthoritiesConverter splits the claim based on the “<em>scope</em>” or “<em>scp</em>” tags and turns it into a list of strings. After that, for each value, it adds the ‘<em>SCOPE_</em>’ prefix and creates a SimpleGrantedAuthority.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t4MUe6tIJZulH4wHntchUA.png" /><figcaption>By default, it looks at the ‘scope’ or ‘scp’ claims and uses the ‘SCOPE_’ prefix.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KyDO0pRklLWteeRlQMELaQ.png" /><figcaption>When the JWT is examined, it’s seen that our user’s roles claim includes the ‘USER’ role.</figcaption></figure><h4>So, what should be the solution?</h4><p>As I mentioned earlier, the JwtGrantedAuthoritiesConverter converts JWT into granted authorities. Writing a custom <em>JwtGrantedAuthoritiesConverter</em> will solve our problem.</p><p>When writing a custom converter, the part we need to pay attention to is ensuring it splits based on the roles in the ‘<em>roles</em>’ claim and fixing the prefix part.</p><pre>    private final class CustomJwtGrantedAuthoritiesConverter implements Converter&lt;Jwt, Collection&lt;GrantedAuthority&gt;&gt; {<br><br>        @Override<br>        public Collection&lt;GrantedAuthority&gt; convert(Jwt jwt) {<br>            var realmAccess = (Map&lt;String, List&lt;String&gt;&gt;) jwt.getClaim(&quot;realm_access&quot;);<br><br>            return realmAccess.get(&quot;roles&quot;).stream()<br>                    .map(role -&gt; new SimpleGrantedAuthority(&quot;ROLE_&quot; + role))<br>                    .collect(Collectors.toList());<br>        }<br><br>        @Override<br>        public &lt;U&gt; Converter&lt;Jwt, U&gt; andThen(Converter&lt;? super Collection&lt;GrantedAuthority&gt;, ? extends U&gt; after) {<br>            return Converter.super.andThen(after);<br>        }<br>    }</pre><p>Then, I override the JwtAuthenticationConverter bean and set the CustomJwtGrantedAuthoritiesConverter.</p><pre>@Bean<br>public JwtAuthenticationConverter customJwtAuthenticationConverter() {<br>    JwtAuthenticationConverter converter = new JwtAuthenticationConverter();<br>    converter.setJwtGrantedAuthoritiesConverter(new CustomJwtGrantedAuthoritiesConverter());<br>    return converter;<br>}</pre><p>After that, we give this JwtAuthenticationConverter to the SecurityFilterChain.</p><pre>    @Bean<br>    protected SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {<br><br>        return http<br>                .csrf(AbstractHttpConfigurer::disable)<br>                .authorizeHttpRequests(requests -&gt;<br>                        requests<br>                                .anyRequest().authenticated()<br>                )<br>                .oauth2ResourceServer(oauth2 -&gt;<br>                        oauth2.jwt(jwt -&gt;<br>                                jwt.jwtAuthenticationConverter(customJwtAuthenticationConverter())<br>                        )<br>                )<br>                .build();<br>    }</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OY7upUrzZjH_NEskVEzGbg.png" /></figure><p>When we check from the SecurityContext, we see that our role has been added.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34247f3ff78e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>