{"generator":"Jekyll","link":[{"@attributes":{"href":"https:\/\/machacekondra.github.io\/feed.xml","rel":"self","type":"application\/atom+xml"}},{"@attributes":{"href":"https:\/\/machacekondra.github.io\/","rel":"alternate","type":"text\/html"}}],"updated":"2022-09-21T12:18:05+00:00","id":"https:\/\/machacekondra.github.io\/feed.xml","title":"Blog","subtitle":"Random stuff.","author":{"name":"Ondra Machacek"},"entry":[{"title":"A plugin to support SAP authentication in Python Requests","link":{"@attributes":{"href":"https:\/\/machacekondra.github.io\/2022\/09\/21\/blog-post-sap-auth-plugin-for-requests-lib.html","rel":"alternate","type":"text\/html","title":"A plugin to support SAP authentication in Python Requests"}},"published":"2022-09-21T00:00:00+00:00","updated":"2022-09-21T00:00:00+00:00","id":"https:\/\/machacekondra.github.io\/2022\/09\/21\/blog-post-sap-auth-plugin-for-requests-lib","content":"<p><a href=\"https:\/\/requests.readthedocs.io\/en\/latest\/\">Requests<\/a> is a simple HTTP library for Python. It supports many authentication mechanisms, but also\ncreating a custom one.<\/p>\n\n<p>In this blog post I will explain how to use custom authentication plugin for SAP portal authentication.<\/p>\n\n<p>Why would we need it? In cases we want to automate the file management on SAP portal, we may have an automation to check if files are valid or similar.<\/p>\n\n<hr \/>\n\n<h3 id=\"the-sap-portal-authentication\">The SAP portal authentication<\/h3>\n\n<p>The SAP portal authenation consist of few steps. In each step the client must send the hidden data to the post from, which is returned in the HTTP response.\nAt the end we receive <code>SAMLResponse<\/code> data, which we can use to create a HTTP session and access our HTTP link.<\/p>\n\n<h4 id=\"the-code\">The code<\/h4>\n\n<script src=\"https:\/\/gist.github.com\/be1007b3a99e41a23d997d43e09b3ab3.js\"> <\/script>\n\n<h3 id=\"usage\">Usage<\/h3>\n\n<p>The usage of the <a href=\"https:\/\/pypi.org\/project\/requests-sap\/\">library<\/a> is very simple, you just need to pass the <code>SAPAuth<\/code> instance to the <code>requests<\/code> HTTP call.\nAnd the authentication is happening behind the scene for you, no need to know any details, just <code>username<\/code> and <code>password<\/code>.<\/p>\n\n<h4 id=\"install\">Install<\/h4>\n\n<p>The library is hosted on <a href=\"https:\/\/pypi.org\/project\/requests-sap\/\">pypi<\/a>, so you can simply install it via <code>pip<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">pip install requests-sap\n<\/code><\/pre>\n\n<h4 id=\"sample-code\">Sample code<\/h4>\n\n<p>This sample code will login to SAP portal, and fetch information and print the information to stdout.<\/p>\n\n<script src=\"https:\/\/gist.github.com\/6d652ed1fae08deb147a28abfc2772fc.js\"> <\/script>\n\n<p>The output is:<\/p>\n\n<pre><code class=\"language-bash\">SAP HANA Platform Edt. 2.0 SPS05 rev57 Linux x86_64 is AVAILABLE\n<\/code><\/pre>","author":{"name":"Ondra Machacek"},"category":{"@attributes":{"term":"Other"}},"summary":"Requests is a simple HTTP library for Python. It supports many authentication mechanisms, but also creating a custom one."},{"title":"Extend urrlib http connection with local unix socket","link":{"@attributes":{"href":"https:\/\/machacekondra.github.io\/2022\/09\/18\/blog-post-unix-socket-for-urllib.html","rel":"alternate","type":"text\/html","title":"Extend urrlib http connection with local unix socket"}},"published":"2022-09-18T00:00:00+00:00","updated":"2022-09-18T00:00:00+00:00","id":"https:\/\/machacekondra.github.io\/2022\/09\/18\/blog-post-unix-socket-for-urllib","content":"<p>By default Python <a href=\"https:\/\/docs.python.org\/3\/library\/urllib.html\">urllib<\/a> library don\u2019t support connection via local unix socket, because <a href=\"https:\/\/docs.python.org\/3\/library\/http.client.html#module-http.client\">http.client<\/a> library, which is used by <code>urllib<\/code> set <a href=\"https:\/\/github.com\/python\/cpython\/blob\/3.7\/Lib\/http\/client.py#L949\">IPPROTO_TCP<\/a> socket type.<\/p>\n\n<p>In this blog post I will explain how to use local unix socket for your urllib connection.<\/p>\n\n<p>Why would we need it? In situation you connect to remote host via Ansible using SSH, and later using authenticated sockets.\nSo it\u2019s a way of using secure connection via SSH and not exposing the password via HTTP. The reason we had to use it,\nwas for <a href=\"https:\/\/github.com\/suds-community\/suds\">suds<\/a> SOAP client for SAP management. SAP administrators are not used to use\npasswords for SOAP, but are rather used to SSH to SAP machine and execute the specific command, which is levaraing the local unix socket.<\/p>\n\n<hr \/>\n\n<h3 id=\"extending-the-urllib\">Extending the urllib<\/h3>\n\n<p>urllib has handler mechanism. You can write custom handler to handle the connections. We will create the custom handler which will\nuse the local unix socket instead of the TCP socket.<\/p>\n\n<h4 id=\"urllib-handler\">Urllib handler<\/h4>\n\n<pre><code class=\"language-python\">import http.client\nimport socket\nimport urllib.request\n\n\nclass LocalHttpConnection(http.client.HTTPConnection):\n    def __init__(self, host, port=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,\n                 source_address=None, socketpath=None):\n        super().__init__(host, port, timeout, source_address)\n        self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)\n        self.sock.connect(socketpath)\n\n\nclass ExtendedHandler(urllib.request.HTTPHandler):\n\n    def __init__(self, debuglevel=0, socketpath=None):\n        self._debuglevel = debuglevel\n        self._socketpath = socketpath\n\n    def http_open(self, req):\n        return self.do_open(LocalHttpConnection, req, self._socketpath)\n<\/code><\/pre>\n\n<h3 id=\"suds-client\">suds client<\/h3>\n\n<p>The <a href=\"https:\/\/github.com\/suds-community\/suds\">suds<\/a> client is a simple SOAP client. The suds client support custom transport method.\nTo use our custom handler in the suds client, just pass the our handler in the suds contructor.<\/p>\n\n<h4 id=\"final-code\">Final code<\/h4>\n\n<script src=\"https:\/\/gist.github.com\/eb1abd1c73024911adb2668b3cfbab30.js\"> <\/script>","author":{"name":"Ondra Machacek"},"category":{"@attributes":{"term":"Other"}},"summary":"By default Python urllib library don\u2019t support connection via local unix socket, because http.client library, which is used by urllib set IPPROTO_TCP socket type."}]}