Beispiel #1
0
def call_fip_worker_leave(fip, lip):
    requests_unixsocket.monkeypatch()
    payload = {'FloatingIP': fip, 'LocalIP': lip}
    r = requests.post(URL_NETWORK_DRIVER_LEAVE, data=json.dumps(payload))
    if r.status_code != 200:
        return False
    return True
Beispiel #2
0
 def __init__(
     self,
     fps: int,
     width: int,
     height: int,
     codec: str,
     scale_factor: float,
     no_background: bool,
     background_blur: int,
     background_keep_aspect: bool,
     use_foreground: bool,
     hologram: bool,
     tiling: bool,
     bodypix_url: str,
     socket: str,
     background_image: str,
     foreground_image: str,
     foreground_mask_image: str,
     webcam_path: str,
     v4l2loopback_path: str,
     use_akvcam: bool,
 ) -> None:
     self.no_background = no_background
     self.use_foreground = use_foreground
     self.hologram = hologram
     self.tiling = tiling
     self.background_blur = background_blur
     self.background_keep_aspect = background_keep_aspect
     self.background_image = background_image
     self.foreground_image = foreground_image
     self.foreground_mask_image = foreground_mask_image
     self.scale_factor = scale_factor
     self.real_cam = RealCam(webcam_path, width, height, fps, codec)
     # In case the real webcam does not support the requested mode.
     self.width = self.real_cam.get_frame_width()
     self.height = self.real_cam.get_frame_height()
     self.use_akvcam = use_akvcam
     if not use_akvcam:
         self.fake_cam = pyfakewebcam.FakeWebcam(v4l2loopback_path,
                                                 self.width, self.height)
     else:
         self.fake_cam = AkvCameraWriter(v4l2loopback_path, self.width,
                                         self.height)
     self.foreground_mask = None
     self.inverted_foreground_mask = None
     self.session = requests.Session()
     if bodypix_url.startswith("/"):
         print("Looks like you want to use a unix socket")
         # self.session = requests_unixsocket.Session()
         self.bodypix_url = "http+unix:/" + bodypix_url
         self.socket = bodypix_url
         requests_unixsocket.monkeypatch()
     else:
         self.bodypix_url = bodypix_url
         self.socket = ""
         # self.session = requests.Session()
     self.images: Dict[str, Any] = {}
     self.image_lock = asyncio.Lock()
def test_unix_domain_adapter_monkeypatch():
    with UnixSocketServerThread() as usock_thread:
        with requests_unixsocket.monkeypatch('http+unix://'):
            urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
            url = 'http+unix://%s/path/to/page' % urlencoded_usock

            for method in ['get', 'post', 'head', 'patch', 'put', 'delete',
                           'options']:
                logger.debug('Calling session.%s(%r) ...', method, url)
                r = getattr(requests, method)(url)
                logger.debug(
                    'Received response: %r with text: %r and headers: %r',
                    r, r.text, r.headers)
                assert r.status_code == 200
                assert r.headers['server'] == 'waitress'
                assert r.headers['X-Transport'] == 'unix domain socket'
                assert r.headers['X-Requested-Path'] == '/path/to/page'
                assert r.headers['X-Socket-Path'] == usock_thread.usock
                assert isinstance(r.connection,
                                  requests_unixsocket.UnixAdapter)
                assert r.url == url
                if method == 'head':
                    assert r.text == ''
                else:
                    assert r.text == 'Hello world!'

    for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
        with pytest.raises(requests.exceptions.InvalidSchema):
            getattr(requests, method)(url)
 def aptly_call(self, path):
     """Runs a GET request on aptly using the given path."""
     endpoint = self.config['api']['endpoint']
     with requests_unixsocket.monkeypatch():
         url = f'{endpoint}/{path}'
         r = requests.get(url)
         return r.json()
Beispiel #5
0
def test_peercreds_unix_sock_with_lookup(peercreds_enabled_server_and_client):
    """Check that ``PEERCRED`` resolution works when enabled."""
    httpserver, testclient = peercreds_enabled_server_and_client
    httpserver.peercreds_resolve_enabled = True

    bind_addr = httpserver.bind_addr

    if isinstance(bind_addr, six.binary_type):
        bind_addr = bind_addr.decode()

    unix_base_uri = 'http+unix://{}'.format(
        bind_addr.replace('\0', '%00').replace('/', '%2F'),
    )

    import grp
    import pwd
    expected_textcreds = (
        pwd.getpwuid(os.getuid()).pw_name,
        grp.getgrgid(os.getgid()).gr_name,
    )
    expected_textcreds = '!'.join(map(str, expected_textcreds))
    with requests_unixsocket.monkeypatch():
        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        peercreds_text_resp.raise_for_status()
        assert peercreds_text_resp.text == expected_textcreds
