Skip to content

Commit

Permalink
Merged origin/ingress-restrictions into ingress-restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
devzspy committed Aug 14, 2024
2 parents 3d5b7f5 + 7309209 commit 151d55f
Show file tree
Hide file tree
Showing 20 changed files with 425 additions and 0 deletions.
4 changes: 4 additions & 0 deletions external/sketch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ A very simple set of provision scripts for zero-trust reflector proxies.
# Moving Parts

If you are using Linode, skip `provision.sh` and use the Linode instructions
If you are using Digital Ocean, skip `provision.sh` and use the Digital Ocean instructions

## provision.sh

Expand All @@ -28,6 +29,9 @@ You will need to add a SSH public key to this script.
## Linode
See `linode/README.md`

## Digital Ocean
See `digitalocean/README.md`

### How to Play with provision.sh

1. On trusted host:
Expand Down
24 changes: 24 additions & 0 deletions external/sketch/ansible/roles/ingress-restrictions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ingress-restrictions
=========

This role is used to help reduce the chances for backflip correlation from public ssh keys from tools like Censys.

This will automatically tell middle hosts to only allow connections on port 2222 from edge hosts.

This role only handles the sketch infrastructure as of now.

Requirements
------------

Once you've ran this in your Sketch infrastructure, you will need to grab the IP Addresses or IP Address range from your middles and add them to your OCI/AWS infrastructure manually.

Whenever we do the grand restructure, this should be done automatically for you everywhere.

Example Playbook
----------------

```yml
- hosts: all
roles:
- ingress-restrictions
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for ingress-restrictions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# handlers file for ingress-restrictions
52 changes: 52 additions & 0 deletions external/sketch/ansible/roles/ingress-restrictions/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

#
# Provide a list of supported platforms, and for each platform a list of versions.
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
# To view available platforms and versions (or releases), visit:
# https://galaxy.ansible.com/api/v1/platforms/
#
# platforms:
# - name: Fedora
# versions:
# - all
# - 25
# - name: SomePlatform
# versions:
# - all
# - 1.0
# - 7
# - 99.99

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
33 changes: 33 additions & 0 deletions external/sketch/ansible/roles/ingress-restrictions/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# tasks file for ingress-restrictions

- name: Set fact for 'edge' hosts
set_fact:
is_edge_host: true
when: "'edge' in inventory_hostname"

- name: Gather Edge IPs into a dictionary
set_fact:
edge_ips_dict: >-
{{
edge_ips_dict | default({}) | combine(
{ item: hostvars[item]['host_ip_address'] }
)
}}
loop: "{{ groups['all'] }}"
when: hostvars[item]['is_edge_host'] is defined and hostvars[item]['is_edge_host']
delegate_to: localhost

- name: Allow port 2222 from Edges to Middles
ufw:
rule: allow
port: 2222
from_ip: "{{ item.value }}"
with_items: "{{ edge_ips_dict | dict2items }}"
when: "'middle' in inventory_hostname"

- name: Deny port 2222
ufw:
rule: deny
port: 2222
when: "'middle' in inventory_hostname"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
localhost

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- hosts: localhost
remote_user: root
roles:
- ingress-restrictions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# vars file for ingress-restrictions
89 changes: 89 additions & 0 deletions external/sketch/digitalocean/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# How to Play

## How to start

The first thing you'll need to do is set up a Digital Ocean account.

You will need to generate a [Digital Ocean Personal Access Token (PAT)](https://cloud.digitalocean.com/account/api/tokens)

Next, copy `variables.tfvars.example` to `variables.tfvars` and edit to suit your needs:

``` terraform
# Your Digital Ocean Personal Accecss Token
do_pat = ""
# Which Digital Ocean region should these be built in
middle_region = ""
edge_regions = [""]
# Host image you would like to use
docean_image = ""
# Shortname for the engagement, will be used to identify resources in Digital Ocean and hostnames
engagement_name = ""
# Path to place your ssh configuration for this infrastructure
ssh_config_path = ""
# Change me if you are using different keys than those that are generated with homebase instantiation.
# Give the path to the private
# ssh_priv_key_path =
# ssh_pub_key_path =
```

## Spin up Instances

From there you can run

1. `terraform init`
2. `terraform apply -var-file=variables.tfvars`
3. Run the output ansible command to configure your hosts `ansible-playbook ../ansible/sketch-playbook.yml -i inventory.ini -e "ssh_key=KEY" -e "ssh_config_path=PATH"` or use `provision.sh` to configure them manually.

## Advanced configuration

A basic configuration will allow you to setup one `middle` and one `edge` instance in your region of choice. You may use these variables in your `variables.tfvars` to change them:

### ssh keys
By default the public keys placed on the instances will be `~/.ssh/${engagement_name}.pub`. You can change this by overw ritting the default value of `ssh_key_path` in `variables.tfvars`.

```terraform
### Regions
Configure different regions for your middle and edge instances.
```terraform
middle_region = "REGION-A"
edge_regions = ["REGION-B", "REGION-C", "REGION-D"]
```

### Instance Count
Configure multiple edge instances. (Can also be done with middle)

```terraform
edge_count_per_region = "N"
```

#### Multiple Regions and Instance Count > 1

If you configure you variables like so:

```terraform
edge_regions = ["REGION-B", "REGION-C", "REGION-D"]
edge_count_per_region = "2"
```

You will produce a total of **6** instances:

- `engagement-REGION-B-01`
- `engagement-REGION-B-02`
- `engagement-REGION-C-01`
- `engagement-REGION-C-02`
- `engagement-REGION-D-01`
- `engagement-REGION-D-02`

