wborn / openhab-grafana

JavaScript utilities and examples for using Grafana with openHAB
MIT License
46 stars 13 forks source link

Openhab + Grafana + Reverseproxy in Docker #16

Closed matthijsvisser closed 4 years ago

matthijsvisser commented 4 years ago

Hi,

I'm really struggling to get the Grafana webview panels to work with Openhab and Grafana behind a reverse proxy in Docker.

I get the following errors (in the Firefox console):

SecurityError: Permission denied to get property "href" on cross-origin object smarthome-grafana.js:350

TypeError: smartHomeSubscriber is undefined

Docker-compose file:

version: '2'

services:
   reverseproxy:
     image: reverseproxy
     ports:
     # openhab
     - 5001:5001
     restart: always

   openhab:
     depends_on:
     - reverseproxy
     image: "openhab/openhab:2.4.0"
     restart: always
     ports:
     - "8080:8080"
     - "8443:8443"
     volumes:
     - "/etc/localtime:/etc/localtime:ro"
     - "/etc/timezone:/etc/timezone:ro"
     - "/opt/openhab/addons:/openhab/addons"
     - "/opt/openhab/conf:/openhab/conf"
     - "/opt/openhab/userdata:/openhab/userdata"
     environment:
       OPENHAB_HTTP_PORT: "8080"
       OPENHAB_HTTPS_PORT: "8443"
       EXTRA_JAVA_OPTS: "-Duser.timezone=Europe/Berlin"
       USER_ID: "999"
       GROUP_ID: "999"

   grafana:
     depends_on:
     - reverseproxy
     image: grafana/grafana:latest
     ports:
       - '3000:3000'
     volumes:
       - grafana-storage:/var/lib/grafana
       - grafana-conf:/etc/grafana
     environment:
       - GF_SECURITY_ADMIN_USER=${GRAFANA_USERNAME}
       - GF_SECURITY_ADMIN_PASSWORD=${GRAFANA_PASSWORD}
       - GF_AUTH_ANONYMOUS_ENABLED=true
       - GF_USERS_ALLOW_SIGN_UP=false
       - GF_SERVER_ROOT_URL=http://localhost/grafana/
       - GF_ALLOW_EMBEDDING=true
volumes:
  grafana-storage:
    driver: local
  grafana-conf:
    driver: local

nginx.conf:

worker_processes 1;

events { worker_connections 1024; }

http {

    sendfile on;

    upstream docker-openhab {
        server openhab:8080;
#       server 20.0.0.210:8080;
    }

    server {
        listen 5001;
        server_name     localhost;
        location / {
#           auth_basic           "Restricted Access!";
#           auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
            proxy_pass           http://docker-openhab;
#           proxy_pass           http://localhost:8080;
#           proxy_redirect       off;
            proxy_buffering      off;
            proxy_set_header     Host $http_host;
#           proxy_set_header     Host $host;
            proxy_set_header     X-Real-IP $remote_addr;
            proxy_set_header     X-Forwarded-For $proxy_add_x_forwarded_for;
#           proxy_set_header     X-Forwarded-Host $server_name;
            proxy_set_header     X-Forwarded-Proto $scheme;
#           add_header           X-Frame-Options  ALLOWALL;
#           add_header         X-Frame-Options SAMEORIGIN;
        }

        location /grafana/ {
            proxy_pass         http://20.0.0.210:3000/;
#           proxy_pass         http://docker-grafana/;
#           add_header         X-Frame-Options ALLOWALL;
#           add_header         X-Frame-Options SAMEORIGIN;
        }
    }
}

sitemap:

sitemap default label="test"
{
Webview height=9 icon=none 
url="http://20.0.0.210:5001/static/combo-300px.html?dashboardItem=gf_dashboard&fromItem=temp_view&panelItem=gf_panel_temperature&w=000000"
}
// the prefix that is used for each Grafana panel URL
SMARTHOME_GRAFANA_DEFAULTS["urlPrefix"] = "http://20.0.0.210:5001/grafana";

Openhab can be accessed at: http://20.0.0.210:5001 Grafana at: http://20.0.0.210:5001/grafana

Your help is much appreciated, thank you!

wborn commented 4 years ago

It should be possible to get it all working with Docker because I use a similar setup.

Since both openHAB and Grafana seem to work correctly on the same port, maybe the library doesn't generate the correct Webview URLs? You can check the generated URLs by enabling the debug mode by appending &debug=true to your browser URL. Firefox might also show the blocked URL requests in the debug view Network panel.

matthijsvisser commented 4 years ago

Hi,

Thanks for the quick reply. I managed to get rid of the errors, but an other one showed up:

Load denied by X-Frame-Options: http://20.0.0.210:5001/grafana/d-solo/FydmKUdZz/home?from=now-1y&to=now-1d&panelId=33&theme=light does not permit framing.

This suggests that the Docker environment variable 'GF_ALLOW_EMBEDDING=true' for Grafana is not set? But I did, as shown above.

I've cloned your repository again and started with a minimal configuration. Only the static panel is shown on the sitemap and debug mode is on. The URL is correctly generated and works when accessing it in another tab:

http://20.0.0.210:5001/grafana/dashboard-solo/db/home?from=now-1y&to=now-1d&panelId=33&theme=light

The URL in the error message also works outside openhab. http://20.0.0.210:5001/grafana/d-solo/FydmKUdZz/home?from=now-1y&to=now-1d&panelId=33&theme=light

Any ideas?

wborn commented 4 years ago

Looks like the section name is missing from your allow embedding variable, see: https://grafana.com/docs/installation/configuration/#using-environment-variables

So maybe it works when you use GF_SECURITY_ALLOW_EMBEDDING instead?

I see it's also being used like that in the community.

I'm still using an older Grafana version myself. But it's good to know I need to add this environment variable while upgrading.

matthijsvisser commented 4 years ago

I can't believe that I've missed that..! Got everything working now, really appreciate the help!

wborn commented 4 years ago

That's great news! :-)