rsa_private_key.rb 2.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

chef_version_for_provides '< 14.0' if respond_to?(:chef_version_for_provides)
resource_name :openssl_rsa_private_key
19
20
provides :openssl_rsa_key # legacy name

Tim Smith's avatar
Tim Smith committed
21
include OpenSSLCookbook::Helpers
Charles Johnson's avatar
Charles Johnson committed
22

23
property :path,        String, name_property: true
Tim Smith's avatar
Tim Smith committed
24
25
property :key_length,  equal_to: [1024, 2048, 4096, 8192], default: 2048
property :key_pass,    String
26
property :key_cipher,  String, default: 'des3', equal_to: ::OpenSSL::Cipher.ciphers
27
28
property :owner,       String
property :group,       String
29
property :mode,        [Integer, String], default: '0640'
30
property :force,       [true, false], default: false
Charles Johnson's avatar
Charles Johnson committed
31

Tim Smith's avatar
Tim Smith committed
32
action :create do
33
  unless new_resource.force || priv_key_file_valid?(new_resource.path, new_resource.key_pass)
34
    converge_by("Create an RSA private key #{new_resource.path}") do
Tim Smith's avatar
Tim Smith committed
35
      log "Generating #{new_resource.key_length} bit "\
36
          "RSA key file at #{new_resource.path}, this may take some time"
Tim Smith's avatar
Tim Smith committed
37
38

      if new_resource.key_pass
39
        unencrypted_rsa_key = gen_rsa_priv_key(new_resource.key_length)
40
        rsa_key_content = encrypt_rsa_key(unencrypted_rsa_key, new_resource.key_pass, new_resource.key_cipher)
Tim Smith's avatar
Tim Smith committed
41
      else
42
        rsa_key_content = gen_rsa_priv_key(new_resource.key_length).to_pem
Tim Smith's avatar
Tim Smith committed
43
44
      end

45
      file new_resource.path do
Tim Smith's avatar
Tim Smith committed
46
        action :create
47
48
        owner new_resource.owner unless new_resource.owner.nil?
        group new_resource.group unless new_resource.group.nil?
Tim Smith's avatar
Tim Smith committed
49
50
51
52
53
54
55
        mode new_resource.mode
        sensitive true
        content rsa_key_content
      end
    end
  end
end