Class: SafeDb::Rename

Inherits:
UseCase show all
Defined in:
lib/usecase/update/rename.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

Example | Bill Clinton’s Secrets

In our fictitious example Bill Clinton uses safe to lock away the names and dates of his lady friends.

$ safe init [email protected]
$ safe open my/friends

$ safe put monica/surname lewinsky
$ safe put monica/from "April 1989"
$ safe put monica/to "September 1994"

$ safe put hilary/surname clinton
$ safe put hilary/from "January 1988"
$ safe put hilary/to "Present Day"

$ safe lock

Soon follow up use cases will be unveiled, enabling us to

  • get

  • read

  • list

  • look

  • peep and

  • peek

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

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.



58
59
60
# File 'lib/usecase/update/rename.rb', line 58

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.



58
59
60
# File 'lib/usecase/update/rename.rb', line 58

def secret_value=(value)
  @secret_value = value
end

Instance Method Details

#executeObject

The put use case follows open and it adds secrets into an (encrypted at rest) envelope. Put can be called many times to add secrets. Finally the lock use case commits 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

How to Pretty Print a Hash in JSON Format

This pretty prints a Hash (dictionary) data structure in JSON format.

puts "---\n"
puts JSON.pretty_generate( master_db )
puts "---\n"


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/usecase/update/rename.rb', line 93

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 )
  print_put_success

# --->      secret_ids = @secret_id.split("/")
# --->      if ( envelope.has_key? secret_ids.first )
# --->        envelope[secret_ids.first][secret_ids.last] = @secret_value
# --->      else
# --->        envelope[secret_ids.first] = { secret_ids.last => @secret_value }
# --->      end

end