Beispiel #6
0
def test_peercreds_unix_sock_with_lookup(peercreds_enabled_server):
    """Check that ``PEERCRED`` resolution works when enabled."""
    httpserver = peercreds_enabled_server
    httpserver.peercreds_resolve_enabled = True

    bind_addr = httpserver.bind_addr

    if isinstance(bind_addr, six.binary_type):
        bind_addr = bind_addr.decode()

    # pylint: disable=possibly-unused-variable
    quoted = urllib.parse.quote(bind_addr, safe='')
    unix_base_uri = 'http+unix://{quoted}'.format(**locals())

    import grp
    import pwd
    expected_textcreds = (
        pwd.getpwuid(os.getuid()).pw_name,
        grp.getgrgid(os.getgid()).gr_name,
    )
    expected_textcreds = '!'.join(map(str, expected_textcreds))
    with requests_unixsocket.monkeypatch():
        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        peercreds_text_resp.raise_for_status()
        assert peercreds_text_resp.text == expected_textcreds
Beispiel #7
0
def test_unix_domain_adapter_monkeypatch():
    with UnixSocketServerThread() as usock_thread:
        with requests_unixsocket.monkeypatch('http+unix://'):
            urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
            url = 'http+unix://%s/path/to/page' % urlencoded_usock

            for method in [
                    'get', 'post', 'head', 'patch', 'put', 'delete', 'options'
            ]:
                logger.debug('Calling session.%s(%r) ...', method, url)
                r = getattr(requests, method)(url)
                logger.debug(
                    'Received response: %r with text: %r and headers: %r', r,
                    r.text, r.headers)
                assert r.status_code == 200
                assert r.headers['server'] == 'waitress'
                assert r.headers['X-Transport'] == 'unix domain socket'
                assert r.headers['X-Requested-Path'] == '/path/to/page'
                assert r.headers['X-Socket-Path'] == usock_thread.usock
                assert isinstance(r.connection,
                                  requests_unixsocket.UnixAdapter)
                assert r.url == url
                if method == 'head':
                    assert r.text == ''
                else:
                    assert r.text == 'Hello world!'

    for method in ['get', 'post', 'head', 'patch', 'put', 'delete', 'options']:
        with pytest.raises(requests.exceptions.InvalidSchema):
            getattr(requests, method)(url)
Beispiel #8
0
def test_unix_socket() -> None:
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    file_name = "test.socket"

    if os.path.exists(file_name):
        os.remove(file_name)

    sock.bind(file_name)
    with run_bokeh_serve(["--unix-socket", file_name, "--glob",
                          APPS]) as (p, nbsr):
        # The server is not ready is binds to the unix socket
        # very quickly, having some sleep helps
        with requests_unixsocket.monkeypatch():
            for t in range(1, 11):
                time.sleep(1)
                try:
                    r = requests.get(
                        f"http+unix://{file_name.replace('/', '%2F')}/line_on_off"
                    )
                    assert r.status_code == 200
                    break
                except:
                    if t == 10:
                        assert False
                    pass
    os.remove(file_name)
