2019-04-29 14:25:45 +02:00
# hsadminNg Development
2019-04-01 13:14:56 +02:00
2022-07-30 14:41:11 +02:00
This documents gives an overview of the development environment and tools.
For architecture consider the files in the `doc` and `adr` folder.
2022-07-29 15:39:35 +02:00
<!-- generated TOC begin: -->
- [Setting up the Development Environment ](#setting-up-the-development-environment )
2022-07-29 16:13:38 +02:00
- [SDKMAN ](#sdkman )
- [PostgreSQL Server ](#postgresql-server )
- [Markdown ](#markdown )
- [Render Markdown embedded PlantUML ](#render-markdown-embedded-plantuml )
- [Other Tools ](#other-tools )
2022-07-29 15:39:35 +02:00
- [Running the SQL files ](#running-the-sql-files )
2022-07-29 16:13:38 +02:00
- [For RBAC ](#for-rbac )
- [For Historization ](#for-historization )
2022-07-29 15:39:35 +02:00
<!-- generated TOC end. -->
2019-04-29 14:25:45 +02:00
## Setting up the Development Environment
2019-04-01 13:14:56 +02:00
2022-07-30 14:41:11 +02:00
All instructions assume that you're using a current _Linux_ or _MacOS_ operating system.
Everything is tested on _Ubuntu Linux 22.04_ and _MacOS Monterey (12.4)_ .
2022-07-29 09:03:21 +02:00
To be able to build and run the Java Spring Boot application, you need the following tools:
2022-07-30 14:41:11 +02:00
- Docker 20.x (on MacOS you also need *Docker Desktop* or similar)
2022-08-05 13:10:11 +02:00
- PostgreSQL Server 13.7-bullseye
(see instructions below to install and run in Docker)
- Java JDK at least recent enough to run Gradle
(JDK 17.x will be automatically installed by Gradle toolchain support)
2022-07-29 09:03:21 +02:00
- Gradle in some not too outdated version (7.4 will be installed via wrapper)
You also might need an IDE (e.g. *IntelliJ IDEA* or *Eclipse* or *VS Code* with *[STS](https://spring.io/tools)* and a GUI Frontend for *PostgreSQL* like *Postbird* .
If you have at least Docker, the Java JDK and Gradle installed in appropriate versions and in your `PATH` , then you can start like this:
cd your-hsadmin-ng-directory
2022-08-04 13:07:16 +02:00
gradle wrapper # downloads the configured Gradle version into the project
2022-07-30 14:41:11 +02:00
source .aliases # creates some comforable bash aliases, e.g. 'gw'='./gradlew'
2022-07-29 09:03:21 +02:00
gw test # compiles and runs unit- and integration-tests
pg-sql-run # downloads + runs PostgreSQL in a Docker container on localhost:5432
gw bootRun # compiles and runs the application on localhost:8080
2022-07-29 15:39:35 +02:00
# the following command should reply with "pong":
curl http://localhost:8080/api/ping
2022-07-29 16:13:38 +02:00
# the following command should return a JSON array with just all customers:
2022-07-29 15:39:35 +02:00
curl \
-H 'current-user: mike@hostsharing.net' \
2022-07-30 18:17:45 +02:00
http://localhost:8080/api/customers
2022-07-29 09:03:21 +02:00
2022-07-29 16:13:38 +02:00
# the following command should return a JSON array with just all packages visible for the admin of the customer aab:
curl \
2022-08-02 14:31:48 +02:00
-H 'current-user: mike@hostsharing.net' -H 'assumed-roles: customer#aab.admin' \
2022-07-30 18:17:45 +02:00
http://localhost:8080/api/packages
2022-07-29 16:13:38 +02:00
2022-08-02 14:31:48 +02:00
# add a new customer
curl \
-H 'current-user: mike@hostsharing.net' -H "Content-Type: application/json" \
-d '{ "prefix":"baa", "reference":80001, "adminUserName":"admin@baa.example.com" }' \
-X POST http://localhost:8080/api/customers
2022-08-03 09:06:21 +02:00
If you wonder who 'mike@hostsharing.net' and 'sven@hostsharing.net' are and where the data comes from:
Mike and Sven are just example Hostsharing hostmaster accounts as part of the example data which is automatically inserted in Testcontainers and Development environments.
2022-08-02 14:31:48 +02:00
Also try for example 'admin@aaa.example.com' or 'unknown@example.org'.
2022-07-29 09:03:21 +02:00
2022-07-30 14:41:11 +02:00
If you want a formatted JSON output, you can pipe the result to `jq` or similar.
2022-07-29 15:39:35 +02:00
2022-08-03 09:06:21 +02:00
And to see the full, currently implemented, API, open http://localhost:8080/swagger-ui/index.html.
2022-07-29 09:03:21 +02:00
If you still need to install some of these tools, find some hints in the next chapters.
### SDKMAN
*SdkMan* is not necessary, but helpful to install and switch between different versions of SDKs (Software-Development-Kits) and development tools in general, e.g. *JDK* and *Gradle* .
2022-07-30 14:41:11 +02:00
It is available for _Linux_ and _MacOS_ , _WSL_ , _Cygwin_ , _Solaris_ and _FreeBSD_ .
2022-07-29 09:03:21 +02:00
You can get it from: https://sdkman.io/.
< big > **⚠ **< / big >
Yeah, the `curl ... | bash` install method looks quite scary;
but in a development environment you're downloading executables all the time,
e.g. through `npm` , `Maven` or `Gradle` when downloading dependencies.
Thus, maybe you should at least use a separate Linux account for development.
Once it's installed, you can install *JDK* and *Gradle* :
sdk install java 17.0.3-tem
sdk install gradle
sdk use java 17.0.3-tem # use this to switch between installed JDK versions
2022-07-22 13:31:37 +02:00
### PostgreSQL Server
2019-05-01 14:37:22 +02:00
2022-07-29 09:03:21 +02:00
You could use any PostgreSQL Server (from version 13 on) installed on your machine.
You might amend the port and user settings in `src/main/resources/application.yml` , though.
2022-07-28 10:43:49 +02:00
2022-07-29 09:03:21 +02:00
But the easiest way to run PostgreSQL is via Docker.
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
Initially, pull an image compatible to current PostgreSQL version of Hostsharing:
2019-05-01 14:37:22 +02:00
2022-07-29 09:03:21 +02:00
docker pull postgres:13.7-bullseye
2019-05-01 14:37:22 +02:00
2022-07-28 10:43:49 +02:00
< big > **⚠ **< / big >
If we switch the version, please also amend the documentation as well as the aliases file. Thanks!
2022-07-22 13:31:37 +02:00
Create and run a container with the given PostgreSQL version:
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
docker run --name hsadmin-ng-postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:13.7-bullseye
2019-05-01 14:37:22 +02:00
2022-07-28 10:43:49 +02:00
# or via alias:
pg-sql-run
2022-07-22 13:31:37 +02:00
To check if the PostgreSQL container is running, the following command should list a container with the name "hsadmin-ng-postgres":
2019-05-01 14:37:22 +02:00
2022-07-28 10:43:49 +02:00
docker container ls
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
Stop the PostgreSQL container:
docker stop hsadmin-ng-postgres
2022-07-28 10:43:49 +02:00
# or via alias: pg-sql-stop
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
Start the PostgreSQL container again:
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
docker container start hsadmin-ng-postgres
2022-07-28 10:43:49 +02:00
# or via alias: pg-sql-start
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
Remove the PostgreSQL container:
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
docker rm hsadmin-ng-postgres
2022-07-28 10:43:49 +02:00
# or via alias:
pg-sql-remove
To reset to a clean database, use:
pg-sql-stop; pg-sql-remove; pg-sql-run
# or via alias:
pg-sql-reset
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
After the PostgreSQL container is removed, you need to create it again as shown in "Create and run ..." above.
2019-05-01 14:37:22 +02:00
2022-07-26 13:19:17 +02:00
Given the container is running, to create a backup in ~/backup, run:
2022-07-28 10:43:49 +02:00
docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump --clean --create -U postgres postgres | gzip -9 > ~/backup/hsadmin-ng-postgres.sql.gz
# or via alias:
pg-sql-backup >~/backup/hsadmin-ng-postgres.sql.gz
2022-07-26 13:19:17 +02:00
Again, given the container is running, to restore the backup from ~/backup, run:
gunzip --stdout --keep ~/backup/hsadmin-ng-postgres.sql.gz | docker exec -i hsadmin-ng-postgres psql -U postgres -d postgres
2022-07-28 10:43:49 +02:00
# or via alias:
pg-sql-restore < ~/backup/hsadmin-ng-postgres.sql.gz
2022-07-26 13:19:17 +02:00
2022-07-29 15:39:35 +02:00
### Markdown
2019-05-01 14:37:22 +02:00
2022-07-29 15:39:35 +02:00
To generate the TOC (Table of Contents), a little bash script from a
[Blog Article ](https://medium.com/@acrodriguez/one-liner-to-generate-a-markdown-toc-f5292112fd14 ) was used.
To render the Markdown files, especially to watch embedded PlantUML diagrams, you can use one of the following methods:
#### Render Markdown embedded PlantUML
Can you see the following diagram right in your IDE?
2022-07-30 14:41:11 +02:00
I mean a real graphic diagram, not just some markup code.
2019-05-08 10:56:48 +02:00
2022-07-22 13:31:37 +02:00
```plantuml
@startuml
me -> you: Can you see this diagram?
you -> me: Sorry, I don't :-(
me -> you: Install some tooling!
@enduml
```
2019-05-08 10:56:48 +02:00
2022-07-22 13:31:37 +02:00
If not, you need to install some tooling.
2019-05-08 10:56:48 +02:00
2022-07-29 15:39:35 +02:00
##### for IntelliJ IDEA (or derived products)
2019-05-08 10:56:48 +02:00
2022-08-04 14:56:19 +02:00
You just need the bundled Markdown plugin enabled and install and activate the PlantUML plugin in its [settings ](jetbrains://idea/settings?name=Languages+%26+Frameworks--Markdown ).
2019-05-08 10:56:48 +02:00
2022-07-22 13:31:37 +02:00
You might also need to install Graphviz on your operating system.
For Debian-based Linux systems this might work:
2019-05-08 10:56:48 +02:00
2022-07-22 13:31:37 +02:00
```sh
sudo apt install graphviz
```
2019-05-08 10:56:48 +02:00
2022-07-29 15:39:35 +02:00
##### Ubuntu Linux command line
2019-05-08 10:56:48 +02:00
2022-07-22 13:31:37 +02:00
```sh
sudo apt-get install pandoc texlive-latex-base texlive-fonts-recommended texlive-extra-utils texlive-latex-extra pandoc-plantuml-filter
```
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
```sh
pandoc --filter pandoc-plantuml rbac.md -o rbac.pdf
```
2019-05-01 14:37:22 +02:00
2022-07-29 15:39:35 +02:00
##### for other IDEs / operating systems
2019-05-01 14:37:22 +02:00
2022-07-22 13:31:37 +02:00
If you have figured out how it works, please add instructions above this section.
2022-07-28 10:43:49 +02:00
2022-08-05 13:10:11 +02:00
### IDE Specific Settings
#### IntelliJ IDEA
Go to [Gradle Settings}(jetbrains://idea/settings?name=Build%2C+Execution%2C+Deployment--Build+Tools--Gradle) and select "Build and run using" and "Run tests using" both to "gradle".
Otherwise, settings from `build.gradle` , like compiler arguments, are not applied when compiling through *IntelliJ IDEA* .
Go to [Annotations Processors ](jetbrains://idea/settings?name=Build%2C+Execution%2C+Deployment--Compiler--Annotation+Processors ) and activate annotation processing.
Otherwise, *IntelliJ IDEA* can't see *Lombok* generated classes
and will show false errors (missing identifiers).
2022-07-29 15:39:35 +02:00
### Other Tools
2022-07-30 14:41:11 +02:00
**jq**: a JSON formatter.
On _Debian_ 'oid systems you can install it with `sudo apt-get install jq` .
On _MacOS_ you can install it with `brew install jq` , given you have _brew_ installed.
2022-07-28 10:43:49 +02:00
## Running the SQL files
### For RBAC
2022-07-29 09:03:21 +02:00
The Schema is automatically created via *Liquibase* , a database migration library.
Currently, also some test data is automatically created.
2022-07-28 10:43:49 +02:00
2022-07-29 09:03:21 +02:00
To increase the amount of test data, increase the number of generated customers in `2022-07-28-051-hs-customer.sql` and run that
2022-07-28 10:43:49 +02:00
If you already have data, e.g. for customers 0..999 (thus with reference numbers 10000..10999) and want to add another 1000 customers, amend the for loop to 1000...1999 and also uncomment and amend the `CONTINUE WHEN` or `WHERE` conditions in the other test data generators, using the first new customer reference number (in the example that's 11000).
### For Historization
2022-07-29 09:03:21 +02:00
The historization is not yet integrated into the *Liquibase* -scripts.
You can explore the prototype as follows:
2022-07-28 10:43:49 +02:00
- start with an empty database
(the example tables are currently not compatible with RBAC),
2022-07-29 09:03:21 +02:00
- then run `historization.sql` in the database,
- finally run `examples.sql` in the database.
2022-08-02 11:51:36 +02:00
2022-08-04 09:41:27 +02:00
## Coding Guidelines
### Directory and Package Structure
2022-08-05 13:10:11 +02:00
#### General Directory Structure
.aliases
build/
build.gradle
doc/
.editorconfig
etc/
.git/
.gitattributes
.gitignore
.gradle/
gradle/
gradlew
gradlew.bat
.idea/
LICENSE.md
out/
README.md
.run/
settings.gradle
sql/
src/
TODO.md
TODO-progress.png
tools/
#### Source Code Package Structure
For the source code itself, the general standard Java directory structure is used, where productive and test code are separated like this:
2022-08-04 09:41:27 +02:00
```
src
main/
java/
net.hostsharing.hasadminng/
resources/
test/
java/
net.hostsharing.hasadminng/
resources/
```
The Java package structure below contains:
- config and global (utility) packages,
these should not access any other packages within the project
- rbac, containing all packages related to the RBAC subsystem
- hs, containing Hostsharing business object related packages
Underneath of rbac and hs, the structure is business oriented, NOT technical / layer -oriented.
Some of these rules are checked with *ArchUnit* unit tests.
2022-08-04 12:26:41 +02:00
2022-08-04 09:41:27 +02:00
### Spotless Code Formatting
Code formatting for Java is checked via *spotless* .
The formatting style can be checked with this command:
```shell
gw spotlessCheck
```
2022-08-04 12:26:41 +02:00
This task is also included in `gw build` and `gw check` .
2022-08-04 09:41:27 +02:00
To apply formatting rules, use:
```shell
gw spotlessApply
```
2022-08-04 12:26:41 +02:00
### OWASP Security Vulnerability Check
An OWASP security vulnerability is configured and can be utilized by running:
```shell
gw dependencyCheckAnalyze
```
This task is also included in `gw build` and `gw check` .
It is configured in [build.gradle ](build.gradle ).
Often vulnerability reports don't apply to our use cases.
Therefore, reports can be [suppressed ](./etc/owasp-dependency-check-suppression.xml ).
In case of suppression, a note must be added to explain why it does not apply to us.
See also: https://jeremylong.github.io/DependencyCheck/dependency-check-gradle/index.html.
2022-08-08 10:03:26 +02:00
### Dependency-License-Compatibility
2022-08-04 09:41:27 +02:00
2022-08-08 10:03:26 +02:00
The `gw check` phase depends on a dependency-license-compatibility check.
If any dependency violates the configured [list of allowed licenses ](etc/allowed-licenses.json ), the build will fail.
New licenses can be added to that list after a legal investigation.
< big > **⚠ **< / big >
*GPL* (*GNU General Public License*) is only allowed with classpath exception.
Do < u > not</ u > use any dependencies under *GPL* without this exception,
except if these offer an alternative license which is allowed.
To run just the dependency-license-compatibility check, use:
```shell
gw checkLicense
```
If the check fails, a report can be found here: The generated license can be found under [dependencies-without-allowed-license.json ](/build/reports/dependency-license/dependencies-without-allowed-license.json ).
And to generate a report, use:
```shell
gw generateLicenseReport
```
The generated license can be found here: [index.html ](build/reports/dependency-license/index.html ).
More information can be found on the [project's website ](https://github.com/jk1/Gradle-License-Report ).
## How To ...
### How to Run the Application on a Different Port
2022-08-04 13:07:16 +02:00
By default, `gw bootRun` starts the application on port 8080.
This port can be changed in
[src/main/resources/application.yml ](src/main/resources/application.yml ) through the property `server.port` .
2022-08-02 11:51:36 +02:00
### How to Use a Persistent Database for Integration Tests?
Usually, the `DataJpaTest` integration tests run against a database in a temporary docker container.
As soon as the test ends, the database is gone; this might make debugging difficult.
2022-08-04 13:07:16 +02:00
Alternatively, a persistent database could be used by amending the
[resources/application.yml ](src/main/resources/application.yml ) through the property `spring.datasource.url` in [src/test/resources/application.yml ](src/test/resources/application.yml ) , e.g. to the JDBC-URL from [src/main/resources/application.yml ](src/main/resources/application.yml ).
2022-08-02 11:51:36 +02:00
If the persistent database and the temporary database show different results, one of these reasons could be the cause:
1. You might have some changesets only running in either context,
check the `context: ...` in the changeset control lines.
2. You might have changes in the database which interfere with the tests,
e.g. from a previous run of tests or manually applied.
It's best to run `pg-sql-reset && gw bootRun` before each test run, to have a clean database.
2022-08-02 14:31:48 +02:00
## How to Amend Liquibase SQL Changesets?
Liquibase changesets are meant to be immutable and based on each other.
That means, once a changeset is written, it never changes, not even a whitespace or comment.
Liquibase is a *database migration tool* , not a *database initialization tool* .
This, if you need to add change a table, stored procedure or whatever,
create a new changeset and apply `ALTER` , `DROP` , `CREATE OR REPLACE` or whatever SQL commands to perform your changes.
These changes will be automatically applied once the application starts up again.
This way, any staging or production database will always match the application code.
But, during initial development that can be a big hassle because the database structure changes a lot in that stage.
Also, the actual structure of the database won't be easily recognized anymore through lots of migration changesets.
Therefore, during initial development, it's good approach just to amend the existing changesets and delete the database:
```shell
pg-sql-reset
gw bootRun
```
< big > **⚠ **< / big >
Just don't forget switching to the migration mode, once there is a production database!
2022-08-04 10:35:28 +02:00
## Further Documentation
- the `doc` directory contains architecture concepts and a glossary
- TODO.md tracks requirements and progress for the contract of the initial project,
please do not amend anything in this document