Skip to content

Commit

Permalink
PRC-430: Evolve template to use as basis for new API and trial deploy…
Browse files Browse the repository at this point in the history
…ment (#1)

* PRC-430: Evolve template to use as basis for new API and trial deployment

* PRC-430: Adding PGSQL test containers

* PRC-430: Tidying small copy errors

* PRC-430: Updated for client creds secret name - to match expectation
  • Loading branch information
timharrison-moj authored Feb 3, 2025
1 parent 7d737b8 commit d7a7296
Show file tree
Hide file tree
Showing 27 changed files with 319 additions and 580 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
.kotlin/
build/

.env

# CMake
cmake-build-debug/

Expand Down
3 changes: 3 additions & 0 deletions .trivyignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
# Suppression for h2 2.1.214 password on command line vulnerability
# can be suppressed as we only run h2 locally and not on build environments
CVE-2022-45868
# Suppression for tomcat vulnerability affecting jsp compilation in the default servlet
# can be suppressed as we do not use the default servlet and haven't configured it for write either
CVE-2024-50379
12 changes: 10 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM --platform=$BUILDPLATFORM eclipse-temurin:21-jdk-jammy AS builder
FROM --platform=$BUILDPLATFORM eclipse-temurin:21-jre-jammy AS builder

ARG BUILD_NUMBER
ENV BUILD_NUMBER ${BUILD_NUMBER:-1_0_0}
Expand All @@ -7,6 +7,10 @@ WORKDIR /app
ADD . .
RUN ./gradlew --no-daemon assemble

# Grab AWS RDS Root cert
RUN apt-get update && apt-get install -y curl
RUN curl https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem > root.crt

FROM eclipse-temurin:21-jre-jammy
LABEL maintainer="HMPPS Digital Studio <info@digital.justice.gov.uk>"

Expand All @@ -23,6 +27,10 @@ RUN ln -snf "/usr/share/zoneinfo/$TZ" /etc/localtime && echo "$TZ" > /etc/timezo
RUN addgroup --gid 2000 --system appgroup && \
adduser --uid 2000 --system appuser --gid 2000

# Install AWS RDS Root cert into Java truststore
RUN mkdir /home/appuser/.postgresql
COPY --from=builder --chown=appuser:appgroup /app/root.crt /home/appuser/.postgresql/root.crt

WORKDIR /app
COPY --from=builder --chown=appuser:appgroup /app/build/libs/hmpps-organisations-api*.jar /app/app.jar
COPY --from=builder --chown=appuser:appgroup /app/build/libs/applicationinsights-agent*.jar /app/agent.jar
Expand All @@ -31,4 +39,4 @@ COPY --from=builder --chown=appuser:appgroup /app/applicationinsights.dev.json /

USER 2000

ENTRYPOINT ["java", "-XX:+AlwaysActAsServerClassMachine", "-javaagent:/app/agent.jar", "-jar", "/app/app.jar"]
ENTRYPOINT ["java", "-XX:+AlwaysActAsServerClassMachine", "-javaagent:/app/agent.jar", "-jar", "/app/app.jar"]
130 changes: 13 additions & 117 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,133 +4,29 @@
[![Docker Repository on ghcr](https://img.shields.io/badge/ghcr.io-repository-2496ED.svg?logo=docker)](https://ghcr.io/ministryofjustice/hmpps-organisations-api)
[![API docs](https://img.shields.io/badge/API_docs_-view-85EA2D.svg?logo=swagger)](https://hmpps-organisations-api-dev.hmpps.service.justice.gov.uk/webjars/swagger-ui/index.html?configUrl=/v3/api-docs)

Template github repo used for new Kotlin based projects.
API to support the creation, management and sharing of data related to organisations external to HMPPS.

# Instructions

If this is a HMPPS project then the project will be created as part of bootstrapping -
see [dps-project-bootstrap](https://github.com/ministryofjustice/dps-project-bootstrap). You are able to specify a
template application using the `github_template_repo` attribute to clone without the need to manually do this yourself
within GitHub.
# Building the project

This project is community managed by the mojdt `#kotlin-dev` slack channel.
Please raise any questions or queries there. Contributions welcome!

Our security policy is located [here](https://github.com/ministryofjustice/hmpps-organisations-api/security/policy).

Documentation to create new service is located [here](https://tech-docs.hmpps.service.justice.gov.uk/applicationplatform/newservice-GHA/).

## Creating a Cloud Platform namespace

When deploying to a new namespace, you may wish to use the
[templates project namespace](https://github.com/ministryofjustice/cloud-platform-environments/tree/main/namespaces/live.cloud-platform.service.justice.gov.uk/hmpps-templates-dev)
as the basis for your new namespace. This namespace contains both the kotlin and typescript template projects,
which is the usual way that projects are setup.

Copy this folder and update all the existing namespace references to correspond to the environment to which you're deploying.

If you only need the kotlin configuration then remove all typescript references and remove the elasticache configuration.

To ensure the correct github teams can approve releases, you will need to make changes to the configuration in `resources/service-account-github` where the appropriate team names will need to be added (based on [lines 98-100](https://github.com/ministryofjustice/cloud-platform-environments/blob/main/namespaces/live.cloud-platform.service.justice.gov.uk/hmpps-templates-dev/resources/serviceaccount-github.tf#L98) and the reference appended to the teams list below [line 112](https://github.com/ministryofjustice/cloud-platform-environments/blob/main/namespaces/live.cloud-platform.service.justice.gov.uk/hmpps-templates-dev/resources/serviceaccount-github.tf#L112)). Note: hmpps-sre is in this list to assist with deployment issues.

Submit a PR to the Cloud Platform team in
#ask-cloud-platform. Further instructions from the Cloud Platform team can be found in
the [Cloud Platform User Guide](https://user-guide.cloud-platform.service.justice.gov.uk/#cloud-platform-user-guide)

## Renaming from HMPPS Organisations Api - github Actions

Once the new repository is deployed. Navigate to the repository in github, and select the `Actions` tab.
Click the link to `Enable Actions on this repository`.

Find the Action workflow named: `rename-project-create-pr` and click `Run workflow`. This workflow will
execute the `rename-project.bash` and create Pull Request for you to review. Review the PR and merge.

Note: ideally this workflow would run automatically however due to a recent change github Actions are not
enabled by default on newly created repos. There is no way to enable Actions other then to click the button in the UI.
If this situation changes we will update this project so that the workflow is triggered during the bootstrap project.
Further reading: <https://github.community/t/workflow-isnt-enabled-in-repos-generated-from-template/136421>

The script takes six arguments:

### New project name

This should start with `hmpps-` e.g. `hmpps-prison-visits` so that it can be easily distinguished in github from
other departments projects. Try to avoid using abbreviations so that others can understand easily what your project is.

### Slack channel for release notifications

By default, release notifications are only enabled for production. The circleci configuration can be amended to send
release notifications for deployments to other environments if required. Note that if the configuration is amended,
the slack channel should then be amended to your own team's channel as `dps-releases` is strictly for production release
notifications. If the slack channel is set to something other than `dps-releases`, production release notifications
will still automatically go to `dps-releases` as well. This is configured by `releases-slack-channel` in
`.circleci/config.yml`.

### Slack channel for pipeline security notifications

Ths channel should be specific to your team and is for daily / weekly security scanning job results. It is your team's
responsibility to keep up-to-date with security issues and update your application so that these jobs pass. You will
only be notified if the jobs fail. The scan results can always be found in circleci for your project. This is
configured by `alerts-slack-channel` in `.circleci/config.yml`.

### Non production kubernetes alerts

By default Prometheus alerts are created in the application namespaces to monitor your application e.g. if your
application is crash looping, there are a significant number of errors from the ingress. Since Prometheus runs in
cloud platform AlertManager needs to be setup first with your channel. Please see
[Create your own custom alerts](https://user-guide.cloud-platform.service.justice.gov.uk/documentation/monitoring-an-app/how-to-create-alarms.html)
in the Cloud Platform user guide. Once that is setup then the `custom severity label` can be used for
`alertSeverity` in the `helm_deploy/values-*.yaml` configuration.

Normally it is worth setting up two separate labels and therefore two separate slack channels - one for your production
alerts and one for your non-production alerts. Using the same channel can mean that production alerts are sometimes
lost within non-production issues.

### Production kubernetes alerts

This is the severity label for production, determined by the `custom severity label`. See the above
#non-production-kubernetes-alerts for more information. This is configured in `helm_deploy/values-prod.yaml`.

### Product ID

This is so that we can link a component to a product and thus provide team and product information in the Developer
Portal. Refer to the developer portal at https://developer-portal.hmpps.service.justice.gov.uk/products to find your
product id. This is configured in `helm_deploy/<project_name>/values.yaml`.

## Manually branding from template app

Run the `rename-project.bash` without any arguments. This will prompt for the six required parameters and create a PR.
The script requires a recent version of `bash` to be installed, as well as GNU `sed` in the path.

## TODOs and Examples

We have tried to provide some examples of best practice in the application - so there are lots of TODOs in the code
where changes are required to meet your requirements. There is an `ExampleResource` that includes best practice and also
serve as spring security examples. The template typescript project has a demonstration that calls this endpoint as well.

For the demonstration, rather than introducing a dependency on a different service, this application calls out to
itself. This is only to show a service calling out to another service and is certainly not recommended!
```bash
./gradlew
./gradlew clean test
```

## Running the application locally

The application comes with a `dev` spring profile that includes default settings for running locally. This is not
necessary when deploying to kubernetes as these values are included in the helm configuration templates -
e.g. `values-dev.yaml`.

There is also a `docker-compose.yml` that can be used to run a local instance of the template in docker and also an
instance of HMPPS Auth (required if your service calls out to other services using a token).

```bash
docker compose pull && docker compose up
./run-local.sh
```

will build the application and run it and HMPPS Auth within a local docker instance.
The application comes with a `dev` spring profile that includes default settings for running locally. This is not
necessary when deploying to kubernetes as these values are included in the helm configuration templates -
e.g. `values-dev.yaml`.

### Running the application in Intellij
Most services which run locally will rely upon the DEV environment for dependant APIs, but will need a database
running locally. This can be started with:

```bash
docker compose pull && docker compose up --scale hmpps-organisations-api=0
docker compose pull && docker compose up -d
```

will just start a docker instance of HMPPS Auth. The application should then be started with a `dev` active profile
in Intellij.
44 changes: 40 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,55 @@
plugins {
id("uk.gov.justice.hmpps.gradle-spring-boot") version "6.1.0"
id("uk.gov.justice.hmpps.gradle-spring-boot") version "6.1.2"
id("org.openapi.generator") version "7.10.0"
kotlin("plugin.spring") version "2.0.21"
kotlin("plugin.jpa") version "2.0.21"
}

allOpen {
annotations(
"javax.persistence.Entity",
"javax.persistence.MappedSuperclass",
"javax.persistence.Embeddable",
)
}

configurations {
testImplementation { exclude(group = "org.junit.vintage") }
}

dependencies {
implementation("uk.gov.justice.service.hmpps:hmpps-kotlin-spring-boot-starter:1.1.0")
// Spring boot dependencies
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.7.0")
implementation("uk.gov.justice.service.hmpps:hmpps-kotlin-spring-boot-starter:1.1.1")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("uk.gov.justice.service.hmpps:hmpps-sqs-spring-boot-starter:5.2.2")

testImplementation("uk.gov.justice.service.hmpps:hmpps-kotlin-spring-boot-starter-test:1.1.0")
implementation("org.springframework.boot:spring-boot-starter-validation")

// Database dependencies
runtimeOnly("org.flywaydb:flyway-database-postgresql")
runtimeOnly("org.postgresql:postgresql:42.7.5")

// OpenAPI
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.8.4")
implementation("org.openapitools:jackson-databind-nullable:0.2.6")

implementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations:2.12.0")

// Test dependencies
testImplementation("io.jsonwebtoken:jjwt-impl:0.12.6")
testImplementation("io.jsonwebtoken:jjwt-jackson:0.12.6")
testImplementation("net.javacrumbs.json-unit:json-unit:4.1.0")
testImplementation("net.javacrumbs.json-unit:json-unit-assertj:4.1.0")
testImplementation("net.javacrumbs.json-unit:json-unit-json-path:4.1.0")
testImplementation("org.awaitility:awaitility-kotlin:4.2.2")
testImplementation("org.mockito:mockito-inline:5.2.0")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.security:spring-security-test")
testImplementation("org.testcontainers:postgresql:1.20.4")
testImplementation("org.wiremock:wiremock-standalone:3.9.2")
testImplementation("uk.gov.justice.service.hmpps:hmpps-kotlin-spring-boot-starter-test:1.1.0")
testImplementation("io.swagger.parser.v3:swagger-parser:2.1.24") {
exclude(group = "io.swagger.core.v3")
}
Expand Down
36 changes: 9 additions & 27 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
version: "3"
services:
hmpps-organisations-api:
build:
context: .
db:
image: postgres:latest
networks:
- hmpps
container_name: hmpps-organisations-api
- hmpps
container_name: organisations-db
restart: always
ports:
- "8080:8080"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/health/ping"]
- "5432:5432"
environment:
- SERVER_PORT=8080
- HMPPS_AUTH_URL=http://hmpps-auth:8080/auth
# TODO: Remove this URL and replace with outgoing service URLs
- EXAMPLE_URL=http://hmpps-organisations-api:8080
- SPRING_PROFILES_ACTIVE=dev

hmpps-auth:
image: quay.io/hmpps/hmpps-auth:latest
networks:
- hmpps
container_name: hmpps-auth
ports:
- "8090:8080"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8080/auth/health"]
environment:
- SERVER_PORT=8080
- SPRING_PROFILES_ACTIVE=dev
- APPLICATION_AUTHENTICATION_UI_ALLOWLIST=0.0.0.0/0
POSTGRES_DB: organisations-db
POSTGRES_USER: organisations
POSTGRES_PASSWORD: organisations

networks:
hmpps:
16 changes: 13 additions & 3 deletions helm_deploy/hmpps-organisations-api/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ generic-service:
host: app-hostname.local # override per environment
tlsSecretName: hmpps-organisations-api-cert

# Used to access resources like SQS queues and SNS topics
serviceAccountName: hmpps-organisations-api

# Environment variables to load into the deployment
env:
JAVA_OPTS: "-Xmx512m"
SERVER_PORT: "8080"
APPLICATIONINSIGHTS_CONFIGURATION_FILE: applicationinsights.json
DB_SSL_MODE: "verify-full"

# Pre-existing kubernetes secrets to load as environment variables in the deployment.
# namespace_secrets:
Expand All @@ -27,11 +31,17 @@ generic-service:

namespace_secrets:
hmpps-organisations-api-client-creds:
# Example client registration secrets
EXAMPLE_API_CLIENT_ID: "API_CLIENT_ID"
EXAMPLE_API_CLIENT_SECRET: "API_CLIENT_SECRET"
SYSTEM_CLIENT_ID : "SYSTEM_CLIENT_ID"
SYSTEM_CLIENT_SECRET: "SYSTEM_CLIENT_SECRET"
hmpps-organisations-api-application-insights:
APPLICATIONINSIGHTS_CONNECTION_STRING: "APPLICATIONINSIGHTS_CONNECTION_STRING"
rds-postgresql-instance-output:
DB_SERVER: "rds_instance_address"
DB_NAME: "database_name"
DB_USER: "database_username"
DB_PASS: "database_password"
hmpps-domain-events-topic:
HMPPS_SQS_TOPICS_DOMAINEVENTS_ARN: "topic_arn"

allowlist:
groups:
Expand Down
5 changes: 1 addition & 4 deletions helm_deploy/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ generic-service:

env:
APPLICATIONINSIGHTS_CONFIGURATION_FILE: "applicationinsights.dev.json"
HMPPS_AUTH_URL: "https://sign-in-dev.hmpps.service.justice.gov.uk/auth"
# Template kotlin calls out to itself to provide an example of a service call
# TODO: This should be replaced by a call to a different service, or removed
EXAMPLE_API_URL: "https://organisations-api-dev.hmpps.service.justice.gov.uk"
API_BASE_URL_HMPPS_AUTH: "https://sign-in-dev.hmpps.service.justice.gov.uk/auth"

# CloudPlatform AlertManager receiver to route prometheus alerts to slack
# See https://user-guide.cloud-platform.service.justice.gov.uk/documentation/monitoring-an-app/how-to-create-alarms.html#creating-your-own-custom-alerts
Expand Down
5 changes: 1 addition & 4 deletions helm_deploy/values-preprod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ generic-service:

env:
APPLICATIONINSIGHTS_CONFIGURATION_FILE: "applicationinsights.dev.json"
HMPPS_AUTH_URL: "https://sign-in-preprod.hmpps.service.justice.gov.uk/auth"
# Template kotlin calls out to itself to provide an example of a service call
# TODO: This should be replaced by a call to a different service, or removed
EXAMPLE_API_URL: "https://organisations-api-preprod.hmpps.service.justice.gov.uk"
API_BASE_URL_HMPPS_AUTH: "https://sign-in-preprod.hmpps.service.justice.gov.uk/auth"

# CloudPlatform AlertManager receiver to route prometheus alerts to slack
# See https://user-guide.cloud-platform.service.justice.gov.uk/documentation/monitoring-an-app/how-to-create-alarms.html#creating-your-own-custom-alerts
Expand Down
5 changes: 1 addition & 4 deletions helm_deploy/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ generic-service:
host: organisations-api.hmpps.service.justice.gov.uk

env:
HMPPS_AUTH_URL: "https://sign-in.hmpps.service.justice.gov.uk/auth"
# Template kotlin calls out to itself to provide an example of a service call
# TODO: This should be replaced by a call to a different service, or removed
EXAMPLE_API_URL: "https://organisations-api.hmpps.service.justice.gov.uk"
API_BASE_URL_HMPPS_AUTH: "https://sign-in.hmpps.service.justice.gov.uk/auth"

# CloudPlatform AlertManager receiver to route prometheus alerts to slack
# See https://user-guide.cloud-platform.service.justice.gov.uk/documentation/monitoring-an-app/how-to-create-alarms.html#creating-your-own-custom-alerts
Expand Down
Loading

0 comments on commit d7a7296

Please sign in to comment.