Beispiel #9
0
    def __init__(self, fps: int, width: int, height: int, scale_factor: float,
                 no_background: bool, background_blur: int,
                 use_foreground: bool, hologram: bool, tiling: bool,
                 bodypix_url: str, socket: str, background_image: str,
                 foreground_image: str, foreground_mask_image: str,
                 webcam: int) -> None:
        self.no_background = no_background
        self.use_foreground = use_foreground
        self.hologram = hologram
        self.tiling = tiling
        self.background_blur = background_blur
        self.background_image = background_image
        self.foreground_image = foreground_image
        self.foreground_mask_image = foreground_mask_image
        self.scale_factor = scale_factor
        self.real_cam = RealCam(webcam, width, height, fps)
        # In case the real webcam does not support the requested mode.
        self.width = self.real_cam.get_frame_width()
        self.height = self.real_cam.get_frame_height()
        self.fake_cam = pyvirtualcam.Camera(width=640, height=480, fps=30)

        self.foreground_mask = None
        self.inverted_foreground_mask = None
        self.session = requests.Session()
        if bodypix_url.startswith('/'):
            print("Looks like you want to use a unix socket")
            # self.session = requests_unixsocket.Session()
            self.bodypix_url = "http+unix:/" + bodypix_url
            self.socket = bodypix_url
            requests_unixsocket.monkeypatch()
        else:
            self.bodypix_url = bodypix_url
            self.socket = ""
            # self.session = requests.Session()
        self.images: Dict[str, Any] = {}
        self.image_lock = asyncio.Lock()
Beispiel #10
0
def accounting(p):
    print "*** accounting ***"
    #    radiusd.radlog(radiusd.L_INFO, '*** radlog call in accounting (0) ***')
    #    print
    print "NAS request", p
    # NAS
    with requests_unixsocket.monkeypatch():
        response = requests.post(
            'http+unix://%2Fvar%2Fwww%2Fbilling%2Fbilling.sock/radius_accounting/',
            json=p,
            timeout=3)
    print "response code:", response.status_code
    if not response.status_code == 200:
        return radiusd.RLM_MODULE_REJECT
    return radiusd.RLM_MODULE_OK
def test_peercreds_unix_sock(peercreds_enabled_server_and_client):
    """Check that peercred lookup works when enabled."""
    httpserver, testclient = peercreds_enabled_server_and_client

    unix_base_uri = 'http+unix://{}'.format(
        httpserver.bind_addr.replace('/', '%2F'), )

    expected_peercreds = os.getpid(), os.getuid(), os.getgid()
    expected_peercreds = '|'.join(map(str, expected_peercreds))

    with requests_unixsocket.monkeypatch():
        peercreds_resp = requests.get(unix_base_uri + PEERCRED_IDS_URI)
        peercreds_resp.raise_for_status()
        assert peercreds_resp.text == expected_peercreds

        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        assert peercreds_text_resp.status_code == 500
Beispiel #12
0
def test_peercreds_unix_sock(peercreds_enabled_server_and_client):
    """Check that peercred lookup works when enabled."""
    httpserver, testclient = peercreds_enabled_server_and_client

    unix_base_uri = 'http+unix://{}'.format(
        httpserver.bind_addr.replace('/', '%2F'),
    )

    expected_peercreds = os.getpid(), os.getuid(), os.getgid()
    expected_peercreds = '|'.join(map(str, expected_peercreds))

    with requests_unixsocket.monkeypatch():
        peercreds_resp = requests.get(unix_base_uri + PEERCRED_IDS_URI)
        peercreds_resp.raise_for_status()
        assert peercreds_resp.text == expected_peercreds

        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        assert peercreds_text_resp.status_code == 500
Beispiel #13
0
def authorize(p):
    print "*** authorize ***"
    print "NAS request:", p
    with requests_unixsocket.monkeypatch():
        response = requests.post(
            'http+unix://%2Fvar%2Fwww%2Fbilling%2Fbilling.sock/radius_authorize/', json=p, timeout=3)
        print
        print "response code:", response.status_code
        if not response.status_code == 200:
            return radiusd.RLM_MODULE_REJECT
        parsed_response = response.json()
    print "parsed response:", parsed_response
    reply = tuple((str(key), str(value))
                  for key, value in parsed_response["reply"].items())
    print "reply: ", reply
    config = tuple((str(key), str(value))
                   for key, value in parsed_response["config"].items())
    print "config: ", config
    return (radiusd.RLM_MODULE_OK, reply, config)
Beispiel #14
0
def test_unix_domain_adapter_monkeypatch():
    with UnixSocketServerThread() as usock_thread:
        with requests_unixsocket.monkeypatch('http+unix://'):
            urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
            url = 'http+unix://%s/path/to/page' % urlencoded_usock
            logger.debug('Calling requests.get(%r) ...', url)
            r = requests.get(url)
            logger.debug('Received response: %r with text: %r and headers: %r',
                         r, r.text, r.headers)
            assert r.status_code == 200
            assert r.headers['server'] == 'waitress'
            assert r.headers['X-Transport'] == 'unix domain socket'
            assert r.headers['X-Requested-Path'] == '/path/to/page'
            assert r.headers['X-Socket-Path'] == usock_thread.usock
            assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
            assert r.url == url
            assert r.text == 'Hello world!'

        with pytest.raises(requests.exceptions.InvalidSchema):
            requests.get(url)
