Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding Java SDK client example and scripts to run it on openshift #46

Merged
merged 1 commit into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions demos/java-api-client/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:8-jre-alpine
ADD target/ConjurJavaClient-1.0-SNAPSHOT-with-dependencies.jar ConjurJavaClient-1.0-SNAPSHOT-with-dependencies.jar
ENTRYPOINT ["java", "-jar", "ConjurJavaClient-1.0-SNAPSHOT-with-dependencies.jar"]

8 changes: 8 additions & 0 deletions demos/java-api-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Instructions for building java client
-------------------------------------

For compiling java test application please run: ./build.sh
Java SDK API fat jar will appear in the directory and docker image of the client application will be available
In addition, JAVA API SDK fat jar will be installed in local maven repository and will be usable for other applications
For Running this application on openshift please look for instructions in ../openshift-install/README.txt

76 changes: 76 additions & 0 deletions demos/java-api-client/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/bin/bash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding an -x flag? Scripts like this should exit if there are any issues during the run to make it obvious there is a problem.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should not stop on any error. For example if we have some error in prune images - it does not have to be a problem. Thus I have added exit conditions related to specific commands

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have commands that you don't mind failing, I think you should prevent an error message from appearing or add a message that explains that it's ok. Otherwise, the user won't have high confidence that the run was successful. With oc commands, you can add the flag --ignore-not-found to prevent these errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


set -e
#set -x

function validate_app {
APPNAME=$1
CHECK_APP=$( which $APPNAME )
if [ -z "$CHECK_APP" ]
then
echo "Please install $APPNAME"
exit 1
fi
}

validate_app git
validate_app mvn
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we use the Maven container to compile: https://hub.docker.com/_/maven. This reduces the dependencies a user needs on their machine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it mean to run script from docker ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have thought about it a little bit more - it is a very good idea - but if I will do it it will mean that after running my script in docker the JAVA API SDK will be installed only in maven repository inside that docker container, and thus will not be usable for another developers. The intention was to solve the problem JFC was facing with - and it means to have Java API SDK be installed in local maven repo after running the script.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is intended to be demo tool, not a build tool. Could we move this build specific functionality into the cyberark/conjur-api-java project instead?

The goal for this project is to minimize dependencies required by the user, hence the push to use Docker images. It's not intended to replicate a development environment. Each project should enable development within it's scope.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

conjur-java-api build specific functionality should be added to conjur-java-api by US Team (please inform me if it your team going to do it) - and this build should be a part of pipeline and put conjur-java-api FAT JAR to global maven repository.
As soon as it will be done I will remove conjur-java-api building part from my build script - and I will leave there only build of Java Conjur API client because it is an example for Demos

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding adding docker to the project: I was told that there is explicit requirement from customers not to add docker because many of them are not going to use docker in their developments and thus example using docker is less relevant to them

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once again. This is not a developer project. This is a project for to demonstrate scenarios. Development tools should be part of the original project (cyberark/conjur-api-java in this case).

validate_app docker

COMMAND=$0
echo "$COMMAND"
suffix="/build.sh";
HOME_DIR=${COMMAND%$suffix};
pushd $HOME_DIR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd never heard of pushd. Neat!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks


rm -rf target
rm -rf conjur-api-java

echo "Cloning Conjur Java SDK repository from Github"

git clone https://github.com/cyberark/conjur-api-java.git

if [ ! -d "./conjur-api-java" ]
then
echo "Git clone failed"
exit 1
fi

BRANCH_NAME=$( git rev-parse --abbrev-ref HEAD )

git checkout $BRANCH_NAME

pushd conjur-api-java

echo "Building Conjur Java SDK JAR"

mvn install -DskipTests -Dmaven.javadoc.skip=true

popd

