sporkmonger / uuidtools

A simple universally unique ID generation library.
http://uuidtools.rubyforge.org/
Apache License 2.0
424 stars 63 forks source link

Missing ip & ifconfig causes mysterious errors #53

Open brentkearney opened 2 years ago

brentkearney commented 2 years ago

I used a new docker image to boot up an older Rails application that depends on the uuidtools gem, specifically the UUIDTools::UUID.timestamp_create method. After the image update, I was seeing a these errors in rspec tests:

      Failure/Error: uuid { UUIDTools::UUID.timestamp_create.to_s }

      NoMethodError:
        undefined method `[]' for nil:NilClass

This comes from the assumption that /sbin/ip will exist, at https://github.com/sporkmonger/uuidtools/blob/e4f0e052546336ca23694790eca0a8d85a05d08b/lib/uuidtools.rb#L639. It would be good if it throws a more informative error instead.

My solution was to add the OS package that included the ifconfig command (which was smaller than the one that included the ip command).

rcalosso commented 1 year ago

Same situation with docker image without ifconfig/ip. My errors were 5: from /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:232:intimestamp_create' 4: from /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:232:in synchronize' 3: from /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:242:inblock in timestamp_create' 2: from /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:634:in mac_address' 1: from /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:587:inifconfig' /srv/k2_web/vendor/bundle/ruby/2.5.0/gems/uuidtools-2.1.5/lib/uuidtools.rb:587:in `': No such file or directory - addr (Errno::ENOENT) If assumes ip_path is not nil: ifconfig_path = "#{UUID.ip_path} addr list"

There is code for nil list but the error handling does not allow it.

Richard-Degenne commented 1 year ago

In the case neither ifconfig nor ip are available, could the gem rely on the /sys/class/net/*/address files instead?

Here is an example on Ubuntu 20.04:

$ cat /sys/class/net/*/address

02:42:5f:1b:f2:0a
02:42:7f:45:71:59
02:42:17:45:21:84
02:42:1c:c1:50:a2
02:42:58:b1:16:d5
02:42:97:94:0c:2b
02:42:c3:25:ad:02
02:42:e7:fc:98:51
02:42:6e:15:07:28
02:42:af:ad:23:70
02:42:eb:67:d5:9d
02:42:2c:cf:24:1c
02:42:c2:1a:95:fa
02:42:a5:df:68:25
02:42:ea:b1:59:39
02:42:7e:99:6f:8e
02:42:f1:4e:b5:ad
02:42:8d:71:42:39
02:42:f3:62:c3:be
74:78:27:fe:40:85
00:00:00:00:00:00
4e:f8:71:d6:dc:ed
66:87:a2:42:bf:17
1c:99:57:f2:59:0e
Richard-Degenne commented 1 year ago

Upon closer inspection, it seems that the gem already generates a "fake" MAC address if a real one could not be found.

https://github.com/sporkmonger/uuidtools/blob/e4f0e052546336ca23694790eca0a8d85a05d08b/lib/uuidtools.rb#L307

A quick fix to this bug could be to return nil instead of failing, and let the fallback generate a substitute instead.