diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml new file mode 100644 index 0000000..d006987 --- /dev/null +++ b/.github/workflows/pipeline.yml @@ -0,0 +1,77 @@ +name: AuditServer CI/CD pipeline +on: [push] +jobs: + Build: + runs-on: ubuntu-latest + env: + GITHUB_LOGIN: ${{ github.actor }} + GITHUB_PACKAGES: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '16' + - run: | + ./gradlew build -x test --no-daemon + + Test: + runs-on: ubuntu-latest + needs: [Build] + env: + GITHUB_LOGIN: ${{ github.actor }} + GITHUB_PACKAGES: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '16' + - name: Tests + run: | + cp application.properties.sample application.properties + sed -i "s/REPLACEME/${{ secrets.APP_GITHUB_TOKEN }}/g" application.properties + ./gradlew test --no-daemon + Deploy: + runs-on: ubuntu-latest + needs: [Test] + env: + GITHUB_LOGIN: ${{ github.actor }} + GITHUB_PACKAGES: ${{ secrets.GITHUB_TOKEN }} + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '16' + - name: Build java app + run: | + echo "APP_VERSION=$(grep "version =" build.gradle | awk '{print $3}' | sed "s/'//g")" >> $GITHUB_ENV + ./gradlew build -x test --no-daemon + - name: Build docker image + uses: docker/build-push-action@v2 + with: + context: . + push: false + tags: audit-server:${{ env.APP_VERSION }} + build-args: VERSION=${{ env.APP_VERSION }} + - name: Create k8s Kind Cluster + uses: helm/kind-action@v1.2.0 + with: + cluster_name: audit-server + - name: Deploy audit-server helm chart to local cluster + run: | + kind load docker-image audit-server:${{ env.APP_VERSION }} --name audit-server + helm upgrade --install audit-server ./deploy/charts/audit-server \ + -f ./deploy/local.yaml \ + --set image.tag="${{ env.APP_VERSION }}" \ + --set config.githubToken="${{ secrets.APP_GITHUB_TOKEN }}" \ + --atomic \ + --debug \ + --timeout 5m0s + - name: Validate audit-server deployment is successfully serving traffic + run: | + kubectl port-forward svc/audit-server 8000:80 & + sleep 10 + curl -f --connect-timeout 3 --retry 10 --retry-delay 5 http://localhost:8000/healthz + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..203f25a --- /dev/null +++ b/.gitignore @@ -0,0 +1,66 @@ +HELP.md +gradle.properties +application.properties +.gradle +# Ignore Gradle build output directory +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ +# Ignora la carpeta de compilados +bin + +# Compiled class file +*.class + +# Log file +*.log + +# Package Files # +*.jar +#excluyo el gradle wrapper de esta regla +!gradle/wrapper/gradle-wrapper.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +#las pruebas harán generar informes excel, que no quiero subir al repositorio remoto +*.xls +/javadoc.xml + +### Ficheros que no hay que controlar, para diferentes IDES ### +#Ignoro los directorios que crea el IDE en el espacio de trabajo, que no se suba a git +### STS ### +.gitattributes +.metadata +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f279ff9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM openjdk:16-alpine +ARG VERSION +COPY ./build/libs/auditserver-${VERSION}.jar /app/main.jar +WORKDIR /app +EXPOSE 8080 +CMD ["java", "-jar", "main.jar"] diff --git a/README.md b/README.md index 401b217..19ecf9d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,189 @@ # AuditServer + Servidor de métricas, apoyado en la librería Audit4Improve +## Objetivo + +El objetivo de este código es practicar con: +- La gestión de dependencias +- La integración continua +- El despliegue continuo + +## Dependencias + +Este servidor utiliza la api [Audit4Improve](https://github.com/MIT-FS/Audit4Improve-API), en la que los alumnos han trabajado a lo largo de las prácticas de la asignatura. + +Para el entorno local de desarrollo vamos a usar [Kubernetes] en la +máquina local. Para ello, vamos a necesitar installar previamente +[Kind] y [kubectl]. Una vez instalado comprueba que podemos ejecutar el binario: + +```shell +kind --version +``` + +La aplicación se va a desplegar en Kubernetes usando una herramienta de gestión +de la configuración muy liviana llamada [helm], que además es capaz de +automatizar el proceso de release y rollback. + +Para ello [descarga e instala helm]. Una vez instalado, comprueba que podemos +ejecutar el binario: + +```shell +helm version +``` + +Para finalizar, necesitaremos un [Personal Access Token de Github] para poder +probar el funcionamiento de la aplicación. + +[Kubernetes]: https://kubernetes.io/ +[Kind]: https://kind.sigs.k8s.io/docs/user/quick-start#installation +[kubectl]: https://kubernetes.io/docs/tasks/tools/#kubectl +[helm]: https://helm.sh/ +[descarga e instala helm]: https://helm.sh/docs/intro/install/ +[Personal Access Token de Github]: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token + +## Desarrollo local + +Antes de empezar, necesitamos cargar nuestro Personal Access Token en la shell +donde probemos nuestro código. + +```shell +export GITHUB_TOKEN= +``` + +Para ejecutar el servidor web de en la máquina local, ejecuta el siguiente comando: + +```shell +./gradlew bootRun +``` + +Prueba que el servicio expone un endpoint de metricas en /metricsInfo: + +```shell +curl http://localhost:8080/metricsInfo/issues +``` + +El endpoint debe devolver la siguiente respuesta: + +```shell + +StatusCode : 200 +StatusDescription : +Content : {"name":"issues","unit":"issues","description":"Tareas sin finalizar en el repositorio","type":"java.lang.Integer"} +RawContent : HTTP/1.1 200 + Transfer-Encoding: chunked +... +``` + +### Ejecutar los tests + +Para ejecutar los tests unitarios, ejecuta el siguiente comando: + +```shell +./gradlew test +``` + +### Desplegar el entorno local de desarrollo + +Para ejecutar el entorno local de desarrollo, ejecuta el siguiente comando: + +```shell +./gradlew localenv-up +``` + +La tarea `localenv` levanta un cluster de Kubernetes y configura de forma +automática el contexto de Kubernetes para que podamos acceder a la API +de Kubernetes de manera local usando `kubectl`. Levantar el entorno local +debe tardar alrededor de 3 minutos. + +Finalmente comprobamos que tenemos acceso al cluster de Kubernetes: + +```shell +kubectl get po --all-namespaces +``` + +Deberemos obtener uns salida similar a la siguiente: + +```shell +NAMESPACE NAME READY STATUS RESTARTS AGE +kube-system coredns-558bd4d5db-l8q2g 1/1 Running 0 83s +kube-system coredns-558bd4d5db-rqcbm 1/1 Running 0 84s +kube-system etcd-audit-server-control-plane 1/1 Running 0 94s +kube-system kindnet-glbk7 1/1 Running 0 85s +kube-system kube-apiserver-audit-server-control-plane 1/1 Running 1 94s +kube-system kube-controller-manager-audit-server-control-plane 1/1 Running 0 94s +kube-system kube-proxy-db56r 1/1 Running 0 85s +kube-system kube-scheduler-audit-server-control-plane 1/1 Running 0 94s +local-path-storage local-path-provisioner-547f784dff-vz7c2 1/1 Running 0 83s +``` + +Cuando hayamos terminado, simplemente borramos el cluster: + +```shell +./gradlew localenv-down +``` + +### Despliegue de la aplicación en el entorno local + +Para desplegar la aplicación, ejecuta el siguiente comando: + +```shell +./gradlew localenv-deploy +``` + +Al finalizar, aplicación se encuentra en el `default` namespace, y debe +de haber 1 pod en estado running: + +```shell +âžœ ~ kubectl get po +NAME READY STATUS RESTARTS AGE +audit-server-7b7f9cbb96-x6kfw 1/1 Running 0 98s +``` + +Podemos interactuar con la aplicación y simular que recibe peticiones +HTTP haciendo port-forwarding del servicio a nuestra máquina local. De esta +forma, no necesitamos un Load Balancer real en nuestra infraestructura, ni +configuración DNS extra: + +```shell +kubectl port-forward svc/audit-server 8000:80 +``` + +Esto abre un tunel al cluster de Kubernetes y expone el puerto 80 del servicio, +que mapea al puerto 8080 del container que se ejecuta en la pod, al puerto 8000 +de nuestra máquina local. Y ahora podemos abrir otro terminal y lanzarle +peticiones a nuestro servicio: + +```shell +âžœ ~ curl http://localhost:8000/healthz +{"healthy":true} +âžœ ~ curl http://localhost:8000/metricsInfo/forks +{"name":"forks","unit":"forks","description":"Número de forks, no son los forks de la web","type":"java.lang.Integer"} +``` + +## Comenzar con Spring Boot para el desarrollo de servicios REST + +Enlaces generados automáticamente al crear el esqueleto del servicio en [start.spring.io](https://start.spring.io/) + +### Documentación de referencia + +For further reference, please consider the following sections: + +* [Official Gradle documentation](https://docs.gradle.org) +* [Spring Boot Gradle Plugin Reference Guide](https://docs.spring.io/spring-boot/docs/2.6.7/gradle-plugin/reference/html/) +* [Create an OCI image](https://docs.spring.io/spring-boot/docs/2.6.7/gradle-plugin/reference/html/#build-image) +* [Spring Web](https://docs.spring.io/spring-boot/docs/2.6.7/reference/htmlsingle/#boot-features-developing-web-applications) + +### Guías + +The following guides illustrate how to use some features concretely: + +* [Building a RESTful Web Service](https://spring.io/guides/gs/rest-service/) +* [Serving Web Content with Spring MVC](https://spring.io/guides/gs/serving-web-content/) +* [Building REST services with Spring](https://spring.io/guides/tutorials/bookmarks/) + +### Enlaces adicionales + +These additional references should also help you: + +* [Gradle Build Scans – insights for your project's build](https://scans.gradle.com#gradle) + diff --git a/application.properties.sample b/application.properties.sample new file mode 100644 index 0000000..524e3b7 --- /dev/null +++ b/application.properties.sample @@ -0,0 +1,4 @@ +app.githubToken=REPLACEME +app.githubApiUrl=https://api.github.com +app.healthzGithubOrg=MIT-FS +app.healthzGithubRepo=AuditServer diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..e371609 --- /dev/null +++ b/build.gradle @@ -0,0 +1,74 @@ +plugins { + id 'org.springframework.boot' version '2.6.7' + id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'java' + id 'war' +} + +group = 'us.mitfs.samples' +version = '0.0.1-SNAPSHOT' +sourceCompatibility = '16' + +repositories { + mavenCentral() + maven { + name = "GitHubPackages" + url = uri("https://maven.pkg.github.com/MIT-FS/Audit4Improve-API") + credentials { + //las propiedades gpr.user y gpr.key est�n configuradas en gradle.properties en el raiz del proyecto, y se a�ade a .gitignore para que no se suban + //O bien configuro las variables de entorno GITHUB_LOGIN y GITHUB_PACKAGES + username = project.findProperty("gpr.user") ?: System.getenv("GITHUB_LOGIN") + password = project.findProperty("gpr.key") ?: System.getenv("GITHUB_PACKAGES") + + } + } + maven { + url "https://mvnrepository.com/artifact/com.spotify/github-client" + } +} + +dependencies { + implementation 'us.mitfs.samples:a4i:0.0.1' + implementation group: 'com.spotify', name: 'github-client', version: '0.1.28' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} + +tasks.named('test') { + useJUnitPlatform() + +} + +tasks.register('localenv-up') { + doLast { + exec { + executable "./scripts/localenv/up.sh" + } + } +} + +tasks.register('localenv-down') { + doLast { + exec { + executable "./scripts/localenv/down.sh" + } + } +} + +tasks.register('localenv-build') { + dependsOn 'localenv-up' + doLast { + exec { + executable "./scripts/localenv/build.sh" + } + } +} + +tasks.register('localenv-deploy') { + dependsOn 'localenv-build' + doLast { + exec { + executable "./scripts/localenv/deploy.sh" + } + } + +} diff --git a/deploy/charts/audit-server/.helmignore b/deploy/charts/audit-server/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/deploy/charts/audit-server/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/deploy/charts/audit-server/Chart.yaml b/deploy/charts/audit-server/Chart.yaml new file mode 100644 index 0000000..6865543 --- /dev/null +++ b/deploy/charts/audit-server/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: audit-server +description: A Helm chart for audit-server +type: application +version: 0.0.1 +appVersion: "0.0.1-SNAPSHOT" diff --git a/deploy/charts/audit-server/templates/_helpers.tpl b/deploy/charts/audit-server/templates/_helpers.tpl new file mode 100644 index 0000000..8284782 --- /dev/null +++ b/deploy/charts/audit-server/templates/_helpers.tpl @@ -0,0 +1,62 @@ +{{/* +Expand the name of the chart. +*/}} +{{- define "audit-server.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "audit-server.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "audit-server.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{/* +Common labels +*/}} +{{- define "audit-server.labels" -}} +helm.sh/chart: {{ include "audit-server.chart" . }} +{{ include "audit-server.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{/* +Selector labels +*/}} +{{- define "audit-server.selectorLabels" -}} +app.kubernetes.io/name: {{ include "audit-server.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{/* +Create the name of the service account to use +*/}} +{{- define "audit-server.serviceAccountName" -}} +{{- if .Values.serviceAccount.create }} +{{- default (include "audit-server.fullname" .) .Values.serviceAccount.name }} +{{- else }} +{{- default "default" .Values.serviceAccount.name }} +{{- end }} +{{- end }} diff --git a/deploy/charts/audit-server/templates/deployment.yaml b/deploy/charts/audit-server/templates/deployment.yaml new file mode 100644 index 0000000..d330519 --- /dev/null +++ b/deploy/charts/audit-server/templates/deployment.yaml @@ -0,0 +1,71 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "audit-server.fullname" . }} + labels: + {{- include "audit-server.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "audit-server.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + checksum/config: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }} + {{- with .Values.podAnnotations }} + {{- toYaml . | nindent 8 }} + {{- end }} + labels: + {{- include "audit-server.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + serviceAccountName: {{ include "audit-server.serviceAccountName" . }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + lifecycle: # Secret mount of single files is not supported, exposing application.properties in /app + postStart: + exec: + command: + - /bin/sh + - -c + - ln -s /config/application.properties /app/application.properties + ports: + - name: http + containerPort: 8080 + protocol: TCP + readinessProbe: + httpGet: + path: /healthz + port: http + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: config + mountPath: "/config" + readOnly: true + volumes: + - name: config + secret: + secretName: {{ include "audit-server.fullname" . }}-config-secret + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/deploy/charts/audit-server/templates/secret.yaml b/deploy/charts/audit-server/templates/secret.yaml new file mode 100644 index 0000000..127a32b --- /dev/null +++ b/deploy/charts/audit-server/templates/secret.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "audit-server.fullname" . }}-config-secret +type: Opaque +stringData: + application.properties: | + app.githubToken={{ .Values.config.githubToken }} + app.githubApiUrl={{ .Values.config.githubApiUrl }} + app.healthzGithubOrg={{ .Values.config.healthzGithubOrg }} + app.healthzGithubRepo={{ .Values.config.healthzGithubRepo }} diff --git a/deploy/charts/audit-server/templates/service.yaml b/deploy/charts/audit-server/templates/service.yaml new file mode 100644 index 0000000..867d9bc --- /dev/null +++ b/deploy/charts/audit-server/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "audit-server.fullname" . }} + labels: + {{- include "audit-server.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "audit-server.selectorLabels" . | nindent 4 }} diff --git a/deploy/charts/audit-server/templates/serviceaccount.yaml b/deploy/charts/audit-server/templates/serviceaccount.yaml new file mode 100644 index 0000000..41bb98d --- /dev/null +++ b/deploy/charts/audit-server/templates/serviceaccount.yaml @@ -0,0 +1,12 @@ +{{- if .Values.serviceAccount.create -}} +apiVersion: v1 +kind: ServiceAccount +metadata: + name: {{ include "audit-server.serviceAccountName" . }} + labels: + {{- include "audit-server.labels" . | nindent 4 }} + {{- with .Values.serviceAccount.annotations }} + annotations: + {{- toYaml . | nindent 4 }} + {{- end }} +{{- end }} diff --git a/deploy/charts/audit-server/values.yaml b/deploy/charts/audit-server/values.yaml new file mode 100644 index 0000000..9a17ea3 --- /dev/null +++ b/deploy/charts/audit-server/values.yaml @@ -0,0 +1,63 @@ +# Default values for audit-server. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +replicaCount: 1 + +image: + repository: audit-server + pullPolicy: IfNotPresent + # Overrides the image tag whose default is the chart appVersion. + tag: "" + +config: + githubToken: REPLACEME + githubApiUrl: https://api.github.com + healthzGithubOrg: MIT-FS + healthzGithubRepo: AuditServer + +imagePullSecrets: [] +nameOverride: "" +fullnameOverride: "" + +serviceAccount: + # Specifies whether a service account should be created + create: true + # Annotations to add to the service account + annotations: {} + # The name of the service account to use. + # If not set and create is true, a name is generated using the fullname template + name: "" + +podAnnotations: {} + +podSecurityContext: {} + # fsGroup: 2000 + +securityContext: {} + # capabilities: + # drop: + # - ALL + # readOnlyRootFilesystem: true + # runAsNonRoot: true + # runAsUser: 1000 + +service: + type: ClusterIP + port: 80 + +resources: {} + # We usually recommend not to specify default resources and to leave this as a conscious + # choice for the user. This also increases chances charts run on environments with little + # resources, such as Minikube. If you do want to specify resources, uncomment the following + # lines, adjust them as necessary, and remove the curly braces after 'resources:'. + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi + +nodeSelector: {} +tolerations: [] +affinity: {} diff --git a/deploy/local.yaml b/deploy/local.yaml new file mode 100644 index 0000000..a426282 --- /dev/null +++ b/deploy/local.yaml @@ -0,0 +1,11 @@ +replicaCount: 1 +image: + repository: audit-server + pullPolicy: Never +resources: + limits: + cpu: 1000m + memory: 512Mi + requests: + cpu: 1000m + memory: 512Mi diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..41d9927 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..00e33ed --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.1-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1b6c787 --- /dev/null +++ b/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${0##*/} + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..477c896 --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,89 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega \ No newline at end of file diff --git a/scripts/localenv/build.sh b/scripts/localenv/build.sh new file mode 100755 index 0000000..bf536db --- /dev/null +++ b/scripts/localenv/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +set -e + +./gradlew build +export VERSION=$(grep "version =" build.gradle | awk '{print $3}' | sed "s/'//g") +docker build --build-arg VERSION=${VERSION} -t audit-server:${VERSION} . diff --git a/scripts/localenv/deploy.sh b/scripts/localenv/deploy.sh new file mode 100755 index 0000000..4f3dbe0 --- /dev/null +++ b/scripts/localenv/deploy.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e + +export VERSION=$(grep "version =" build.gradle | awk '{print $3}' | sed "s/'//g") +export IMAGE_LOADED=$(kubectl get node audit-server-control-plane -o yaml | grep -i docker.io/library/audit-server:${VERSION}) +if [ -z "${IMAGE_LOADED}" ]; then + echo "Loading image" + kind load docker-image audit-server:${VERSION} --name audit-server +else + echo "Image already loaded" +fi +helm upgrade --install audit-server ./deploy/charts/audit-server \ + -f ./deploy/local.yaml \ + --set image.tag="${VERSION}" \ + --set config.githubToken="${GITHUB_TOKEN}" \ + --atomic \ + --timeout 3m0s diff --git a/scripts/localenv/down.sh b/scripts/localenv/down.sh new file mode 100755 index 0000000..588c852 --- /dev/null +++ b/scripts/localenv/down.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +export CLUSTER_ALREADY_EXISTS=$(kind get clusters | grep "audit-server") +if [ ! -z "${CLUSTER_ALREADY_EXISTS}" ]; then + echo "Deleting cluster" + kind delete cluster --name audit-server +else + echo "Cluster does not exist" +fi + diff --git a/scripts/localenv/up.sh b/scripts/localenv/up.sh new file mode 100755 index 0000000..2d0046b --- /dev/null +++ b/scripts/localenv/up.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -e + +export CLUSTER_ALREADY_EXISTS=$(kind get clusters | grep "audit-server") +if [ -z "${CLUSTER_ALREADY_EXISTS}" ]; then + echo "Creating cluster" + kind create cluster --name audit-server +else + echo "Cluster already exists" +fi diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..b1d960b --- /dev/null +++ b/settings.gradle @@ -0,0 +1 @@ +rootProject.name = 'auditserver' \ No newline at end of file diff --git a/src/main/java/us/mitfs/samples/auditserver/AppProperties.java b/src/main/java/us/mitfs/samples/auditserver/AppProperties.java new file mode 100644 index 0000000..c00e2d1 --- /dev/null +++ b/src/main/java/us/mitfs/samples/auditserver/AppProperties.java @@ -0,0 +1,45 @@ +package us.mitfs.samples.auditserver; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Component +@ConfigurationProperties(prefix = "app") +class AppProperties { + private String githubToken; + private String githubApiUrl; + private String healthzGithubOrg; + private String healthzGithubRepo; + + public String getGithubToken() { + return githubToken; + } + + public void setGithubToken(String githubToken) { + this.githubToken = githubToken; + } + + public String getGithubApiUrl() { + return githubApiUrl; + } + + public void setGithubApiUrl(String githubApiUrl) { + this.githubApiUrl = githubApiUrl; + } + + public String getHealthzGithubOrg() { + return healthzGithubOrg; + } + + public void setHealthzGithubOrg(String healthzGithubOrg) { + this.healthzGithubOrg = healthzGithubOrg; + } + + public String getHealthzGithubRepo() { + return healthzGithubRepo; + } + + public void setHealthzGithubRepo(String healthzGithubRepo) { + this.healthzGithubRepo = healthzGithubRepo; + } +} diff --git a/src/main/java/us/mitfs/samples/auditserver/AuditserverApplication.java b/src/main/java/us/mitfs/samples/auditserver/AuditserverApplication.java new file mode 100644 index 0000000..ca1d8b9 --- /dev/null +++ b/src/main/java/us/mitfs/samples/auditserver/AuditserverApplication.java @@ -0,0 +1,14 @@ +package us.mitfs.samples.auditserver; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AuditserverApplication { + + public static void main(String[] args) { + SpringApplication.run(AuditserverApplication.class, args); + } + +} + diff --git a/src/main/java/us/mitfs/samples/auditserver/HealthController.java b/src/main/java/us/mitfs/samples/auditserver/HealthController.java new file mode 100644 index 0000000..6b99563 --- /dev/null +++ b/src/main/java/us/mitfs/samples/auditserver/HealthController.java @@ -0,0 +1,81 @@ +package us.mitfs.samples.auditserver; + +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.spotify.github.v3.clients.GitHubClient; +import com.spotify.github.v3.clients.RepositoryClient; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class HealthController { + @Autowired + AppProperties config; + + private String getGithubToken() + { + return config.getGithubToken(); + } + + private String getGithubApiUrl() + { + return config.getGithubApiUrl(); + } + + private String getHealthzGithubOrg() + { + return config.getHealthzGithubOrg(); + } + + private String getHealthzGithubRepo() + { + return config.getHealthzGithubRepo(); + } + /* + /healthz endpoint valida el estado del servicio y sus dependencias, + y reporta en todo momento si el servicio esta operativo y acepta + solicitudes HTTP. En caso afirmativo, devuelve 200 OK. En cualquier + otro caso devuelve 500. Este endpoint lo consume directamente la + readinessProbe de Kubernetes para gestionar de manera automatica el service + discovery. +*/ +@RequestMapping("/healthz") +@GetMapping(path = "/healthz", produces=MediaType.APPLICATION_JSON_VALUE) +ResponseEntity> healthz() { + /* + En esta seccion se evaluan las dependencias del servicio + */ + try{ + String githubToken = this.getGithubToken(); + String githubApiUrl = this.getGithubApiUrl(); + String healthzGithubOrg = this.getHealthzGithubOrg(); + String healthzGithubRepo = this.getHealthzGithubRepo(); + Map body = new HashMap<>(); + final GitHubClient githubClient = GitHubClient.create(URI.create(githubApiUrl), githubToken); + final RepositoryClient repositoryClient = githubClient.createRepositoryClient(healthzGithubOrg, healthzGithubRepo); + repositoryClient.getCommitStatus("main").thenAccept(commitStatus -> { + body.put("check", commitStatus.sha()); + }).get(); + + body.put("healthy", true); + + return ResponseEntity.status(HttpStatus.OK).body(body); + } catch(Exception ref) { + Map body = new HashMap<>(); + + body.put("healthy", false); + body.put("error", ref.getMessage()); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body); + } +} + +} diff --git a/src/main/java/us/mitfs/samples/auditserver/MetricController.java b/src/main/java/us/mitfs/samples/auditserver/MetricController.java new file mode 100644 index 0000000..b560d3f --- /dev/null +++ b/src/main/java/us/mitfs/samples/auditserver/MetricController.java @@ -0,0 +1,34 @@ +package us.mitfs.samples.auditserver; + + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import us.muit.fs.a4i.config.Context; + +@RestController +public class MetricController { + @GetMapping("/metricsInfo") + public HashMap allMetrics() throws FileNotFoundException, IOException { + HashMap metrics=new HashMap(); + + List metricsNames=Context.getContext().getChecker().listAllMetrics(); + for(String name:metricsNames) { + metrics.put(name,new MetricInfo(name)); + } + return metrics; + + } + @GetMapping("/metricsInfo/{name}") + public MetricInfo metric(@PathVariable String name) { + return new MetricInfo(name); + + } +} diff --git a/src/main/java/us/mitfs/samples/auditserver/MetricInfo.java b/src/main/java/us/mitfs/samples/auditserver/MetricInfo.java new file mode 100644 index 0000000..389f316 --- /dev/null +++ b/src/main/java/us/mitfs/samples/auditserver/MetricInfo.java @@ -0,0 +1,56 @@ +package us.mitfs.samples.auditserver; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; + +import us.muit.fs.a4i.config.Context; + +public class MetricInfo { + + private final String name; + private String unit=null; + private String description=null; + private String type=null; + + + public MetricInfo(String name) { + this.name = name; + HashMap info; + try { + info = Context.getContext().getChecker().getMetricInfo(name); + if (info!=null) { + unit=info.get("unit"); + description=info.get("description"); + type=info.get("type"); + } + + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + public String getName() { + return name; + } + + public String getUnit() { + return unit; + } + + public String getDescription() { + return description; + } + + public String getType() { + return type; + } + + + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..f3ee13a --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +app.githubToken=REPLACEME +app.githubApiUrl=https://api.github.com +app.healthzGithubOrg=MIT-FS +app.healthzGithubRepo=AuditServer \ No newline at end of file diff --git a/src/test/java/us/mitfs/samples/auditserver/AuditserverApplicationTests.java b/src/test/java/us/mitfs/samples/auditserver/AuditserverApplicationTests.java new file mode 100644 index 0000000..d0c326e --- /dev/null +++ b/src/test/java/us/mitfs/samples/auditserver/AuditserverApplicationTests.java @@ -0,0 +1,75 @@ +package us.mitfs.samples.auditserver; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.Map; + +@SpringBootTest +class AuditserverApplicationTests { + + @Autowired + private HealthController controller; + + @Test + void contextLoads() { + assertThat(controller).isNotNull(); + } + +} + +@SpringBootTest +class SmokeTest { + + @Autowired + private HealthController controller; + + @Test + void contextLoads() { + assertThat(controller).isNotNull(); + } + +} + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +class SuccessfulHealthzTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void healthz() throws Exception { + String endpoint = "http://localhost:" + port + "/healthz"; + Map endpointResponse = this.restTemplate.getForObject(endpoint, Map.class); + assertThat(endpointResponse).containsKeys("healthy", "check"); + assertThat(endpointResponse.get("healthy")).isEqualTo(true); + } +} + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "app.githubToken=faketoken" }) +class UnsuccessfulHealthzTest { + + @LocalServerPort + private int port; + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void healthz() throws Exception { + String endpoint = "http://localhost:" + port + "/healthz"; + Map endpointResponse = this.restTemplate.getForObject(endpoint, Map.class); + assertThat(endpointResponse).containsKeys("healthy", "error"); + assertThat(endpointResponse.get("healthy")).isEqualTo(false); + } +} +