Class: Sekrets

Inherits:
Object
  • Object
show all
Extended by:
Blowfish
Defined in:
lib/sekrets.rb

Defined Under Namespace

Modules: Blowfish

Class Method Summary collapse

Methods included from Blowfish

cipher, cycle, decrypt, encrypt, recrypt

Class Method Details

.ask(question) ⇒ Object



126
127
128
129
# File 'lib/sekrets.rb', line 126

def Sekrets.ask(question)
  @highline ||= HighLine.new
  @highline.ask(prompt_for(question))
end

.binstubObject



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/sekrets.rb', line 269

def Sekrets.binstub
  @binstub ||= (
    unindent(
      <<-'_v_'
        #! /usr/bin/env ruby

        require 'pathname'
        ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
          Pathname.new(__FILE__).realpath)

        require 'rubygems'
        require 'bundler/setup'

        dirname = File.dirname(__FILE__)

        root = File.dirname(dirname)

        ciphertext = File.expand_path('ciphertext', dirname)

        case ARGV.size
          when 0
            ENV['SEKRETS_ARGV'] = "edit #{ ciphertext }"
          when 1
            ENV['SEKRETS_ARGV'] = "#{ ARGV[0] } #{ ciphertext }"
        end

        key = File.join(root, '.sekrets.key')

        if test(?s, key)
          ENV['SEKRETS_KEY'] = IO.binread(key).strip
        end

        exec(Gem.bin_path('sekrets', 'sekrets'))
      _v_
    )
  )
end

.console?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/sekrets.rb', line 132

def Sekrets.console?
  STDIN.tty?
end

