Class: Autosign::Journal

Inherits:
Object
  • Object
show all
Defined in:
lib/autosign/journal.rb

Overview

processes are complete.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Autosign::Journal

Returns instance of the Autosign::Journal class.

Parameters:

  • settings (Hash) (defaults to: {})

    config settings for the new journal instance



17
18
19
20
21
22
# File 'lib/autosign/journal.rb', line 17

def initialize(settings = {})
  @log = Logging.logger[self.class]
  @log.debug "initializing #{self.class.name}"
  @settings = settings
  fail unless setup
end

Instance Attribute Details

#settingsHash

Returns settings of the autosign journal instance, such as the location of the journal file.

Returns:

  • (Hash)

    settings of the autosign journal instance, such as the location of the journal file



13
14
15
# File 'lib/autosign/journal.rb', line 13

def settings
  @settings
end

Instance Method Details

#add(uuid, validto, data = {}) ⇒ Object

Add a new token to the journal. Only succeeds if the token is not in the journal already.

This will only succeed if the token has not previously been added This is the primary way this class is expected to be used

Examples:

attempt adding a token to the journal

journal = Autosign::Journal.new({journalfile = '/etc/autosign/journal')
fail unless journal.add('d2e601c8-93df-4459-be18-1877eaf00920')

Parameters:

  • uuid (String)

    RFC4122 v4 UUID functioning as unique journal entry identifier

  • validto (Integer)

    POSIX timestamp in seconds since epoch that the token will be valid until

  • data (Hash) (defaults to: {})

    Arbitrary hash that will be serialized and stored in the journal for auditing purposes



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/autosign/journal.rb', line 38

def add(uuid, validto, data = {})
  @log.debug "attempting to add UUID: '#{uuid.to_s}' which is valid to '#{Time.at(validto.to_i)}' with data #{data.to_s}"
  return false unless validate_uuid(uuid)

  store = setup
  # wrap the change in a transaction because multiple autosign instances
  # may try to run simultaneously. This will block until another process
  # releases the transaction lock.
  result = store.transaction do
    # check whether the UUID is already in the store
    if store.root?(uuid)
      @log.warn "Token with UUID '#{uuid}' is already saved in the journal, will not add'"
      store.abort
    else
      # save the token identified by UUID
      store[uuid.to_s] = {:validto => validto.to_s, :data => data}
    end
  end

  # return true if the transaction went through
  return !!result
end