Beispiel #15
0
def test_peercreds_unix_sock_with_lookup(peercreds_enabled_server_and_client):
    """Check that peercred resolution works when enabled."""
    httpserver, testclient = peercreds_enabled_server_and_client
    httpserver.peercreds_resolve_enabled = True

    unix_base_uri = 'http+unix://{}'.format(
        httpserver.bind_addr.replace('/', '%2F'),
    )

    import grp
    import pwd
    expected_textcreds = (
        pwd.getpwuid(os.getuid()).pw_name,
        grp.getgrgid(os.getgid()).gr_name,
    )
    expected_textcreds = '!'.join(map(str, expected_textcreds))
    with requests_unixsocket.monkeypatch():
        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        peercreds_text_resp.raise_for_status()
        assert peercreds_text_resp.text == expected_textcreds
Beispiel #16
0
def test_peercreds_unix_sock(peercreds_enabled_server):
    """Check that ``PEERCRED`` lookup works when enabled."""
    httpserver = peercreds_enabled_server
    bind_addr = httpserver.bind_addr

    if isinstance(bind_addr, six.binary_type):
        bind_addr = bind_addr.decode()

    quoted = urllib.parse.quote(bind_addr, safe='')
    unix_base_uri = 'http+unix://{quoted}'.format(**locals())

    expected_peercreds = os.getpid(), os.getuid(), os.getgid()
    expected_peercreds = '|'.join(map(str, expected_peercreds))

    with requests_unixsocket.monkeypatch():
        peercreds_resp = requests.get(unix_base_uri + PEERCRED_IDS_URI)
        peercreds_resp.raise_for_status()
        assert peercreds_resp.text == expected_peercreds

        peercreds_text_resp = requests.get(unix_base_uri + PEERCRED_TEXTS_URI)
        assert peercreds_text_resp.status_code == 500
def test_unix_domain_adapter_monkeypatch():
    with UnixSocketServerThread() as usock_thread:
        with requests_unixsocket.monkeypatch('http+unix://'):
            urlencoded_usock = requests.compat.quote_plus(usock_thread.usock)
            url = 'http+unix://%s/path/to/page' % urlencoded_usock
            logger.debug('Calling requests.get(%r) ...', url)
            r = requests.get(url)
            logger.debug(
                'Received response: %r with text: %r and headers: %r',
                r, r.text, r.headers)
            assert r.status_code == 200
            assert r.headers['server'] == 'waitress'
            assert r.headers['X-Transport'] == 'unix domain socket'
            assert r.headers['X-Requested-Path'] == '/path/to/page'
            assert r.headers['X-Socket-Path'] == usock_thread.usock
            assert isinstance(r.connection, requests_unixsocket.UnixAdapter)
            assert r.url == url
            assert r.text == 'Hello world!'

        with pytest.raises(requests.exceptions.InvalidSchema):
            requests.get(url)
Beispiel #18
0
    def _post(self, payload: Dict[str, Any]) -> Optional[Any]:
        """Posts request to server.

    Args:
      payload: dictionary containing rpc params

    Returns:
      result from rpc call

    Raises:
      RuntimeError: An error from the server.
      HTTPError: An error communicating with server.
    """
        for i in range(_MAX_RETRIES):
            if self.url.startswith(_UNIX_URL_PREFIX):
                with requests_unixsocket.monkeypatch():
                    response = requests.post(self.url, json=payload)
            else:
                response = requests.post(self.url, json=payload)

            if response.status_code != requests.codes.ok:
                #response.raise_for_status()
                print(f'error from server: {response}')
                time.sleep(_RETRY_DELAY_SEC)
                continue

            json_response = response.json()
            if 'result' not in json_response:
                #raise RuntimeError(f'response from server: {json_response}')
                print(f'error: bad response from server: {json_response}')
                time.sleep(_RETRY_DELAY_SEC)
                continue

            #print(f'response:\n{json_response["result"]}')
            return json_response['result']

        raise RuntimeError(f'error: retry count exceeded')
