Class: KStor::Model::SecretMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/kstor/model.rb

Overview

Metadata for a secret.

This is not a “real” model object: just a helper class to convert metadata to and from database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ KStor::Model::SecretMeta

Create new metadata for a secret.

Hash param can contains keys for “app”, “database”, “login”, “server” and “url”. Any other key is ignored.

Parameters:



320
321
322
323
324
325
326
# File 'lib/kstor/model.rb', line 320

def initialize(values)
  @app = values['app']
  @database = values['database']
  @login = values['login']
  @server = values['server']
  @url = values['url']
end

Instance Attribute Details

#appObject

Secret is defined for this application



303
304
305
# File 'lib/kstor/model.rb', line 303

def app
  @app
end

#databaseObject

Secret is defined for this database



305
306
307
# File 'lib/kstor/model.rb', line 305

def database
  @database
end

#loginObject

Secret is defined for this login



307
308
309
# File 'lib/kstor/model.rb', line 307

def 
  @login
end

#serverObject

Secret is related to this server



309
310
311
# File 'lib/kstor/model.rb', line 309

def server
  @server
end

#urlObject

Secret should be used at this URL



311
312
313
# File 'lib/kstor/model.rb', line 311

def url
  @url
end

Instance Method Details

#match?(meta) ⇒ Boolean

Match against wildcards.

Metadata will be matched against another metadata object with wildcard values. This uses roughly the same rules that shell wildcards (e.g. fnmatch(3) C function).

rubocop:disable Metrics/CyclomaticComplexity

Parameters:

Returns:

  • (Boolean)

    true if matched

See Also:

  • File.fnmatch?


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/kstor/model.rb', line 366

def match?(meta)
  self_h = to_h
  other_h = meta.to_h
  other_h.each do |k, wildcard|
    val = self_h[k]
    return false if val.nil? && !wildcard.nil? && wildcard != '*'
    next if val.nil?
    next if wildcard.nil?

    key_matched = File.fnmatch?(
      wildcard, val, File::FNM_CASEFOLD | File::FNM_DOTMATCH
    )
    return false unless key_matched
  end
  true
end

#merge(other) ⇒ Object

Merge metadata.

Parameters:



349
350
351
352
353
# File 'lib/kstor/model.rb', line 349

def merge(other)
  values = to_h.merge(other.to_h)
  values.reject! { |_, v| v.empty? }
  self.class.new(values)
end

#serializeKStor::Crypto::ArmoredHash

Prepare metadata to be written to disk or database.

Returns:



341
342
343
# File 'lib/kstor/model.rb', line 341

def serialize
  Crypto::ArmoredHash.from_hash(to_h)
end

#to_hHash[String, String]

Convert this metadata to a Hash.

Empty values will not be included.

Returns:

  • (Hash[String, String])

    metadata as a Hash



333
334
335
336
# File 'lib/kstor/model.rb', line 333

def to_h
  { 'app' => @app, 'database' => @database, 'login' => @login,
    'server' => @server, 'url' => @url }.compact
end