Feature Request / Question: Adding custom data to auth events from Minion
Description of Issue/Question
Hello,
Would it be possible to add custom fields / info to the salt auth events? In particular, I would like to have the mac address/es of the minion. The reason for this is I would like to accept keys based on some identifiers (like name and mac-address) which I can cross-check against a database like cobbler.
Something like the following in the minion config:
custom_auth_data:
key_1: "value 1"
mac: grains.get('hwaddr_interfaces')
would generate the following auth event:
salt/auth
{
"_stamp": "2016-11-14T19:49:41.004689",
"act": "pend",
"id": "test-01-v",
"pub": "-----BEGIN PUBLIC KEY-----\nWls6am6f39sS-----END PUBLIC KEY-----",
"key_1": "value 1",
"mac":
{
"eth0": "xx:xx:xx:xx:xx:xx",
"eth1": "xx:xx:xx:xx:xx:xx"
},
"result": true
}
Apologies if there is already something in place that can do this.
Versions Report
Minion:
Salt Version:
Salt: 2016.3.4
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: Not Installed
gitdb: Not Installed
gitpython: Not Installed
ioflo: Not Installed
Jinja2: 2.7.2
libgit2: Not Installed
libnacl: Not Installed
M2Crypto: Not Installed
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: 2.6.1
pygit2: Not Installed
Python: 2.7.5 (default, Sep 15 2016, 22:37:39)
python-gnupg: Not Installed
PyYAML: 3.11
PyZMQ: 15.3.0
RAET: Not Installed
smmap: Not Installed
timelib: Not Installed
Tornado: 4.2.1
ZMQ: 4.1.4
System Versions:
dist: centos 7.2.1511 Core
machine: x86_64
release: 3.10.0-327.36.1.el7.x86_64
system: Linux
version: CentOS Linux 7.2.1511 Core
Master:
Salt Version:
Salt: 2016.3.4
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: Not Installed
gitdb: Not Installed
gitpython: Not Installed
ioflo: Not Installed
Jinja2: 2.7.2
libgit2: Not Installed
libnacl: Not Installed
M2Crypto: Not Installed
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: 2.6.1
pygit2: Not Installed
Python: 2.7.5 (default, Sep 15 2016, 22:37:39)
python-gnupg: Not Installed
PyYAML: 3.11
PyZMQ: 15.3.0
RAET: Not Installed
smmap: Not Installed
timelib: Not Installed
Tornado: 4.2.1
ZMQ: 4.1.4
System Versions:
dist: centos 7.2.1511 Core
machine: x86_64
release: 3.10.0-327.36.3.el7.x86_64
system: Linux
version: CentOS Linux 7.2.1511 Core
This would be cool to have, but I am not sure which machine fires the salt/auth event, the master or the minion.
What you could do is use startup_states: sls, and have an event.send state that fires the event to the master with the necessary information.
I am going to look into where the auth event is fired from.
Thanks Daniel
@gtmanfred the minion will fire this event when it starts up for the first time. I will process the event using reactor. Here is the reactor file:
reactor:
- 'salt/auth':
- /srv/reactor/process_minion_auth.sls
Here's what I would like the process_minion_auth.sls to be:
#!mako|yaml
## vim: sts=2 ts=2 sw=2 et ai
<%!
import xmlrpclib
%>
<%
MinionName = data['id']
MinionMacs = data['mac']
Valid = False
Server = xmlrpclib.Server('https://cobbler/cobbler_api')
Result = Server.find_system({'hostname': MinionName, "mac_address": MinionMacs['eth0']})
del Server
if len(Result) > 0:
if Result[0] == MinionName:
Valid = True
%>
% if 'act' in data and data['act'] == 'pend' and Valid:
minion_auth_pass:
wheel.key.accept:
- match: ${MinionName}
% else:
minion_auth_fail:
wheel.key.reject:
- match: ${MinionName}
% endif
Hope this makes sense.
@gtmanfred I'll try out the startup states as recommended. My only concern is that the states won't fire until the minion is authenticated.
Ahh, that is correct, if you wanted to use that to accept the key, you won't be able to.
I had the same question under https://github.com/saltstack/salt/issues/43394.
I understand @haeac demands regarding the addition of static data. I'd be tempted to suggest this together with several other details (by default enabled, or toggled through a config option flag), details that the minion is able to collect dynamically and has access to, e.g., fqdn, IPv4, IPv6 etc.
But I'd love to have the following conversation: who inserts the IP address into the payload? If the minion sends it, that cannot be entirely safe (i.e., it can set any address it wants); if the master inserts it, when using the ZMQ as the transport, IIRC, you can't determine who sent you the message.
@saltstack/team-core Is this possible ^^
Would we be able to reliably get information like the ip address or other stuff from the minion that is authenticating, to provide in the auth event?
Thanks, Daniel
It's strange but it's truth: you can get all that information from a unix socket object but you can't retrieve this information from ZeroMQ API. Probably possible by some hacks but not by using documented API
But it should be possible for tcp salt transport based on tornado.
I would like to suggest an addition to this feature request. During the auth process the ability to provide an OTP such as from a Yubikey. The reactor can then validate the Yubikey and if the user id of the key has authorization and the OTP checks out the reactor will accept the key. I have this working but I've overloaded the minion-id with the OTP.
I believe that 2018.3 covered this, by allowing to add grains to the auth data for autosign_grains.
https://docs.saltstack.com/en/latest/topics/tutorials/autoaccept_grains.html#tutorial-autoaccept-grains
That's great, I think this should suffice for a large variety of use-cases. Thanks @gtmanfred!