Skip to content

Commit 31d25a5

Browse files
Merge pull request #3 from michaelstepner/systemctl-auto-launch
Automatically launch the proxy using systemctl
2 parents f587648 + 8215117 commit 31d25a5

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

README.md

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ For more information about the Email OAuth 2.0 Proxy, see the README in [simonro
1818

1919
I am paying ***approximately US$4.45 per month***, based on the prices I observed for my own usage in June 2022:
2020
* $5/year paid upfront for a .link domain registered on Route 53
21-
* As of 2022-06-11, the cheapest TLD is .click at $3/year *but it has no WHOIS privacy protection*.
22-
* As of 2022-06-11, the second-cheapest TLD is .link at $5/year **and it has WHOIS privacy protection**.
21+
* As of 2022-06-11, the cheapest TLD is .click at $3/year *but it has no WHOIS privacy protection*.
22+
* As of 2022-06-11, the second-cheapest TLD is .link at $5/year **and it has WHOIS privacy protection**.
2323
* $0.50/month for a Route 53 hosted zone
2424
* $3.07/month for a t4g EC2 instance (cheapest instance type)
2525
* $0.16/month for a 2GB EBS volume
@@ -30,57 +30,62 @@ I am paying ***approximately US$4.45 per month***, based on the prices I observe
3030
### Pre-requisites:
3131

3232
* An AWS account, with the [AWS CLI](https://aws.amazon.com/cli/) configured on your local machine.
33-
* You must have an AWS profile configured on your computer with admin access to your account, or at a minimum, sufficient privileges to manage the AWS resources used by this Terraform config.
34-
* The default profile will be used, although [an alternative profile can be specified](https://github.com/michaelstepner/email-oauth2-proxy-aws/blob/6c31fef7bbc091b1f756ce969fb60bb951786e29/terraform/variables.tf#L5).
33+
* You must have an AWS profile configured on your computer with admin access to your account, or at a minimum, sufficient privileges to manage the AWS resources used by this Terraform config.
34+
* The default profile will be used, although [an alternative profile can be specified](https://github.com/michaelstepner/email-oauth2-proxy-aws/blob/6c31fef7bbc091b1f756ce969fb60bb951786e29/terraform/variables.tf#L5).
3535
* A local installation of [Terraform](https://www.terraform.io/downloads).
3636

3737
### Installation steps:
38+
3839
1. Manually [register a domain using AWS Route 53](https://us-east-1.console.aws.amazon.com/route53/home#DomainRegistration).
39-
* It may take a few minutes to a few hours for Amazon to complete the domain registration. There may be manual steps involved, such as validating your email address.
40+
* It may take a few minutes to a few hours for Amazon to complete the domain registration. There may be manual steps involved, such as validating your email address.
41+
4042
2. From the AWS Console, navigate to [Route 53: Hosted Zones](https://us-east-1.console.aws.amazon.com/route53/v2/hostedzones#). Note down the "Hosted zone ID" for your chosen domain, which you will need in step 5.
43+
4144
3. Clone this repository onto your local computer.
45+
4246
4. Make a copy of the `terraform/config_example.tfvars` file, save it under a new name, and fill in the values with your own configuration settings.
43-
* You can consult the readme in [simonrob/email-oauth2-proxy](https://github.com/simonrob/email-oauth2-proxy#readme) for more details about the `email_oauth2_proxy_config` settings.
44-
* There are additional settings that can be configured, which may not be detailed in the example file. The full list of config settings is in [variables.tf](https://github.com/michaelstepner/email-oauth2-proxy-aws/blob/main/terraform/variables.tf).
47+
* You can consult the readme in [simonrob/email-oauth2-proxy](https://github.com/simonrob/email-oauth2-proxy#readme) for more details about the `email_oauth2_proxy_config` settings.
48+
* There are additional settings that can be configured, which may not be detailed in the example file. The full list of config settings is in [variables.tf](https://github.com/michaelstepner/email-oauth2-proxy-aws/blob/main/terraform/variables.tf).
49+
4550
5. Using a terminal, navigate to the `terraform` subdirectory of this repo and run the following commands, replacing ALL_CAPS values with your own:
46-
```
47-
terraform init
48-
terraform import -var-file=YOUR_CONFIG.tfvars aws_route53_zone.primary ZONE_ID_FROM_STEP_2
49-
```
51+
```
52+
terraform init
53+
terraform import -var-file=YOUR_CONFIG.tfvars aws_route53_zone.primary ZONE_ID_FROM_STEP_2
54+
```
55+
5056
6. You are now ready to create the AWS server. Using a terminal, in the `terraform` subdirectory of this repo, run the following command. Terraform will prompt you to review the resources that will be created, then type `yes` to confirm.
51-
```
52-
terraform apply -var-file=YOUR_CONFIG.tfvars
53-
```
54-
7. Using a terminal on your local computer, run `ssh -L 8080:127.0.0.1:8080 ec2-user@<PUBLIC_IP OR DOMAIN_FULL_NAME>`
55-
* This will connect to the email-oauth2-proxy server, while also forwarding its port 8080 to your local computer for OAuth2 authentication purposes.
56-
8. On the remote server you just SSH-ed into, run:
57-
```
58-
tmux
59-
cd email-oauth2-proxy/
60-
python3 emailproxy.py --no-gui --local-server-auth --config-file personal.config
61-
```
62-
9. In your email client, configure SMTP using the server settings:
63-
* Outgoing SMTP server: `DOMAIN_FULL_NAME`
64-
* Port: `465`
65-
* Username: `YOUR_EMAIL_ADDRESS`
66-
* Password: `ANY_STRING_OF_YOUR_CHOICE`
67-
10. When your email client attempts to connect to the SMTP server, you should see an authentication request appear in the email-oauth2-proxy server log via your SSH session. It will look like the text below. Copy and paste the URL from your terminal into your local browser, then complete the authentication prompts.
68-
```
69-
YYYY-MM-DD HH:MM:SS: Email OAuth 2.0 Proxy Local server auth mode: please authorise a request for account [email protected]
70-
YYYY-MM-DD HH:MM:SS: Please visit the following URL to authenticate account [email protected]: URL
71-
```
72-
11. After you've completed the authentication prompts in your local browser, you should see the successful authentication appear in the email-oauth2-proxy server log via your SSH session. It will look like the text below. At this point you can close your local browser tab. Your email client should be able to successfully connect to the SMTP server and send outgoing emails.
73-
```
74-
YYYY-MM-DD HH:MM:SS: SMTP ('1.2.3.4', 5678) [ Successfully authenticated SMTP connection - releasing session ]
75-
```
76-
12. In your terminal connected to the remote server, leave the email-oauth2-proxy server running while detaching from the tmux session by typing `Ctrl`+`b` then `d`. You can now log out of the remote server. It will continue running the email-oauth2-proxy server in the background.
57+
```
58+
terraform apply -var-file=YOUR_CONFIG.tfvars
59+
```
60+
61+
7. Using a terminal on your local computer, run `ssh -L 8080:127.0.0.1:8080 ec2-user@<PUBLIC_IP OR DOMAIN_FULL_NAME> journalctl --follow -u emailproxy`
62+
* This will display a live view of the email-oauth2-proxy logs, while also forwarding port 8080 on the server to your local computer for OAuth2 authentication purposes.
63+
64+
8. In your email client, configure SMTP using the server settings:
65+
* Outgoing SMTP server: `DOMAIN_FULL_NAME`
66+
* Port: `465`
67+
* Username: `YOUR_EMAIL_ADDRESS`
68+
* Password: `ANY_STRING_OF_YOUR_CHOICE`
69+
70+
9. When your email client attempts to connect to the SMTP server, you should see an authentication request appear in the email-oauth2-proxy server log via your SSH session. It will look like the text below. Copy and paste the URL from your terminal into your local browser, then complete the authentication prompts.
71+
```
72+
Email OAuth 2.0 Proxy Local server auth mode: please authorise a request for account [email protected]
73+
Please visit the following URL to authenticate account [email protected]: URL
74+
```
75+
76+
10. After you've completed the authentication prompts in your local browser, you should see the successful authentication appear in the email-oauth2-proxy server log via your SSH session. It will look like the text below. At this point you can close your local browser tab. Your email client should be able to successfully connect to the SMTP server and send outgoing emails.
77+
```
78+
SMTP ('1.2.3.4', 5678) [ Successfully authenticated SMTP connection - releasing session ]
79+
```
80+
81+
11. You can now log out of the remote server by typing `Ctrl`+`c` to end your SSH session. It will continue running the email-oauth2-proxy server in the background.
7782
7883
### Limitations
7984
8085
* Your TLS certificate from Let's Encrypt will expire automatically after 90 days.
81-
* After 60 to 90 days, you can renew the certificate by restarting the installation steps from Step 6 (`terraform apply ...`).
82-
* If any settings (such as the TLS certificate) have changed, this will destroy the existing server and create a brand new server.
83-
* You will therefore need to proceed through all the subsequent installation steps.
86+
* After 60 to 90 days, you can renew the certificate by re-running installation step 6 (`terraform apply ...`).
87+
* If any settings (such as the TLS certificate) have changed, this will destroy the existing server and create a brand new server.
88+
* You should not need to re-authenticate (installation steps 7-11), because your OAuth 2.0 tokens are stored persistently in AWS Secrets Manager. Your authentication is not lost when the server is destroyed and re-created.
8489
8590
## Contributing
8691
@@ -94,7 +99,7 @@ If you have a **feature request**, it is unlikely that I will be able to impleme
9499
95100
- [x] [Use certificate](https://github.com/simonrob/email-oauth2-proxy/blob/b26c7b4d25f431e2a1ea12a30667cb9746401211/emailproxy.config#L28) to secure the connection between email client and proxy server
96101
- [x] Add support for storing OAuth2 tokens using a secrets manager (e.g. AWS Secrets Manager) instead of locally, so it persists across servers
97-
- [ ] [Automatically launch](https://github.com/simonrob/email-oauth2-proxy/issues/2#issuecomment-839713677) `email-oauth2-proxy` and configure OAuth2 token without SSHing into the server
102+
- [x] [Automatically launch](https://github.com/simonrob/email-oauth2-proxy/issues/2#issuecomment-839713677) `email-oauth2-proxy` on server via `systemctl`
98103
- [ ] Add support for automatically rotating TLS certificate, which expires automatically after 90 days
99104
100105
## License

terraform/server-cloud-config.yaml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ ssh_keys:
99
# Install packages
1010
packages:
1111
- nano
12-
- tmux
1312
- git
1413
- python3-pip
1514
- iptables
@@ -28,6 +27,21 @@ write_files:
2827
encoding: base64
2928
content: ${base64encode(cert_privkey)}
3029
permissions: '0640'
30+
- path: /etc/systemd/system/emailproxy.service
31+
owner: root:root
32+
permissions: '0755'
33+
content: |
34+
[Unit]
35+
Description=Email OAuth 2.0 Proxy
36+
37+
[Service]
38+
User=ec2-user
39+
WorkingDirectory=/home/ec2-user/email-oauth2-proxy
40+
ExecStart=python3 emailproxy.py --no-gui --local-server-auth --config-file personal.config
41+
Restart=always
42+
43+
[Install]
44+
WantedBy=multi-user.target
3145
3246
# Run commands
3347
runcmd:
@@ -67,3 +81,6 @@ runcmd:
6781
region = ${aws_region}
6882
EOL
6983
- chown -R ec2-user:ec2-user /home/ec2-user/.aws/
84+
# Launch email-oauth2-proxy
85+
- systemctl daemon-reload
86+
- systemctl enable emailproxy.service --now

0 commit comments

Comments
 (0)