Class: Zokor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/zokor/config.rb

Constant Summary collapse

DEFAULT_CONFIG_DIR =
File.join(File.expand_path('~'), '.config', 'zokor')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_dir = nil) ⇒ Config

Returns a new instance of Config.

Parameters:

  • config_dir (String) (defaults to: nil)

    (DEFAULT_CONFIG_DIR)



14
15
16
# File 'lib/zokor/config.rb', line 14

def initialize(config_dir=nil)
  @config_dir = config_dir || DEFAULT_CONFIG_DIR
end

Instance Attribute Details

#config_dirObject (readonly)

Returns the value of attribute config_dir.



11
12
13
# File 'lib/zokor/config.rb', line 11

def config_dir
  @config_dir
end

Instance Method Details

#config_file(name) ⇒ Object



29
30
31
# File 'lib/zokor/config.rb', line 29

def config_file(name)
  File.join(config_dir, name)
end

#config_yaml_fileObject



25
26
27
# File 'lib/zokor/config.rb', line 25

def config_yaml_file
  config_file('zokor.yaml')
end

#create_client_keypair(key_file, csr_file) ⇒ Object

Parameters:

  • key_file (String)

    Key filename

  • csr_file (String)

    CSR filename



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/zokor/config.rb', line 119

def create_client_keypair(key_file, csr_file)
  log.info('Generating SSL/TLS key and certificate request')

  key = generate_rsa_key
  File.open(key_file, File::WRONLY|File::CREAT|File::EXCL, 0600) do |f|
    f.write(key.to_s)
  end

  log.info("Wrote key to #{key_file.inspect}")

  csr = generate_csr(key, user_address)
  csr.to_s

  File.write(csr_file, csr.to_s)

  log.info("Wrote request to #{csr_file.inspect}")

  log.warn('Certificate request follows:')

  puts csr.to_s

  log.warn('Please send the above certificate request.')

  return true
end

#init_config(remote_host, remote_port, local_host: '127.0.0.1', local_port: 8080) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/zokor/config.rb', line 82

def init_config(remote_host, remote_port,
                local_host: '127.0.0.1', local_port: 8080)
  unless Dir.exist?(config_dir)
    log.info("mkdir #{config_dir}")
    FileUtils.mkdir_p(config_dir)
  end

  path = config_yaml_file

  if File.exist?(path)
    log.warn('Config file already exists: ' + path)
    return false
  end

  data = {
    use_ssl: true,
    ssl_opts: {
      ca_file: :builtin,
      cert_file: config_file('client.crt'),
      key_file: config_file('client.key'),
    },
    local_host: local_host,
    local_port: local_port,
    remote_host: remote_host,
    remote_port: remote_port,
  }

  log.info('Initializing config: ' + YAML.dump(data))

  File.write(path, YAML.dump(data))

  create_client_keypair(config_file('client.key'),
                        config_file('client.csr'))
end

#interactive_init(opts) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/zokor/config.rb', line 69

def interactive_init(opts)
  unless opts[:remote_host]
    log.error('Please pass --ext-host')
    return false
  end
  unless opts[:remote_port]
    log.error('Please pass --ext-port')
    return false
  end

  init_config(opts.fetch(:remote_host), opts.fetch(:remote_port))
end

#interactive_install_cert(filename = 'client.crt') ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zokor/config.rb', line 33

def interactive_install_cert(filename='client.crt')
  path = config_file(filename)
  log.debug('Will install certificate to ' + path.inspect)
  puts 'Please paste your certificate now from -----BEGIN CERTIFICATE-----'

  cert_data = ''

  in_cert = false
  while line = STDIN.gets
    next if line.strip.empty?

    # check for begin marker
    if !in_cert
      if line.strip == '-----BEGIN CERTIFICATE-----'
        in_cert = true
      else
        log.warn "Certificate should start with: -----BEGIN CERTIFICATE-----"
        return false
      end
    end

    cert_data << line

    # end
    break if line.strip == '-----END CERTIFICATE-----'
  end

  File.open(path, File::WRONLY|File::CREAT|File::EXCL, 0644) do |f|
    f.write(cert_data)
  end

  log.info("Saved certificate to #{path.inspect}")

  path
end

#load_config(filename = nil) ⇒ Object



18
19
20
21
22
23
# File 'lib/zokor/config.rb', line 18

def load_config(filename=nil)
  filename ||= config_yaml_file
  ret = YAML.load_file(filename)
  log.info("Read config from #{filename.inspect}")
  ret
end