Skip to content

Commit 41a6f83

Browse files
authored
Merge pull request #129 from cirrax/dev_add_permissions
add user/group/mode parameter to custom types
2 parents 87efeda + 172415c commit 41a6f83

File tree

14 files changed

+346
-4
lines changed

14 files changed

+346
-4
lines changed

REFERENCE.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,18 @@ The basic property that the resource should be in.
12041204

12051205
Default value: `present`
12061206

1207+
##### `group`
1208+
1209+
group of the file
1210+
1211+
##### `mode`
1212+
1213+
mode of the file
1214+
1215+
##### `owner`
1216+
1217+
owner of the file
1218+
12071219
#### Parameters
12081220

12091221
The following parameters are available in the `dhparam` type.
@@ -1252,6 +1264,18 @@ The basic property that the resource should be in.
12521264

12531265
Default value: `present`
12541266

1267+
##### `group`
1268+
1269+
group of the file
1270+
1271+
##### `mode`
1272+
1273+
mode of the file
1274+
1275+
##### `owner`
1276+
1277+
owner of the file
1278+
12551279
#### Parameters
12561280

12571281
The following parameters are available in the `ssl_pkey` type.
@@ -1314,6 +1338,18 @@ The basic property that the resource should be in.
13141338

13151339
Default value: `present`
13161340

1341+
##### `group`
1342+
1343+
group of the file
1344+
1345+
##### `mode`
1346+
1347+
mode of the file
1348+
1349+
##### `owner`
1350+
1351+
owner of the file
1352+
13171353
#### Parameters
13181354

13191355
The following parameters are available in the `x509_cert` type.
@@ -1408,6 +1444,18 @@ The basic property that the resource should be in.
14081444

14091445
Default value: `present`
14101446

1447+
##### `group`
1448+
1449+
group of the file
1450+
1451+
##### `mode`
1452+
1453+
mode of the file
1454+
1455+
##### `owner`
1456+
1457+
owner of the file
1458+
14111459
#### Parameters
14121460

14131461
The following parameters are available in the `x509_request` type.

lib/puppet/provider/dhparam/openssl.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# frozen_string_literal: true
22

33
require 'pathname'
4-
Puppet::Type.type(:dhparam).provide(:openssl) do
4+
require File.join(__dir__, '..', '..', '..', 'puppet/provider/openssl')
5+
6+
Puppet::Type.type(:dhparam).provide(
7+
:openssl,
8+
parent: Puppet::Provider::Openssl
9+
) do
510
desc 'Manages dhparam files with OpenSSL'
611

712
commands openssl: 'openssl'
@@ -19,6 +24,7 @@ def create
1924
options.insert(1, '-dsaparam') if resource[:fastmode]
2025

2126
openssl options
27+
set_file_perm(resource[:path], resource[:owner], resource[:group], resource[:mode])
2228
end
2329

2430
def destroy

lib/puppet/provider/openssl.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
require 'etc'
4+
5+
# class to use in openssl providers to handle file permission (mode, group and owner)
6+
class Puppet::Provider::Openssl < Puppet::Provider
7+
include Puppet::Util::POSIX
8+
9+
def owner
10+
if File.exist?(@resource[:path])
11+
Etc.getpwuid(File.stat(@resource[:path]).uid).name
12+
else
13+
:absent
14+
end
15+
end
16+
17+
def owner=(should)
18+
File.chown(uid(should), nil, resource[:path])
19+
rescue StandardError => e
20+
raise Puppet::Error, _("Failed to set owner to '#{should}': #{e}"), detail.backtrace
21+
end
22+
23+
def group
24+
if File.exist?(@resource[:path])
25+
Etc.getgrgid(File.stat(@resource[:path]).gid).name
26+
else
27+
:absent
28+
end
29+
end
30+
31+
def group=(should)
32+
File.chown(nil, gid(should), resource[:path])
33+
rescue StandardError => e
34+
raise Puppet::Error, _("Failed to set group to '#{should}': #{e}"), detail.backtrace
35+
end
36+
37+
# Return the mode as an octal string, not as an integer.
38+
def mode
39+
if File.exist?(@resource[:path])
40+
format('0%o', (File.stat(@resource[:path]).mode & 0o07777))
41+
else
42+
:absent
43+
end
44+
end
45+
46+
# Set the file mode, converting from a string to an integer.
47+
def mode=(should)
48+
File.chmod(Integer("0#{should}"), @resource[:path])
49+
end
50+
51+
def set_file_perm(filename, owner = nil, group = nil, mode = nil)
52+
File.chown(uid(owner), nil, resource[:path]) if owner
53+
File.chown(nil, gid(group), resource[:path]) if group
54+
File.chmod(Integer("0#{mode}"), filename) if mode
55+
end
56+
end

lib/puppet/provider/ssl_pkey/openssl.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
require 'pathname'
44
require 'openssl'
5-
Puppet::Type.type(:ssl_pkey).provide(:openssl) do
5+
require File.join(__dir__, '..', '..', '..', 'puppet/provider/openssl')
6+
Puppet::Type.type(:ssl_pkey).provide(
7+
:openssl,
8+
parent: Puppet::Provider::Openssl
9+
) do
610
desc 'Manages private keys with OpenSSL'
711

812
def self.dirname(resource)
@@ -38,6 +42,7 @@ def create
3842
key = self.class.generate_key(resource)
3943
pem = self.class.to_pem(resource, key)
4044
File.write(resource[:path], pem)
45+
set_file_perm(resource[:path], resource[:owner], resource[:group], resource[:mode])
4146
end
4247

