FactoryBot sometimes does not properly return root pass

Ok this is weird, some of our discovery tests were failing and after long investigation I was able to identify this:

require 'test_helper'

class AlphanumericValidatorTest < ActiveSupport::TestCase
  test "validation passes on alphanumeric input" do
    (1..500).each do
      @hostgroup = FactoryBot.create(:hostgroup, :with_rootpass)
      puts @hostgroup.name
      assert_match /\$\d+\$.*/, @hostgroup.root_pass

When you run this test, it will fail after several rounds (for me it’s always hostgroup7). Root password is set to “*0” and I have no idea why. Any ideas?

Holy Moly, looks like we use SecureRandom in BASE64 for random seed and when it contains plus + it breaks the crypt method:

>> x = SecureRandom.base64(6); puts x; "test".crypt("$5$#{x}")
=> "$5$ardeALd3$Qok7xO6ConFcg0KasVX4FRrm/FNABHsL7h2xnNh0uo1"

>> x = SecureRandom.base64(6); puts x; "test".crypt("$5$#{x}")
=> "*0"

This is an ancient code in Foreman, root password had to “sometimes” set incorrectly when saved into database. Nice one.


I call it a day!