Module: Ruby::Rego::Builtins::Uuid

Extended by:
RegistryHelpers
Defined in:
lib/ruby/rego/builtins/uuid.rb

Overview

UUID parsing (uuid.parse), matching OPA’s internal/uuid (a port of google/uuid). The result carries the version and variant for every UUID, plus the decoded time, node id, MAC bits, and clock sequence for the time-based versions 1 and 2 (and the DCE id/domain for version 2). A non-string or an unparseable UUID yields undefined.

uuid.rfc4122(key) returns a random RFC 4122 version-4 UUID. Impure: within one evaluation OPA memoizes the result by the key (same key → same UUID), which the per-evaluation overlay in Evaluator#impure_registry supplies; this module’s base handler generates a fresh UUID per call and is the value outside an evaluation. The key accepts any value at runtime — OPA’s type-checker requires a string literal, but the gem evaluates at runtime semantics (it has no compile-time type checker), like its other builtins. rubocop:disable Metrics/ModuleLength

Constant Summary collapse

EPOCH_100NS =

100ns intervals between the RFC 4122 epoch (1582-10-15) and the Unix epoch.

122_192_928_000_000_000
DOMAINS =

DCE Security (version 2) domain names; an unknown domain renders as “Domain".

{ 0 => "Person", 1 => "Group", 2 => "Org" }.freeze
HYPHEN_POSITIONS =
[8, 13, 18, 23].freeze
TIME_BASED_VERSIONS =
[1, 2].freeze
UUID_FUNCTIONS =
{
  "uuid.parse" => { arity: 1, handler: :parse },
  "uuid.rfc4122" => { arity: 1, handler: :rfc4122 }
}.freeze

Class Method Summary collapse

Methods included from RegistryHelpers

register_configured_functions

Class Method Details

.generateString

Returns a fresh random RFC 4122 version-4 UUID.

Returns:

  • (String)

    a fresh random RFC 4122 version-4 UUID



57
58
59
# File 'lib/ruby/rego/builtins/uuid.rb', line 57

def self.generate
  SecureRandom.uuid
end

.parse(value) ⇒ Hash, Ruby::Rego::UndefinedValue

Parses a UUID string into its components, or undefined if it is not a valid UUID.

Parameters:

Returns:



65
66
67
68
69
70
71
# File 'lib/ruby/rego/builtins/uuid.rb', line 65

def self.parse(value)
  Base.assert_type(value, expected: StringValue, context: "uuid.parse")
  bytes = decode(value.value)
  return UndefinedValue.new unless bytes

  fields(bytes)
end

.register!Ruby::Rego::Builtins::BuiltinRegistry



39
40
41
42
43
# File 'lib/ruby/rego/builtins/uuid.rb', line 39

def self.register!
  registry = BuiltinRegistry.instance
  register_configured_functions(registry, UUID_FUNCTIONS)
  registry
end

.rfc4122(_key) ⇒ String

The base (out-of-evaluation) handler: a fresh random v4 UUID per call. During evaluation the overlay in Evaluator#impure_registry shadows this to memoize by the key argument, so the key is ignored here. See the module doc.

Parameters:

Returns:

  • (String)


52
53
54
# File 'lib/ruby/rego/builtins/uuid.rb', line 52

def self.rfc4122(_key)
  generate
end