From 5eba6e52c63e6ff4fac403b32e95476daa181009 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 9 Nov 2023 13:32:00 -0500 Subject: [PATCH] OpenAPI: Fix API calls pointing to subpath Closes https://github.com/grafana/terraform-provider-grafana/issues/1137 Currently, the test suite is pointing to a subpath BUT Grafana redirects from the root to the subpath The provider is badly configured but the tests still pass because of the redirect To fix the test suite, I add a nginx proxy in this PR, so that calling the host directly fails and you have to go through the subpath --- GNUmakefile | 10 +++++----- docker-compose.yml | 12 +++++++++++- internal/provider/provider.go | 6 +++++- testdata/nginx.conf | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 testdata/nginx.conf diff --git a/GNUmakefile b/GNUmakefile index 8acab0ee5..6475e8e90 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -41,10 +41,10 @@ testacc-enterprise-docker: testacc-tls-docker: make -C testdata generate - GRAFANA_VERSION=$(GRAFANA_VERSION) docker compose --profile tls up --force-recreate --detach --remove-orphans --wait + TEST_PORT=3001 GRAFANA_VERSION=$(GRAFANA_VERSION) docker compose --profile tls up --force-recreate --detach --remove-orphans --wait GRAFANA_VERSION=$(GRAFANA_VERSION) \ - GRAFANA_URL="http://0.0.0.0:3000" \ + GRAFANA_URL="https://0.0.0.0:3001" \ GRAFANA_AUTH="admin:admin" \ GRAFANA_TLS_KEY=$$(pwd)/testdata/client.key \ GRAFANA_TLS_CERT=$$(pwd)/testdata/client.crt \ @@ -54,14 +54,14 @@ testacc-tls-docker: docker compose --profile tls down testacc-subpath-docker: - GRAFANA_VERSION=$(GRAFANA_VERSION) GRAFANA_SUBPATH=/grafana/ GF_SERVER_SERVE_FROM_SUB_PATH=true docker compose up --force-recreate --detach --remove-orphans --wait + TEST_PORT=3001 GRAFANA_VERSION=$(GRAFANA_VERSION) GRAFANA_SUBPATH=/grafana/ GF_SERVER_SERVE_FROM_SUB_PATH=true docker compose --profile proxy up --force-recreate --detach --remove-orphans --wait GRAFANA_VERSION=$(GRAFANA_VERSION) \ - GRAFANA_URL="http://0.0.0.0:3000/grafana" \ + GRAFANA_URL="http://0.0.0.0:3001/grafana" \ GRAFANA_AUTH="admin:admin" \ make testacc-oss - docker compose down + docker compose --profile proxy down release: @test $${RELEASE_VERSION?Please set environment variable RELEASE_VERSION} diff --git a/docker-compose.yml b/docker-compose.yml index ce4629575..ad5aef620 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,7 +5,7 @@ services: - 3000:3000 image: ${GRAFANA_IMAGE:-grafana/grafana}:${GRAFANA_VERSION} environment: - - GF_SERVER_ROOT_URL=http://0.0.0.0:3000${GRAFANA_SUBPATH:-} + - GF_SERVER_ROOT_URL=http://0.0.0.0:${TEST_PORT:-3000}${GRAFANA_SUBPATH:-} - GF_ENTERPRISE_LICENSE_TEXT=${GF_ENTERPRISE_LICENSE_TEXT:-} - GF_SERVER_SERVE_FROM_SUB_PATH=${GF_SERVER_SERVE_FROM_SUB_PATH:-} healthcheck: @@ -32,3 +32,13 @@ services: - ./testdata:/certs ports: - 3001:3001 + nginx: + profiles: + - "proxy" + depends_on: + - grafana + image: nginx:latest + ports: + - 3001:3001 + volumes: + - ./testdata/nginx.conf:/etc/nginx/nginx.conf diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 07302bc3a..7d3fb677b 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -430,6 +430,10 @@ func createGrafanaOAPIClient(apiURL string, d *schema.ResourceData) (*goapi.Graf if err != nil { return nil, fmt.Errorf("failed to parse API url: %v", err.Error()) } + apiPath, err := url.JoinPath(u.Path, "api") + if err != nil { + return nil, fmt.Errorf("failed to join API path: %v", err.Error()) + } userInfo, orgID, APIKey, err := parseAuth(d) if err != nil { @@ -438,7 +442,7 @@ func createGrafanaOAPIClient(apiURL string, d *schema.ResourceData) (*goapi.Graf cfg := goapi.TransportConfig{ Host: u.Host, - BasePath: "/api", + BasePath: apiPath, Schemes: []string{u.Scheme}, NumRetries: d.Get("retries").(int), RetryTimeout: time.Second * time.Duration(d.Get("retry_wait").(int)), diff --git a/testdata/nginx.conf b/testdata/nginx.conf new file mode 100644 index 000000000..c581f8732 --- /dev/null +++ b/testdata/nginx.conf @@ -0,0 +1,34 @@ +events{} + +http { + # this is required to proxy Grafana Live WebSocket connections. + map $http_upgrade $connection_upgrade { + default upgrade; + '' close; + } + + upstream grafana { + server grafana:3000; + } + + server { + listen 3001; + root /usr/share/nginx/html; + index index.html index.htm; + server_name 0.0.0.0; + + location /grafana/ { + proxy_set_header Host $host; + proxy_pass http://grafana; + } + + # Proxy Grafana Live WebSocket connections. + location /api/live/ { + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection $connection_upgrade; + proxy_set_header Host $host; + proxy_pass http://grafana; + } + } +}