174
175
176
177
178
179
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
218
219
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
|
# File 'lib/puppet/parser/functions/ssh_keygen.rb', line 174
newfunction(:ssh_keygen, :type => :rvalue) do |args|
unless args.first.class == Hash then
raise Puppet::ParseError, "ssh_keygen(): config argument must be a Hash"
end
config = args.first
config = {
'dir' => 'ssh',
'type' => 'rsa',
'hostkey' => false,
'hostaliases' => nil,
'authkey' => false,
'request' => nil,
'comment' => nil,
'as_hash' => false,
}.merge(config)
if config['request'].nil?
raise Puppet::ParseError, "ssh_keygen(): request argument is required"
end
request = config['request']
if config['name'].nil? and (request != 'authorized_keys' and request != 'known_hosts')
raise Puppet::ParseError, "ssh_keygen(): name argument is required"
end
if config['comment'].nil?
hostname = lookupvar('hostname')
if config['hostkey'] == true
config['comment'] = hostname
elsif config['authkey'] == true
config['comment'] = "root@#{hostname}"
end
end
fullpath = "/etc/puppet/#{config['dir']}"
init(fullpath)
create_key_if_not_exists(
fullpath,
config['name'],
config['comment'],
config['type'],
config['hostkey'],
config['hostaliases'],
config['authkey'],
config['request']
)
begin
keyfile = "#{fullpath}/#{config['name']}"
case config['request']
when "public"
return get_pubkey(keyfile)
when "private"
return get_privkey(keyfile)
when "known_hosts"
return get_known_hosts(fullpath)
when "authorized_keys"
return get_authorized_keys(fullpath, config['as_hash'])
end
rescue => e
raise Puppet::ParseError, "ssh_keygen(): unable to fulfill request '#{config['request']}': #{e}"
end
end
|