Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ansible: SSH into shell servers as root #44

Merged
merged 13 commits into from
Feb 28, 2017
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vault_newpassphrase.pgp
*.retry
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ There are several playbooks present here:
- `mail.yml` deploy the mail aliases and Postfix configuration.
- `irc.yml` deploys static and templated configuration to the IRC servers,
including oper blocks for users defined in `group_vars/all/users.yml`.
- `ldap_ban.yml` disables a user's account in LDAP and terminate their
sessions on the shell servers. Invoke as follows:

ansible-playbook ldap_ban.yml -e 'user=${USERNAME}'


## Usage
Expand Down
2 changes: 1 addition & 1 deletion credentials.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- name: Deploy SSH keys on CoreOS hosts
hosts: coreos
gather_facts: False
strategy: free
roles:
- coreos-bootstrap
- role: coreos-authorized_keys
Expand All @@ -10,7 +11,6 @@

- name: Deploy SSH keys for root
hosts: shell_servers
become: true
tasks:
- block:
- command: mktemp /root/.ssh/authorized_keys.XXXXX
Expand Down
31 changes: 24 additions & 7 deletions doc/Blocking_account.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# How to block an account -- LDAP edition

Unfortunately, sometimes asshats don't take a hint and can't
be reasoned in using shared ressources respectfully.
Sadly, sometimes abuse happen.
Sometimes, admins are required to block a user's account.
Sometimes, there is automation for it.

For those times, the banhammer can be a reasonable option.
ansible-playbook ldap_ban.yml -e 'user=dude55b1'

The playbook will take care of disabling the account and
kicking the user from all shell servers.


# How the sausage is made

The playbook mainly performs two actions that can easily be done by hand:
- set the user's shell to `/usr/sbin/nologin` in LDAP;
- on each shell server:
- invalidate the SSSd cache for that user;
- terminate the user sessions.

The following details how to do it manually.


## Disabling the account
Expand All @@ -21,14 +36,16 @@ On `ldap.hashbang.sh`:
EOF


## Terminating the user
## Closing the user's sessions

Using ansible:
Using Ansible:

ansible shell_servers -u root -m shell -a "sss_cache -u dude55b1; loginctl terminate-user dude55b1"

This invalidates that user's record in the SSS cache, then forcefully
terminates the user's sessions (including all processes).
This does two things, in order:
- invalidate that user's record in the SSS cache,
otherwise the change in LDAP might not be picked up immediately;
- forcefully terminates the user's sessions (including all processes).

Unfortunately, `terminate-user` does not send a Russian mob
hitperson to dispatch the miscreant...
29 changes: 0 additions & 29 deletions doc/Blocking_user.md

This file was deleted.

10 changes: 10 additions & 0 deletions host_vars/ldap.hashbang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
$ANSIBLE_VAULT;1.1;AES256
34306139353035626239353739643532323431393635336566306533613635663433386132643237
6564306336326135303234393065336335613265323434610a326632643533633334626437313234
34393962316163366536616632663432393763313034616637653533386332623036313865633832
6566316434643762390a353638663966393132343634653163366163306336663062313231613934
30353766626533333862376230343230616162333138383735313534373038623136616236306531
32313861643032353764386362313232633533326465646537383864383335613232386261363935
64666637316463663363636534613166313961393263333565343438373633653061646263316463
36666335653161666662623732303162656464363336636638366566343331356131393234316231
35316233613266353135393630346138363139633062373032383835303034366538
4 changes: 4 additions & 0 deletions hosts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ny1.hashbang.sh
sf1.hashbang.sh
to1.hashbang.sh

[shell_servers]
ansible_user=root
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a link to issue 74 in a comment here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arg. Will do... in a separate PR? :V

In general, though, there is no benefit not to do this: direct root isn't any more privileged than admin+sudo, and this avoids the dependency on LDAP/userdb.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I like to disable root login via sshd. I'd like to do that for hashbang too but... this. :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eeeh, experience (#! and otherwise) says that having a method of getting root without relying on LDAP is good :3



[coreos]
hashbang.sh
im.hashbang.sh
Expand Down
1 change: 1 addition & 0 deletions irc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
- hosts: irc_servers
become: false
strategy: free
tasks:
# # Does not work on CoreOS, due to the fucked up
# # Python install and lack of installable packages.
Expand Down
31 changes: 31 additions & 0 deletions ldap_ban.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
# This is a sucky way to make LDAP changes,
# but Ansible's ldap_attr module is v2.3 only
- hosts: ldap.hashbang.sh
become: false
vars:
ldif: |
dn: uid={{ user }},ou=People,dc=hashbang,dc=sh
changetype: modify
replace: loginShell
loginShell: /usr/sbin/nologin

tasks:
- name: Disable account in LDAP
shell: echo "{{ ldif }}" | docker exec -i slapd ldapmodify -D {{ ldap.user }} -w {{ ldap.password }}

- hosts: shell_servers
become: true
strategy: free
tasks:
- name: Invalidate SSSd cache for {{ user }}
command: sss_cache -u {{ user }}
changed_when: false

- name: Terminate sessions for {{ user }}
command: loginctl terminate-user {{ user }}
register: terminate
failed_when: |
'Could not terminate user: No user' not in terminate.stderr
and terminate|failed
changed_when: terminate.stderr == "" and terminate|success
4 changes: 2 additions & 2 deletions shell.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
- name: Pull latest /etc files
hosts: shell_servers
become: true
gather_facts: false
strategy: free
tasks:
- block:
- name: Create a temporary GNUPGHOME
Expand Down Expand Up @@ -41,7 +41,7 @@
- name: Update/Sync system packages
hosts: shell_servers
gather_facts: false
become: true
strategy: free
tasks:
- name: Update apt cache
apt: update_cache=yes
Expand Down
1 change: 1 addition & 0 deletions vault_passphrase.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NEW_KEY_FILE=".vault_newpassphrase.pgp"
VAULT_FILES=(
group_vars/all/vault.yml
group_vars/irc
host_vars/ldap.hashbang.sh
)
RECIPIENTS=(
0x54BA90995CCBD6D6B0E68D27CDAB3CCDA649FFDA! # lrvick's 4k encryption key
Expand Down