pagopa-checkout-auth-service
is a Kotlin-based microservice designed to support authentication for the checkout process.
This service leverages Kotlin's native compilation to achieve optimal performance and efficiency.
- Kotlin
- Spring Boot (native)
-- TODO --
An example configuration of these environment variables is in the .env.example
file.
It is recommended to create a new .env file by copying the example one, using the following command (make sure you are in the .env.example folder):
cp .env.example .env
If you are developing on Windows, it is recommended the use of WSL2 combined with IntelliJ IDEA.
The IDE should be installed on Windows, with the repository cloned into a folder in WSL2. All the necessary tools will be installed in the Linux distro of your choice.
You can find more info on how to set up the environment following the link below.
https://www.jetbrains.com/help/idea/how-to-use-wsl-development-environment-in-product.html
After setting up the WSL environment, you can test the application by building it through either Spring Boot or Docker.
- You must use GraalVM Java SDK to build native executable locally. https://www.graalvm.org/downloads/. It is recommended to use SDKMAN
- You must use GraalVM gradle plugin which allows to configure a lot of setting for native compilation, like automatic toolchain detection https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html
If you're experiencing issue with GraalVM not found like errors, be sure to use GraalVM for the project and try to enable automatic toolchain detection. Also, you can use SDKMAN to provide a better JVM env "switching".
To compile microservice to native executable you can use the following gradle task:
gradle :nativeCompile
This will produce an executable inside build/native/nativeCompile/
N.B. Compiling into a native executable takes a long time. Locally, it is recommended to launch it normally (in java) in order to test the service.
Also exist a gradle command to compile and run it directly:
gradle :nativeRun
The project can be built and run using Docker and docker-compose. You should install Docker Desktop on Windows and go through its settings to set up the WSL integration.
You can find more info at the following link: https://docs.docker.com/desktop/wsl/
After setting up Docker, you can use the command:
docker-compose up
The docker-compose up command will build the image and start the containers.
The main issue with native image is related to Java Reflection. GraalVM produces a metadata files containing reflection data. There is also a repository containing the metadata of some of the most widely used external libraries. You can include this metadata via the gradle plugin
graalvmNative {
metadataRepository {
enabled.set(true)
version.set("0.2.6")
}
}
Spring using AOT try automatically to do the best, but you can also find issues.
Here https://docs.spring.io/spring-framework/reference/core/aot.html#aot.hints you can find a lot of tips, like @RegisterReflectionForBinding
This feature use the content of gradle.lockfile
to check the declared dependencies against the locked one.
If a transitive dependencies have been upgraded the build will fail because of the locked version mismatch.
The following command can be used to upgrade dependency lockfile:
./gradlew dependencies --write-locks
Running the above command will cause the gradle.lockfile
to be updated against the current project dependency
configuration
This feature is enabled by adding the gradle ./gradle/verification-metadata.xml
configuration file.
Perform checksum comparison against dependency artifact (jar files, zip, ...) and metadata (pom.xml, gradle module
metadata, ...) used during build
and the ones stored into verification-metadata.xml
file raising error during build in case of mismatch.
The following command can be used to recalculate dependency checksum:
./gradlew --write-verification-metadata sha256 clean spotlessApply build --no-build-cache --refresh-dependencies
In the above command the clean
, spotlessApply
build
tasks where chosen to be run
in order to discover all transitive dependencies used during build and also the ones used during
spotless apply task used to format source code.
The above command will upgrade the verification-metadata.xml
adding all the newly discovered dependencies' checksum.
Those checksum should be checked against a trusted source to check for corrispondence with the library author published
checksum.
/gradlew --write-verification-metadata sha256
command appends all new dependencies to the verification files but does
not remove
entries for unused dependencies.
This can make this file grow every time a dependency is upgraded.
To detect and remove old dependencies make the following steps:
- Delete, if present, the
gradle/verification-metadata.dryrun.xml
- Run the gradle write-verification-metadata in dry-mode (this will generate a verification-metadata-dryrun.xml file leaving untouched the original verification file)
- Compare the verification-metadata file and the verification-metadata.dryrun one checking for differences and removing old unused dependencies
The 1-2 steps can be performed with the following commands
rm -f ./gradle/verification-metadata.dryrun.xml
./gradlew --write-verification-metadata sha256 clean spotlessApply build --dry-run
The resulting verification-metadata.xml
modifications must be reviewed carefully checking the generated
dependencies checksum against official websites or other secure sources.
If a dependency is not discovered during the above command execution it will lead to build errors.
You can add those dependencies manually by modifying the verification-metadata.xml
file adding the following component:
<verification-metadata>
<!-- other configurations... -->
<components>
<!-- other components -->
<component group="GROUP_ID" name="ARTIFACT_ID" version="VERSION">
<artifact name="artifact-full-name.jar">
<sha256 value="sha value"
origin="Description of the source of the checksum value"/>
</artifact>
<artifact name="artifact-pom-file.pom">
<sha256 value="sha value"
origin="Description of the source of the checksum value"/>
</artifact>
</component>
</components>
</verification-metadata>
Add those components at the end of the components list and then run the
./gradlew --write-verification-metadata sha256 clean spotlessApply build --no-build-cache --refresh-dependencies
that will reorder the file with the added dependencies checksum in the expected order.
Finally, you can add new dependencies both to gradle.lockfile writing verification metadata running
./gradlew dependencies --write-locks --write-verification-metadata sha256 --no-build-cache --refresh-dependencies
For more information read the following article
Made with ❤️ by PagoPA S.p.A.
See CODEOWNERS
file