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

Add ability to allow ssh commands only for specific users #141

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_allow_ssh_commands"></a> [allow\_ssh\_commands](#input\_allow\_ssh\_commands) | Allows the SSH user to execute one-off commands. Pass true to enable. Warning: These commands are not logged and increase the vulnerability of the system. Use at your own discretion. | `bool` | `false` | no |
| <a name="input_allow_ssh_commands_for_users"></a> [allow\_ssh\_commands\_for\)users](#input\_allow\_ssh\_commands\_for\_users) | Allows a list of users to execute one-off commands. Warning: These commands are not logged and increase the vulnerability of the system. Use at your own discretion. | `list(string)` | `[]` | no |
| <a name="input_associate_public_ip_address"></a> [associate\_public\_ip\_address](#input\_associate\_public\_ip\_address) | n/a | `bool` | `true` | no |
| <a name="input_auto_scaling_group_subnets"></a> [auto\_scaling\_group\_subnets](#input\_auto\_scaling\_group\_subnets) | List of subnets where the Auto Scaling Group will deploy the instances | `list(string)` | n/a | yes |
| <a name="input_bastion_additional_security_groups"></a> [bastion\_additional\_security\_groups](#input\_bastion\_additional\_security\_groups) | List of additional security groups to attach to the launch template | `list(string)` | `[]` | no |
Expand Down
14 changes: 8 additions & 6 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ data "aws_kms_alias" "kms-ebs" {
}

resource "aws_s3_object" "bucket_public_keys_readme" {
acl = "private"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pretty sure this was missing and is needed, but not sure if it belongs in the PR.

bucket = aws_s3_bucket.bucket.id
key = "public-keys/README.txt"
content = "Drop here the ssh public keys of the instances you want to control"
Expand Down Expand Up @@ -230,12 +231,13 @@ resource "aws_launch_template" "bastion_launch_template" {
key_name = var.bastion_host_key_pair

user_data = base64encode(templatefile("${path.module}/user_data.sh", {
aws_region = var.region
bucket_name = var.bucket_name
extra_user_data_content = var.extra_user_data_content
allow_ssh_commands = lower(var.allow_ssh_commands)
public_ssh_port = var.public_ssh_port
sync_logs_cron_job = var.enable_logs_s3_sync ? "*/5 * * * * /usr/bin/bastion/sync_s3" : ""
aws_region = var.region
bucket_name = var.bucket_name
extra_user_data_content = var.extra_user_data_content
allow_ssh_commands = lower(var.allow_ssh_commands)
allow_ssh_commands_for_users = var.allow_ssh_commands_for_users
public_ssh_port = var.public_ssh_port
sync_logs_cron_job = var.enable_logs_s3_sync ? "*/5 * * * * /usr/bin/bastion/sync_s3" : ""
}))

block_device_mappings {
Expand Down
2 changes: 1 addition & 1 deletion user_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ if [[ -z $SSH_ORIGINAL_COMMAND ]]; then
else

# If the module consumer wants to allow remote commands (for ansible or other) then allow that command through.
if [ "${allow_ssh_commands}" == "true" ]; then
if [ "${allow_ssh_commands}" == "true" ]%{ for user in allow_ssh_commands_for_users } || [ "${user}" == "`whoami`" ]%{endfor ~}; then
exec /bin/bash -c "$SSH_ORIGINAL_COMMAND"
else
# The "script" program could be circumvented with some commands (e.g. bash, nc).
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ variable "allow_ssh_commands" {
default = false
}

variable "allow_ssh_commands_for_users" {
description = "Allows a list of users to execute one-off commands. Warning: These commands are not logged and increase the vulnerability of the system. Use at your own discretion."
type = list(string)
default = []
}

variable "associate_public_ip_address" {
type = bool
default = true
Expand Down