.key_for(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/sekrets.rb', line 17

def Sekrets.key_for(*args)
  options = Map.options_for!(args)
  path = args.shift || options[:path]

  if options.has_key?(:key)
    key = options[:key]
    return(key)
  end

  path = path_for(path)

  if path
    dirname, basename = File.split(path)

    keyfiles =
      Coerce.list_of_strings(
        [:keyfile, :keyfiles].map{|k| options[k]},
        %W[ #{ dirname }/.#{ basename }.key #{ dirname }/.#{ basename }.k ]
      )

    keyfiles.each do |file|
      if test(?s, file)
        key = IO.binread(file).strip
        return(key)
      end
    end
  end

  if Sekrets.project_key and test(?s, Sekrets.project_key)
    return IO.binread(Sekrets.project_key).strip
  end

  env_key = (options[:env] || Sekrets.env).to_s
  if ENV.has_key?(env_key)
    key = ENV[env_key]
    return(key)
  end

  if Sekrets.global_key and test(?s, Sekrets.global_key)
    return IO.binread(Sekrets.global_key).strip
  end

  unless options[:prompt] == false
    if console?
      key = Sekrets.ask(path)
      return(key)
    end
  end

  return nil
end

.key_for!(*args, &block) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
# File 'lib/sekrets.rb', line 70

def Sekrets.key_for!(*args, &block)
  key = Sekrets.key_for(*args, &block)
  raise(ArgumentError, 'no key!') unless key
  key
end

.openr(arg, &block) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/sekrets.rb', line 220

def Sekrets.openr(arg, &block)
  opened = false

  io =
    case
      when arg.respond_to?(:read)
        arg
      when arg.to_s.strip == '-'
        STDIN
      else
        opened = true
        open(arg, 'rb+')
    end

  close =
    proc do
      io.close if opened
    end

  if block
    begin
      block.call(io)
    ensure
      close.call
    end
  else
    at_exit{ close.call }
    io
  end
end

.openw(arg, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sekrets.rb', line 180

def Sekrets.openw(arg, &block)
  opened = false
  atomic_move = proc{}

  io =
    case
      when arg.respond_to?(:read)
        arg
      when arg.to_s.strip == '-'
        STDOUT
      else
        opened = true
        path = File.expand_path(arg.to_s)
        dirname, basename = File.split(path)
        FileUtils.mkdir_p(dirname)
        tmp = path + ".sekrets.tmp.#{ Process.ppid }.#{ Process.pid }"
        at_exit{ FileUtils.rm_f(tmp) }
        atomic_move = proc{ FileUtils.mv(tmp, path) }
        open(tmp, 'wb+')
    end

  close =
    proc do
      io.close if opened
      atomic_move.call
    end

  if block
    begin
      block.call(io)
    ensure
      close.call
    end
  else
    at_exit{ close.call }
    io
  end
end

.path_for(object) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/sekrets.rb', line 252

def Sekrets.path_for(object)
  path = nil

  if object.is_a?(String) or object.is_a?(Pathname)
    return(path = object.to_s)
  end

  [:original_path, :original_filename, :path, :filename, :pathname].each do |msg|
    if object.respond_to?(msg)
      path = object.send(msg)
      break
    end
  end

  path
end

.prompt_for(*words) ⇒ Object



121
122
123
# File 'lib/sekrets.rb', line 121

def Sekrets.prompt_for(*words)
  ["sekrets:", words, "> "].flatten.compact.join(' ')
end

.read(*args, &block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sekrets.rb', line 77

def Sekrets.read(*args, &block)
  options = Map.options_for!(args)
  path = args.shift || options[:path]
  key = args.shift || Sekrets.key_for!(path, options)

  return nil unless test(?s, path)

  encrypted = IO.binread(path)
  decrypted = Sekrets.decrypt(key, encrypted)
  new(decrypted)
end

.settings_for(*args, &block) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/sekrets.rb', line 110

def Sekrets.settings_for(*args, &block)
  decrypted = read(*args, &block)

  if decrypted
    expanded = ERB.new(decrypted).result(TOPLEVEL_BINDING)
    object = YAML.load(expanded)
    object.is_a?(Hash) ? Map.for(object) : object
  end
end

.system(command) ⇒ Object



168
169
170
171
172
173
174
175
176
177
# File 'lib/sekrets.rb', line 168

def Sekrets.system(command)
  if defined?(Bundler)
    msg = (Bundler.respond_to?('with_original_env') ? 'with_original_env' : 'with_clean_env')
    Bundler.send(msg){ Kernel.system(command) }
  else
    Kernel.system(command)
  end

  return $?.exitstatus
end

.tmpdir(&block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sekrets.rb', line 137

def Sekrets.tmpdir(&block)
  dirname = File.join(Dir.tmpdir, 'sekrets', Process.ppid.to_s, Process.pid.to_s, rand.to_s)

  FileUtils.mkdir_p(dirname)

  cleanup = proc do
    if dirname and test(?d, dirname)
      FileUtils.rm_rf(dirname)
    end
  end

  if block
    begin
      Dir.chdir(dirname) do
        block.call(dirname)
      end
    ensure
      cleanup.call
    end
  else
    at_exit{ cleanup.call }
    dirname
  end
end

.tmpfile_for(basename, data) ⇒ Object



162
163
164
165
166
# File 'lib/sekrets.rb', line 162

def Sekrets.tmpfile_for(basename, data)
  tmpfile = File.join(Sekrets.tmpdir, basename)
  IO.binwrite(tmpfile, data)
  tmpfile
end

.unindent(string) ⇒ Object



307
308
309
310
# File 'lib/sekrets.rb', line 307

def Sekrets.unindent(string)
  indent = string.split("\n").select {|line| !line.strip.empty? }.map {|line| line.index(/[^\s]/) }.compact.min || 0
  string.gsub(/^[[:blank:]]{#{indent}}/, '')
end

.unindent!(string) ⇒ Object



312
313
314
# File 'lib/sekrets.rb', line 312

def Sekrets.unindent!(string)
  string.replace(string.unindent)
end

.write(*args, &block) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sekrets.rb', line 90

def Sekrets.write(*args, &block)
  options = Map.options_for!(args)
  path = args.shift || options[:path]
  content = args.shift || options[:content]
  key = args.shift || Sekrets.key_for!(path, options)

  dirname, basename = File.split(File.expand_path(path))
  FileUtils.mkdir_p(dirname)

  encrypted = Sekrets.encrypt(key, content)

  tmp = path + '.tmp'
  IO.binwrite(tmp, encrypted)
  FileUtils.mv(tmp, path)

  encrypted
  new(encrypted)
end