And Todays’s Quote Would Be..

Life begins at the end of your comfort zone.

Neale Donald Walsch

How to Connect and Uploading Files to Openstack using Java

Currently im involved in a project where i have to use OpenStack Object Store for storing images and files. After Googling for a while, i come up with a solution of using JOSS library to connect to OpenStack, despite OpenStack provide REST API to connect to it directly.

But first, we need to see the API URL that is needed for login to OpenStack. And it can be seen on below screenshot, inside the red box.

After we got the url, next is importing JOSS library to be used in our app. We can import it by using a simple maven command,

<dependency>
	<groupId>org.javaswift</groupId>
	<artifactId>joss</artifactId>
	<version>0.10.2</version>
</dependency>

And a simple java file, to do a simple upload using a provided credential.

@Test
public void uploadImageToDCTest() {
	AccountConfig config = new AccountConfig();
	config.setAuthenticationMethod(AuthenticationMethod.KEYSTONE_V3);
	config.setUsername("user");
	config.setPassword("password");
	config.setAuthUrl("https://osphdc.server01.com:13000/v3/auth/tokens");
	config.setTenantName("default");

	Account account = new AccountFactory(config).createAccount();

	Container container = account.getContainer("images");
	if(!container.exists()) {
		container.create();
		container.makePublic();
	}

	StoredObject object = container.getObject("jenkins-logo.png");
	object.uploadObject(new File("D:\\jenkins-logo.png"));
	System.out.println("Public URL: "+object.getPublicURL());
}

Later on, we can check the result on our OpenStack object store page.

Google+

[Keycloak] How to Solve “/openid-connect/userinfo” which Gives JWT Response instead of A Simple JSON

Previously expecting a json response from “/openid-connect/userinfo” API , such as below screenshot,

But suddenly someone change configuration somewhere which makes previous response changed into,

Took quite sometime for me to findout which part of Kecloak configuration which makes this happens. Finally i found the proper configuration, it is located on “Fine Grain OpenID Connect Configuration”. Previously it is “RS512”, changing into “unsigned” solve my problem.

Google+

Run as a Root User on Openshift

Sometimes my docker images got permission issue when deployed to Openshift, due to Openshift gives a random userid as enforced by its default security policy. In order to “bypass” those constrain and run my image as root, i run below command,

oc adm policy add-scc-to-user anyuid -z default -n project-name
Google+

Deploying A Simple Hello World App using OpenLiberty S2I to Openshift

For this example im using OpenLiberty version 19.0.0.6, and install corresponding image to my Openshift registry using below command,

oc import-image openliberty/open-liberty-s2i:19.0.0.6

Can check our list of images on our imagestream by using this command,

oc get is

Next is creating a simple hello-world webapps, with below pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>hello-world-servlet</groupId>
    <artifactId>com.edw</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

And web.xml,

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>My Web Application</display-name>

    <servlet>
        <servlet-name>helloServlet</servlet-name>
        <servlet-class>com.edw.MyServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>helloServlet</servlet-name>
        <url-pattern>/hello.servlet</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/hello.jsp</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>

A simple JSP file,

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>

And a simple java file,

package com.edw;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet  extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.getWriter().println("Hello");
    }
}

And a simple server.xml file,

<?xml version="1.0" encoding="UTF-8"?>
<server description="OpenLiberty Server">
    <httpEndpoint id="defaultHttpEndpoint" host="*" httpPort="9080" httpsPort="9443"/>
    <webApplication location="com.edw-1.0-SNAPSHOT.war"/>
</server>

After project is properly setup, we can do a simple mvn build,

mvn clean package

And push our application to Openshift, run below command on the root of your project location

oc new-build --name=my-openliberty-full --image-stream=open-liberty-s2i:19.0.0.6 --binary=true

oc start-build my-openliberty-full --from-dir=.

oc new-app my-openliberty-full --name=my-openliberty-full

We can access our newly created app directly thru browser,

Google+

Create JWT Token based on Public – Private Key Certificate On Spring Security

Previously im always creating JWT token using a simple 20digit random character string, despite it works well but somehow it feels like there is something wrong. It feels like it is less secure.
Therefore, after spending some time researching, im changing below code where im using a 20 digits random String character


	String signingKey = "V5iwaQYUyqyi71ij3OtP";
	
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(signingKey);
        return converter;
    }

To signing and veryfing using a simple public and private key, first we need to create a simple public key file, public.txt

