Skip to content

Commit

Permalink
OpenAPI: Fix API calls pointing to subpath
Browse files Browse the repository at this point in the history
Closes #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
  • Loading branch information
julienduchesne committed Nov 9, 2023
1 parent 9b479bf commit 5eba6e5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
10 changes: 5 additions & 5 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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}
Expand Down
12 changes: 11 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
6 changes: 5 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)),
Expand Down
34 changes: 34 additions & 0 deletions testdata/nginx.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 5eba6e5

Please sign in to comment.