Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Merge branch 'staging'
Browse files Browse the repository at this point in the history
  • Loading branch information
tifayuki committed May 18, 2016
2 parents 526e7cd + 8494e4e commit ac5e672
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dockercloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from dockercloud.api.events import Events
from dockercloud.api.nodeaz import AZ

__version__ = '1.0.4'
__version__ = '1.0.5'

dockercloud_auth = os.environ.get('DOCKERCLOUD_AUTH')
basic_auth = auth.load_from_file("~/.docker/config.json")
Expand Down
2 changes: 1 addition & 1 deletion dockercloud/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def _ws_init(self, url):
header = {'User-Agent': user_agent}
header.update(dockercloud.auth.get_auth_header())
self.header = [": ".join([key, value]) for key, value in header.items()]
logger.info("websocket: %s %s" % (self.url, self.header))
logger.info("Websocket: %s %s" % (self.url, self.header))
self.open_handler = None
self.message_handler = None
self.error_handler = None
Expand Down
21 changes: 15 additions & 6 deletions dockercloud/api/events.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
from __future__ import absolute_import

import json
import logging

import websocket

import dockercloud
from .base import StreamingAPI
from .exceptions import AuthError

logger = logging.getLogger("python-dockercloud")


class Events(StreamingAPI):
def __init__(self):
endpoint = "events"
url = "/".join([dockercloud.stream_host.rstrip("/"), "api", "audit", self._api_version, endpoint.lstrip("/")])
self.invaid_auth_headers = set()
self.auth_error = ""
super(self.__class__, self).__init__(url)

def _on_message(self, ws, message):
logger.info("Websocket Message: %s" % message)
try:
event = json.loads(message)
except ValueError:
return

if event.get("type") == "error" and event.get("data", {}).get("errorMessage") == "UNAUTHORIZED":
self.auth_error = True
raise AuthError("Not authorized")
if event.get("type") == "auth":
return

if self.message_handler:
self.message_handler(message)

def _on_error(self, ws, e):
if isinstance(e, websocket._exceptions.WebSocketBadStatusException) and getattr(e, "status_code") == 401:
self.auth_error = "Not Authorized"
self.invaid_auth_headers.add(str(dockercloud.auth.get_auth_header()))

super(self.__class__, self)._on_error(ws, e)

def run_forever(self, *args, **kwargs):
while True:
if self.auth_error:
raise AuthError("Not authorized")
if str(dockercloud.auth.get_auth_header()) in self.invaid_auth_headers:
raise AuthError(self.auth_error)
ws = websocket.WebSocketApp(self.url, header=self.header,
on_open=self._on_open,
on_message=self._on_message,
Expand Down

0 comments on commit ac5e672

Please sign in to comment.