Files
chef/site-cookbooks/kosmos-base/recipes/default.rb
raucao 301565b910 Move allowed node attrs to config file, apply automatically
This moves the whitelist for nodes' "normal" attributes to a config
file, which is loaded live during every run and applied in memory in
case the node's local file hasn't been updated before the client run.

Fixes allowed attributes being overwritten/removed in the node info
JSON files.
2026-07-05 17:07:56 +02:00

133 lines
4.4 KiB
Ruby

#
# Cookbook Name:: kosmos-base
# Recipe:: default
#
# The MIT License (MIT)
#
# Copyright:: 2019, Kosmos Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
include_recipe "apt"
cookbook_file "/etc/chef/chef_normal_attributes.yml" do
source "chef_normal_attributes.yml"
owner "root"
group "root"
mode "0644"
end
ruby_block "update allowed_normal_attributes in client.rb" do
block do
whitelist = YAML.load_file("/etc/chef/chef_normal_attributes.yml")
fe = Chef::Util::FileEdit.new("/etc/chef/client.rb")
fe.search_file_replace_line(
/^allowed_normal_attributes.*/,
"allowed_normal_attributes #{whitelist.inspect}"
)
fe.write_file
Chef::Config[:allowed_normal_attributes] = whitelist
Chef::Config[:normal_attribute_allowlist] = whitelist
end
not_if do
whitelist = YAML.load_file("/etc/chef/chef_normal_attributes.yml")
client_rb = ::File.read("/etc/chef/client.rb")
whitelist.all? { |attr| client_rb.include?(attr) }
end
end
directory "/etc/apt/keyrings" do
mode "0755"
action :create
end
include_recipe "timezone_iii"
include_recipe "ntp" if node["platform"] == "ubuntu" && node["platform_version"].to_f < 24.04
include_recipe "kosmos-base::journald_conf"
include_recipe "kosmos-base::systemd_emails"
node.override["apt"]["unattended_upgrades"]["enable"] = true
node.override["apt"]["unattended_upgrades"]["mail_only_on_error"] = false
node.override["apt"]["unattended_upgrades"]["sender"] = "ops@kosmos.org"
node.override["apt"]["unattended_upgrades"]["allowed_origins"] = [
"${distro_id}:${distro_codename}-security",
"${distro_id}:${distro_codename}-updates",
"${distro_id}ESMApps:${distro_codename}-apps-security",
"${distro_id}ESMApps:${distro_codename}-apps-updates",
"${distro_id}ESM:${distro_codename}-infra-security",
"${distro_id}ESM:${distro_codename}-infra-updates"
]
node.override["apt"]["unattended_upgrades"]["mail"] = "ops@kosmos.org"
node.override["apt"]["unattended_upgrades"]["syslog_enable"] = true
include_recipe "apt::unattended-upgrades"
package "mailutils"
package "mosh"
package "vim"
# Don't create users and rewrite the sudo config in development environment.
# It breaks the vagrant user
unless node.chef_environment == "development"
# Searches data bag "users" for groups attribute "sysadmin".
# Places returned users in Unix group "sysadmin" with GID 2300.
users_manage "sysadmin" do
group_id 2300
action %i[remove create]
end
sudo "sysadmin" do
groups "sysadmin"
nopasswd true
defaults [
# not default on Ubuntu, explicitely enable. Uses a minimal white list of
# environment variables
"env_reset",
# Send emails on unauthorized attempts
"mail_badpass",
'secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"'
]
end
include_recipe "kosmos-base::firewall"
include_recipe "kosmos-postfix"
node.override["set_fqdn"] = "*"
include_recipe "hostname"
package "ca-certificates"
directory "/usr/local/share/ca-certificates/cacert" do
action :create
end
["http://www.cacert.org/certs/root.crt", "http://www.cacert.org/certs/class3.crt"].each do |cert|
remote_file "/usr/local/share/ca-certificates/cacert/#{File.basename(cert)}" do
source cert
action :create_if_missing
notifies :run, "execute[update-ca-certificates]", :immediately
end
end
execute "update-ca-certificates" do
action :nothing
end
end