diff --git a/.github/workflows/ci-cd.yaml b/.github/workflows/ci-cd.yaml index f76461186..d776886af 100644 --- a/.github/workflows/ci-cd.yaml +++ b/.github/workflows/ci-cd.yaml @@ -82,7 +82,6 @@ jobs: run: pip install -r requirements.dev.txt - name: ansible playbooks and roles lint check run: | - export ANSIBLE_ACTION_PLUGINS=$(python3 -m ara.setup.action_plugins) ansible-lint -vv -x role-name,risky-shell-pipe,no-tabs -w yaml \ runner/ansible/* runner/ansible/roles/* \ runner/ansible/roles/checks/* runner/ansible/vars/* diff --git a/Dockerfile b/Dockerfile index 4b1887c82..33facf9fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,7 +23,7 @@ RUN make build FROM python:3.7-slim AS trento-runner RUN ln -s /usr/local/bin/python /usr/bin/python \ && /usr/bin/python -m venv /venv \ - && /venv/bin/pip install 'ansible~=4.6.0' 'ara~=1.5.7' 'rpm==0.0.2' 'pyparsing~=2.0' \ + && /venv/bin/pip install 'ansible~=4.6.0' 'requests~=2.26.0' 'rpm==0.0.2' 'pyparsing~=2.0' \ && apt-get update && apt-get install -y --no-install-recommends \ ssh \ && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ diff --git a/README.md b/README.md index f12992549..805b1e88e 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ of existing clusters, rather than deploying new one. * [Manually running Trento](#manually-running-trento) + [Trento Agents](#trento-agents) + [Trento Runner](#trento-runner) - - [Setting up and starting ARA](#setting-up-and-starting-ara) - [Starting the Trento Runner](#starting-the-trento-runner) + [Trento Web UI](#trento-web-ui) - [Configuration](#configuration) @@ -68,11 +67,10 @@ The _Trento Server_ is an independent, cloud-native, distributed system and shou - The `trento web` application; - The `trento runner` worker; -- An [ARA] service. The _Trento Agent_ is a single background process (`trento agent`) running in each host of the target infrastructure the user desires to monitor. -Please note that, except for the third-party ones like ARA, all the components are embedded within one single `trento` binary. +Please note that, except for the third-party ones like Ansible, all the components are embedded within one single `trento` binary. See the [architecture document](./docs/trento-architecture.md) for additional details. @@ -331,49 +329,19 @@ Those are useful in order to test `mTLS` communication between the Agent and the ### Trento Runner -The Trento Runner is a worker process responsible for driving automated configuration audits. It is based on [Ansible](https://docs.ansible.com/ansible/latest/index.html) and [ARA](https://ara.recordsansible.org/). -These 2 components (the Runner and ARA) can be executed in the same host as the Web UI, but it is not mandatory: they can be executed in any other host with network access to the Trento Agents. - -The Runner itself and ARA can be even executed in different hosts too, as long as the network connection is available between them. +The Trento Runner is a worker process responsible for driving automated configuration audits. It is based on [Ansible](https://docs.ansible.com/ansible/latest/index.html). +This component can be executed in the same host as the Web UI, but it is not mandatory: it can be executed in any other host with network access to the Trento Agents. Find more information about how to create more Trento health checks [here](docs/runner.md). In order to start them, some packages must be installed and started. Here a quick go through: -#### Setting up and starting ARA - -```shell -# Install ARA with server dependencies -pip install 'ara[server]~=1.5.7' -# Setup ARA database -ara-manage migrate -# Start ARA server. This process can be started in background or in other shell terminal -ara-manage runserver ip:port -``` - -> The installed ara version should be at least ara~=1.5.7 - -If the requests to ARA server fail with a message like the next one, it means that the server address must be allowed: - -``` -2021-09-02 07:13:48,715 ERROR django.security.DisallowedHost: Invalid HTTP_HOST header: '10.74.1.5:8000'. You may need to add '10.74.1.5' to ALLOWED_HOSTS. -2021-09-02 07:13:48,732 WARNING django.request: Bad Request: /api/ -``` - -To fix it run: - -``` -export ARA_ALLOWED_HOSTS="['10.74.1.5']" -# Or allow all the addresses with -export ARA_ALLOWED_HOSTS=['*'] -``` - #### Starting the Trento Runner -Independently where you decide to run ARA, the Runner needs the `ansible` and `ara` Python packages available locally: +The Runner needs the `ansible` Python package available locally: ```shell -pip install 'ansible~=4.6.0' 'ara~=1.5.7' +pip install 'ansible~=4.6.0' ``` > The installed ansible components versions should be at least ansible~=4.6.0 and ansible-core~=2.11.5 @@ -381,7 +349,7 @@ pip install 'ansible~=4.6.0' 'ara~=1.5.7' Once dependencies are in place, you can start the Runner itself: ```shell -./trento runner start --ara-server http://$ARA_IP:$ARA_PORT --api-host $WEB_IP --api-port $WEB_PORT -i 5 +./trento runner start --api-host $WEB_IP --api-port $WEB_PORT -i 5 ``` > *Note:* The Trento Runner component must have SSH access to all the agents via a password-less SSH key pair. @@ -392,8 +360,6 @@ At this point, we can start the web application as follows: ```shell ./trento web serve -# If ARA server is not running in the same machine set the ara-addr flag -./trento web serve --ara-addr araIP:port ``` Please consult the `help` CLI command for more insights on the various options. @@ -430,7 +396,7 @@ Example locations: `/etc/trento/runner.yaml` -or +or `/usr/etc/trento/agent.yaml` @@ -579,5 +545,4 @@ 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. -[ARA]: https://ara.recordsansible.org [K3S]: https://k3s.io diff --git a/cmd/runner/config.go b/cmd/runner/config.go index 8e33bdf6e..4206467f0 100644 --- a/cmd/runner/config.go +++ b/cmd/runner/config.go @@ -11,7 +11,6 @@ func LoadConfig() *runner.Config { return &runner.Config{ ApiHost: viper.GetString("api-host"), ApiPort: viper.GetInt("api-port"), - AraServer: viper.GetString("ara-server"), Interval: time.Duration(interval) * time.Minute, AnsibleFolder: viper.GetString("ansible-folder"), } diff --git a/cmd/runner/runner.go b/cmd/runner/runner.go index e95b316d0..b3aca0fa5 100644 --- a/cmd/runner/runner.go +++ b/cmd/runner/runner.go @@ -15,7 +15,6 @@ import ( "github.com/trento-project/trento/runner" ) -var araServer string var apiHost string var apiPort int var interval int @@ -36,7 +35,6 @@ func NewRunnerCmd() *cobra.Command { }, } - startCmd.Flags().StringVar(&araServer, "ara-server", "http://127.0.0.1:8000", "ARA server url (ex: http://localhost:8000)") startCmd.Flags().StringVar(&apiHost, "api-host", "0.0.0.0", "Trento web server API host") startCmd.Flags().IntVar(&apiPort, "api-port", 8080, "Trento web server API port") startCmd.Flags().IntVarP(&interval, "interval", "i", 5, "Interval in minutes to run the checks") diff --git a/cmd/web/web.go b/cmd/web/web.go index 4f8796e9b..0e673017b 100644 --- a/cmd/web/web.go +++ b/cmd/web/web.go @@ -52,7 +52,6 @@ func NewWebCmd() *cobra.Command { func addServeCmd(webCmd *cobra.Command) { var host string var port int - var araAddr string var collectorPort int var enablemTLS bool @@ -68,7 +67,6 @@ func addServeCmd(webCmd *cobra.Command) { serveCmd.Flags().StringVar(&host, "host", "0.0.0.0", "The host to bind the HTTP service to") serveCmd.Flags().IntVarP(&port, "port", "p", 8080, "The port for the HTTP service to listen on") - serveCmd.Flags().StringVar(&araAddr, "ara-addr", "127.0.0.1:8000", "Address where ARA is running (ex: localhost:80)") serveCmd.Flags().IntVar(&collectorPort, "collector-port", 8081, "The port for the data collector service to listen on") serveCmd.Flags().BoolVar(&enablemTLS, "enable-mtls", false, "Enable mTLS authentication between server and agents") diff --git a/packaging/helm/trento-server/Chart.lock b/packaging/helm/trento-server/Chart.lock index ad3177797..30b7e9353 100644 --- a/packaging/helm/trento-server/Chart.lock +++ b/packaging/helm/trento-server/Chart.lock @@ -5,11 +5,8 @@ dependencies: - name: trento-runner repository: "" version: '>0.0.0' -- name: ara - repository: "" - version: '>0.0.0' - name: postgresql repository: https://charts.bitnami.com/bitnami/ version: 10.1.4 -digest: sha256:5cdbc1f203be1b601168ea486fa0fb95d76e7d3ee2210dcbb84f8ed2f0a90264 -generated: "2021-12-10T22:17:58.374025204+01:00" +digest: sha256:4ca440f423831ceb17665407466664a3193b3c7cf251f15466e71be87676df20 +generated: "2021-12-23T14:21:08.608734238+01:00" diff --git a/packaging/helm/trento-server/Chart.yaml b/packaging/helm/trento-server/Chart.yaml index 112a9d349..4b21df7d8 100644 --- a/packaging/helm/trento-server/Chart.yaml +++ b/packaging/helm/trento-server/Chart.yaml @@ -1,12 +1,12 @@ -#!BuildTag: trento/trento-server:0.2.5 -#!BuildTag: trento/trento-server:0.2.5-build%RELEASE% +#!BuildTag: trento/trento-server:0.2.6 +#!BuildTag: trento/trento-server:0.2.6-build%RELEASE% apiVersion: v2 name: trento-server description: The trento server chart contains all the components necessary to run a Trento server. type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates -version: 0.2.5 +version: 0.2.6 dependencies: - name: trento-web @@ -15,9 +15,6 @@ dependencies: - name: trento-runner version: ">0.0.0" condition: trento-runner.enabled - - name: ara - version: ">0.0.0" - condition: ara.enabled - name: postgresql version: ~10.1.4 repository: https://charts.bitnami.com/bitnami/ diff --git a/packaging/helm/trento-server/charts/ara/.helmignore b/packaging/helm/trento-server/charts/ara/.helmignore deleted file mode 100644 index 0e8a0eb36..000000000 --- a/packaging/helm/trento-server/charts/ara/.helmignore +++ /dev/null @@ -1,23 +0,0 @@ -# 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/packaging/helm/trento-server/charts/ara/Chart.yaml b/packaging/helm/trento-server/charts/ara/Chart.yaml deleted file mode 100644 index 7026b9221..000000000 --- a/packaging/helm/trento-server/charts/ara/Chart.yaml +++ /dev/null @@ -1,24 +0,0 @@ -apiVersion: v2 -name: ara -description: A Helm chart for Kubernetes - -# A chart can be either an 'application' or a 'library' chart. -# -# Application charts are a collection of templates that can be packaged into versioned archives -# to be deployed. -# -# Library charts provide useful utilities or functions for the chart developer. They're included as -# a dependency of application charts to inject those utilities and functions into the rendering -# pipeline. Library charts do not define any templates and therefore cannot be deployed. -type: application - -# This is the chart version. This version number should be incremented each time you make changes -# to the chart and its templates, including the app version. -# Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.1.0 - -# This is the version number of the application being deployed. This version number should be -# incremented each time you make changes to the application. Versions are not expected to -# follow Semantic Versioning. They should reflect the version the application is using. -# It is recommended to use it with quotes. -appVersion: "0.0.1" diff --git a/packaging/helm/trento-server/charts/ara/templates/NOTES.txt b/packaging/helm/trento-server/charts/ara/templates/NOTES.txt deleted file mode 100644 index 99d226dcb..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/NOTES.txt +++ /dev/null @@ -1 +0,0 @@ -ARA Chart installed! diff --git a/packaging/helm/trento-server/charts/ara/templates/_helpers.tpl b/packaging/helm/trento-server/charts/ara/templates/_helpers.tpl deleted file mode 100644 index 4c7a944ea..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/_helpers.tpl +++ /dev/null @@ -1,62 +0,0 @@ -{{/* -Expand the name of the chart. -*/}} -{{- define "ara.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 "ara.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 "ara.chart" -}} -{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} -{{- end }} - -{{/* -Common labels -*/}} -{{- define "ara.labels" -}} -helm.sh/chart: {{ include "ara.chart" . }} -{{ include "ara.selectorLabels" . }} -{{- if .Chart.AppVersion }} -app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} -{{- end }} -app.kubernetes.io/managed-by: {{ .Release.Service }} -{{- end }} - -{{/* -Selector labels -*/}} -{{- define "ara.selectorLabels" -}} -app.kubernetes.io/name: {{ include "ara.name" . }} -app.kubernetes.io/instance: {{ .Release.Name }} -{{- end }} - -{{/* -Create the name of the service account to use -*/}} -{{- define "ara.serviceAccountName" -}} -{{- if .Values.serviceAccount.create }} -{{- default (include "ara.fullname" .) .Values.serviceAccount.name }} -{{- else }} -{{- default "default" .Values.serviceAccount.name }} -{{- end }} -{{- end }} diff --git a/packaging/helm/trento-server/charts/ara/templates/datavolume.yaml b/packaging/helm/trento-server/charts/ara/templates/datavolume.yaml deleted file mode 100644 index a7aa6ecd9..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/datavolume.yaml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: {{ include "ara.fullname" . }}-data-pvc - namespace: default -spec: - accessModes: - - ReadWriteOnce - storageClassName: local-path - resources: - requests: - storage: {{ .Values.dataVolumeSize }} \ No newline at end of file diff --git a/packaging/helm/trento-server/charts/ara/templates/deployment.yaml b/packaging/helm/trento-server/charts/ara/templates/deployment.yaml deleted file mode 100644 index cb039f86b..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/deployment.yaml +++ /dev/null @@ -1,73 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: {{ include "ara.fullname" . }} - labels: - {{- include "ara.labels" . | nindent 4 }} -spec: - {{- if not .Values.autoscaling.enabled }} - replicas: {{ .Values.replicaCount }} - {{- end }} - selector: - matchLabels: - {{- include "ara.selectorLabels" . | nindent 6 }} - template: - metadata: - {{- with .Values.podAnnotations }} - annotations: - {{- toYaml . | nindent 8 }} - {{- end }} - labels: - {{- include "ara.selectorLabels" . | nindent 8 }} - spec: - {{- with .Values.imagePullSecrets }} - imagePullSecrets: - {{- toYaml . | nindent 8 }} - {{- end }} - serviceAccountName: {{ include "ara.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 }} - env: - - name: ARA_DATABASE_NAME - value: /data/ansible.sqlite - - name: ARA_ALLOWED_HOSTS - value: "['*']" - ports: - - name: ara-http - containerPort: {{ .Values.service.port }} - protocol: TCP - livenessProbe: - httpGet: - path: /healthcheck/ - port: ara-http - readinessProbe: - httpGet: - path: / - port: ara-http - volumeMounts: - - name: {{ include "ara.fullname" . }}-data - mountPath: "/data" - resources: - {{- toYaml .Values.resources | nindent 12 }} - {{- with .Values.nodeSelector }} - nodeSelector: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.affinity }} - affinity: - {{- toYaml . | nindent 8 }} - {{- end }} - {{- with .Values.tolerations }} - tolerations: - {{- toYaml . | nindent 8 }} - {{- end }} - volumes: - - name: {{ include "ara.fullname" . }}-data - persistentVolumeClaim: - claimName: {{ include "ara.fullname" . }}-data-pvc diff --git a/packaging/helm/trento-server/charts/ara/templates/hpa.yaml b/packaging/helm/trento-server/charts/ara/templates/hpa.yaml deleted file mode 100644 index 9e8b66ae7..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/hpa.yaml +++ /dev/null @@ -1,28 +0,0 @@ -{{- if .Values.autoscaling.enabled }} -apiVersion: autoscaling/v2beta1 -kind: HorizontalPodAutoscaler -metadata: - name: {{ include "ara.fullname" . }} - labels: - {{- include "ara.labels" . | nindent 4 }} -spec: - scaleTargetRef: - apiVersion: apps/v1 - kind: Deployment - name: {{ include "ara.fullname" . }} - minReplicas: {{ .Values.autoscaling.minReplicas }} - maxReplicas: {{ .Values.autoscaling.maxReplicas }} - metrics: - {{- if .Values.autoscaling.targetCPUUtilizationPercentage }} - - type: Resource - resource: - name: cpu - targetAverageUtilization: {{ .Values.autoscaling.targetCPUUtilizationPercentage }} - {{- end }} - {{- if .Values.autoscaling.targetMemoryUtilizationPercentage }} - - type: Resource - resource: - name: memory - targetAverageUtilization: {{ .Values.autoscaling.targetMemoryUtilizationPercentage }} - {{- end }} -{{- end }} diff --git a/packaging/helm/trento-server/charts/ara/templates/service.yaml b/packaging/helm/trento-server/charts/ara/templates/service.yaml deleted file mode 100644 index db78763fe..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/service.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: {{ include "ara.fullname" . }} - labels: - {{- include "ara.labels" . | nindent 4 }} -spec: - type: {{ .Values.service.type }} - ports: - - port: {{ .Values.service.port }} - targetPort: ara-http - protocol: TCP - name: ara-http - selector: - {{- include "ara.selectorLabels" . | nindent 4 }} diff --git a/packaging/helm/trento-server/charts/ara/templates/serviceaccount.yaml b/packaging/helm/trento-server/charts/ara/templates/serviceaccount.yaml deleted file mode 100644 index dac15dc20..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/serviceaccount.yaml +++ /dev/null @@ -1,12 +0,0 @@ -{{- if .Values.serviceAccount.create -}} -apiVersion: v1 -kind: ServiceAccount -metadata: - name: {{ include "ara.serviceAccountName" . }} - labels: - {{- include "ara.labels" . | nindent 4 }} - {{- with .Values.serviceAccount.annotations }} - annotations: - {{- toYaml . | nindent 4 }} - {{- end }} -{{- end }} diff --git a/packaging/helm/trento-server/charts/ara/templates/tests/test-connection.yaml b/packaging/helm/trento-server/charts/ara/templates/tests/test-connection.yaml deleted file mode 100644 index 21c24b7dd..000000000 --- a/packaging/helm/trento-server/charts/ara/templates/tests/test-connection.yaml +++ /dev/null @@ -1,15 +0,0 @@ -apiVersion: v1 -kind: Pod -metadata: - name: "{{ include "ara.fullname" . }}-test-connection" - labels: - {{- include "ara.labels" . | nindent 4 }} - annotations: - "helm.sh/hook": test -spec: - containers: - - name: wget - image: busybox - command: ['wget'] - args: ['{{ include "ara.fullname" . }}:{{ .Values.service.port }}'] - restartPolicy: Never diff --git a/packaging/helm/trento-server/charts/ara/values.yaml b/packaging/helm/trento-server/charts/ara/values.yaml deleted file mode 100644 index 9bd2a981a..000000000 --- a/packaging/helm/trento-server/charts/ara/values.yaml +++ /dev/null @@ -1,71 +0,0 @@ -# Default values for ara. -# This is a YAML-formatted file. -# Declare variables to be passed into your templates. - -replicaCount: 1 - -image: - repository: recordsansible/ara-api - pullPolicy: IfNotPresent - # Overrides the image tag whose default is the chart appVersion. - tag: "fedora33-pypi-latest" - -imagePullSecrets: [] -nameOverride: "" -fullnameOverride: "" - -dataVolumeSize: 1Gi - -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: 8000 - -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 - -autoscaling: - enabled: false - minReplicas: 1 - maxReplicas: 100 - targetCPUUtilizationPercentage: 80 - # targetMemoryUtilizationPercentage: 80 - -nodeSelector: {} - -tolerations: [] - -affinity: {} diff --git a/packaging/helm/trento-server/charts/trento-runner/Chart.yaml b/packaging/helm/trento-server/charts/trento-runner/Chart.yaml index 233dfb2f9..55d827b73 100644 --- a/packaging/helm/trento-server/charts/trento-runner/Chart.yaml +++ b/packaging/helm/trento-server/charts/trento-runner/Chart.yaml @@ -15,4 +15,4 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.3.1 +version: 0.3.2 diff --git a/packaging/helm/trento-server/charts/trento-runner/templates/deployment.yaml b/packaging/helm/trento-server/charts/trento-runner/templates/deployment.yaml index 9f9324adf..3098a55de 100644 --- a/packaging/helm/trento-server/charts/trento-runner/templates/deployment.yaml +++ b/packaging/helm/trento-server/charts/trento-runner/templates/deployment.yaml @@ -35,8 +35,6 @@ spec: - start - --log-level - {{ .Values.global.logLevel }} - - --ara-server - - http://{{ .Release.Name }}-{{ .Values.dependencies.ara.name }}:{{ .Values.dependencies.ara.port }} - --api-host - {{ .Release.Name }}-{{ .Values.dependencies.trentoWeb.name }} - --api-port diff --git a/packaging/helm/trento-server/charts/trento-runner/values.yaml b/packaging/helm/trento-server/charts/trento-runner/values.yaml index 953534ea9..b8db97785 100644 --- a/packaging/helm/trento-server/charts/trento-runner/values.yaml +++ b/packaging/helm/trento-server/charts/trento-runner/values.yaml @@ -90,6 +90,3 @@ dependencies: trentoCollector: name: trento-web-collector port: 8081 - ara: - name: ara - port: 8000 diff --git a/packaging/helm/trento-server/charts/trento-web/Chart.yaml b/packaging/helm/trento-server/charts/trento-web/Chart.yaml index 3b750adf0..e25709de8 100644 --- a/packaging/helm/trento-server/charts/trento-web/Chart.yaml +++ b/packaging/helm/trento-server/charts/trento-web/Chart.yaml @@ -15,4 +15,4 @@ type: application # This is the chart version. This version number should be incremented each time you make changes # to the chart and its templates, including the app version. # Versions are expected to follow Semantic Versioning (https://semver.org/) -version: 0.3.0 +version: 0.3.1 diff --git a/packaging/helm/trento-server/charts/trento-web/templates/deployment.yaml b/packaging/helm/trento-server/charts/trento-web/templates/deployment.yaml index a9e0f1331..15cfdba0f 100644 --- a/packaging/helm/trento-server/charts/trento-web/templates/deployment.yaml +++ b/packaging/helm/trento-server/charts/trento-web/templates/deployment.yaml @@ -35,8 +35,6 @@ spec: - serve - --log-level - {{ .Values.global.logLevel }} - - --ara-addr - - {{ .Release.Name }}-{{ .Values.dependencies.ara.name }}:{{ .Values.dependencies.ara.port }} - --db-host - {{ .Release.Name }}-{{ .Values.dependencies.postgresql.name }} - --db-port diff --git a/packaging/helm/trento-server/charts/trento-web/values.yaml b/packaging/helm/trento-server/charts/trento-web/values.yaml index 04a163259..a98db8b10 100644 --- a/packaging/helm/trento-server/charts/trento-web/values.yaml +++ b/packaging/helm/trento-server/charts/trento-web/values.yaml @@ -94,9 +94,6 @@ affinity: {} # service dependencies to be consumed by the web deployment dependencies: - ara: - name: ara - port: 8000 postgresql: name: postgresql port: 5432 diff --git a/packaging/helm/trento-server/values.yaml b/packaging/helm/trento-server/values.yaml index de7604f07..b0d34cf5f 100644 --- a/packaging/helm/trento-server/values.yaml +++ b/packaging/helm/trento-server/values.yaml @@ -14,9 +14,6 @@ trento-runner: trentoWeb: name: web -ara: - enabled: true - postgresql: enabled: true image: diff --git a/requirements.dev.txt b/requirements.dev.txt index 34d70dbb0..7ab06be80 100644 --- a/requirements.dev.txt +++ b/requirements.dev.txt @@ -1,5 +1,5 @@ ansible==4.6.0 ansible-core==2.11.5 ansible-lint==5.1.3 -ara==1.5.7 PyYAML +requests==2.26.0 diff --git a/runner/ansiblerunner.go b/runner/ansiblerunner.go index 30608a5f8..920f89961 100644 --- a/runner/ansiblerunner.go +++ b/runner/ansiblerunner.go @@ -1,29 +1,18 @@ -// Find ARA ansible resources information at: -// https://ara.readthedocs.io/en/latest/ -// https://github.com/ansible-community/ara -// https://ara.readthedocs.io/en/latest/index.html - package runner import ( "bufio" "fmt" - "net/http" "os" "os/exec" - "strings" log "github.com/sirupsen/logrus" ) const ( - TrentoWebApiHost = "TRENTO_WEB_API_HOST" - TrentoWebApiPort = "TRENTO_WEB_API_PORT" - AnsibleConfigFileEnv = "ANSIBLE_CONFIG" - AnsibleCallbackPlugins = "ANSIBLE_CALLBACK_PLUGINS" - AnsibleActionPlugins = "ANSIBLE_ACTION_PLUGINS" - AraApiClient = "ARA_API_CLIENT" - AraApiServer = "ARA_API_SERVER" + TrentoWebApiHost = "TRENTO_WEB_API_HOST" + TrentoWebApiPort = "TRENTO_WEB_API_PORT" + AnsibleConfigFileEnv = "ANSIBLE_CONFIG" ) //go:generate mockery --name=CustomCommand @@ -47,15 +36,6 @@ func DefaultAnsibleRunner() *AnsibleRunner { } } -func DefaultAnsibleRunnerWithAra() (*AnsibleRunner, error) { - a := DefaultAnsibleRunner() - if err := a.LoadAraPlugins(); err != nil { - return a, err - } - - return a, nil -} - func (a *AnsibleRunner) setEnv(name, value string) { a.Envs[name] = value } @@ -89,70 +69,6 @@ func (a *AnsibleRunner) SetTrentoApiData(host string, port int) { a.setEnv(TrentoWebApiPort, fmt.Sprintf("%d", port)) } -// ARA_API_CLIENT is always set to "http" to ensure the usage of the REST API -// "offline" mode could be used, but it would only work if the ansible runner is -// running in the same host, and it doesn't provide much value -func (a *AnsibleRunner) SetAraServer(host string) { - a.setEnv(AraApiClient, "http") - a.setEnv(AraApiServer, host) -} - -func (a *AnsibleRunner) LoadAraPlugins() error { - log.Info("Loading ARA plugins...") - - araCallback := customExecCommand("python3", "-m", "ara.setup.callback_plugins") - araCallbackPath, err := araCallback.Output() - if err != nil { - log.Errorf("An error occurred getting the ARA callback plugin path: %s", err) - return err - } - araCallbackPathStr := strings.TrimSpace(string(araCallbackPath)) - log.Debugf("ARA callback plugin found: %s", araCallbackPathStr) - - a.setEnv(AnsibleCallbackPlugins, araCallbackPathStr) - - araAction := customExecCommand("python3", "-m", "ara.setup.action_plugins") - araActionPath, err := araAction.Output() - if err != nil { - log.Errorf("An error occurred getting the ARA actions plugin path: %s", err) - return err - } - araActionPathStr := strings.TrimSpace(string(araActionPath)) - log.Debugf("ARA actions plugin found: %s", araActionPathStr) - - a.setEnv(AnsibleActionPlugins, araActionPathStr) - - log.Info("ARA plugins loaded successfully") - - return nil -} - -func (a *AnsibleRunner) IsAraServerUp() bool { - server, ok := a.Envs[AraApiServer] - if !ok { - log.Warn("ARA server usage not configured") - return false - } - - host := fmt.Sprintf("%s/api/", server) - log.Debugf("Looking for the ARA server at: %s", host) - - resp, err := http.Get(host) - - if err != nil || resp.StatusCode != http.StatusOK { - log.Debugf("Error requesting ARA server api: %s", err) - return false - } - - log.Debugf("ARA server response code: %s", resp.Status) - - if resp.StatusCode != http.StatusOK { - return false - } - - return true -} - func (a *AnsibleRunner) RunPlaybook() error { var cmdItems []string diff --git a/runner/ansiblerunner_test.go b/runner/ansiblerunner_test.go index 27d5ed213..365be96bf 100644 --- a/runner/ansiblerunner_test.go +++ b/runner/ansiblerunner_test.go @@ -9,42 +9,6 @@ import ( "github.com/trento-project/trento/runner/mocks" ) -func TestLoadAraPlugins(t *testing.T) { - - a := DefaultAnsibleRunner() - - cmdCallback := exec.Command("echo", "callback") - cmdAction := exec.Command("echo", "action") - - mockCommand := new(mocks.CustomCommand) - customExecCommand = mockCommand.Execute - mockCommand.On("Execute", "python3", "-m", "ara.setup.callback_plugins").Return( - cmdCallback, - ) - mockCommand.On("Execute", "python3", "-m", "ara.setup.action_plugins").Return( - cmdAction, - ) - - err := a.LoadAraPlugins() - a.SetAraServer("127.0.0.1") - - expectedMetaRunner := &AnsibleRunner{ - Playbook: "main.yml", - Envs: map[string]string{ - "ANSIBLE_CALLBACK_PLUGINS": "callback", - "ANSIBLE_ACTION_PLUGINS": "action", - "ARA_API_CLIENT": "http", - "ARA_API_SERVER": "127.0.0.1", - }, - Check: false, - } - - assert.NoError(t, err) - assert.Equal(t, expectedMetaRunner, a) - - mockCommand.AssertExpectations(t) -} - func TestRunPlaybookSimple(t *testing.T) { runnerInst := &AnsibleRunner{ diff --git a/runner/runner.go b/runner/runner.go index ec07397f0..fccc150de 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -36,7 +36,6 @@ type Runner struct { type Config struct { ApiHost string ApiPort int - AraServer string Interval time.Duration AnsibleFolder string } @@ -72,10 +71,6 @@ func (c *Runner) Start() error { return err } - if !metaRunner.IsAraServerUp() { - return fmt.Errorf("ARA server not available") - } - if err = metaRunner.RunPlaybook(); err != nil { return err } @@ -148,32 +143,25 @@ func createAnsibleFiles(folder string) error { func NewAnsibleMetaRunner(config *Config) (*AnsibleRunner, error) { playbookPath := path.Join(config.AnsibleFolder, AnsibleMeta) - ansibleRunner, err := DefaultAnsibleRunnerWithAra() - if err != nil { - return ansibleRunner, err - } + ansibleRunner := DefaultAnsibleRunner() - if err = ansibleRunner.SetPlaybook(playbookPath); err != nil { + if err := ansibleRunner.SetPlaybook(playbookPath); err != nil { return ansibleRunner, err } configFile := path.Join(config.AnsibleFolder, AnsibleConfigFile) ansibleRunner.SetConfigFile(configFile) ansibleRunner.SetTrentoApiData(config.ApiHost, config.ApiPort) - ansibleRunner.SetAraServer(config.AraServer) - return ansibleRunner, err + return ansibleRunner, nil } func NewAnsibleCheckRunner(config *Config) (*AnsibleRunner, error) { playbookPath := path.Join(config.AnsibleFolder, AnsibleMain) - ansibleRunner, err := DefaultAnsibleRunnerWithAra() - if err != nil { - return ansibleRunner, err - } + ansibleRunner := DefaultAnsibleRunner() - if err = ansibleRunner.SetPlaybook(playbookPath); err != nil { + if err := ansibleRunner.SetPlaybook(playbookPath); err != nil { return ansibleRunner, err } @@ -181,7 +169,6 @@ func NewAnsibleCheckRunner(config *Config) (*AnsibleRunner, error) { configFile := path.Join(config.AnsibleFolder, AnsibleConfigFile) ansibleRunner.SetConfigFile(configFile) ansibleRunner.SetTrentoApiData(config.ApiHost, config.ApiPort) - ansibleRunner.SetAraServer(config.AraServer) return ansibleRunner, nil } @@ -210,10 +197,6 @@ func (c *Runner) startCheckRunnerTicker() { return } - if !checkRunner.IsAraServerUp() { - log.Error("ARA server not found. Skipping ansible execution as the data is not recorded") - return - } checkRunner.RunPlaybook() } diff --git a/runner/runner_test.go b/runner/runner_test.go index 0f812be65..8d5af496e 100644 --- a/runner/runner_test.go +++ b/runner/runner_test.go @@ -3,12 +3,10 @@ package runner import ( "io/ioutil" "os" - "os/exec" "path" "testing" "github.com/stretchr/testify/assert" - "github.com/trento-project/trento/runner/mocks" ) const ( @@ -33,41 +31,22 @@ func TestNewAnsibleMetaRunner(t *testing.T) { ApiHost: "127.0.0.1", ApiPort: 8000, AnsibleFolder: TestAnsibleFolder, - AraServer: "127.0.0.1", } - cmdCallback := exec.Command("echo", "callback") - cmdAction := exec.Command("echo", "action") - - mockCommand := new(mocks.CustomCommand) - customExecCommand = mockCommand.Execute - mockCommand.On("Execute", "python3", "-m", "ara.setup.callback_plugins").Return( - cmdCallback, - ) - mockCommand.On("Execute", "python3", "-m", "ara.setup.action_plugins").Return( - cmdAction, - ) - a, err := NewAnsibleMetaRunner(cfg) expectedMetaRunner := &AnsibleRunner{ Playbook: path.Join(TestAnsibleFolder, "ansible/meta.yml"), Envs: map[string]string{ - "ANSIBLE_CONFIG": path.Join(TestAnsibleFolder, "ansible/ansible.cfg"), - "ANSIBLE_CALLBACK_PLUGINS": "callback", - "ANSIBLE_ACTION_PLUGINS": "action", - "ARA_API_CLIENT": "http", - "ARA_API_SERVER": "127.0.0.1", - "TRENTO_WEB_API_HOST": "127.0.0.1", - "TRENTO_WEB_API_PORT": "8000", + "ANSIBLE_CONFIG": path.Join(TestAnsibleFolder, "ansible/ansible.cfg"), + "TRENTO_WEB_API_HOST": "127.0.0.1", + "TRENTO_WEB_API_PORT": "8000", }, Check: false, } assert.NoError(t, err) assert.Equal(t, expectedMetaRunner, a) - - mockCommand.AssertExpectations(t) } func TestNewAnsibleCheckRunner(t *testing.T) { @@ -76,39 +55,20 @@ func TestNewAnsibleCheckRunner(t *testing.T) { ApiHost: "127.0.0.1", ApiPort: 8000, AnsibleFolder: TestAnsibleFolder, - AraServer: "127.0.0.1", } - cmdCallback := exec.Command("echo", "callback") - cmdAction := exec.Command("echo", "action") - - mockCommand := new(mocks.CustomCommand) - customExecCommand = mockCommand.Execute - mockCommand.On("Execute", "python3", "-m", "ara.setup.callback_plugins").Return( - cmdCallback, - ) - mockCommand.On("Execute", "python3", "-m", "ara.setup.action_plugins").Return( - cmdAction, - ) - a, err := NewAnsibleCheckRunner(cfg) expectedMetaRunner := &AnsibleRunner{ Playbook: path.Join(TestAnsibleFolder, "ansible/check.yml"), Envs: map[string]string{ - "ANSIBLE_CONFIG": path.Join(TestAnsibleFolder, "ansible/ansible.cfg"), - "ANSIBLE_CALLBACK_PLUGINS": "callback", - "ANSIBLE_ACTION_PLUGINS": "action", - "ARA_API_CLIENT": "http", - "ARA_API_SERVER": "127.0.0.1", - "TRENTO_WEB_API_HOST": "127.0.0.1", - "TRENTO_WEB_API_PORT": "8000", + "ANSIBLE_CONFIG": path.Join(TestAnsibleFolder, "ansible/ansible.cfg"), + "TRENTO_WEB_API_HOST": "127.0.0.1", + "TRENTO_WEB_API_PORT": "8000", }, Check: true, } assert.NoError(t, err) assert.Equal(t, expectedMetaRunner, a) - - mockCommand.AssertExpectations(t) } diff --git a/web/alert.go b/web/alert.go index 243ff2ddb..c8e906aab 100644 --- a/web/alert.go +++ b/web/alert.go @@ -13,8 +13,7 @@ var AlertCatalogNotFound = func() Alert { return Alert{ Type: "danger", Title: "Error loading the checks catalog", - Text: "Checks catalog couldn't be retrieved. Check if the ARA service is running" + - " and the --ara-addr flag is pointing corretly to the service", + Text: "Checks catalog couldn't be retrieved", } } diff --git a/web/checks_catalog_test.go b/web/checks_catalog_test.go index 1fde9d71a..9060be96e 100644 --- a/web/checks_catalog_test.go +++ b/web/checks_catalog_test.go @@ -120,8 +120,7 @@ func TestChecksCatalogHandlerError(t *testing.T) { assert.NoError(t, err) assert.Contains(t, responseBody, "

Ooops

") - tipMsg := "Checks catalog couldn't be retrieved. Check if the ARA service is running" + - " and the --ara-addr flag is pointing corretly to the service" + tipMsg := "Checks catalog couldn't be retrieved" assert.Regexp(t, regexp.MustCompile("Error during GetChecksCatalogByGroup
"), responseBody) assert.Regexp(t, regexp.MustCompile(fmt.Sprintf("%s
", tipMsg)), responseBody) diff --git a/web/services/ara/ara.go b/web/services/ara/ara.go deleted file mode 100644 index 39a449a92..000000000 --- a/web/services/ara/ara.go +++ /dev/null @@ -1,24 +0,0 @@ -package ara - -import ( - "fmt" -) - -//go:generate mockery --name=AraService - -type AraService interface { - GetRecordList(filter string) (*RecordList, error) - GetRecord(recordId int) (*Record, error) -} - -type araService struct { - araAddr string -} - -func NewAraService(araAddr string) AraService { - return &araService{araAddr: araAddr} -} - -func (a *araService) composeQuery(handler, filter string) string { - return fmt.Sprintf("http://%s/api/v1/%s?%s", a.araAddr, handler, filter) -} diff --git a/web/services/ara/mocks/AraService.go b/web/services/ara/mocks/AraService.go deleted file mode 100644 index c61a1d106..000000000 --- a/web/services/ara/mocks/AraService.go +++ /dev/null @@ -1,59 +0,0 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. - -package mocks - -import ( - mock "github.com/stretchr/testify/mock" - ara "github.com/trento-project/trento/web/services/ara" -) - -// AraService is an autogenerated mock type for the AraService type -type AraService struct { - mock.Mock -} - -// GetRecord provides a mock function with given fields: recordId -func (_m *AraService) GetRecord(recordId int) (*ara.Record, error) { - ret := _m.Called(recordId) - - var r0 *ara.Record - if rf, ok := ret.Get(0).(func(int) *ara.Record); ok { - r0 = rf(recordId) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*ara.Record) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(int) error); ok { - r1 = rf(recordId) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} - -// GetRecordList provides a mock function with given fields: filter -func (_m *AraService) GetRecordList(filter string) (*ara.RecordList, error) { - ret := _m.Called(filter) - - var r0 *ara.RecordList - if rf, ok := ret.Get(0).(func(string) *ara.RecordList); ok { - r0 = rf(filter) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(*ara.RecordList) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(string) error); ok { - r1 = rf(filter) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} diff --git a/web/services/ara/mocksutils/GetJson.go b/web/services/ara/mocksutils/GetJson.go deleted file mode 100644 index 6a3961ae8..000000000 --- a/web/services/ara/mocksutils/GetJson.go +++ /dev/null @@ -1,33 +0,0 @@ -// Code generated by mockery v0.0.0-dev. DO NOT EDIT. - -package mocks - -import mock "github.com/stretchr/testify/mock" - -// GetJson is an autogenerated mock type for the GetJson type -type GetJson struct { - mock.Mock -} - -// Execute provides a mock function with given fields: query -func (_m *GetJson) Execute(query string) ([]byte, error) { - ret := _m.Called(query) - - var r0 []byte - if rf, ok := ret.Get(0).(func(string) []byte); ok { - r0 = rf(query) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]byte) - } - } - - var r1 error - if rf, ok := ret.Get(1).(func(string) error); ok { - r1 = rf(query) - } else { - r1 = ret.Error(1) - } - - return r0, r1 -} diff --git a/web/services/ara/records.go b/web/services/ara/records.go deleted file mode 100644 index f0336e6a3..000000000 --- a/web/services/ara/records.go +++ /dev/null @@ -1,60 +0,0 @@ -package ara - -import ( - "encoding/json" - "fmt" -) - -type RecordList struct { - Count int `json:"count,omitempty"` - Results []*RecordListResult `json:"results,omitempty"` -} - -type RecordListResult struct { - ID int `json:"id,omitempty"` - Playbook int `json:"playbook,omitempty"` - Key string `json:"key,omitempty"` - Type string `json:"type,omitempty"` -} - -type Record struct { - ID int `json:"id,omitempty"` - Value interface{} `json:"value,omitempty"` - Key string `json:"key,omitempty"` - Type string `json:"type,omitempty"` -} - -func (a *araService) GetRecordList(filter string) (*RecordList, error) { - rList := &RecordList{} - - var err error - resp, err := getJson(a.composeQuery("records", filter)) - if err != nil { - return rList, err - } - - err = json.Unmarshal(resp, rList) - if err != nil { - return rList, err - } - - return rList, nil -} - -func (a *araService) GetRecord(recordId int) (*Record, error) { - r := &Record{} - - var err error - - resp, err := getJson(a.composeQuery(fmt.Sprintf("records/%d", recordId), "")) - if err != nil { - return r, err - } - - err = json.Unmarshal(resp, r) - if err != nil { - return r, err - } - - return r, nil -} diff --git a/web/services/ara/records_test.go b/web/services/ara/records_test.go deleted file mode 100644 index e674b0103..000000000 --- a/web/services/ara/records_test.go +++ /dev/null @@ -1,161 +0,0 @@ -package ara - -import ( - "fmt" - "testing" - - "github.com/stretchr/testify/assert" - mocks "github.com/trento-project/trento/web/services/ara/mocksutils" -) - -func TestGetRecordList(t *testing.T) { - - mockGetJson := new(mocks.GetJson) - - returnJson := []byte(`{"count":3,"next":null,"previous":null,"results": - [{"id":3,"playbook":1,"created":"2021-07-20T14:21:28.281795+02:00","updated": - "2021-07-20T14:21:28.281837+02:00","key":"metadata","type":"json"}, - {"id":2,"playbook":1,"created":"2021-07-20T14:18:44.991988+02:00","updated": - "2021-07-20T14:18:44.992031+02:00","key":"metadata","type":"json"}, - {"id":1,"playbook":1,"created":"2021-07-20T14:17:02.110605+02:00", - "updated":"2021-07-20T14:17:02.110644+02:00","key":"metadata","type":"json"}]}`) - - mockGetJson.On("Execute", "http://127.0.0.1:80/api/v1/records?my_filter").Return( - returnJson, nil, - ) - - getJson = mockGetJson.Execute - - araService := NewAraService("127.0.0.1:80") - rList, err := araService.GetRecordList("my_filter") - - expectedRecordList := &RecordList{ - Count: 3, - Results: []*RecordListResult{ - &RecordListResult{ - ID: 3, - Playbook: 1, - Key: "metadata", - Type: "json", - }, - &RecordListResult{ - ID: 2, - Playbook: 1, - Key: "metadata", - Type: "json", - }, - &RecordListResult{ - ID: 1, - Playbook: 1, - Key: "metadata", - Type: "json", - }, - }, - } - - assert.Equal(t, expectedRecordList, rList) - assert.NoError(t, err) - - mockGetJson.AssertExpectations(t) -} - -func TestGetRecordListError(t *testing.T) { - - mockGetJson := new(mocks.GetJson) - - mockGetJson.On("Execute", "http://127.0.0.1:80/api/v1/records?my_filter").Return( - []byte(""), fmt.Errorf("Some error"), - ) - - getJson = mockGetJson.Execute - - araService := NewAraService("127.0.0.1:80") - rList, err := araService.GetRecordList("my_filter") - - expectedRecordList := &RecordList{} - - assert.Equal(t, expectedRecordList, rList) - assert.EqualError(t, err, "Some error") - - mockGetJson.AssertExpectations(t) -} - -func TestGetRecord(t *testing.T) { - - mockGetJson := new(mocks.GetJson) - - returnJson := []byte(`{"id":1,"playbook":{"id":230,"items":{"plays":1, - "tasks":7,"results":7,"hosts":1,"files":3,"records":1},"arguments":{}, - "labels":[{"id":1,"name":"check:False"},{"id":2,"name":"tags:all"}, - {"id":3,"name":"meta"}],"started":"2021-07-20T14:21:27.198249+02:00", - "ended":"2021-07-20T14:21:28.587075+02:00","duration":"00:00:01.388826", - "name":null,"ansible_version":"2.11.2","status":"completed","path": - "/usr/etc/trento/ansible/meta.yml","controller":"xarbulu-monitoring"}, - "value":[{"id":"1.1.1","name":"This is my test name","description": - "This is my test description","remediation":"remediation","labels":"generic", - "implementation":"my impl"}, - {"id":"1.1.2","name":"This is my test name","description": - "This is my test description","remediation":"This is my test remediation", - "labels":"generic","implementation":"impl"}],"created": - "2021-07-20T14:21:28.281795+02:00","updated":"2021-07-20T14:21:28.281837+02:00", - "key":"metadata","type":"json"}`) - - mockGetJson.On("Execute", "http://127.0.0.1:80/api/v1/records/1?").Return( - returnJson, nil, - ) - - getJson = mockGetJson.Execute - - araService := NewAraService("127.0.0.1:80") - r, err := araService.GetRecord(1) - - expectedRecord := &Record{ - ID: 1, - Value: []interface{}{ - map[string]interface{}{ - "id": "1.1.1", - "name": "This is my test name", - "description": "This is my test description", - "remediation": "remediation", - "labels": "generic", - "implementation": "my impl", - }, - map[string]interface{}{ - "id": "1.1.2", - "name": "This is my test name", - "description": "This is my test description", - "remediation": "This is my test remediation", - "labels": "generic", - "implementation": "impl", - }, - }, - Key: "metadata", - Type: "json", - } - - assert.Equal(t, expectedRecord, r) - assert.NoError(t, err) - - mockGetJson.AssertExpectations(t) -} - -func TestGetRecordError(t *testing.T) { - - mockGetJson := new(mocks.GetJson) - - mockGetJson.On("Execute", "http://127.0.0.1:80/api/v1/records/1?").Return( - []byte(""), fmt.Errorf("Some error"), - ) - - getJson = mockGetJson.Execute - - araService := NewAraService("127.0.0.1:80") - rList, err := araService.GetRecord(1) - - expectedRecord := &Record{} - - assert.Equal(t, expectedRecord, rList) - assert.EqualError(t, err, "Some error") - - mockGetJson.AssertExpectations(t) -} diff --git a/web/services/ara/utils.go b/web/services/ara/utils.go deleted file mode 100644 index 27c0e936e..000000000 --- a/web/services/ara/utils.go +++ /dev/null @@ -1,26 +0,0 @@ -package ara - -import ( - "io" - "net/http" -) - -//go:generate mockery --name=GetJson --output mocksutils - -type GetJson func(query string) ([]byte, error) - -var getJson GetJson = func(query string) ([]byte, error) { - var err error - resp, err := http.Get(query) - if err != nil { - return nil, err - } - - defer resp.Body.Close() - body, err := io.ReadAll(resp.Body) - if err != nil { - return nil, err - } - - return body, nil -}