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

Don't keep fetching snitun token when sub expired #168

Merged
merged 1 commit into from
Jun 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions hass_nabucasa/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class RemoteNotConnected(RemoteError):
"""Raise if a request need connection and we are not ready."""


class SubscriptionExpired(RemoteError):
"""Raise if we cannot connect because subscription expired."""


@attr.s
class SniTunToken:
"""Handle snitun token."""
Expand Down Expand Up @@ -241,13 +245,17 @@ async def _refresh_snitun_token(self) -> None:
_LOGGER.debug("Don't need refresh snitun token")
return

if self.cloud.subscription_expired:
raise SubscriptionExpired()

# Generate session token
aes_key, aes_iv = generate_aes_keyset()
try:
async with async_timeout.timeout(30):
resp = await cloud_api.async_remote_token(self.cloud, aes_key, aes_iv)
assert resp.status == 200
except (asyncio.TimeoutError, AssertionError):
if resp.status != 200:
raise RemoteBackendError()
except asyncio.TimeoutError:
raise RemoteBackendError() from None

data = await resp.json()
Expand Down Expand Up @@ -283,6 +291,8 @@ async def connect(self) -> None:
_LOGGER.error("Connection problem to snitun server")
except RemoteBackendError:
_LOGGER.error("Can't refresh the snitun token")
except SubscriptionExpired:
pass
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we communicate this state some other way to interested parties?

Copy link
Member Author

Choose a reason for hiding this comment

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

No. We just have this property which is updated once an hour when we update auth tokens. Future PR we can add some logic for responding to that.

except AttributeError:
pass # Ignore because HA shutdown on snitun token refresh
finally:
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def _executor(call, *args):
def auth_cloud_mock(cloud_mock):
"""Return an authenticated cloud instance."""
cloud_mock.auth.async_check_token.side_effect = mock_coro
cloud_mock.subscription_expired = False
return cloud_mock


Expand Down
10 changes: 9 additions & 1 deletion tests/test_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
DISPATCH_REMOTE_CONNECT,
DISPATCH_REMOTE_DISCONNECT,
)
from hass_nabucasa.remote import RemoteUI
from hass_nabucasa.remote import RemoteUI, SubscriptionExpired
from hass_nabucasa.utils import utcnow

from .common import MockAcme, MockSnitun, mock_coro
Expand Down Expand Up @@ -468,3 +468,11 @@ async def test_certificate_task_renew_cert(
await remote.load_backend()
await asyncio.sleep(0.1)
assert acme_mock.call_issue


async def test_refresh_token_no_sub(auth_cloud_mock):
"""Test that we rais SubscriptionExpired if expired sub."""
auth_cloud_mock.subscription_expired = True

with pytest.raises(SubscriptionExpired):
await RemoteUI(auth_cloud_mock)._refresh_snitun_token()