Skip to content
This repository was archived by the owner on Nov 8, 2021. It is now read-only.

Commit fd7b057

Browse files
mvanbaakmichaelwittig
authored andcommitted
Setup cron in rpm (#48)
* Have the RPM handle setting up the cron to import the users * Fix path to config file and make sure the RPM_BUILD_ROOT has the config and cron.d directories * Update readme to state the RPM installs a crontab config * When installed using the RPM, tell the user they need to configure the tool before it will actually do some work.
1 parent 66fbabd commit fd7b057

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

README.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,7 @@ A picture is worth a thousand words:
5050
2. Attach the IAM permissions defined in `iam_ssh_policy.json` to the EC2 instances (by creating an IAM role and an Instance Profile)
5151
3. Install the RPM: `rpm -i ttps://s3-eu-west-1.amazonaws.com/widdix-aws-ec2-ssh-releases-eu-west-1/aws-ec2-ssh-1.1.0-1.el7.centos.noarch.rpm`
5252
4. The configuration file is placed into `/etc/aws-ec2-ssh.conf`
53-
5. Install a cronjob to sync the IAM users
54-
```
55-
echo "*/10 * * * * root /usr/bin/import_users.sh" > /etc/cron.d/import_users
56-
chmod 0644 /etc/cron.d/import_users
57-
/usr/bin/import_users.sh
58-
```
53+
5. The RPM creates a crontab file to run import_users.sh every 10 minutes. This file is placed in `/etc/cron.d/import_users`
5954

6055
### Install via install.sh script
6156

aws-ec2-ssh.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ LOCAL_MARKER_GROUP="iam-synced-users"
33
LOCAL_GROUPS=""
44
SUDOERSGROUP=""
55
ASSUMEROLE=""
6+
7+
# Remove or set to 0 if you are done with configuration
8+
# To change the interval of the sync change the file
9+
# /etc/cron.d/aws-ec2-ssh
10+
DONOTSYNC=0

aws-ec2-ssh.spec

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,28 @@ Use your IAM user's public SSH key to get access via SSH to an EC2 instance.
3434
%install
3535
rm -rf ${RPM_BUILD_ROOT}
3636
mkdir -p ${RPM_BUILD_ROOT}%{_bindir}
37+
mkdir -p ${RPM_BUILD_ROOT}%{_sysconfdir}/cron.d
3738
install -m 755 import_users.sh ${RPM_BUILD_ROOT}%{_bindir}
3839
install -m 755 authorized_keys_command.sh ${RPM_BUILD_ROOT}%{_bindir}
39-
install -m 755 aws-ec2-ssh.conf ${RPM_BUILD_ROOT}/etc/aws-ec2-ssh.conf
40+
install -m 755 aws-ec2-ssh.conf ${RPM_BUILD_ROOT}%{_sysconfdir}/aws-ec2-ssh.conf
41+
sed -i '/DONOTSYNC=0/DONOTSYNC=1/' ${RPM_BUILD_ROOT}%{_sysconfdir}/aws-ec2-ssh.conf
42+
echo "*/10 * * * * root /usr/bin/import_users.sh" > ${RPM_BUILD_ROOT}%{_sysconfdir}/cron.d/import_users
43+
chmod 0644 ${RPM_BUILD_ROOT}%{_sysconfdir}/cron.d/import_users
4044

4145
%post
4246
sed -i 's:#AuthorizedKeysCommand none:AuthorizedKeysCommand /usr/bin/authorized_keys_command.sh:g' /etc/ssh/sshd_config
4347
sed -i 's:#AuthorizedKeysCommandUser nobody:AuthorizedKeysCommandUser nobody:g' /etc/ssh/sshd_config
4448
/etc/init.d/sshd restart
49+
/sbin/service crond condrestart 2>&1 > /dev/null || :
50+
51+
echo "To configure the aws-ec2-ssh package, edit /etc/aws-ec-ssh.conf. No users will be synchronized before you did this."
4552

4653

4754
%postun
4855
sed -i 's:AuthorizedKeysCommand /usr/bin/authorized_keys_command.sh:#AuthorizedKeysCommand none:g' /etc/ssh/sshd_config
4956
sed -i 's:AuthorizedKeysCommandUser nobody:#AuthorizedKeysCommandUser nobody:g' /etc/ssh/sshd_config
5057
/etc/init.d/sshd restart
58+
/sbin/service crond condrestart 2>&1 > /dev/null || :
5159

5260

5361
%clean
@@ -58,11 +66,15 @@ rm -rf ${RPM_BUILD_ROOT}
5866
%defattr(-,root,root)
5967
%attr(755,root,root) %{_bindir}/import_users.sh
6068
%attr(755,root,root) %{_bindir}/authorized_keys_command.sh
61-
%config /etc/aws-ec2-ssh.conf
69+
%config %{_sysconfdir}/aws-ec2-ssh.conf
70+
%config %{_sysconfdir}/cron.d/import_users
6271

6372

6473
%changelog
6574

75+
* Wed May 3 2017 Michiel van Baak <[email protected]> - 1.1.0-2
76+
- Create cron.d file and run import_users on install
77+
6678
* Thu Apr 27 2017 Michiel van Baak <[email protected]> - post-1.0-master
6779
- use correct versioning based on fedora package versioning guide
6880

import_users.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
# source configuration if it exists
44
[ -f /etc/aws-ec2-ssh.conf ] && . /etc/aws-ec2-ssh.conf
55

6+
# Should we actually do something?
7+
: ${DONOTSYNC:=0}
8+
9+
if [ ${DONOTSYNC} -eq 1 ]
10+
then
11+
echo "Please configure aws-ec2-ssh by editing /etc/aws-ec2-ssh.conf"
12+
exit 1
13+
fi
14+
615
# Which IAM groups have access to this instance
716
# Comma seperated list of IAM groups. Leave empty for all available IAM users
817
: ${IAM_AUTHORIZED_GROUPS:=""}

0 commit comments

Comments
 (0)