Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate dialogflow over to the new webhook component #17804

Merged
merged 5 commits into from Oct 28, 2018

Conversation

rohankapoorcom
Copy link
Member

@rohankapoorcom rohankapoorcom commented Oct 26, 2018

Description:

Migrating Dialogflow over to the webhook API so that it doesn't need an api password.

Breaking Change: Each instance of Home Assistant will now generate it's own unique webhook url for Dialogflow to use. One will need to be generated and provided to Dialogflow via the webhook integration to replace the existing url: https://myhome.duckdns.org/api/dialogflow?api_password=HA_PASSWORD

I followed the approach used in #16817 to implement a new config flow to register the webhook. Existing configuration (account_sid, auth_token) still occurs via the configuration.yaml file.

I do not actually use dialogflow myself so I cannot test the behavior with Dialogflow , but playing with the request JSON looks okay and all unit tests are passing.

Related issue (if applicable): Related to #15376

Pull request in home-assistant.io with documentation (if applicable): home-assistant/home-assistant.io#7109

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@@ -40,7 +40,8 @@ def context(self, request):

return Context(user_id=user.id)

def json(self, result, status_code=200, headers=None):
@staticmethod
def json(result, status_code=200, headers=None):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to expose this and the json_message as static methods so that they could be accessed by things that don't implement HomeAssistantView. Not sure if that is acceptable or if it would be preferred to move them out of the class (within the same module)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not do this. This is conflation of concerns.

Webhooks should work without any http component related things.

@rohankapoorcom
Copy link
Member Author

This is a Breaking Change


try:
response = await async_handle_message(hass, message)
return b'' if response is None else HomeAssistantView.json(response)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that it's a webhook when the caller expects a response.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forget about the comment above.

However, we should not use HomeAssistantView but instead use aiohttp directly.

from aiohttp import web

return web.json_response(…)

Docs

@balloob
Copy link
Member

balloob commented Oct 26, 2018

https://dialogflow.com/docs/fulfillment/configure

DialogFlow supports setting headers so I suggest DialogFlow just uses an authenticated webview with a long lived access token.

@rohankapoorcom
Copy link
Member Author

Makes sense. I'm going to convert this pull request over to support that then (and keep the async test changes)

@balloob
Copy link
Member

balloob commented Oct 26, 2018

We can still use a config entry to tell people what url to use. We might even want to generate a new config flow helper for that?

@rohankapoorcom
Copy link
Member Author

Sorry, I'm a little lost. Wouldn't they just be using the existing url? /api/dialogflow?

Or are you suggesting that we use the config entry to show them that and generate them a long lived access token? In that case I agree, a new config flow helper is probably a good idea.

I have one concern with using long lived tokens for things like this. They allow access to everything right? There's no scoping like the webhook component provides. Not a huge fan of using that here for security reasons if that's the correct understanding. If Dialog Flow is comprised, then a lot more damage can be done.

@balloob
Copy link
Member

balloob commented Oct 26, 2018

that is a good point. We could use a simliar strategy for generating a url but still have that be registered as a webview, ie /api/dialogflow/zxcljkdsjlkdsljkcxljkq12312

@rohankapoorcom
Copy link
Member Author

Yeah that makes sense. Couple of other thoughts here:

I'm not a huge fan of exposing the purpose of the webview and would prefer if the component name wasn't inside the url.

For people whitelisting specific urls to be publicly accessible (for example, I require client certificates for auth on the reverse proxy and whitelist /api/webhook/*), this makes a bunch of additional paths (1 per component with a webview). Are you okay with creating a generalized component (similar in construction to the webhook) to be used for all these things that need authenticated webviews.

We could call the component authenticated_webview and then build the urls similarly to the webhook component webview/zxcljkdsjlkdsljkcxljkq12312

@balloob
Copy link
Member

balloob commented Oct 26, 2018

@rohankapoorcom I've been researching webhooks, and I think that you're right, we should allow responses. So let's stick with a webhook for dialog flow.

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

Traceback (most recent call last):
Traceback (most recent call last):
  File "/home/linters/.local/bin/flake8", line 7, in 
    from flake8.main.cli import main
ModuleNotFoundError: No module named 'flake8'

@rohankapoorcom
Copy link
Member Author

@balloob thanks for the feedback. Changes are committed and pushed up.

@balloob balloob merged commit 60080a5 into home-assistant:dev Oct 28, 2018
@balloob
Copy link
Member

balloob commented Oct 28, 2018

Nice!

@ghost ghost removed the in progress label Oct 28, 2018
@rohankapoorcom rohankapoorcom deleted the dialogflow-webhooks branch October 28, 2018 18:31
@balloob balloob mentioned this pull request Nov 9, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants