denisbrodbeck / machineid

Get the unique machine id of any host (without admin privileges)
MIT License
1k stars 143 forks source link

Docker machine-id fails #21

Open 0xprofessooor opened 1 year ago

0xprofessooor commented 1 year ago

Hi, I am running machineid in a code sandbox that uses a docker container. The package fails with error:

Traceback (most recent call last):
  File "/workspace/run.py", line 8, in <module>
  File "/usr/local/lib/python3.10/site-packages/machineid/__init__.py", line 103, in hashed_id
    return hmac.new(bytes(app_id.encode()), id().encode(), hashlib.sha256).hexdigest()
  File "/usr/local/lib/python3.10/site-packages/machineid/__init__.py", line 94, in id
    raise Exception('failed to obtain id on platform {}'.format(platform))
Exception: failed to obtain id on platform linux

Checking the package, I have the following id() method:

def id() -> str:
  """
  id returns the platform specific device GUID of the current host OS.
  """
  if platform == 'darwin':
    id = __exec__("ioreg -d2 -c IOPlatformExpertDevice | awk -F\\\" '/IOPlatformUUID/{print $(NF-1)}'")

  if platform == 'win32' or platform == 'cygwin' or platform == 'msys':
    id = __reg__('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography', 'MachineGuid')
    if not id:
      id = __exec__('wmic csproduct get uuid').split('\n')[2] \
                                              .strip()

  if platform.startswith('linux'):
    id = __read__('/var/lib/dbus/machine-id')
    if not id:
      id = __read__('/etc/machine-id')
    if not id:
      cgroup = __read__('/proc/self/cgroup')
      if cgroup:
        if 'docker' in cgroup:
          id = __exec__('head -1 /proc/self/cgroup | cut -d/ -f3')
    if not id:
      mountinfo = __read__('/proc/self/mountinfo')
      if mountinfo:
        if 'docker' in mountinfo:
          id = __exec__("grep 'systemd' /proc/self/mountinfo | cut -d/ -f3")

  if platform.startswith('openbsd') or platform.startswith('freebsd'):
    id = __read__('/etc/hostid')
    if not id:      id = __exec__('kenv -q smbios.system.uuid')

  if not id:
    raise Exception('failed to obtain id on platform {}'.format(platform))

  return id

In particular, I am running docker in linux, so I'm interested in the commands. As expected by the error, none of the linux specific paths has a valid machine id associated with it. We're wondering if this should be trying to read /etc/hostname if the other two checks fail?

pirate commented 1 month ago

I'm also running into this, it seems machineid doesn't work during docker builds.

For now I just added this line so that machineid has something to work with to avoid the error:

RUN openssl rand -hex 16 > /etc/machine-id