theforeman / foreman_hooks

Run custom hook scripts on Foreman events
http://m0dlx.com/blog/Extending_Foreman_quickly_with_hook_scripts.html
GNU General Public License v3.0
56 stars 50 forks source link

FOREMAN_HOOKS_USER not working with audits and user logins #63

Open wiad opened 2 years ago

wiad commented 2 years ago

The following line: ENV['FOREMAN_HOOKS_USER'] = User.current.login

in lib/foreman_hooks/util.rb sets current user as an environment variable. This does not work when a new external user logs in and Foreman tries to update user attributes (firstname, lastname, email). The changes are executed and audited which will trigger any hooks you may have in audited/audit/, which will fail with backtrace:

2022-05-12T08:59:08 [D|app|d0bc1f0d] Running 6 hooks for Audited::Audit#after_create
2022-05-12T08:59:08 [D|sql|d0bc1f0d] Running hook: /usr/share/foreman/config/hooks/audited/audit/after_create/05-test.sh after_create 166891
2022-05-12T08:59:08 [D|sql|d0bc1f0d]    (0.5ms)  ROLLBACK
2022-05-12T08:59:08 [W|app|d0bc1f0d] undefined method `login' for nil:NilClass
2022-05-12T08:59:08 [I|app|d0bc1f0d] Backtrace for 'undefined method `login' for nil:NilClass' error (NoMethodError): undefined method `login' for nil:NilClass
 d0bc1f0d | /usr/share/gems/gems/foreman_hooks-0.3.17/lib/foreman_hooks/util.rb:56:in `exec_hook_int'

Probably because the User.current.login is not available at this stage. So we would need some validation and fallback setting here.