Jira (FACT-2930) Inconsistent handling of Date types in custom facts

28 views
Skip to first unread message

Josh Cooper (Jira)

unread,
Jan 29, 2021, 3:11:12 PM1/29/21
to puppe...@googlegroups.com
Josh Cooper created an issue
 
Facter / Bug FACT-2930
Inconsistent handling of Date types in custom facts
Issue Type: Bug Bug
Assignee: Unassigned
Created: 2021/01/29 12:08 PM
Priority: Normal Normal
Reporter: Josh Cooper

If a custom fact returns a Time value, then facter will report:

$ mkdir facts
$ cat > facts/time.rb <<END
Facter.add(:now) do
  setcode { Time.now }
end
END
$ bx facter --custom-dir /Users/josh/work/facter/facts --trace
[2021-01-29 11:59:02.519014 ] ERROR Facter - Fact resolution fact='now', resolution='<anonymous>' resolved to an invalid value: Expected 2021-01-29 11:59:02 -0800 to be one of [Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash], but was Time
backtrace:
/Users/josh/work/facter/lib/facter/custom_facts/util/normalization.rb:29:in `normalize'
/Users/josh/work/facter/lib/facter/custom_facts/core/resolvable.rb:71:in `value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:209:in `block in find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `each'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:133:in `block (2 levels) in value'

Note that Date is missing from https://github.com/puppetlabs/facter/blob/0654f8f6858ca0163680b3d8dfc53d90345f0f35/lib/facter/custom_facts/util/normalization.rb#L8, but facter does accept Date: https://github.com/puppetlabs/facter/blob/0654f8f6858ca0163680b3d8dfc53d90345f0f35/lib/facter/custom_facts/util/normalization.rb#L19.

So we probably need to add Date to VALID_TYPES...

However, if facter serializes a Date, it does so using Date#to_s which will be different for different locales. I would expect facter to always serialize using a well known format like ISO 8601.

