summaryrefslogtreecommitdiffstats
path: root/spec/unit/ssl/key.rb
blob: 4978a591b7e7e4761be8a632e3c012163e931bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/ssl/key'

describe Puppet::SSL::Key do
    before do
        @class = Puppet::SSL::Key
    end

    it "should be extended with the Indirector module" do
        @class.metaclass.should be_include(Puppet::Indirector)
    end

    it "should indirect key" do
        @class.indirection.name.should == :key
    end

    it "should default to the :file terminus class" do
        @class.indirection.terminus(:file).expects(:find).with "myname"
        @class.find("myname")
    end

    describe "when managing instances" do
        before do
            @key = @class.new("myname")
        end

        it "should have a name attribute" do
            @key.name.should == "myname"
        end

        it "should have a content attribute" do
            @key.should respond_to(:content)
        end

        it "should be able to read keys from disk" do
            path = "/my/path"
            File.expects(:read).with(path).returns("my key")
            key = mock 'key'
            OpenSSL::PKey::RSA.expects(:new).with("my key").returns(key)
            @key.read(path).should equal(key)
            @key.content.should equal(key)
        end

        it "should read the key with the password retrieved from the password file if one is provided" do
            FileTest.stubs(:exist?).returns true
            @key.password_file = "/path/to/password"

            path = "/my/path"
            File.expects(:read).with(path).returns("my key")
            File.expects(:read).with("/path/to/password").returns("my password")

            key = mock 'key'
            OpenSSL::PKey::RSA.expects(:new).with("my key", "my password").returns(key)
            @key.read(path).should equal(key)
            @key.content.should equal(key)
        end

        it "should return an empty string when converted to a string with no key" do
            @key.to_s.should == ""
        end

        it "should convert the key to pem format when converted to a string" do
            key = mock 'key', :to_pem => "pem"
            @key.content = key
            @key.to_s.should == "pem"
        end
    end

    describe "when generating the private key" do
        before do
            @instance = @class.new("test")

            @key = mock 'key'
        end

        it "should create an instance of OpenSSL::PKey::RSA" do
            OpenSSL::PKey::RSA.expects(:new).returns(@key)

            @instance.generate
        end

        it "should create the private key with the keylength specified in the settings" do
            Puppet.settings.expects(:value).with(:keylength).returns(50)
            OpenSSL::PKey::RSA.expects(:new).with(50).returns(@key)

            @instance.generate
        end

        it "should fail if a provided password file does not exist" do
            FileTest.expects(:exist?).with("/path/to/pass").returns false

            lambda { @instance.password_file = "/path/to/pass" }.should raise_error(ArgumentError)
        end

        it "should return the contents of the password file as its password" do
            FileTest.expects(:exist?).with("/path/to/pass").returns true
            File.expects(:read).with("/path/to/pass").returns "my password"

            @instance.password_file = "/path/to/pass"

            @instance.password.should == "my password"
        end

        it "should create the private key with any provided password" do
            Puppet.settings.stubs(:value).with(:keylength).returns(50)

            FileTest.expects(:exist?).with("/path/to/pass").returns true
            File.expects(:read).with("/path/to/pass").returns "my password"

            @instance.password_file = "/path/to/pass"

            OpenSSL::PKey::RSA.expects(:new).with(50, "my password").returns(@key)

            @instance.generate
        end

        it "should set the content to the generated key" do
            OpenSSL::PKey::RSA.stubs(:new).returns(@key)
            @instance.generate
            @instance.content.should equal(@key)
        end

        it "should return the generated key" do
            OpenSSL::PKey::RSA.stubs(:new).returns(@key)
            @instance.generate.should equal(@key)
        end
    end
end