Class: Caco::Ssh::AuthorizedKeysAdd

Inherits:
Trailblazer::Operation
  • Object
show all
Defined in:
lib/caco/ssh/authorized_keys_add.rb

Instance Method Summary collapse

Instance Method Details

#add_key(ctx, key:, identifier:, output:) ⇒ Object



43
44
45
46
47
# File 'lib/caco/ssh/authorized_keys_add.rb', line 43

def add_key(ctx, key:, identifier:, output:, **)
  output << "#{key} #{identifier}\n"
  ctx[:created] = true
  ctx[:content] = output
end

#change_key(ctx, key:, identifier:, output:) ⇒ Object



37
38
39
40
# File 'lib/caco/ssh/authorized_keys_add.rb', line 37

def change_key(ctx, key:, identifier:, output:, **)
  output.gsub!(/^.*#{identifier}$/, "#{key} #{identifier}")
  ctx[:content] = output
end

#check_user_ssh_authorized_keys(ctx, ssh_home:, user:) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/caco/ssh/authorized_keys_add.rb', line 68

def check_user_ssh_authorized_keys(ctx, ssh_home:, user:, **)
  ctx[:authorized_keys_path] = authorized_keys_path = "#{ssh_home}/authorized_keys"
  unless File.exist?(authorized_keys_path)
    FileUtils.touch(authorized_keys_path)
    File.chmod(0600, authorized_keys_path)
    begin
      FileUtils.chown user, nil, authorized_keys_path
    rescue Errno::EPERM
      true
    end
  else
    true
  end
end

#check_user_ssh_folder(ctx, user:, user_home:) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/caco/ssh/authorized_keys_add.rb', line 59

def check_user_ssh_folder(ctx, user:, user_home:, **)
  ctx[:ssh_home] = ssh_home = "#{Caco.config.write_files_root}#{user_home}/.ssh"
  FileUtils.mkdir_p(ssh_home)
  File.chmod(0700, ssh_home)
  FileUtils.chown user, nil, ssh_home
rescue Errno::EPERM
  true
end