Skip to content

Commit b848bbe

Browse files
hanwen-clusterhanwen-pcluste
authored andcommitted
Convert IMDS lockdown from SysV init to systemd
`test_create_imds_secured` integration test has been passed Signed-off-by: Hanwen <[email protected]>
1 parent e0d5616 commit b848bbe

File tree

7 files changed

+104
-80
lines changed

7 files changed

+104
-80
lines changed

cookbooks/aws-parallelcluster-environment/recipes/config/imds.rb

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
command "mkdir -p $(dirname #{ip6tables_rules_file}) && ip6tables-save > #{ip6tables_rules_file}"
6161
end
6262

63-
template '/etc/init.d/parallelcluster-iptables' do
64-
source 'imds/parallelcluster-iptables.erb'
63+
template '/usr/local/sbin/restore_tables.sh' do
64+
source 'imds/restore_tables.sh.erb'
6565
user 'root'
6666
group 'root'
6767
mode '0744'
@@ -71,6 +71,25 @@
7171
)
7272
end
7373

74+
template '/usr/local/sbin/save_tables.sh' do
75+
source 'imds/save_tables.sh.erb'
76+
user 'root'
77+
group 'root'
78+
mode '0744'
79+
variables(
80+
iptables_rules_file: iptables_rules_file,
81+
ip6tables_rules_file: ip6tables_rules_file
82+
)
83+
end
84+
85+
template '/etc/systemd/system/parallelcluster-iptables.service' do
86+
source 'imds/parallelcluster-iptables.service.erb'
87+
cookbook 'aws-parallelcluster-environment'
88+
owner 'root'
89+
group 'root'
90+
mode '0644'
91+
end
92+
7493
service "parallelcluster-iptables" do
7594
action %i(enable start)
7695
end

cookbooks/aws-parallelcluster-environment/spec/unit/recipes/imds_spec.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,19 @@
5656
is_expected.to run_execute("Save ip6tables rules").with(command: /ip6tables-save/)
5757
end
5858

59-
it 'creates iptables init.d file' do
60-
is_expected.to create_template("/etc/init.d/parallelcluster-iptables")
61-
.with(source: 'imds/parallelcluster-iptables.erb')
59+
it 'creates iptables systemd unit file' do
60+
is_expected.to create_template("/etc/systemd/system/parallelcluster-iptables.service")
61+
.with(source: 'imds/parallelcluster-iptables.service.erb')
62+
end
63+
64+
it 'creates restore table script' do
65+
is_expected.to create_template("/usr/local/sbin/restore_tables.sh")
66+
.with(source: 'imds/restore_tables.sh.erb')
67+
end
68+
69+
it 'creates save table script' do
70+
is_expected.to create_template("/usr/local/sbin/save_tables.sh")
71+
.with(source: 'imds/save_tables.sh.erb')
6272
end
6373

6474
it 'starts parallelcluster-iptables service' do

cookbooks/aws-parallelcluster-environment/templates/imds/parallelcluster-iptables.erb

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Backup and restore iptables rules (both for IPv4 and IPv6)
3+
After=network-online.target
4+
5+
[Service]
6+
ExecStart=/usr/local/sbin/restore_tables.sh
7+
ExecStop=/usr/local/sbin/save_tables.sh
8+
9+
[Install]
10+
WantedBy=multi-user.target
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the
4+
# License. A copy of the License is located at
5+
#
6+
# http://aws.amazon.com/apache2.0/
7+
#
8+
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
9+
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
IPTABLES_RULES_FILE="<%= @iptables_rules_file %>"
13+
IP6TABLES_RULES_FILE="<%= @ip6tables_rules_file %>"
14+
15+
function restore_tables() {
16+
local iptables_command=$1
17+
local iptables_file=$2
18+
if [[ -f $iptables_file ]]; then
19+
$iptables_command < $iptables_file
20+
echo "iptables rules restored from file: $iptables_file"
21+
else
22+
echo "iptables rules left unchanged as file was not found: $iptables_file"
23+
fi
24+
}
25+
26+
function main {
27+
restore_tables iptables-restore $IPTABLES_RULES_FILE
28+
restore_tables ip6tables-restore $IP6TABLES_RULES_FILE
29+
}
30+
31+
main
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the
4+
# License. A copy of the License is located at
5+
#
6+
# http://aws.amazon.com/apache2.0/
7+
#
8+
# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
9+
# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
IPTABLES_RULES_FILE="<%= @iptables_rules_file %>"
13+
IP6TABLES_RULES_FILE="<%= @ip6tables_rules_file %>"
14+
15+
function save_tables() {
16+
local iptables_command=$1
17+
local iptables_file=$2
18+
echo "saving iptables rules to file: $iptables_file"
19+
mkdir -p $(dirname $iptables_file)
20+
$iptables_command > $iptables_file
21+
echo "iptables rules saved to file: $iptables_file"
22+
}
23+
24+
function main {
25+
save_tables iptables-save $IPTABLES_RULES_FILE
26+
save_tables ip6tables-save $IP6TABLES_RULES_FILE
27+
}
28+
29+
main

cookbooks/aws-parallelcluster-environment/test/controls/imds_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@
3838
describe service('parallelcluster-iptables') do
3939
it { should be_installed }
4040
it { should be_enabled }
41-
it { should be_running }
42-
end
43-
44-
%w(1 2 3 4 5).each do |level|
45-
describe "Check parallelcluster-iptables run level #{level} on" do
46-
subject { bash("ls /etc/rc#{level}.d/ | egrep '^S[0-9]+parallelcluster-iptables$'") }
47-
its('exit_status') { should eq(0) }
48-
end
49-
end
50-
51-
%w(0 6).each do |level|
52-
describe "Check parallelcluster-iptables run level #{level} off" do
53-
subject { bash("ls /etc/rc#{level}.d/ | egrep '^K[0-9]+parallelcluster-iptables$'") }
54-
its('exit_status') { should eq(0) }
55-
end
5641
end
5742

5843
describe file("#{node['cluster']['etc_dir']}/sysconfig/iptables.rules") do

0 commit comments

Comments
 (0)