Class: SafeDb::Put

Inherits:
UseCase show all
Defined in:
lib/usecase/put.rb

Overview

The put use case follows open and it adds secrets into an (encrypted at rest) envelope. Put can be called many times and when done, the lock use case can be called to commit all opened secrets into the configured storage engines.

Calling put before calling open or after calling lock is not allowed and will result in an error.

Put Pre-Conditions

When the put use case is called - the below conditions ring true.

  • the folder path ending in ../../my must exist

  • a session id, filename and encryption key ( in workstation config )

Observable Value

The observable value delivered by put boils down to

  • a new friends.xyz123abc.os.txt file if this is the first put.

  • a new group_name/key_name (like monica/surname) entry is added if required

  • a secret value is added against the key or updated if it already exists

  • a new session id and encryption key is generated and used to re-encrypt

Constant Summary

Constants inherited from UseCase

UseCase::APP_DIR_NAME, UseCase::COMMANDMENT, UseCase::ENV_VAR_KEY_NAME, UseCase::ENV_VAR_PREFIX_A, UseCase::ENV_VAR_PREFIX_B, UseCase::FILE_CONTENT_KEY, UseCase::FILE_KEY_PREFIX, UseCase::FILE_NAME_KEY

Instance Attribute Summary collapse

Attributes inherited from UseCase

#from_script

Instance Method Summary collapse

Methods inherited from UseCase

#check_post_conditions, #check_pre_conditions, #cleanup, #config_directory, #config_file, #flow_of_events, #get_master_database, #initialize, #post_validation, #pre_validation

Constructor Details

This class inherits a constructor from SafeDb::UseCase

Instance Attribute Details

#secret_id=(value) ⇒ Object (writeonly)

Sets the attribute secret_id

Parameters:

  • value

    the value to set the attribute secret_id to.



30
31
32
# File 'lib/usecase/put.rb', line 30

def secret_id=(value)
  @secret_id = value
end

#secret_value=(value) ⇒ Object (writeonly)

Sets the attribute secret_value

Parameters:

  • value

    the value to set the attribute secret_value to.



30
31
32
# File 'lib/usecase/put.rb', line 30

def secret_value=(value)
  @secret_value = value
end

Instance Method Details

#executeObject

Execute the act of putting a string key and string value pair into a map at the chapter and verse location, overwriting if need be.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/usecase/put.rb', line 34

def execute

  return unless ops_key_exists?
  master_db = KeyApi.read_master_db()

  return if unopened_envelope?( master_db )

  envelope_id = ENVELOPE_KEY_PREFIX + master_db[ ENV_PATH ]
  has_content = KeyApi.db_envelope_exists?( master_db[ envelope_id ] )

  # To get hold of the content we must either
  #
  #   a) unlock it using the breadcrumbs or
  #   b) start afresh with a new content db
  content_box = KeyDb.from_json( KeyApi.content_unlock( master_db[ envelope_id ] ) ) if has_content
  content_box = KeyDb.new() unless has_content
  content_hdr = create_header()

  # If no content envelope exists we need to place
  # an empty one inside the appdb content database.
  master_db[ envelope_id ] = {} unless has_content

  # This is the PUT use case so we append a
  #
  #   a) key for the new dictionary entry
  #   b) value for the new dictionary entry
  #
  # into the current content envelope and write
  # the envelope to the content filepath.
  crumbs_dict = master_db[ envelope_id ]
  content_box.create_entry( master_db[ KEY_PATH ], @secret_id, @secret_value )
  KeyApi.content_lock( crumbs_dict, content_box.to_json, content_hdr )

  # Three envelope crumbs namely the external ID, the
  # random iv and the crypt key are written afresh into
  # the master database.
  KeyApi.write_master_db( content_hdr, master_db )

  # Show the mini dictionary at the opened chapter and verse location
  Show.new.flow_of_events

end