summaryrefslogtreecommitdiffstats
path: root/lib/puppet/type/ssh_authorized_key.rb
blob: 997afb81e74fa1aed8b72c2f9367134bfe4c0523 (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
module Puppet
    newtype(:ssh_authorized_key) do
        @doc = "Manages SSH authorized keys. Currently only type 2 keys are
        supported."

        ensurable

        newparam(:name) do
            desc "The SSH key comment."

            isnamevar
        end

        newproperty(:type) do
            desc "The encryption type used: ssh-dss or ssh-rsa."

            newvalue("ssh-dss")
            newvalue("ssh-rsa")

            aliasvalue(:dsa, "ssh-dss")
            aliasvalue(:rsa, "ssh-rsa")
        end

        newproperty(:key) do
            desc "The key itself; generally a long string of hex digits."
        end

        newproperty(:user) do
            desc "The user account in which the SSH key should be installed."
        end

        newproperty(:target) do
            desc "The file in which to store the SSH key."

            defaultto :absent

            def should
                if defined? @should and @should[0] != :absent
                    return super
                end

                if user = resource[:user]
                    return File.expand_path("~%s/.ssh/authorized_keys" % user)
                end

                return nil
            end
        end

        newproperty(:options, :array_matching => :all) do
            desc "Key options, see sshd(8) for possible values. Multiple values 
                  should be specified as an array."

            defaultto do :absent end

            def is_to_s(value)
                if value == :absent or value.include?(:absent)
                    super
                else
                    value.join(",")
                end
            end

            def should_to_s(value)
                if value == :absent or value.include?(:absent)
                    super
                else
                    value.join(",")
                end
            end
        end

        autorequire(:user) do
            if should(:user)
                should(:user)
            end
        end

        validate do
            unless should(:target) or should(:user)
                raise Puppet::Error, "Attribute 'user' or 'target' is mandatory"
            end
        end
    end
end