Beispiel #19
0
import os
import site
import sys
import urllib

site.main()

# to be PEP-8 compliant, the imports must be indented
if True:
    import requests
    import requests_unixsocket
    import pbs
    import pwd
    import copy

requests_unixsocket.monkeypatch()

# ============================================================================
# Utility functions
# ============================================================================


class OfflineError(Exception):
    """
    Exception that will offline the node and reject the event
    """

    def __init__(self, msg):
        super().__init__(pbs.event().job.id + ': ' + msg)

Beispiel #20
0
 def fetch_url(url):
     # Lazily import so it is not required at the module level
     if os.name != 'nt':
         import requests_unixsocket
     with requests_unixsocket.monkeypatch():
         return requests.get(url)
Beispiel #21
0
from collections import namedtuple
from enum import Enum

import requests
import requests_unixsocket
from six.moves.urllib import parse
try:
    from ws4py.client import WebSocketBaseClient
    _ws4py_installed = True
except ImportError:  # pragma: no cover
    WebSocketBaseClient = object
    _ws4py_installed = False

from pylxd import exceptions, managers

requests_unixsocket.monkeypatch()

LXD_PATH = '.config/lxc/'
SNAP_ROOT = os.path.expanduser('~/snap/lxd/current/')
APT_ROOT = os.path.expanduser('~/')
CERT_FILE_NAME = 'client.crt'
KEY_FILE_NAME = 'client.key'
# check that the cert file and key file exist at the appopriate path
if os.path.exists(os.path.join(
        SNAP_ROOT, LXD_PATH, CERT_FILE_NAME)):  # pragma: no cover
    CERTS_PATH = os.path.join(SNAP_ROOT, LXD_PATH)  # pragma: no cover
else:  # pragma: no cover
    CERTS_PATH = os.path.join(APT_ROOT, LXD_PATH)  # pragma: no cover

Cert = namedtuple('Cert', ['cert', 'key'])  # pragma: no cover
DEFAULT_CERTS = Cert(
Beispiel #22
0
def api_get(url, headers={}):
    url = "{}/{}".format(FLEET_API, url)
    with requests_unixsocket.monkeypatch():
        r = requests.get(url)
        r.raise_for_status()
        return r.json()
Beispiel #23
0
 def fetch_url(url):
     with requests_unixsocket.monkeypatch():
         return requests.get(url)
Beispiel #24
0
def main():
    parser = ArgParser(prog="remote_sge")
    parser.add_argument('test_server', help="Command to run")
    parser.add_argument('configdir', help=CONFIGDIR_HELP)
    parser.add_argument('-H',
                        '--host',
                        help="Hostname.  Default: localhost",
                        default="localhost")
    parser.add_argument('-p',
                        '--port',
                        help="Port to connect to.  Default: 443",
                        default="443")
    parser.add_argument(
        '-s',
        '--socket',
        help="Path to Unix domain socket to connect to.  Default: None." +
        " If supplied, -h and -p are ignored and unix domain socket will be " +
        "connected to rather than a TCP connection.  Requires the " +
        "requests-unixsocket package which must be installed manually.")
    parser.add_argument(
        '-k',
        '--key',
        help=
        "Path to client private key.  Default is $configdir/certs/client.key",
        default=None)
    parser.add_argument(
        '-c',
        '--cert',
        help=
        "Path to client certificate.  Default is $configdir/certs/client.crt",
        default=None)

    args = parser.parse_args()

    configdir = abspath(expandvars(expanduser(args.configdir)))
    if not exists(configdir):
        raise NotFound("Couldn't find configdir you gave.")

    if args.key:
        key = args.key
    else:
        key = join(configdir, 'certs', 'client.key')

    if args.cert:
        cert = args.cert
    else:
        cert = join(configdir, 'certs', 'client.crt')

    if args.socket:
        import requests_unixsocket
        from urllib.parse import urlencode
        requests_unixsocket.monkeypatch()
        response = requests.get("http+unix://%s/" % urlencode(args.socket))
    else:
        params = dict(cert=(cert, key), verify=False)
        response = requests.get("https://%s:%s/" % (args.host, args.port),
                                **params)
    if response.status_code == 200:
        print("It works!")
    else:
        print("Responded with %s" % response.status_code)
        print("Response body: " + response.text)