salt icon indicating copy to clipboard operation
salt copied to clipboard

Duplicating master token when no match for Minion policy (Vault Module)

Open rcallphin opened this issue 8 years ago • 12 comments

Description of Issue/Question

I'm hoping that I'm just missing something here (maybe a master token policy config I'm not aware of), but currently it seems that when a minion does not match any policies configured in the vault.conf file the master defaults to simply reproducing it's master token in order to attempt to perform the requested action of the minion.

In order for the salt master to create a stepped down tmp token for the minion it has to have either: a root token (terrible idea) or a token with all minion policies as sub policies. Bug shown in the logs below.

Setup

Policy for master token giving ability to create tokens.

❯ vault policies salt_test path "auth/token/create" { capabilities = ["update"] }

❯ vault policies minion_web path "secret/web/test" { policy = "read" }

Role for Salt master with needed policies

❯ vault read auth/token/roles/salt_test Key Value


allowed_policies [minion_web salt_test] disallowed_policies [] explicit_max_ttl 0 name salt_test orphan true path_suffix period 0 renewable true

Master config

[root@saltmaster master.d]# cat vault.conf vault: url: https://vault.server auth: method: token token: ********************* policies: - minion_{grains[roles]}

[root@saltmaster master.d]# cat peer_run.conf peer_run: .*: - vault.generate_token

Minion1 with grain matching the policy, minion2 with grain not matching the policy. Both get the secret as an empty string is passed to "auth/token/create" meaning that Vault simply replicates the master token.

Vault API Docs

policies (array: "") – A list of policies for the token. This must be a subset of the policies belonging to the token making the request, unless root. If not specified, defaults to all the policies of the calling token.

###Minion 1 [DEBUG ] Token generation request for minion1 (impersonated by master: False) [DEBUG ] salt.crypt.verify_signature: Loading public key [DEBUG ] salt.crypt.verify_signature: Verifying signature [DEBUG ] LazyLoaded localfs.init_kwargs [DEBUG ] minion1 policies: ['minion_web'] [INFO ] Starting new HTTPS connection (1): vault.server [DEBUG ] "POST /v1/auth/token/create HTTP/1.1" 200 373 [DEBUG ] LazyLoaded local_cache.prep_jid

###Minion 2 [DEBUG ] Token generation request for minion2 (impersonated by master: False) [DEBUG ] salt.crypt.verify_signature: Loading public key [DEBUG ] salt.crypt.verify_signature: Verifying signature [DEBUG ] LazyLoaded localfs.init_kwargs [WARNING ] Could not resolve policy pattern minion_{grains[roles]} [DEBUG ] minion2 policies: [] [INFO ] Starting new HTTPS connection (1): vault.server [DEBUG ] "POST /v1/auth/token/create HTTP/1.1" 200 385 [DEBUG ] LazyLoaded local_cache.prep_jid

[root@saltmaster ~]# salt minion1 vault.read_secret 'secret/web/test' minion1: ---------- foo: bar [root@saltmaster ~]# salt minion2 vault.read_secret 'secret/web/test' minion2: ---------- foo: bar

Steps to Reproduce Issue

Included above

Versions Report

[root@saltmaster ~]# salt --versions-report Salt Version: Salt: 2017.7.2

Dependency Versions: cffi: Not Installed cherrypy: Not Installed dateutil: Not Installed docker-py: 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 pycryptodome: Not Installed pygit2: Not Installed Python: 2.7.5 (default, Nov 6 2016, 00:28:07) 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.3.1611 Core locale: UTF-8 machine: x86_64 release: 3.10.0-514.el7.x86_64 system: Linux version: CentOS Linux 7.3.1611 Core

rcallphin avatar Dec 11 '17 18:12 rcallphin

@carlpett can you help out with this issue?

Thanks, Daniel

gtmanfred avatar Dec 12 '17 16:12 gtmanfred

Hi @rcallphin, Yes, that is quite strange behavior. I no longer have access to the environment for which I originally wrote the Vault module, but I will try to create a test environment to see why this is. If memory serves, the Vault policy model has changed quite a bit during this time as well, so perhaps something there could affect it also. What version of Vault are you using?

carlpett avatar Dec 12 '17 16:12 carlpett

@carlpett I work with @rcallphin and the version was probably 0.8.1+ent. It may have been higher in his test environment.

FeatherKing avatar Dec 12 '17 17:12 FeatherKing

Thanks! I'll do some testing and see what I can find.

carlpett avatar Dec 12 '17 17:12 carlpett

thanks @carlpett !

gtmanfred avatar Dec 12 '17 17:12 gtmanfred

So, I'm not sure how I configured this back when I set it up at the previous place, perhaps we just gave the salt token all the different policies. Looking back, it looks like token roles weren't a thing when I first wrote this, so that could be why. Anyway, that is how it should be done now, so you're doing the right thing, but the code doesn't support it. I put an experimental patch here, which allows you to set a role configuration in the auth section:

vault:
    url: https://vault.server
    auth:
        method: token
        token: *********************
        role: salt_test # <--- New 
    policies:
        - minion_{grains[roles]}

Could you try this out? I didn't manage to get a salt environment up tonight, so I can't easily test it myself :(

carlpett avatar Dec 12 '17 18:12 carlpett

Sweet. I'm giving an all day training today, but will test this out as soon as I finish. Thanks @carlpett

rcallphin avatar Dec 12 '17 18:12 rcallphin

Actually using token roles is not the issue here. Below is the output from testing your patch @carlpett. Instead of attempting to create a token based on policies (like the code is built to do) it tries "auth/token/create/salt_test" which fails. But that is not the functionality we want anyway as that would just be attempting to duplicate that masters own access.

[DEBUG ] Token generation request for centos_prod (impersonated by master: False) [DEBUG ] salt.crypt.verify_signature: Loading public key [DEBUG ] salt.crypt.verify_signature: Verifying signature [DEBUG ] LazyLoaded localfs.init_kwargs [WARNING ] Could not resolve policy pattern cst_{grains[roles]} [DEBUG ] centos_prod policies: [] [INFO ] Starting new HTTPS connection (1): vault.dev.or1.adobe.net [DEBUG ] "POST /v1/auth/token/create/salt_test HTTP/1.1" 403 33 [DEBUG ] LazyLoaded local_cache.prep_jid

What we want to see is when a minion does not match any policies defined in vault.conf that it throws an error does not even attempt to create a token. Becuase if it tries to create a token with an empty policy string then the master will simply duplicate its access.

So I wrote a quick patch that I've tested. Seems to do the trick.

payload = { 'policies': _get_policies(minion_id, config), 'num_uses': 1, 'metadata': audit_data }

    if payload['policies'] == []:
        return {'error': 'No policies matched minion'} 
    log.trace('Sending token creation request to Vault')

###Same config as above but now I get: [root@saltmaster ~]# salt minion1 vault.read_secret 'secret/web/test' minion1:

foo: bar [root@saltmaster ~]# salt minion2 vault.read_secret 'secret/web/test' minion2: ERROR: {'error': 'No policies matched minion'}

###Logs [DEBUG ] Token generation request for minion2 (impersonated by master: False) [DEBUG ] salt.crypt.verify_signature: Loading public key [DEBUG ] salt.crypt.verify_signature: Verifying signature [DEBUG ] LazyLoaded localfs.init_kwargs [WARNING ] Could not resolve policy pattern minion_{grains[roles]} [DEBUG ] minion policies: [] [DEBUG ] LazyLoaded local_cache.prep_jid [DEBUG ] Sending event: tag = salt/run/20171212161909816137/ret; data = {'fun_args': ['minion2', 'MMcO0ADieRLdt0XsYQLPs8V6Ru+HMt2iWFQW5jX32F8m2YEeqMcoAXq/nGh987oRAtN7jVyi8EGBs+Q56UtefpnTikDBpjHgf8LtFaJ4jR7721VKnF9vSjdc5xpO9ESWGszj6fP80nlIfPyU6cAZ/wMd6MPoDw+o9b29PBwmxrdVHu+gSUksRU8oD5pnnEXJs5yMceCj8MG4y5u18VuqoPsWDRzblH8b9z21wiqznzxdkj5JfBPlxNhlJy6a+4i+b7qc82zpxBt4reve5i+0QYXgE5nJjMsCY0BTdSlj3gi2rcOfxsJpetvOm9d+OfN0RTmu5PL/iwKZs3olds+ogg=='], 'jid': '20171212161909816137', 'return': {'error': 'No policies matched minion'}, 'success': True, '_stamp': '2017-12-13T00:19:10.150126', 'user': 'sudo_vagrant', 'fun': 'runner.vault.generate_token'} [DEBUG ] LazyLoaded nested.output error: No policies matched minion [INFO ] Runner completed: 20171212161909816137

Thoughts?

rcallphin avatar Dec 13 '17 00:12 rcallphin

Oh, right. I was a bit tired yesterday and got the entire problem backwards, it seems. Sorry! And your solution looks right for this case, very nice!

carlpett avatar Dec 13 '17 08:12 carlpett

@gtmanfred, Off of @carlpett review I've created a pull request. Let me know if you need anything else.

rcallphin avatar Dec 13 '17 16:12 rcallphin

PR looks good, i updated it to be against the right branch.

That should be it!

Thanks all for your help! Daniel

gtmanfred avatar Dec 13 '17 16:12 gtmanfred

Great, Thanks!

rcallphin avatar Dec 13 '17 17:12 rcallphin