Class: RailsDevSsl::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rails_dev_ssl.rb

Constant Summary collapse

@@dir =
''
@@config =
{}

Instance Method Summary collapse

Instance Method Details

#add_ca_to_keychainObject



47
48
49
50
# File 'lib/rails_dev_ssl.rb', line 47

def add_ca_to_keychain
  puts 'Adding rootCA.pem to system keychain'
  `sudo -p 'sudo password:' security add-trusted-cert -d -r trustRoot -k /Library/Keychains/System.keychain #{File.join(dir, 'rootCA.pem')}`
end

#display_certificateObject



24
25
26
27
28
# File 'lib/rails_dev_ssl.rb', line 24

def display_certificate
  raise 'Certificate missing. Have you generated the certificate already?' unless File.exist?(File.join(dir, 'server.crt'))

  puts `openssl x509 -text -in #{File.join(dir, 'server.crt')} -noout`
end

#generate_certificatesObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails_dev_ssl.rb', line 32

def generate_certificates
  raise "Directory (#{dir}) doesn't exist" unless Dir.exist?(dir)

  generate_config unless File.exist?(File.join(dir, 'server.csr.cnf'))
  begin
    temp_file = password_file
    safe_path = Shellwords.escape(temp_file.path)
    generate_ca(safe_path)
    generate_crt_and_key(options['pem-file'], safe_path)
  ensure
    temp_file.close!
  end
end

#generate_configObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rails_dev_ssl.rb', line 65

def generate_config
  unless options['non-interactive']
    country = ask("Enter the country of your organization [#{default_config[:C]}]")
    state = ask("Enter the state of province of your organization [#{default_config[:ST]}]")
    city = ask("Enter the city of your organization [#{default_config[:L]}]")
    org = ask("Enter your organization name [#{default_config[:O]}]")
    email = ask("Enter your email [#{default_config[:emailAddress]}]")
    domain = ask("Enter your local SSL domain [#{default_config[:CN]}]")
    @@config = { C: country, ST: state, L: city, O: org, emailAddress: email, CN: domain }
  end
  write_config_file
end

#generate_v3_ext_fileObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rails_dev_ssl.rb', line 79

def generate_v3_ext_file
  raise 'server.csr.cnf missing. run rails_dev_ssl generate_config first' unless File.exist?(File.join(dir, 'server.csr.cnf'))

  puts "\n*** generating v3.ext"
  configs = <<~CONFIG
    authorityKeyIdentifier=keyid,issuer
    basicConstraints=CA:FALSE
    keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
    subjectAltName = @alt_names

    [alt_names]
    DNS.1 = #{config['CN']}
  CONFIG
  File.open(File.join(dir, 'v3.ext'), 'w') { |file| file.write(configs) }
end

#setup(dir = File.join(Dir.pwd, 'ssl')) ⇒ Object



18
19
20
21
# File 'lib/rails_dev_ssl.rb', line 18

def setup(dir = File.join(Dir.pwd, 'ssl'))
  @@dir = dir
  Dir.mkdir(@@dir) unless Dir.exist?(@@dir)
end