Repository: aio-libs/aiodocker

Issue:
ssl_context not used
## Long story short

The `ssl_context` given as parameter to the [Docker class](https://github.com/aio-libs/aiodocker/blob/c8746face698e0045142e06a14173de10f2995a8/aiodocker/docker.py#L59) is never used so you need to give a aiohttp Connector object instead of `ssl_context`.

### Expected behaviour:
Don't have the way to give a `ssl_context` in Docker constructor (and be forced to use the [`connector`](https://github.com/aio-libs/aiodocker/blob/c8746face698e0045142e06a14173de10f2995a8/aiodocker/docker.py#L63) parameter to pass SSL config)
**OR**
Use the `ssl_context` parameter to internaly create the [`connector`](https://github.com/aio-libs/aiodocker/blob/c8746face698e0045142e06a14173de10f2995a8/aiodocker/docker.py#L101)

### Actual behaviour:
`ssl_context` parameter is useless and we need to pass through the `connector` parameter to pass SSL config.

## How to reproduce

```python3
import aiodocker

aiodocker.Docker(ssl_context="will work because not used")
```


Please explain the expected behavior so I can make a PR to fix this.

Comments:

Patch:
diff --git a/CHANGES/536.bugfix b/CHANGES/536.bugfix
new file mode 100644
index 0000000..b11338a
--- /dev/null
+++ b/CHANGES/536.bugfix
@@ -0,0 +1,1 @@
+Use ssl_context passsed to Docker constructor for creating underlying connection to docker engine.
diff --git a/CHANGES/608.bugfix b/CHANGES/608.bugfix
new file mode 100644
index 0000000..0020f4f
--- /dev/null
+++ b/CHANGES/608.bugfix
@@ -0,0 +1,1 @@
+Fix an error when attach/exec when container stops before close connection to it.
diff --git a/aiodocker/docker.py b/aiodocker/docker.py
index 99c3040..7c5c2ef 100644
--- a/aiodocker/docker.py
+++ b/aiodocker/docker.py
@@ -109,8 +109,9 @@ class Docker:
             WIN_PRE_LEN = len(WIN_PRE)
             if _rx_tcp_schemes.search(docker_host):
                 if os.environ.get("DOCKER_TLS_VERIFY", "0") == "1":
-                    ssl_context = self._docker_machine_ssl_context()
-                    docker_host = _rx_tcp_schemes.sub("https://", docker_host)
+                    if ssl_context is None:
+                        ssl_context = self._docker_machine_ssl_context()
+                        docker_host = _rx_tcp_schemes.sub("https://", docker_host)
                 else:
                     ssl_context = None
                 connector = aiohttp.TCPConnector(ssl=ssl_context)
diff --git a/aiodocker/stream.py b/aiodocker/stream.py
index d1365dd..0ee8da3 100644
--- a/aiodocker/stream.py
+++ b/aiodocker/stream.py
@@ -115,7 +115,7 @@ class Stream:
             return
         self._closed = True
         transport = self._resp.connection.transport
-        if transport.can_write_eof():
+        if transport and transport.can_write_eof():
             transport.write_eof()
         self._resp.close()
 


Test patch:
diff --git a/tests/test_integration.py b/tests/test_integration.py
index c2e264a..998237b 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -3,6 +3,7 @@ import datetime
 import io
 import os
 import pathlib
+import ssl
 import sys
 import tarfile
 import time
@@ -70,6 +71,17 @@ async def test_ssl_context(monkeypatch):
     docker = Docker()
     assert docker.connector._ssl
     await docker.close()
+    with pytest.raises(TypeError):
+        docker = Docker(ssl_context="bad ssl context")
+    ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
+    ssl_ctx.set_ciphers(ssl._RESTRICTED_SERVER_CIPHERS)
+    ssl_ctx.load_verify_locations(cafile=str(cert_dir / "ca.pem"))
+    ssl_ctx.load_cert_chain(
+        certfile=str(cert_dir / "cert.pem"), keyfile=str(cert_dir / "key.pem")
+    )
+    docker = Docker(ssl_context=ssl_ctx)
+    assert docker.connector._ssl
+    await docker.close()
 
 
 @pytest.mark.skipif(
@@ -228,6 +240,30 @@ async def test_attach_nontty(docker, image_name, make_container, stderr):
         assert data.strip() == b"Hello"
 
 
+@pytest.mark.asyncio
+async def test_attach_nontty_wait_for_exit(docker, image_name, make_container):
+    cmd = ["python", "-c", "import time; time.sleep(3); print('Hello')"]
+
+    config = {
+        "Cmd": cmd,
+        "Image": image_name,
+        "AttachStdin": False,
+        "AttachStdout": False,
+        "AttachStderr": False,
+        "Tty": False,
+        "OpenStdin": False,
+        "StdinOnce": False,
+    }
+
+    container = await make_container(
+        config,
+        name="aiodocker-testing-attach-nontty-wait-for-exit",
+    )
+
+    async with container.attach(stdin=False, stdout=True, stderr=True):
+        await asyncio.sleep(10)
+
+
 @pytest.mark.asyncio
 async def test_attach_tty(docker, image_name, make_container):
     skip_windows()