How to use secureJsonData in grafana plugin ?

As mentioned, I cannot get the data of secureJsonData in the code. Can anyone kindly help me?

Hi @luketing2001 if you are expecting to, for example, print the values from the secureJsonData in the browser console, you won’t. Those secrets are never passed along to plugins directly or sent to the browser and you won’t be able to use them directly. This is different in the backend though, if you are writing a backend plugin you can decrypt the value from the secureJsonData and use the value directly in the code as you see need it.

In frontend plugins instead you should use the plugin defined routes. When you define a route you specify in the url and attributes, placeholders that grafana will replace with the values from the secureJsonData.

Here’s a more complete guide on how to work with authentication in a datasource plugin which is where more often you need to use secureJsonData.

Could you also explain a bit of what you are trying to do and what kind of plugin you are developing?

Thank you for your response! I am developing a Grafana app plugin. I have already tried the method you mentioned about adding a proxy route to my plugin, and I’ve added a route in the plugin.json .

  "routes": [
    {
      "path": "token",
      "method": "POST",
      "url": "{{ .SecureJsonData.tokenUrl}}",
        "urlParams": [
              {
                "name": "apiKey",
                "content": "{{ .SecureJsonData.apiKey }}"
              }
            ],
      "headers": [
        {
          "name": "Content-Type",
          "content": "application/x-www-form-urlencoded {{ .SecureJsonData.apiKey }}"
        }
      ]
    }
  ]

However, what puzzles me is that while the SecureJsonData is successfully added to the request headers, the urlParams do not seem to be effective.And what I am hoping to achieve is to add the contents of SecureJsonData into the request parameters.

@luketing2001 your code looks correct. Can you tell me which (exact) grafana version are you using to develop this plugin? how are you running it? (docker, built from source code, binary, etc…) and if possible, could you share your plugin’s source code with me?

@academo I am currently using Grafana version v10.1.4, running it directly from the binary. I am planning to open source my plugin code. As soon as the code is open-sourced, I will share the link immediately.

@luketing2001 let me know when we can take a look the code, or if you can reproduce this issue with a minimal code setup in a repository you can share we can take a look to see why it is not working.