vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.36k stars 337 forks source link

socket.gaierror: [Errno -2] Name or service not known #398

Open Dhyanesh97 opened 3 years ago

Dhyanesh97 commented 3 years ago

Below code is working locally in windows machine. But when I deploy it as a docker container it gives below issue as "Name or service not known" for dns_host while authenticating.

Do I need any additional setup for running the application as docker container ?

Code:

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

site_url = "https://XXXX-my.sharepoint.com/personal/XXXXXXXX/"
username_env = 'XXXXX@XXXX.com'
password_env = 'XXXXXX'
relativeUrl = "Documents/XXXXXXX/"

ctx = ClientContext(site_url).with_credentials(UserCredential(username_env, password_env))
web = ctx.web
ctx.load(web)
ctx.execute_query()

Error and Stack Trace:

socket.gaierror: [Errno -2] Name or service not known
'NoneType' object has no attribute 'text'
'NoneType' object has no attribute 'text'
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 169, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/connection.py", line 73, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/local/lib/python3.8/socket.py", line 918, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 353, in connect
    conn = self._new_conn()
  File "/usr/local/lib/python3.8/site-packages/urllib3/connection.py", line 181, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7fa1174fdb80>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/local/lib/python3.8/site-packages/urllib3/connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.8/site-packages/urllib3/util/retry.py", line 574, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='sts.XXXXXXX.com', port=443): Max retries exceeded with url: /adfs/services/trust/2005/usernamemixed (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa1174fdb80>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 97, in get_authentication_cookie
    token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 137, in _acquire_service_token_from_adfs
    response = requests.post(adfs_url, data=payload,
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='sts.XXXXXX.com', port=443): Max retries exceeded with url: /adfs/services/trust/2005/usernamemixed (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fa1174fdb80>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/python-flask/updateDynamoDb/DownloadExcel.py", line 21, in downloadFile
    response = File.open_binary(ctx,
  File "/usr/local/lib/python3.8/site-packages/office365/sharepoint/files/file.py", line 296, in open_binary
    response = ctx.execute_request_direct(request)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/client_runtime_context.py", line 118, in execute_request_direct
    return self.pending_request().execute_request_direct(request)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/odata/odata_request.py", line 36, in execute_request_direct
    return super(ODataRequest, self).execute_request_direct(request)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/client_request.py", line 98, in execute_request_direct
    self.context.authenticate_request(request_options)
  File "/usr/local/lib/python3.8/site-packages/office365/sharepoint/client_context.py", line 168, in authenticate_request
    self._auth_context.authenticate_request(request)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/authentication_context.py", line 86, in authenticate_request
    self._provider.authenticate_request(request)
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 77, in authenticate_request
    self.ensure_authentication_cookie()
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 84, in ensure_authentication_cookie
    self._cached_auth_cookies = self.get_authentication_cookie()
  File "/usr/local/lib/python3.8/site-packages/office365/runtime/auth/providers/saml_token_provider.py", line 102, in get_authentication_cookie
    logger.error(e.response.text)
AttributeError: 'NoneType' object has no attribute 'text'
Dhyanesh97 commented 3 years ago

please find below analysis after debugging the code

=> STSAuthUrl (https://sts.XXXXXX.com/adfs/services/trust/2005/usernamemixed) is picked and code is trying to get host by name as below

socket.gethostbyname("sts.XXXXXXXX.com")

On windows 10 machine it is running without any issues because gethostbyname is returning some value but in docker container and linux machine it is failing with the error mentioned below

socket.gaierror: [Errno -2] Name or service not known

OS

NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"

The gethostbyname method is called 4 times in a row

  1. login.microsoftonline.com
  2. sts.XXXXXXX.com (gets error in linux but not on windows as it returns some value.)
  3. login.microsoftonline.com
  4. XXXXXXX-XX.sharepoint.com

Any help appreciated.