Class: CfnVpn::Actions::Share

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/cfnvpn/actions/share.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



19
20
21
# File 'lib/cfnvpn/actions/share.rb', line 19

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#add_routesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cfnvpn/actions/share.rb', line 35

def add_routes
  if @options['ignore_routes']
    CfnVpn::Log.logger.debug "Ignoring routes pushed by the client vpn"
    @config.concat("\nroute-nopull\n")
    vpn = CfnVpn::ClientVpn.new(@name,@options['region'])
    routes = vpn.get_route_with_mask
    CfnVpn::Log.logger.debug "Found routes #{routes}"
    routes.each do |r|
      @config.concat("route #{r[:route]} #{r[:mask]}\n")
    end
    dns_servers = vpn.get_dns_servers()
    if dns_servers.any?
      CfnVpn::Log.logger.debug "Found DNS servers #{dns_servers.join(' ')}"
      @config.concat("dhcp-option DNS #{dns_servers.first}\n")
    end
  end
end

#copy_config_to_s3Object



27
28
29
30
31
32
33
# File 'lib/cfnvpn/actions/share.rb', line 27

def copy_config_to_s3
  vpn = CfnVpn::ClientVpn.new(@name,@options['region'])
  CfnVpn::Log.logger.debug "downloading client config for #{vpn.endpoint_id}"
  @config = vpn.get_config()
  string = (0...8).map { (65 + rand(26)).chr.downcase }.join
  @config.sub!(vpn.endpoint_id, "#{string}.#{vpn.endpoint_id}")
end

#display_instructionsObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cfnvpn/actions/share.rb', line 68

def display_instructions
  CfnVpn::Log.logger.info "Share the bellow instruction with the user..."
  say "\nDownload the certificates and config from the bellow presigned URLs which will expire in 1 hour."
  say "\nCertificate:"
  say "\tcurl #{@certificate_url} > #{@options['client_cn']}.tar.gz", :cyan
  say "\nConfig:\n"
  say "\tcurl #{@config_url} > #{@name}.config.ovpn", :cyan
  say "\nExtract the certificates from the tar and place into a safe location."
  say "\ttar xzfv #{@options['client_cn']}.tar.gz -C <path> --strip 2", :cyan
  say "\nModify #{@name}.config.ovpn to include the full location of your extracted certificates"
  say "\techo \"key /<path>/#{@options['client_cn']}.key\" >> #{@name}.config.ovpn", :cyan
  say "\techo \"cert /<path>/#{@options['client_cn']}.crt\" >> #{@name}.config.ovpn", :cyan
  say "\nOpen #{@name}.config.ovpn with your favourite openvpn client."
end

#get_certificate_urlObject



58
59
60
61
# File 'lib/cfnvpn/actions/share.rb', line 58

def get_certificate_url
  @certificate_url = @s3.get_url("#{@options['client_cn']}.tar.gz")
  CfnVpn::Log.logger.debug "Certificate presigned url: #{@certificate_url}"
end

#get_config_urlObject



63
64
65
66
# File 'lib/cfnvpn/actions/share.rb', line 63

def get_config_url
  @config_url = @s3.get_url("#{@name}.config.ovpn")
  CfnVpn::Log.logger.debug "Config presigned url: #{@config_url}"
end

#set_loglevelObject



23
24
25
# File 'lib/cfnvpn/actions/share.rb', line 23

def set_loglevel
  CfnVpn::Log.logger.level = Logger::DEBUG if @options['verbose']
end

#upload_configObject



53
54
55
56
# File 'lib/cfnvpn/actions/share.rb', line 53

def upload_config
  @s3 = CfnVpn::S3.new(@options['region'],@options['bucket'],@name)
  @s3.store_config(@config)
end