API_JAR_NAME=$( ls conjur-api-java/target/*with-dependencies.jar | grep conjur-api )
echo "API_JAR_NAME=$API_JAR_NAME"
if [ -z $API_JAR_NAME ]
then
echo "Maven install Conjur Java SDK jar failed"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Maven install Conjur Java SDK jar failed"
echo "Maven install of Conjur Java SDK JAR failed"

exit 1
fi

VERSION=$( echo "$API_JAR_NAME"| cut -d'/' -f 3 | cut -d'-' -f 3 )

echo "Installing Conjur Java SDK JAR to Maven Repo"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "Installing Conjur Java SDK JAR to Maven Repo"
echo "Installing Conjur Java SDK JAR into the Maven repository"


mvn install:install-file -Dfile=conjur-api-java/target/conjur-api-$VERSION-with-dependencies.jar -DgroupId=net.conjur.api -DartifactId=conjur-api -Dversion=$VERSION -Dpackaging=jar

echo "Build Conjur Java Client Example"
mvn install -Dconjur-api-version=2.1.0

cp conjur-api-java/target/conjur-api-2.1.0-with-dependencies.jar .

rm -rf conjur-api-java

echo "Creating docker image of Conjur Java Client Example"
docker build -f Dockerfile -t conjur-java-client .

docker images | grep conjur-java-client

69 changes: 69 additions & 0 deletions demos/java-api-client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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>
<properties>
<conjur-api-version>2.1.0</conjur-api-version>
</properties>
<groupId>com.cyberark.example</groupId>
<artifactId>ConjurJavaClient</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.conjur.api</groupId>
<artifactId>conjur-api</artifactId>
<version>${conjur-api-version}</version>
</dependency>
</dependencies>
<build><plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<systemPropertyVariables>
<CONJUR_ACCOUNT>${env.CONJUR_ACCOUNT}</CONJUR_ACCOUNT>
<CONJUR_APPLIANCE_URL>${env.CONJUR_APPLIANCE_URL}</CONJUR_APPLIANCE_URL>
<CONJUR_AUTHN_LOGIN>${env.CONJUR_AUTHN_LOGIN}</CONJUR_AUTHN_LOGIN>
</systemPropertyVariables>
</configuration>
</plugin>
<!-- Plugin to create jar with dependencies -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedClassifierName>with-dependencies</shadedClassifierName>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.cyberark.example.JavaClient</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/cxf/bus-extensions.txt</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>

</plugins></build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.cyberark.example;

import net.conjur.api.Conjur;
import net.conjur.api.Token;

import java.io.FileOutputStream;
import java.io.PrintWriter;

import java.io.File;
import java.nio.file.Paths;

public class JavaClient {

private static String truststoreFileName = "/run/conjur/truststore.jks";

private static void initialize()
{
System.setProperty("javax.net.ssl.trustStore", truststoreFileName);
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
System.setProperty("CONJUR_ACCOUNT", System.getenv("CONJUR_ACCOUNT"));
if (System.getenv("CONJUR_AUTHN_LOGIN") != null)
{
System.setProperty("CONJUR_AUTHN_LOGIN", System.getenv("CONJUR_AUTHN_LOGIN"));
}
System.setProperty("CONJUR_APPLIANCE_URL", System.getenv("CONJUR_APPLIANCE_URL"));
}

private static void enterPending()
{
try
{
Thread.sleep(500000);
}
catch (Exception e)
{
System.out.println("Timer Exception:" + e);
}
}

public static void main(String args[])
{
System.out.println("Running Conjur Java SDK Example");
initialize();
Token token = null;
try
{
System.out.println("CONJUR_AUTHN_TOKEN_FILE = " + System.getenv("CONJUR_AUTHN_TOKEN_FILE"));
token = Token.fromFile(Paths.get(System.getenv("CONJUR_AUTHN_TOKEN_FILE")));
}
catch (Exception e)
{
System.out.println("Exception:" + e);
return;
}
System.out.println("Create Conjur API Instance");
Conjur conjur = new Conjur(token);
String secret = conjur.variables().retrieveSecret("variables/mypassword");
System.out.println("Retrieved secret = " + secret);
enterPending();
}

}
36 changes: 36 additions & 0 deletions demos/openshift-install/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Explanations:
-------------
The purpose of this demo is to install Conjur on existing OpenShift environment and then run Java Client on top of it
The environent contains 4 pods each with up to 2 containers inside
Pod #1: Postgres
Pod #2: Conjur + Nginx
Pod #3: Conjur CLI
Pod #4: Conjur authenticator client + Java Client

Local Prerequisites:
--------------------
Git - git version 2.24.1
Maven - Apache Maven 3.6.3
Java SDK / JRE - openjdk version "1.8.0_232"
MAC OS Catalina - Version 10.15.1 (19076)
OpenShift client installed on MAC

External Prerequisites:
-----------------------
A GitHub user for GitHub environment
OpenShift - oc v3.11.0+0cbc58b
kubernetes v1.11.0+d4cacc0
features: Basic-Auth

Commands:
---------
1. **Building Java Client:** cd <home-dir>/conjur-intro/demos/java-api-client
./build.sh
2. **Installing Conjur and Conjur-CLI on OpenShift:** <home-dir>/conjur-intro/demos/openshift-install
./installer.sh --with-config --ocp-url <ocp-url>:<port> --project-name <project-name> --account-name <account-name> --authenticator <authenticator>
3. **Verify that all pods are up and running by:** oc get pods
4. **Installing and running java client opn Open Shift:** ./java-client-installer.sh --ocp-url <ocp-url>:<port> --docker-url <docker-url> --project-name <project-name> --account-name <account-name> --authenticator <authenticator>
5. **Verify that all pods are up and running by:** oc get pods
6. **Checking output of Java client container on pod #4:** oc logs <pod-name> -c my-conjur-java-client
It should show that secret was retrieved properly

18 changes: 18 additions & 0 deletions demos/openshift-install/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

#set -x
#set -e

function validate_app {
APPNAME=$1
CHECK_APP=$( which $APPNAME )
if [ -z "$CHECK_APP" ]
then
echo "Please install $APPNAME"
exit 1
fi
}

validate_app oc

oc delete project $1
27 changes: 27 additions & 0 deletions demos/openshift-install/conjur-cli.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: conjur-cli
labels:
app: conjur-cli
spec:
replicas: 1
selector:
matchLabels:
app: conjur-cli
template:
metadata:
name: conjur-cli
labels:
app: conjur-cli
spec:
serviceAccountName: default
containers:
- name: conjur-cli
image: cyberark/conjur-cli:5-latest
imagePullPolicy: IfNotPresent
command: ["sleep"]
args: ["infinity"]
imagePullSecrets:
- name: dockerpullsecret
31 changes: 31 additions & 0 deletions demos/openshift-install/conjur_scripts/cert_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -e
AUTHENTICATOR_ID=$2
CONJUR_ACCOUNT=$1

# Generate OpenSSL private key
openssl genrsa -out ca.key 2048

CONFIG="
[ req ]
distinguished_name = dn
x509_extensions = v3_ca
[ dn ]
[ v3_ca ]
basicConstraints = critical,CA:TRUE
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid:always,issuer:always
"

# Generate root CA certificate
openssl req -x509 -new -nodes -key ca.key -sha1 -days 3650 -set_serial 0x0 -out ca.cert \
-subj "/CN=conjur.authn-k8s.$AUTHENTICATOR_ID/OU=Conjur Kubernetes CA/O=$CONJUR_ACCOUNT" \
-config <(echo "$CONFIG")

# Verify cert
openssl x509 -in ca.cert -text -noout &> /dev/null

# Load variable values
conjur variable values add conjur/authn-k8s/$AUTHENTICATOR_ID/ca/key "$(cat ca.key)"
conjur variable values add conjur/authn-k8s/$AUTHENTICATOR_ID/ca/cert "$(cat ca.cert)"

16 changes: 16 additions & 0 deletions demos/openshift-install/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: '2'
services:

database:
image: postgres:9.4
container_name: postgres_database

conjur:
image: cyberark/conjur
container_name: conjur_server
command: server
environment:
DATABASE_URL: postgres://postgres@database/postgres
CONJUR_DATA_KEY:
depends_on: [ database ]

Loading