-----BEGIN PUBLIC KEY-----
MIICITANBgkqhkiG9w0BAQEFAAOCAg4AMIICCQKCAgBoDqqgVQd0VX0RFBxquFPI
7o0WabMhCV+BplU3MX+RVq8djCVvkb0FwZSuqZT8d3mvxLtEdHxHXI2vRL9qhOvD
pBgyXw6z+IrzSwYM1LrRj+vM61jCD9lhD5y5kbvzonNbTEGNX2//oJHKC0uwCXWP
ZNx6WdQkWrCD3FDtQyuDkPuGWeuXMqBwuLLrhMg1c4B3CF2DUyVxRLT21WfguRKE
8gkNBI6f9PsG/DAFyTbXJNRHjyqam4Uanc/kuiw/kUUCCdiNC7jUD64pPixJy0pC
8gXKH/iZZ4omSygFBCDHkcTJvCYdVOz36Smzzus8V38i2vx0gP5ri+eYgVdCsPG8
WkaL/8QwUB0zJIM6o7ELb4OyDDX8M99nOTovAsgSMvMu2dCAjsPAwwaJKb6Rnn84
zu3xWuDZNOTox/l4+YqV91p3+nvsizPbme3qiYNM6GJJX6mGUXCX7yaC7t4UEcQS
oLBvo/zXa1SOHrIMZVFhbT9PPpspBXC3YX+zS5/4XR1+m3PWvGiXBOpD47r1+Oz1
MWpZXqq5TT53AsbXOhGIGO54TLKq/m7lPJd1Y8KeKMdR5j+a1gmPy6kv68YBHPOx
npvlrpEvNFujNeBftfNrBoeHtiYCUKZjSTHLquCbuq2/w3FF8kRQNUc04btysTJ8
LdulnXqPagx9egAGjjG4QwIDAQAB
-----END PUBLIC KEY-----

and private key, private.txt. And put both of them under resources folder so accessible by our app

-----BEGIN RSA PRIVATE KEY-----
MIIJKAIBAAKCAgBoDqqgVQd0VX0RFBxquFPI7o0WabMhCV+BplU3MX+RVq8djCVv
kb0FwZSuqZT8d3mvxLtEdHxHXI2vRL9qhOvDpBgyXw6z+IrzSwYM1LrRj+vM61jC
D9lhD5y5kbvzonNbTEGNX2//oJHKC0uwCXWPZNx6WdQkWrCD3FDtQyuDkPuGWeuX
MqBwuLLrhMg1c4B3CF2DUyVxRLT21WfguRKE8gkNBI6f9PsG/DAFyTbXJNRHjyqa
m4Uanc/kuiw/kUUCCdiNC7jUD64pPixJy0pC8gXKH/iZZ4omSygFBCDHkcTJvCYd
VOz36Smzzus8V38i2vx0gP5ri+eYgVdCsPG8WkaL/8QwUB0zJIM6o7ELb4OyDDX8
M99nOTovAsgSMvMu2dCAjsPAwwaJKb6Rnn84zu3xWuDZNOTox/l4+YqV91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nt1n5+kpdQlzsEqjMbRIHeloZ1DHnR2solD5XL5/IBQCdu
fTI6nWbFlTBUolW/8UF0Mf2crA6aHmNLXkYRWvATkmqUrsJq52yCjM0wxaZDtNot
3njNLWA+DEjhA1oEnfKIgk76MEhhGyRuEwdxtCVEga/bTGmT1Wti9If0EDHERxL8
Fao3UBFaT3RW31UZZaWFajW27dfLyLsCggEBAKJ38cPxdEL1nwqmcrNUqUDjMrc6
9Qai/L5RkpkD6VALzfF4p9OsHF1z8Ye1rVx+HyTYvWo7crDdb8e5CyamyhsciJSr
U9rjd6TyEOCrNER81zsBMX4wtl7SRls/kkSAI5jvLXCvCllQP3Zgw0dDcu6SBmUU
zzUmy3UTS2k+RHs2sx/krXSZSQsvqxv2HzbTvVXPTDJ2p5B6kdATGSaVu7G2jUVR
q6O9MJKICSVO1pL6B7EsQHCpVizjK6gtrLhfUDc2UXObM0g47ghUFiEazvCKJJiP
Ld9/Nf1pP0BU2Ci10ph2oCJzIXAKiSvQw8KTCvDWYn8axmC/2hpLDyBMSRU=
-----END RSA PRIVATE KEY-----

And change our code to below,

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        Resource resource = new ClassPathResource("public.txt");
        Resource privateResource = new ClassPathResource("private.txt");
        String publicKey = null;
        String privateKey = null;
        try {
            publicKey = IOUtils.toString(resource.getInputStream());
            privateKey = IOUtils.toString(privateResource.getInputStream());
        } catch (final IOException e) {
            throw new RuntimeException(e);
        }
        converter.setVerifierKey(publicKey);
        converter.setSigningKey(privateKey);
        return converter;
    }

The downside of this new signing is it almost doubled the size of my JWT token, but hopefully makes it more secure.

Google+