我有一个用例,我有一个承包商需要 ssh 和 sudo 访问一台服务器,但不需要访问我们的其他服务器。我们所有的服务器都由 Chef 管理,我们使用数据包来配置每个用户。关于解决这个问题的最佳方法有什么建议吗?
最佳答案
一如既往,有各种各样的可能性:-)
Here is ,我们如何根据用户
数据包配置每个节点的用户访问。
- 拥有
group: sysadmin
的所有用户都会自动分配到具有 sudo 权限的所有节点。 - 所有其他用户都是根据其分配给特定主机的情况添加的。
users
数据包是 搜索数据包的"nodes"
键中包含node['fqdn']
条目的项目:
{
"id": "a-srv123-admin",
...
"nodes": {
"srv123.typo3.org": {
"sudo": "true"
}
}
}
为了从所有节点中删除用户,可以设置action:remove
在最高级别:
{
"id": "a-user-to-remove",
"action": "remove"
}
为了从某个节点中删除用户,可以设置action:remove
在节点级别:
{
"id": "a-user-to-remove",
...
"nodes": {
"srv123.typo3.org": {
"action": "remove"
}
}
}
implementation of this (不幸的是,事实上并不是很干净),只搜索与 node[fqdn]
关联的所有用户:
node_attribute = "fqdn"
log "Searching for users associated with node #{node[node_attribute]}"
begin
users = search(users_databag_name, "nodes:#{node[node_attribute]}")
rescue Net::HTTPServerException
Chef::Log.warn "Searching for users in the 'users' databag failed, search for users associated with node '#{node[node_attribute]}'"
users = {}
end
users.each do |u|
node_options = u['nodes'][node[node_attribute]]
Chef::Log.info "Got node options: #{node_options}"
if u['action'] == "remove" || node_options['action'] == "remove"
user u['username'] ||= u['id'] do
action :remove
end
else
# snip...
# Create user object.
user u['username'] do
uid u['uid'] if u['uid']
gid u['gid'] if u['gid']
shell u['shell']
comment u['comment']
password u['password'] if u['password']
supports manage_home: true
home home_dir
action u['action'] if u['action']
end
# sudo management
if node_options['sudo'] == "true"
sudo u['username'] do
nopasswd true
user u['username']
end
else
sudo u['username'] do
action :remove
end
end
end
end
编辑:请注意,任何有权访问 Chef 客户端证书的用户都可以根据客户端可以读取的内容查询数据。这可以包括存储在其他节点属性中的密码。 RBAC 或 Chef-Vault 可以缓解这种情况。
关于chef-infra - 使用 Chef,限制用户访问某些服务器/环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554682/