Class: Sekrets
Defined Under Namespace
Modules: Blowfish
Class Method Summary
collapse
Methods included from Blowfish
cipher, cycle, decrypt, encrypt, recrypt
Class Method Details
.ask(question) ⇒ Object
119
120
121
122
|
# File 'lib/sekrets.rb', line 119
def Sekrets.ask(question)
@highline ||= HighLine.new
@highline.ask(prompt_for(question))
end
|
.binstub ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/sekrets.rb', line 245
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
125
126
127
|
# File 'lib/sekrets.rb', line 125
def Sekrets.console?
STDIN.tty?
end
|
.key_for(*args) ⇒ Object
10
11
12
13
14
15
16
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
|
# File 'lib/sekrets.rb', line 10
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
63
64
65
66
67
|
# File 'lib/sekrets.rb', line 63
def Sekrets.key_for!(*args, &block)
key = Sekrets.key_for(*args, &block)
raise(ArgumentError, 'no key!') unless key
key
end
|
.openr(arg, &block) ⇒ Object
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/sekrets.rb', line 196
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/sekrets.rb', line 156
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/sekrets.rb', line 228
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
114
115
116
|
# File 'lib/sekrets.rb', line 114
def Sekrets.prompt_for(*words)
["sekrets:", words, "> "].flatten.compact.join(' ')
end
|
.read(*args, &block) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/sekrets.rb', line 70
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
103
104
105
106
107
108
109
110
111
|
# File 'lib/sekrets.rb', line 103
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
|
.tmpdir(&block) ⇒ Object
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/sekrets.rb', line 130
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
|
.unindent(string) ⇒ Object
283
284
285
286
|
# File 'lib/sekrets.rb', line 283
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
288
289
290
|
# File 'lib/sekrets.rb', line 288
def Sekrets.unindent!(string)
string.replace(string.unindent)
end
|
.write(*args, &block) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/sekrets.rb', line 83
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
|