4348
def destroy

lib/puppet/provider/x509_cert/openssl.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

33
require 'pathname'
4-
Puppet::Type.type(:x509_cert).provide(:openssl) do
4+
require File.join(__dir__, '..', '..', '..', 'puppet/provider/openssl')
5+
Puppet::Type.type(:x509_cert).provide(
6+
:openssl,
7+
parent: Puppet::Provider::Openssl
8+
) do
59
desc 'Manages certificates with OpenSSL'
610

711
commands openssl: 'openssl'
@@ -103,6 +107,7 @@ def create
103107
# openssl(options) doesn't work because it's impossible to pass an env
104108
# https://github.com/puppetlabs/puppet/issues/9493
105109
execute([command('openssl')] + options, { failonfail: true, combine: true, custom_environment: env })
110+
set_file_perm(resource[:path], resource[:owner], resource[:group], resource[:mode])
106111
end
107112

108113
def destroy

lib/puppet/provider/x509_request/openssl.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# frozen_string_literal: true
22

33
require 'pathname'
4-
Puppet::Type.type(:x509_request).provide(:openssl) do
4+
require File.join(__dir__, '..', '..', '..', 'puppet/provider/openssl')
5+
Puppet::Type.type(:x509_request).provide(
6+
:openssl,
7+
parent: Puppet::Provider::Openssl
8+
) do
59
desc 'Manages certificate signing requests with OpenSSL'
610

711
commands openssl: 'openssl'
@@ -45,6 +49,8 @@ def create
4549
# openssl(options) doesn't work because it's impossible to pass an env
4650
# https://github.com/puppetlabs/puppet/issues/9493
4751
execute([command('openssl')] + options, { failonfail: true, combine: true, custom_environment: env })
52+
53+
set_file_perm(resource[:path], resource[:owner], resource[:group], resource[:mode])
4854
end
4955

5056
def destroy

lib/puppet/type/dhparam.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,25 @@
3535
autorequire(:file) do
3636
Pathname.new(self[:path]).parent.to_s
3737
end
38+
39+
newproperty(:owner) do
40+
desc 'owner of the file'
41+
validate do |value|
42+
raise ArgumentError, "#{value} is not a valid user name" unless value =~ %r{^\w+$}
43+
end
44+
end
45+
46+
newproperty(:group) do
47+
desc 'group of the file'
48+
validate do |value|
49+
raise ArgumentError, "#{value} is not a valid group name" unless value =~ %r{^\w+$}
50+
end
51+
end
52+
53+
newproperty(:mode) do
54+
desc 'mode of the file'
55+
validate do |value|
56+
raise ArgumentError, "#{value} is not a valid file mode" unless value =~ %r{^0\d\d\d$}
57+
end
58+
end
3859
end

lib/puppet/type/ssl_pkey.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,25 @@
4242
autorequire(:file) do
4343
Pathname.new(self[:path]).parent.to_s
4444
end
45+
46+
newproperty(:owner) do
47+
desc 'owner of the file'
48+
validate do |value|
49+
raise ArgumentError, "#{value} is not a valid user name" unless value =~ %r{^\w+$}
50+
end
51+
end
52+
53+
newproperty(:group) do
54+
desc 'group of the file'
55+
validate do |value|
56+
raise ArgumentError, "#{value} is not a valid group name" unless value =~ %r{^\w+$}
57+
end
58+
end
59+
60+
newproperty(:mode) do
61+
desc 'mode of the file'
62+
validate do |value|
63+
raise ArgumentError, "#{value} is not a valid file mode" unless value =~ %r{^0\d\d\d$}
64+
end
65+
end
4566
end

lib/puppet/type/x509_cert.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@
7676
desc 'The optional CA key password'
7777
end
7878

79+
newproperty(:owner) do
80+
desc 'owner of the file'
81+
validate do |value|
82+
raise ArgumentError, "#{value} is not a valid user name" unless value =~ %r{^\w+$}
83+
end
84+
end
85+
86+
newproperty(:group) do
87+
desc 'group of the file'
88+
validate do |value|
89+
raise ArgumentError, "#{value} is not a valid group name" unless value =~ %r{^\w+$}
90+
end
91+
end
92+
93+
newproperty(:mode) do
94+
desc 'mode of the file'
95+
validate do |value|
96+
raise ArgumentError, "#{value} is not a valid file mode" unless value =~ %r{^0\d\d\d$}
97+
end
98+
end
99+
79100
autorequire(:file) do
80101
self[:template]
81102
end

lib/puppet/type/x509_request.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@
5454
defaultto true
5555
end
5656

57+
newproperty(:owner) do
58+
desc 'owner of the file'
59+
validate do |value|
60+
raise ArgumentError, "#{value} is not a valid user name" unless value =~ %r{^\w+$}
61+
end
62+
end
63+
64+
newproperty(:group) do
65+
desc 'group of the file'
66+
validate do |value|
67+
raise ArgumentError, "#{value} is not a valid group name" unless value =~ %r{^\w+$}
68+
end
69+
end
70+
71+
newproperty(:mode) do
72+
desc 'mode of the file'
73+
validate do |value|
74+
raise ArgumentError, "#{value} is not a valid file mode" unless value =~ %r{^0\d\d\d$}
75+
end
76+
end
77+
5778
autorequire(:x509_cert) do
5879
path = Pathname.new(self[:private_key])
5980
"#{path.dirname}/#{path.basename(path.extname)}"

0 commit comments

Comments
 (0)