Class: SafeDb::UseCase

Inherits:
Object
  • Object
show all
Defined in:
lib/usecase/cmd.rb

Overview

The parent SafeDb use case is designed to be extended by the cli (command line) use cases like Open, Put and Lock because it describes behaviour common to at least two (but usually more) of the use cases.

Common Use Case Behaviour

This UseCase use case is designed to be extended and does preparatory work to create favourable and useful conditions to make use cases readable, less repetitive, simpler and concise.

Machine (Workstation) Configuration File

The global configuration filepath is found off the home directory using Dir.home.

~/.safedb.net/safedb.net.configuration.ini

The global configuration file in INI format is managed through the methods

  • grab read the value at key_name from the default section

  • stash put directive key/value pair in default section

  • read read the value at key_name from the parameter section

  • write put directive key/value pair in parameter section

Constant Summary collapse

ENV_VAR_PREFIX_A =

This prefix denotes keys and their values should be posted as environment variables within the context (for example terraform) before instigating the main action like terraform apply.

"evar."
ENV_VAR_PREFIX_B =
"@evar."
FILE_KEY_PREFIX =

This prefix precedes keynames whose map value represents a file object.

"file::"
FILE_CONTENT_KEY =

The base64 encoded representation of the file content is placed into a map with this keyname.

"content64"
FILE_NAME_KEY =

The file base name is placed into a map with this keyname.

"filename"
COMMANDMENT =

This is the root command typed into the shell to invoke one of the safe use cases.

"safe"
ENV_VAR_KEY_NAME =

The name of the environment variable that will hold the session token generated by SafeDb::UseCase.selfself.generate_session_token. This environment variable is typically instantiated either manually (for ad-hoc use) or through media such as dot profile.

"SAFE_TTY_TOKEN"
APP_DIR_NAME =

If and when this command line credentials management app needs to write any configuration directives to the machine’s userspace it will use this constant to denote the (off-home) directory name.

"safedb.net"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUseCase

This use case is initialized primary by resolving the configured general and use case specific facts. To access the general facts, a domain name is expected in the parameter delegated by the extension use case classes.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/usecase/cmd.rb', line 238

def initialize

  class_name = self.class.name.split(":").last.downcase
  is_no_token_usecase = [ "token", "init", "id" ].include? class_name
  return if is_no_token_usecase

  exit(100) unless ops_key_exists?

  fact_filepath = File.sister_filepath( self, "ini", :execute )
  log.info(x) { "Search location for INI factfile is [#{fact_filepath}]" }
  return unless File.exists?( fact_filepath )

  @facts = FactFind.new()
  add_secret_facts @facts
  @facts.assimilate_ini_file( fact_filepath )
  @dictionary = @facts.f[ @facts.to_symbol( class_name ) ]

end

Instance Attribute Details

#from_script=(value) ⇒ Object (writeonly)

This variable should be set to true if the use case call originates from a shell different from the one through which the login ocurred.

To proceed, the shell that hosted the safe login must be a parent or at least an ancestor of this shell.

This variable need not be set if the login shell is the direct parent of this one (the every day manual usage scenario).

If however the login occurred from a grand or great grandparent shell (as is the case when nested scripts make an agent-like call), this variable must be set to true.



44
45
46
# File 'lib/usecase/cmd.rb', line 44

def from_script=(value)
  @from_script = value
end

Instance Method Details

#check_post_conditionsObject

After the main flow of events certain state conditions must hold true thus demonstrating that the observable value has indeed ben delivered.

Child classes should subclass this method and place any post execution (post condition) checks in it and then make a call to this method through the “super” keyword.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/usecase/cmd.rb', line 186

def check_post_conditions

  begin

    post_validation

  rescue OpenError::CliError => e

    puts ""
    puts "Your command did not complete successfully."
    puts "Post validation checks failed."
    puts ""
    puts "   => #{e.message}"
    ####        puts "   => #{e.culprit}"
    puts ""
    abort e.message
  end

end

#check_pre_conditionsObject

Validate the input parameters and check that the current state is perfect for executing the use case.

If either of the above fail - the validation function should set a human readable string and then throw an exception.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/usecase/cmd.rb', line 153

def check_pre_conditions

  begin

    pre_validation

  rescue OpenError::CliError => e

    puts ""
    puts "Your command did not complete successfully."
    puts "Pre validation checks failed."
    puts ""
    puts "   => #{e.message}"
    puts ""
    abort e.message
  end

end

#cleanupObject

If the use case validation went well, the memorization went well the



229
230
231
# File 'lib/usecase/cmd.rb', line 229

def cleanup

end

#config_directoryString

This method returns the absolute path to the directory that the configuration file sits in. It is basically just the dot prefixed context name (the APP_DIR_NAME constant).

~/.<<context-name>>

Returns:

  • (String)

    path to directory holding context configuration file



130
131
132
# File 'lib/usecase/cmd.rb', line 130

def config_directory
  return File.join( Dir.home, ".#{APP_DIR_NAME}" )
end

#config_fileString

The path to the initial configuration file below the user’s home directory. The directory name the configuration file sits in is a dot prefixed context name derived from the value inside the APP_DIR_NAME constant.

~/.<<context-name>>/<<context-name>>-configuration.ini

You can see the filename too is derived from the context with a trailing string ending in .ini.

Returns:

  • (String)

    full path to the context configuration file



118
119
120
# File 'lib/usecase/cmd.rb', line 118

def config_file
  return File.join config_directory(), "#{APP_DIR_NAME}.configuration.ini"
end

#executeObject

Execute the main flow of events of the use case. Any exceptions thrown are captured and if the instance variale [@human_readable_message] is set - tell the user about it. Without any message - just tell the user something went wrong and tell them where the logs are that might carry more information.



222
223
224
# File 'lib/usecase/cmd.rb', line 222

def execute

end

#flow_of_eventsObject

Execute the use cases’s flow from beginning when you validate the input and parameters through the memorize, execute and the final cleanup.



138
139
140
141
142
143
144
145
# File 'lib/usecase/cmd.rb', line 138

def flow_of_events

  check_pre_conditions
  execute
  cleanup
  check_post_conditions

end

#get_master_databaseHash

Get the master database. This behaviour can only complete correctly if a successful login precedes this call either in this or an ancestral shell environment.

Returns:

  • (Hash)

    the hash data structure returned represents the master database.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/usecase/cmd.rb', line 88

def get_master_database

  begin

    log.info(x) { "Request for master db with from_script set to #{@from_script}" }
    return KeyApi.read_master_db( @from_script )

  rescue OpenSSL::Cipher::CipherError => e

    log.fatal(x) { "Exception getting master db for the safe book." }
    log.fatal(x) { "The from_script parameter came set as [ #{@from_script} ]" }
    log.fatal(x) { "The exception message is ~> [[ #{e.message} ]]" }
    e.backtrace.log_lines
    abort e.message

  end

end

#post_validationObject

Child classes should subclass this method and place any post execution (post condition) checks in it and then make a call to this method through the “super” keyword if this method gets any global behaviour in it worth calling.



211
212
213
# File 'lib/usecase/cmd.rb', line 211

def post_validation

end

#pre_validationObject

Override me if you need to



174
175
176
# File 'lib/usecase/cmd.rb', line 174

def pre_validation

end