mahendrapaipuri / grafana-dashboard-reporter-app

A Grafana plugin app to create PDF reports of dashboards
Apache License 2.0
47 stars 4 forks source link

Error 401 while generating a report using grafana API #26

Closed Alan1810 closed 5 months ago

Alan1810 commented 5 months ago

Hi @mahendrapaipuri,

I'm trying to generate a report using grafana api, I have "Status 401: Unauthorized".

$ curl -H "Authorization: Bearer <my_token>" -X GET -k "https://xxxxx/api/dashboards/uid/iLdsyDbWz/permissions" | jq
[
  {
    "dashboardId": 190,
    "folderId": 168,
    "created": "2024-04-25T13:55:49+02:00",
    "updated": "2024-04-25T13:55:49+02:00",
    "userId": 99,
    "userLogin": "sa-1-mahen-reporter",
    "userEmail": "sa-1-mahen-reporter",
    "userAvatarUrl": "/avatar/f8a33429582f8dcd39b77cf6d295a9ab",
    "teamId": 0,
    "teamEmail": "",
    "teamAvatarUrl": "",
    "team": "",
    "permission": 1,
    "permissionName": "View",
    "uid": "iLdsyDbWz",
    "title": "HYP - Serveurs par zone",
    "slug": "hyp-serveurs-par-zone",
    "isFolder": false,
    "url": "/d/iLdsyDbWz/hyp-serveurs-par-zone",
    "inherited": false
  }
]

[ "dashboards:", "dashboards:", "folders:", "folders:", "dashboards:uid:iLdsyDbWz" ]


- Using grafana API

$ curl -k -o report.pdf -H "Authorization: Bearer " "https://xxxxx/api/plugins/mahendrapaipuri-dashboardreporter-app/resources/report?dashUid=iLdsyDbWz&from=now-1M%2FM&to=now-1M%2FM&var-datasource=xxx&var-zone=xxx&layout=grid&orientation=landscape" -v

logger=context userId=0 orgId=0 uname= t=2024-04-25T15:23:41.44301195+02:00 level=info msg= error="[auth.unauthorized] cannot authenticate request" remote_addr=127.0.0.1 traceID=
logger=context userId=0 orgId=0 uname= t=2024-04-25T15:23:41.443934498+02:00 level=info msg="Request Completed" method=GET path=/api/dashboards/uid/iLdsyDbWz status=401 remote_addr=127.0.0.1 time_ms=0 duration=990.814µs size=102 referer= handler=/api/dashboards/uid/:uid status_source=server
logger=plugin.mahendrapaipuri-dashboardreporter-app t=2024-04-25T15:23:41.44462634+02:00 level=error msg="error generating report" endpoint=callResource err="error fetching dashboard iLdsyDbWz: error obtaining dashboard from https://localhost:3000/api/dashboards/uid/iLdsyDbWz?var-datasource=xxx&var-zone=xxx. Got Status 401 Unauthorized, message: {\"extra\":null,\"message\":\"Unauthorized\",\"messageId\":\"auth.unauthorized\",\"statusCode\":401,\"traceID\":\"\"}\n " pluginID=mahendrapaipuri-dashboardreporter-app
logger=context userId=99 orgId=1 uname=sa-1-mahen-reporter t=2024-04-25T15:23:41.44510429+02:00 level=error msg="Request Completed" method=GET path=/api/plugins/mahendrapaipuri-dashboardreporter-app/resources/report status=500 remote_addr=10.12.240.232 time_ms=45 duration=45.014819ms size=24 referer= handler=/api/plugins/:pluginId/resources/* status_source=downstream

Any solutions ?

Best regards


grafana open source 10.4.2 reporter 1.2.0 Ubuntu 22.04

mahendrapaipuri commented 5 months ago

@Alan1810 How many organizations you have in your Grafana deployment? It seems to me that you have your service account created in orgId=1 and that account is making a API request to generate a report of a dashboard in orgId=0. Could you try adding orgId=1 to the query parameters for the report API?

Cheers

Alan1810 commented 5 months ago

Hi,

We only have one org with Id=1.

I've added orgId=1 to the query parameters for the report API but i have the same error :

logger=context userId=0 orgId=0 uname= t=2024-04-25T18:29:16.474621771+02:00 level=info msg= error="[auth.unauthorized] cannot authenticate request" remote_addr=127.0.0.1 traceID=
logger=context userId=0 orgId=0 uname= t=2024-04-25T18:29:16.475739121+02:00 level=info msg="Request Completed" method=GET path=/api/dashboards/uid/iLdsyDbWz status=401 remote_addr=127.0.0.1 time_ms=1 duration=1.155599ms size=102 referer= handler=/api/dashboards/uid/:uid status_source=server
logger=plugin.mahendrapaipuri-dashboardreporter-app t=2024-04-25T18:29:16.476427808+02:00 level=error msg="error generating report" endpoint=callResource err="error fetching dashboard iLdsyDbWz: error obtaining dashboard from https://localhost:3000/api/dashboards/uid/iLdsyDbWz?var-datasource=xxx&var-zone=xxx. Got Status 401 Unauthorized, message: {\"extra\":null,\"message\":\"Unauthorized\",\"messageId\":\"auth.unauthorized\",\"statusCode\":401,\"traceID\":\"\"}\n " pluginID=mahendrapaipuri-dashboardreporter-app
logger=context userId=99 orgId=1 uname=sa-1-mahen-reporter t=2024-04-25T18:29:16.47720817+02:00 level=error msg="Request Completed" method=GET path=/api/plugins/mahendrapaipuri-dashboardreporter-app/resources/report status=500 remote_addr=10.12.240.232 time_ms=63 duration=63.30165ms size=24 referer= handler=/api/plugins/:pluginId/resources/* status_source=downstream

Best regards

mahendrapaipuri commented 5 months ago

Cheers @Alan1810 for the info.

Yes, I can reproduce the behaviour with newer Grafana. I have already tested a potential solution but I would like to investigate it more on what is the "recommended" way to do this. I will get back to you ASAP.

Alan1810 commented 5 months ago

Fine.

Thanks

mahendrapaipuri commented 5 months ago

Hello @Alan1810, could you please try this patch? You can download the plugin artifacts from here. The service account token must be configured for the plugin as well as stated in docs. If you are using grafana >= 10.3.0, you can use externalServiceAccounts feature which is stated in docs as well.

Alan1810 commented 5 months ago

Hi @mahendrapaipuri,

The patch works fine.

Thank you for you work.

Regards