[tpm2] Re: tpm2-pytss connect to dockerized swtpm

Roberts, William C <william.c.roberts at intel.com> Mon, 01 Aug 2022 15:37:22 +0000
Newsgroups dev.linux.lists.tpm2
Message-ID <SN6PR11MB343777B0AD0F89F5B19021C9B89A9@SN6PR11MB3437.namprd11.prod.outlook.com>
Oh nevermind I see you expose the ports in the dockerfile and Erik figured it out.
________________________________
From: henry.gadacz(a)stud.h-da.de <henry.gadacz(a)stud.h-da.de>
Sent: Wednesday, July 27, 2022 7:39 AM
To: 'Erik' <who+01tpm(a)cnackers.org>; tpm2(a)lists.01.org <tpm2(a)lists.01.org>
Subject: [tpm2] Re: tpm2-pytss connect to dockerized swtpm

Hi Erik,

Thank you very much! The suggestion about the swtpm IP address was correct!

Best regards,
Henry

-----Ursprüngliche Nachricht-----
Von: Erik <who+01tpm(a)cnackers.org>
Gesendet: Mittwoch, 27. Juli 2022 14:16
An: henry.gadacz(a)stud.h-da.de; tpm2(a)lists.01.org
Betreff: [tpm2] Re: tpm2-pytss connect to dockerized swtpm

HI,

swtpm only listens on 127.0.0.1 by default, so unless you have changed that it wont be accessible by any other address.
docker compose seems to use the same network namespace for each container so localhost should work, but it might be as simple as that the python script runs before swtpm is ready.
The unit tests for tpm2-pytss tries to connect to swtpm a couple of times with a small sleep in between each try to get around that, so try adding for example time.sleep(5) before calling ESAPI(...) to see if that helps.

/Erik