That said facter 3 did not support date or time types (https://github.com/puppetlabs/facter/blob/3.x/lib/schema/facter.yaml#L5-L14) so I'm inclined to think facter 4 shouldn't either, and Date should be removed from the case statement.

Add Comment Add Comment
 
This message was sent by Atlassian Jira (v8.5.2#805002-sha1:a66f935)
Atlassian logo

Josh Cooper (Jira)

unread,
Jan 29, 2021, 3:18:03 PM1/29/21
to puppe...@googlegroups.com
Josh Cooper updated an issue
Change By: Josh Cooper
If a custom fact returns a {{Time}} value, then facter will report:

{noformat}

$ mkdir facts
$ cat > facts/time.rb <<END
Facter.add(:now) do
  setcode { Time.now }
end
END
$ bx facter --custom-dir /Users/josh/work/facter/facts --trace
[2021-01-29 11:59:02.519014 ] ERROR Facter - Fact resolution fact='now', resolution='<anonymous>' resolved to an invalid value: Expected 2021-01-29 11:59:02 -0800 to be one of [Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash], but was Time
backtrace:
/Users/josh/work/facter/lib/facter/custom_facts/util/normalization.rb:29:in `normalize'
/Users/josh/work/facter/lib/facter/custom_facts/core/resolvable.rb:71:in `value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:209:in `block in find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `each'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:133:in `block (2 levels) in value'
{noformat}

Note
that the error message contains:

{ noformat}

[Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash], but was Time
{ noformat}

Note {{
Date}} is missing from the list of [valid types| https://github.com/puppetlabs/facter/blob/0654f8f6858ca0163680b3d8dfc53d90345f0f35/lib/facter/custom_facts/util/normalization.rb#L8 ] , but facter does accept {{Date}}: https://github.com/puppetlabs/facter/blob/0654f8f6858ca0163680b3d8dfc53d90345f0f35/lib/facter/custom_facts/util/normalization.rb#L19.

So we probably need to add Date to {{VALID_TYPES}}...

- However, if facter serializes a Date, it does so using {{Date#to_s}} which will be different for different locales. I would expect facter to always serialize using a well known format like ISO 8601. - Facter calls {{Date#to_s}} which serializes as ISO 8601. It might be better to make that explicit {{#Date#iso8601}}.

That said facter 3 did not support date or time types (https://github.com/puppetlabs/facter/blob/3.x/lib/schema/facter.yaml#L5-L14) so I'm inclined to think facter 4 shouldn't either, and {{Date}} should be removed from the case statement.

Mihai Buzgau (Jira)

unread,
Mar 2, 2021, 7:23:56 PM3/2/21
to puppe...@googlegroups.com
Mihai Buzgau updated an issue
Change By: Mihai Buzgau
Sprint: NW - 2021-03-17

Mihai Buzgau (Jira)

unread,
Mar 2, 2021, 7:23:56 PM3/2/21
to puppe...@googlegroups.com

Mihai Buzgau (Jira)

unread,
Mar 3, 2021, 5:50:03 AM3/3/21
to puppe...@googlegroups.com

Mihai Buzgau (Jira)

unread,
Mar 10, 2021, 9:08:03 AM3/10/21
to puppe...@googlegroups.com

Ciprian Badescu (Jira)

unread,
Mar 11, 2021, 9:49:02 AM3/11/21
to puppe...@googlegroups.com

Josh Cooper (Jira)

unread,
Mar 16, 2021, 12:45:03 PM3/16/21
to puppe...@googlegroups.com
Josh Cooper updated an issue
Change By: Josh Cooper
If a custom fact returns a {{Time}} value, then facter will report:

{noformat}
$ mkdir facts
$ cat > facts/time.rb <<END
Facter.add(:now) do
  setcode { Time.now }
end
END
$ bx facter --custom-dir /Users/josh/work/facter/facts --trace
[2021-01-29 11:59:02.519014 ] ERROR Facter - Fact resolution fact='now', resolution='<anonymous>' resolved to an invalid value: Expected 2021-01-29 11:59:02 -0800 to be one of [Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash], but was Time
backtrace:
/Users/josh/work/facter/lib/facter/custom_facts/util/normalization.rb:29:in `normalize'
/Users/josh/work/facter/lib/facter/custom_facts/core/resolvable.rb:71:in `value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:209:in `block in find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `each'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:207:in `find_first_real_value'
/Users/josh/work/facter/lib/facter/custom_facts/util/fact.rb:133:in `block (2 levels) in value'
{noformat}

Note the error message contains:

{noformat}
[Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash], but was Time
{noformat}



So we probably need to add Date to {{VALID_TYPES}}...

-However, if facter serializes a Date, it does so using {{Date#to_s}} which will be different for different locales. I would expect facter to always serialize using a well known format like ISO 8601.- Facter calls {{Date#to_s}} which serializes as ISO 8601. It might be better to make that explicit {{#Date#iso8601}}.

That said facter 3 did not
support emit core facts as date or time types ( [ https://github.com/puppetlabs/facter/blob/3.x/lib/schema/facter.yaml#L5-L14 ] ) so I , however, it didn ' m inclined to think facter 4 shouldn' t either, and restrict custom facts from doing so. In those cases the fact was serialized as {{ to_s}}:

{noformat}
# mkdir -p /etc/puppetlabs/code/environments/production/modules/customfacts/lib/facter
# cat <<END > /etc/puppetlabs/code/environments/production/modules/customfacts/lib/facter/types.rb
require 'date'

Facter.add(:custom_date) do
  setcode {
Date .new(2020) }
end

Facter.add(:custom_time) do
  setcode { Time.now
} should be removed from the case statement
end
END
# chown -R puppet:puppet /etc/puppetlabs/code/environments/production/modules
# puppet agent -t
. ..
# cat /opt/puppetlabs/server/data/puppetserver/server_data/facts/<name>.json | jq . | grep -E '(custom_(date|time))|puppetversion'
    "custom_date": "2020-01-01",
    "custom_time": "2021-03-11 17:58:55 +0000",
    "puppetversion": "6.22.0",
{noformat}

Josh Cooper (Jira)

unread,
Mar 16, 2021, 12:46:03 PM3/16/21
to puppe...@googlegroups.com
That said facter Facter 3 did not emit core facts as date or time ([https://github.com/puppetlabs/facter/blob/3.x/lib/schema/facter.yaml#L5-L14]), however, it didn't restrict custom facts from doing so. In those cases the fact was serialized as {{to_s}}:

{noformat}
# mkdir -p /etc/puppetlabs/code/environments/production/modules/customfacts/lib/facter
# cat <<END > /etc/puppetlabs/code/environments/production/modules/customfacts/lib/facter/types.rb
require 'date'

Facter.add(:custom_date) do
  setcode { Date.new(2020) }
end

Facter.add(:custom_time) do
  setcode { Time.now }
end
END
# chown -R puppet:puppet /etc/puppetlabs/code/environments/production/modules
# puppet agent -t
...
# cat /opt/puppetlabs/server/data/puppetserver/server_data/facts/<name>.json | jq . | grep -E '(custom_(date|time))|puppetversion'
    "custom_date": "2020-01-01",
    "custom_time": "2021-03-11 17:58:55 +0000",
    "puppetversion": "6.22.0",
{noformat}

Mihai Buzgau (Jira)

unread,
Mar 17, 2021, 4:31:03 AM3/17/21
to puppe...@googlegroups.com
Mihai Buzgau assigned an issue to Gheorghe Popescu
Change By: Mihai Buzgau
Assignee: Dorin Pleava Gheorghe Popescu

Mihai Buzgau (Jira)

unread,
Mar 17, 2021, 8:00:05 AM3/17/21
to puppe...@googlegroups.com
Mihai Buzgau updated an issue
Change By: Mihai Buzgau
Sprint: NW - 2021-03-17 , NW - 2021-03-31

Gheorghe Popescu (Jira)

unread,
Mar 30, 2021, 3:13:03 AM3/30/21
to puppe...@googlegroups.com
Gheorghe Popescu updated an issue
Change By: Gheorghe Popescu
Release Notes Summary: Add Date and Time as supported values for custom facts.

Gheorghe Popescu (Jira)

unread,
Mar 30, 2021, 3:14:03 AM3/30/21
to puppe...@googlegroups.com

Josh Cooper (Jira)

unread,
Mar 30, 2021, 11:42:09 AM3/30/21
to puppe...@googlegroups.com
Josh Cooper updated an issue
Change By: Josh Cooper
Fix Version/s: FACT 4.0.53

Claire Cadman (Jira)

unread,
Apr 13, 2021, 9:33:04 AM4/13/21
to puppe...@googlegroups.com
Claire Cadman updated an issue
Change By: Claire Cadman
Labels: doc_reviewed
This message was sent by Atlassian Jira (v8.13.2#813002-sha1:c495a97)
Atlassian logo
Reply all
Reply to author
Forward
0 new messages