demisto / demisto-py

Demisto Client for Python
Apache License 2.0
70 stars 41 forks source link

Add custom headers? #103

Closed fancybear-dev closed 1 year ago

fancybear-dev commented 1 year ago

It is not a bug, but a question / feature request.

Is it possible to add custom headers to all requests easily?

We have xsoar servers that are behind a proxy - that works just fine, except that the demisto-sdk (which uses this client) does not support custom headers. We need an additional header to pass through the proxy. Is this possible? If not, any suggestions on a quick fix?

dorschw commented 1 year ago

Hi, thanks for the suggestion. This has been added to our internal task queue as CIAC-4509.

Code contributions are welcome! Feel free to fork the repo, implement it and open a pull request, so others can enjoy it as well.

I would add an environment variable, and if it's not empty, send it in each request.

fancybear-dev commented 1 year ago

For those interested, I solved the issue that day by adding this to our GitHub action. It was the most pragmatic / least hacky solution I could come up with. As the client is updated barely, this should actually work for quite a while. It injects lines at certain lines using sed.

    - name: Fix demisto-py client for proxy headers
      run: |
        sed -i "79i\        # Set default proxy header.\n        self.proxy_header = 'Bearer ${{ steps.auth.outputs.id_token }}'" $LD_LIBRARY_PATH/python3.10/site-packages/demisto_client/demisto_api/api_client.py
        sed -i "92i\ \n    @property\n    def proxy_header(self):\n        \"\"\"Proxy header for this API client\"\"\"\n        return self.default_headers['Proxy-Authorization']\n\n    @proxy_header.setter\n    def proxy_header(self, value):\n        self.default_headers['Proxy-Authorization'] = value" $LD_LIBRARY_PATH/python3.10/site-packages/demisto_client/demisto_api/api_client.py
        cat $LD_LIBRARY_PATH/python3.10/site-packages/demisto_client/demisto_api/api_client.py

This could almost be ported 1:1 with the proxy_header being pulled from the os env. To keep it simple, only use it if os env is available - if not just ignore. So you don't need to pass all kinds of parameters everywhere to specify proxy usage. If the os env is set, you already want to use the proxy. Implicit > explicit.

edit; just saw that what I said in the last part - is also what you suggested. So read it as; I agree ;).

dorschw commented 1 year ago

Glad you found a solution!

Implementing this issue as part of the SDK/Demisto-py isn't prioritized at the moment. Please contact XSOAR Customer Success, or open a feature request on Aha!.