Class: Bitca::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/bitca/main.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Main

Returns a new instance of Main.

Raises:



11
12
13
14
15
16
# File 'lib/bitca/main.rb', line 11

def initialize(config_path)
  raise ConfigMissingError, "Config '#{File.expand_path(config_path)}' does not exist" unless File.exists?(config_path)
  @config = symbolize_keys(YAML.load_file(config_path))
  @ca = CertificateAuthority.new(@config[:path], @config[:keysize], @config[:days])
  @pwdgen = Pwdgen.new(@config[:path], @config[:pwdsize])
end

Class Method Details



7
8
9
# File 'lib/bitca/main.rb', line 7

def self.print_sample_config
  STDOUT.puts(File.read("#{APP_DIR}/config/sample_config.yml"))
end

Instance Method Details

#generate(hostname, options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bitca/main.rb', line 27

def generate(hostname, options)
  raise "CA not initialized" unless @ca.ca_exists?

  unless options[:pwd_only]
    if @ca.list.include?(hostname) && !options[:force]
      raise "Keys for #{hostname} already exist"
    else
      @ca.genkey hostname
      @ca.genreq hostname
      @ca.sign   hostname
    end
  end

  unless options[:keys_only]
    if @pwdgen.list.include?(hostname) && !options[:force]
      raise "Password for #{hostname} already exists"
    else
      @pwdgen.genpwd hostname
    end
  end
end

#initObject



18
19
20
21
22
23
24
25
# File 'lib/bitca/main.rb', line 18

def init
  unless @ca.ca_exists?
    @ca.genca @config
    @pwdgen.init
  else
    raise "CA already initialized"
  end
end

#show(hostname, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bitca/main.rb', line 49

def show(hostname, options)
  raise "CA not initialized" unless @ca.ca_exists?

  ret = {}

  unless options[:pwd_only]
    if @ca.list.include?(hostname)
      ret.merge!(@ca.get_bundle(hostname))
    else
      raise "Keys for #{hostname} do not exist"
    end
  end

  unless options[:keys_only]
    if @pwdgen.list.include?(hostname)
      ret.merge!({:password => @pwdgen.getpwd(hostname)})
    else
      raise "Password for #{hostname} does not exist"
    end
  end

  ret
end