Class: CfnVpn::Actions::Embedded

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



21
22
23
# File 'lib/cfnvpn/actions/embedded.rb', line 21

def self.source_root
  File.dirname(__FILE__)
end

Instance Method Details

#add_routesObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/cfnvpn/actions/embedded.rb', line 59

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

#create_config_directoryObject



29
30
31
32
33
34
# File 'lib/cfnvpn/actions/embedded.rb', line 29

def create_config_directory
  @build_dir = "#{CfnVpn.cfnvpn_path}/#{@name}"
  @config_dir = "#{@build_dir}/config"
  CfnVpn::Log.logger.debug("Creating config directory #{@config_dir}")
  FileUtils.mkdir_p(@config_dir)
end

#display_urlObject



99
100
101
102
103
104
105
# File 'lib/cfnvpn/actions/embedded.rb', line 99

def display_url
  CfnVpn::Log.logger.info "Share the below instructions with the user..."
  say "\nDownload the embedded config from the below presigned URL which will expire in 1 hour."
  say "\nConfig:\n"
  say "\tcurl #{@config_url} > #{@name}_#{@cn}.config.ovpn", :cyan
  say "\nOpen #{@name}_#{@cn}.config.ovpn with your favourite openvpn client."
end

#download_certificatesObject



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

def download_certificates
  download = true
  if File.exists?("#{@config_dir}/#{@options['client_cn']}.crt")
    download = yes? "Certificates for #{@options['client_cn']} already exist in #{@config_dir}. Do you want to download again? ", :green
  end

  if download
    CfnVpn::Log.logger.info "Downloading certificates for #{@options['client_cn']} to #{@config_dir}"
    s3 = CfnVpn::S3.new(@options['region'],@options['bucket'],@name)
    s3.get_object("#{@config_dir}/#{@options['client_cn']}.tar.gz")
    cert = CfnVpn::Certificates.new(@build_dir,@name,@options['easyrsa_local'])
    CfnVpn::Log.logger.debug cert.extract_certificate(@options['client_cn'])
  end
end

#download_configObject



51
52
53
54
55
56
57
# File 'lib/cfnvpn/actions/embedded.rb', line 51

def download_config
  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

#embed_certsObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/cfnvpn/actions/embedded.rb', line 77

def embed_certs
  cert = CfnVpn::Certificates.new(@build_dir,@name,@options['easyrsa_local'])
  CfnVpn::Log.logger.debug cert.extract_certificate(@options['client_cn'])
  CfnVpn::Log.logger.debug "Reading extracted certificate and private key"
  key = File.read("#{@config_dir}/#{@options['client_cn']}.key")
  crt = File.read("#{@config_dir}/#{@options['client_cn']}.crt")
  CfnVpn::Log.logger.debug "Embedding certificate and private key into config"
  @config.concat("\n<key>\n#{key}\n</key>\n")
  @config.concat("\n<cert>\n#{crt}\n</cert>\n")
end

#get_presigned_urlObject



93
94
95
96
97
# File 'lib/cfnvpn/actions/embedded.rb', line 93

def get_presigned_url
  @cn = @options['client_cn']
  @config_url = @s3.get_url("#{@name}_#{@cn}.config.ovpn")
  CfnVpn::Log.logger.debug "Config presigned url: #{@config_url}"
end

#set_loglevelObject



25
26
27
# File 'lib/cfnvpn/actions/embedded.rb', line 25

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

#upload_embedded_configObject



88
89
90
91
# File 'lib/cfnvpn/actions/embedded.rb', line 88

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