On Wed, 2022-07-27 at 10:29 +0200, henry.gadacz(a)stud.h-da.de <mailto:henry.gadacz(a)stud.h-da.de>  wrote:

        Hello everyone,

        I am trying to accomplish the following, but did not succeed and I hope someone can help me.
        I want to have a docker compose with two containers. In the first container I want to run the swtpm (https://github.com/stefanberger/swtpm) and in the other a python script that uses tpm2-pytss to connect to the swtpm in the first container.

        When I run swtpm and the python script in the same container it works.
        In order to run them in separate containers I just duplicated the Dockerfile (I know this has some overhead, but to make sure don’t miss any dependencies), changed the docker CMD command to either run the python script or the swtpm and renamed them to Dockerfile_app_test and Dockerfile_tpm_test.

        My docker compose file is looking like this:
        version: '3.7'
        services:

          app:
            container_name: app
            build:
              context: .
              dockerfile: Dockerfile_app_test
            restart: unless-stopped

          tpm:
            container_name: tpm
            build:
              context: .
              dockerfile: Dockerfile_tpm_test
            ports:
              - "2321:2321"
              - "2322:2322"
            restart: unless-stopped


        My python script is:
        from tpm2_pytss import *
        if __name__ == '__main__':
            print("TPM test application")
            tpm = ESAPI(tcti="swtpm:host=tpm,port=2321")
            tpm.startup(TPM2_SU.CLEAR)

            r = tpm.get_random(8)
            print("type is ", type(r))
            print("r    is ", str(r))
            print("as int  ", int(str(r), 16))


        When I run it in one Dockerfile I used
        tpm = ESAPI(tcti="swtpm:host=localhost,port=2321")

        so I thought changing the host name to the docker container name should do it but I always get the following errors:
        app  | WARNING:tcti:src/util/io.c:262:socket_connect() Failed to connect to host 172.21.0.2, port 2321: errno 111: Connection refused
        app  | ERROR:tcti:src/tss2-tcti/tcti-swtpm.c:614:Tss2_Tcti_Swtpm_Init() Cannot connect to swtpm TPM socket
        app  | ERROR:tcti:src/tss2-tcti/tctildr-dl.c:170:tcti_from_file() Could not initialize TCTI file: swtpm
        app  | ERROR:tcti:src/tss2-tcti/tctildr.c:428:Tss2_TctiLdr_Initialize_Ex() Failed to instantiate TCTI
        app  | Traceback (most recent call last):
        app  |   File "/app/main.py", line 70, in <module>
        app  |     tpm = ESAPI(tcti="swtpm:host=tpm,port=2321")
        app  |   File "/usr/local/lib/python3.10/dist-packages/tpm2_pytss/ESAPI.py", line 123, in __init__
        app  |     tcti = TCTILdr.parse(tcti)
        app  |   File "/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py", line 54, in parse
        app  |     return cls(name, conf)
        app  |   File "/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py", line 29, in __init__
        app  |     _chkrc(lib.Tss2_TctiLdr_Initialize_Ex(name, conf, self._ctx_pp))
        app  |   File "/usr/local/lib/python3.10/dist-packages/tpm2_pytss/internal/utils.py", line 32, in _chkr
        app  |     raise TSS2_Exception(rc)
        app  | tpm2_pytss.TSS2_Exception.TSS2_Exception: tcti:IO failure
        app exited with code 1



        I know it’s not a plain tpm2-tss question, but does anyone has experience with that and can help me?

        Kind regards,
        Henry

        _______________________________________________
        tpm2 mailing list -- tpm2(a)lists.01.org <mailto:tpm2(a)lists.01.org>
        To unsubscribe send an email to tpm2-leave(a)lists.01.org <mailto:tpm2-leave(a)lists.01.org>
        %(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s


_______________________________________________
tpm2 mailing list -- tpm2(a)lists.01.org
To unsubscribe send an email to tpm2-leave(a)lists.01.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s
attachment.htm (text/html, 9.9 KB)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Oh nevermind I see you expose the ports in the dockerfile and Erik figured it out.</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> [email protected] &lt;[email protected]&gt;<br>
<b>Sent:</b> Wednesday, July 27, 2022 7:39 AM<br>
<b>To:</b> 'Erik' &lt;[email protected]&gt;; [email protected] &lt;[email protected]&gt;<br>
<b>Subject:</b> [tpm2] Re: tpm2-pytss connect to dockerized swtpm</font>
<div>&nbsp;</div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hi Erik,<br>
<br>
Thank you very much! The suggestion about the swtpm IP address was correct!<br>
<br>
Best regards,<br>
Henry<br>
<br>
-----Ursprüngliche Nachricht-----<br>
Von: Erik &lt;[email protected]&gt; <br>
Gesendet: Mittwoch, 27. Juli 2022 14:16<br>
An: [email protected]; [email protected]<br>
Betreff: [tpm2] Re: tpm2-pytss connect to dockerized swtpm<br>
<br>
HI,<br>
<br>
swtpm only listens on 127.0.0.1 by default, so unless you have changed that it wont be accessible by any other address.<br>
docker compose seems to use the same network namespace for each container so localhost should work, but it might be as simple as that the python script runs before swtpm is ready.<br>
The unit tests for tpm2-pytss tries to connect to swtpm a couple of times with a small sleep in between each try to get around that, so try adding for example time.sleep(5) before calling ESAPI(...) to see if that helps.<br>
<br>
/Erik<br>
<br>
On Wed, 2022-07-27 at 10:29 +0200, [email protected] &lt;<a href="mailto:[email protected]">mailto:[email protected]</a>&gt;&nbsp; wrote:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Hello everyone,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I am trying to accomplish the following, but did not succeed and I hope someone can help me.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I want to have a docker compose with two containers. In the first container I want to run the swtpm (<a href="https://github.com/stefanberger/swtpm">https://github.com/stefanberger/swtpm</a>) and in the other a python script that uses tpm2-pytss to
 connect to the swtpm in the first container. <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; When I run swtpm and the python script in the same container it works.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In order to run them in separate containers I just duplicated the Dockerfile (I know this has some overhead, but to make sure don’t miss any dependencies), changed the docker CMD command to either run the python script or the swtpm and renamed them
 to Dockerfile_app_test and Dockerfile_tpm_test.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; My docker compose file is looking like this:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; version: '3.7'<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; services:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container_name: app<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; build:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context: .<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dockerfile: Dockerfile_app_test<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; restart: unless-stopped<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tpm:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; container_name: tpm<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; build:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context: .<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dockerfile: Dockerfile_tpm_test<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ports:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - &quot;2321:2321&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - &quot;2322:2322&quot;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; restart: unless-stopped<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; My python script is:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from tpm2_pytss import *<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if __name__ == '__main__':<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;TPM test application&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tpm = ESAPI(tcti=&quot;swtpm:host=tpm,port=2321&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tpm.startup(TPM2_SU.CLEAR)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r = tpm.get_random(8)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;type is &quot;, type(r))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;r&nbsp;&nbsp;&nbsp; is &quot;, str(r))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&quot;as int&nbsp; &quot;, int(str(r), 16))<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; When I run it in one Dockerfile I used <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tpm = ESAPI(tcti=&quot;swtpm:host=localhost,port=2321&quot;)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; so I thought changing the host name to the docker container name should do it but I always get the following errors:<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | WARNING:tcti:src/util/io.c:262:socket_connect() Failed to connect to host 172.21.0.2, port 2321: errno 111: Connection refused
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | ERROR:tcti:src/tss2-tcti/tcti-swtpm.c:614:Tss2_Tcti_Swtpm_Init() Cannot connect to swtpm TPM socket
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | ERROR:tcti:src/tss2-tcti/tctildr-dl.c:170:tcti_from_file() Could not initialize TCTI file: swtpm
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | ERROR:tcti:src/tss2-tcti/tctildr.c:428:Tss2_TctiLdr_Initialize_Ex() Failed to instantiate TCTI
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | Traceback (most recent call last):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp; File &quot;/app/main.py&quot;, line 70, in &lt;module&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; tpm = ESAPI(tcti=&quot;swtpm:host=tpm,port=2321&quot;)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/ESAPI.py&quot;, line 123, in __init__<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; tcti = TCTILdr.parse(tcti)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py&quot;, line 54, in parse<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; return cls(name, conf)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/TCTILdr.py&quot;, line 29, in __init__<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; _chkrc(lib.Tss2_TctiLdr_Initialize_Ex(name, conf, self._ctx_pp))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp; File &quot;/usr/local/lib/python3.10/dist-packages/tpm2_pytss/internal/utils.py&quot;, line 32, in _chkr<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; |&nbsp;&nbsp;&nbsp;&nbsp; raise TSS2_Exception(rc)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app&nbsp; | tpm2_pytss.TSS2_Exception.TSS2_Exception: tcti:IO failure<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app exited with code 1<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I know it’s not a plain tpm2-tss question, but does anyone has experience with that and can help me?
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Kind regards, <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Henry<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _______________________________________________<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tpm2 mailing list -- [email protected] &lt;<a href="mailto:[email protected]">mailto:[email protected]</a>&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; To unsubscribe send an email to [email protected] &lt;<a href="mailto:[email protected]">mailto:[email protected]</a>&gt;
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; %(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s<br>
<br>
<br>
_______________________________________________<br>
tpm2 mailing list -- [email protected]<br>
To unsubscribe send an email to [email protected]<br>
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s</div>
</span></font></div>
</body>
</html>