### Instance Type
Can be done with both middle and edge instances.

```terraform
middle_type = "g6-nanode-1"
```
11 changes: 11 additions & 0 deletions external/sketch/digitalocean/edge.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
resource "digitalocean_droplet" "edge" {
# edge-region-number
label = "edge-${var.engagement_name}-${element(var.edge_regions, floor(count.index / var.edge_count_per_region))}-${format("%02g", (count.index % var.edge_count_per_region) + 1)}"
region = element(var.edge_regions, floor(count.index / var.edge_count_per_region))
count = var.edge_count_per_region * length(var.edge_regions)
type = var.edge_type
image = var.docean_image
tags = [var.engagement_name]
root_pass = random_password.root_password.result
authorized_keys = [chomp(file(local.ssh_pub_key_path))]
}
61 changes: 61 additions & 0 deletions external/sketch/digitalocean/firewall.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
resource "digitalocean_firewall" "firewall" {
name = "${var.engagement_name}-firewall"
droplet_ids = flatten([digitalocean_droplet.middle.*.id, digitalocean_droplet.edge.*.id])
tags = [var.engagement_name]
inbound_rule {
protocol = "tcp"
port_range = "0-65535"
source_addresses = ["0.0.0.0/0"]
}

dynamic "inbound_rule" {
// Allow all TCP ports specified in the allowed_tcp_ports variable from anywhere
for_each = var.allowed_tcp_ports
content {
protocol = "tcp"
port_range = inbound_rule.value
source_addresses = ["0.0.0.0/0"]
}
}

dynamic "inbound_rule" {
// Allow engagement hosts to access each other
for_each = ["tcp", "udp", "icmp"]
content {
protocol = inbound_rule.value
port_range = "all"
source_addresses = [for ip in flatten([digitalocean_droplet.middle.*.ipv4_address, digitalocean_droplet.edge.*.ipv4_address]): "${ip}/32"]
}
}

dynamic "inbound_rule" {
// Block all IPv6 traffic inbound
for_each = ["tcp", "udp", "icmp"]
content {
protocol = inbound_rule.value
port_range = "all"
source_addresses = ["::/0"]
}
}

outbound_rule {
protocol = "tcp"
port_range = "0-65535"
destination_addresses = ["0.0.0.0/0"]
}

dynamic "outbound_rule" {
// Block all IPv6 traffic outbound
for_each = ["tcp", "udp", "icmp"]
content {
protocol = outbound_rule.value
port_range = "all"
destination_addresses = ["::/0"]
}
}

outbound_rule {
protocol = "icmp"
destination_addresses = ["0.0.0.0/0"]
}
}
7 changes: 7 additions & 0 deletions external/sketch/digitalocean/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
locals {
abs_ssh_config_path = pathexpand("${var.ssh_config_path}/${var.engagement_name}-sketch")
default_ssh_priv_key = pathexpand("~/.ssh/${var.engagement_name}")
default_ssh_pub_key = pathexpand("~/.ssh/${var.engagement_name}.pub")
ssh_priv_key_path = length(var.ssh_priv_key_path) > 0 ? var.ssh_priv_key_path : local.default_ssh_priv_key
ssh_pub_key_path = length(var.ssh_pub_key_path) > 0 ? var.ssh_pub_key_path : local.default_ssh_pub_key
}
10 changes: 10 additions & 0 deletions external/sketch/digitalocean/middle.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "digitalocean_droplet" "middle" {
label = "middle${format("%02g", count.index + 1)}-${var.engagement_name}"
region = var.middle_region
count = var.middle_count
type = var.middle_type
image = var.docean_image
tags = [var.engagement_name]
root_pass = random_password.root_password.result
authorized_keys = [chomp(file(local.ssh_pub_key_path))]
}
3 changes: 3 additions & 0 deletions external/sketch/digitalocean/password.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
resource "random_password" "root_password" {
length = 30
}
18 changes: 18 additions & 0 deletions external/sketch/digitalocean/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
terraform {
required_providers {
digitalocean = {
source = "digitalocean/digitalocean"
version = "~> 2.0"
}
}
}

variable "pvt_key" {}

provider "digitalocean" {
token = var.do_pat
}

data "digitalocean_ssh_key" "terraform" {
name = "terraform"
}
16 changes: 16 additions & 0 deletions external/sketch/digitalocean/templates/inventory.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[all:vars]
infra_user=ubuntu
ansible_connection=ssh
ansible_ssh_user=root
ansible_ssh_host_key_checking=no
ansible_ssh_common_args=-o UserKnownHostsFile=/dev/null -o ProxyCommand='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ${ssh_priv_key_path} -W %h:%p {{ infra_user }}@homebase-${engagement_name}'

[middle]
%{ for middle in middles ~}
${middle.label}
%{ endfor ~}

[edge]
%{ for edge in edges ~}
${edge.label}
%{ endfor ~}
15 changes: 15 additions & 0 deletions external/sketch/digitalocean/templates/ssh-stanza.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%{ for middle in middles ~}
Host ${middle.label}
User root
IdentityFile ${ssh_private_key}
Hostname ${middle.ip_address}
ProxyJump proxy01-${engagement_name}

%{ for edge in edges ~}
Host ${edge.label}
User root
IdentityFile ${ssh_private_key}
Hostname ${edge.ip_address}
ProxyJump ${middle.label}
%{ endfor ~}
%{ endfor ~}
Loading

0 comments on commit 151d55f

Please sign in to comment.