1
+ #
2
+ # Cookbook:: datadog
3
+ # Recipe:: security-agent
4
+ #
5
+ # Copyright:: 2011-2022, Datadog
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+ #
13
+ # Unless required by applicable law or agreed to in writing, software
14
+ # distributed under the License is distributed on an "AS IS" BASIS,
15
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ # See the License for the specific language governing permissions and
17
+ # limitations under the License.
18
+ #
19
+
20
+ is_windows = platform_family? ( 'windows' )
21
+
22
+ # Set the correct agent startup action
23
+ security_agent_enabled = !is_windows && node [ 'datadog' ] [ 'security_agent' ] [ 'cws' ] [ 'enabled' ] || node [ 'datadog' ] [ 'security_agent' ] [ 'cspm' ] [ 'enabled' ]
24
+
25
+ #
26
+ # Configures security-agent agent
27
+ security_agent_config_file = '/etc/datadog-agent/security-agent.yaml'
28
+ security_agent_config_file_exists = ::File . exist? ( security_agent_config_file )
29
+
30
+ template security_agent_config_file do
31
+ runtime_security_extra_config = { }
32
+ if node [ 'datadog' ] [ 'extra_config' ] && node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] && node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] [ 'runtime_security_config' ]
33
+ node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] [ 'runtime_security_config' ] . each do |k , v |
34
+ next if v . nil?
35
+ runtime_security_extra_config [ k ] = v
36
+ end
37
+ end
38
+
39
+ compliance_extra_config = { }
40
+ if node [ 'datadog' ] [ 'extra_config' ] && node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] && node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] [ 'compliance_config' ]
41
+ node [ 'datadog' ] [ 'extra_config' ] [ 'security_agent' ] [ 'compliance_config' ] . each do |k , v |
42
+ next if v . nil?
43
+ compliance_extra_config [ k ] = v
44
+ end
45
+ end
46
+
47
+ source 'security-agent.yaml.erb'
48
+ variables (
49
+ runtime_security_enabled : node [ 'datadog' ] [ 'security_agent' ] [ 'cws' ] [ 'enabled' ] ,
50
+ runtime_security_extra_config : runtime_security_extra_config ,
51
+ compliance_enabled : node [ 'datadog' ] [ 'security_agent' ] [ 'cspm' ] [ 'enabled' ] ,
52
+ compliance_extra_config : compliance_extra_config
53
+ )
54
+
55
+ owner 'root'
56
+ group 'dd-agent'
57
+ mode '640'
58
+
59
+ notifies :restart , 'service[datadog-agent-security]' , :delayed if security_agent_enabled
60
+
61
+ # Security agent is not enabled and the file doesn't exists, don't create it
62
+ not_if { !security_agent_enabled && !security_agent_config_file_exists }
63
+ end
64
+
65
+ # Common configuration
66
+ service_provider = Chef ::Datadog . service_provider ( node )
67
+
68
+ service_name = 'datadog-agent-security'
69
+
70
+ if security_agent_enabled
71
+ service 'datadog-agent-security' do
72
+ service_name service_name
73
+ action :start
74
+ provider service_provider unless service_provider . nil?
75
+ supports :restart => true , :status => true , :start => true , :stop => true
76
+ subscribes :restart , "template[#{ security_agent_config_file } ]" , :delayed
77
+ end
78
+ end
0 commit comments