Class: EasyCreds::CredentialsIO

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_creds/credentials_io.rb

Constant Summary collapse

GITIGNORE_RULE =
'/config/credentials/*.key'
LOCAL_GITIGNORE_RULE =
'/config/credentials/*_local.yml.enc'

Instance Method Summary collapse

Constructor Details

#initialize(root, git_repo: true) ⇒ CredentialsIO

Returns a new instance of CredentialsIO.



13
14
15
16
# File 'lib/easy_creds/credentials_io.rb', line 13

def initialize(root, git_repo: true)
  @root     = Pathname.new(root)
  @git_repo = git_repo
end

Instance Method Details

#add_gitignore_rule!Object



75
76
77
78
79
# File 'lib/easy_creds/credentials_io.rb', line 75

def add_gitignore_rule!
  return unless @git_repo

  gitignore_path.open('a') { |f| f.puts("\n#{GITIGNORE_RULE}") }
end

#add_local_gitignore_rule!Object



87
88
89
90
91
92
# File 'lib/easy_creds/credentials_io.rb', line 87

def add_local_gitignore_rule!
  return unless @git_repo
  return if gitignore_has_local_rule?

  gitignore_path.open('a') { |f| f.puts('', LOCAL_GITIGNORE_RULE) }
end

#enc_exists?(env, local: false) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/easy_creds/credentials_io.rb', line 65

def enc_exists?(env, local: false)
  enc_path(env, local: local).exist?
end

#ensure_key!(env, local: false) ⇒ Object



48
49
50
51
52
# File 'lib/easy_creds/credentials_io.rb', line 48

def ensure_key!(env, local: false)
  return if key_exists?(env, local: local)

  write_key(env, SecureRandom.hex(16), local: local)
end

#example_templateObject



94
95
96
97
98
99
100
101
# File 'lib/easy_creds/credentials_io.rb', line 94

def example_template
  path = @root.join('config/credentials/example.yml')
  return {} unless path.exist?

  YAML.safe_load(path.read, symbolize_names: true) || {}
rescue Psych::SyntaxError
  {}
end

#gitignore_has_local_rule?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/easy_creds/credentials_io.rb', line 81

def gitignore_has_local_rule?
  return false unless @git_repo

  gitignore_path.exist? && gitignore_path.read.include?(LOCAL_GITIGNORE_RULE)
end

#gitignore_has_rule?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/easy_creds/credentials_io.rb', line 69

def gitignore_has_rule?
  return false unless @git_repo

  gitignore_path.exist? && gitignore_path.read.include?(GITIGNORE_RULE)
end

#key_exists?(env, local: false) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/easy_creds/credentials_io.rb', line 31

def key_exists?(env, local: false)
  key_path(env, local: local).exist?
end

#key_value(env, local: false) ⇒ Object



35
36
37
38
39
# File 'lib/easy_creds/credentials_io.rb', line 35

def key_value(env, local: false)
  key_path(env, local: local).read.strip
rescue Errno::ENOENT
  nil
end

#read(env, local: false) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/easy_creds/credentials_io.rb', line 18

def read(env, local: false)
  content = configuration(env, local: local).read
  return {} if content.blank?

  YAML.safe_load(content, symbolize_names: true, permitted_classes: [Symbol, Date, Time]) || {}
rescue ActiveSupport::EncryptedFile::MissingKeyError
  {}
end

#read_raw(env, local: false) ⇒ Object



54
55
56
57
58
59
# File 'lib/easy_creds/credentials_io.rb', line 54

def read_raw(env, local: false)
  content = configuration(env, local: local).read
  content.presence || "--- {}\n"
rescue ActiveSupport::EncryptedFile::MissingKeyError
  "--- {}\n"
end

#write(env, hash, local: false) ⇒ Object



27
28
29
# File 'lib/easy_creds/credentials_io.rb', line 27

def write(env, hash, local: false)
  configuration(env, local: local).write(hash.deep_stringify_keys.to_yaml)
end

#write_key(env, value, local: false) ⇒ Object



41
42
43
44
45
46
# File 'lib/easy_creds/credentials_io.rb', line 41

def write_key(env, value, local: false)
  path = key_path(env, local: local)
  path.dirname.mkpath
  path.write(value)
  path.chmod(0o600)
end

#write_raw(env, yaml_str, local: false) ⇒ Object



61
62
63
# File 'lib/easy_creds/credentials_io.rb', line 61

def write_raw(env, yaml_str, local: false)
  configuration(env, local: local).write(yaml_str)
end