Skip to content

Commit d16e06d

Browse files
author
Mmadu Manasseh
committed
Initialize Module
0 parents  commit d16e06d

9 files changed

+516
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.tfvars
2+
*.tfstate*
3+
.terraform/
4+
**/inspec.lock
5+
*.gem
6+
Gemfile.lock
7+
terraform.tfstate.d/
8+
eks-admin-cluster-role-binding.yaml
9+
eks-admin-service-account.yaml
10+
.idea/
11+
*.iml
12+
config-map-aws-auth*.yaml
13+
kubeconfig_*
14+
*.swp

.gitlab-ci.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
variables:
2+
DOCKER_USERNAME: _json_key
3+
DOCKER_TLS_CERTDIR: ""
4+
DOCKER_HOST: tcp://docker:2375
5+
GCLOUD_SDK: gcr.io/google.com/cloudsdktool/cloud-sdk
6+
7+
image:
8+
name: hashicorp/terraform:0.13.3
9+
entrypoint:
10+
- "/usr/bin/env"
11+
- "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
12+
13+
stages:
14+
- init
15+
- validate
16+
17+
18+
cache: &cache
19+
key: "${CI_COMMIT_REF_SLUG}-$CI_PROJECT_DIR-$DEPLOY_DIR"
20+
paths:
21+
- $CI_PROJECT_DIR/$DEPLOY_DIR/.terraform
22+
23+
init:
24+
stage: init
25+
script:
26+
- terraform init -input=false
27+
28+
#------------------------------
29+
# TERRAFORM LINT TEMPLATE
30+
#------------------------------
31+
validate:
32+
stage: validate
33+
script:
34+
- terraform validate
35+
cache:
36+
<<: *cache
37+
policy: pull
38+
39+
#------------------------------
40+
# TERRAFORM LINT TEMPLATE
41+
#------------------------------
42+
lint:
43+
stage: validate
44+
script:
45+
- terraform fmt -check -diff -write=false --recursive .
46+
cache:
47+
<<: *cache
48+
policy: pull

.pre-commit-config.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: git://github.com/antonbabenko/pre-commit-terraform
3+
rev: v1.43.0
4+
hooks:
5+
- id: terraform_fmt
6+
- id: terraform_docs
7+
- id: terraform_validate
8+
- id: terraform_tflint

CONTRIBUTIONS.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Contributing
2+
3+
When contributing to this repository, please first discuss the change you wish to make via issue,
4+
email, or any other method with the owners of this repository before making a change.
5+
6+
Please note we have a code of conduct, please follow it in all your interactions with the project.
7+
8+
## Pull Request Process
9+
10+
1. Ensure any install or build dependencies are removed before the end of the layer when doing a build. Follow [these instructions](https://github.com/antonbabenko/pre-commit-terraform#how-to-install) to install pre-commit locally to run pre-commit terraform hooks to validate your changes before committing
11+
2. Update the README.md with details of changes to the interface, this includes new environment variables, exposed ports, useful file locations and container parameters.
12+
3. Once all outstanding comments and checklist items have been addressed, your contribution will be merged! Merged PRs will be included in the next release. The mainteners takes care of updating the CHANGELOG as they merge.
13+
14+
## Checklists for contributions
15+
16+
- [ ] Add [sementics prefix](#semantic-pull-requests) to your PR or Commits (at leats one of your commit groups)
17+
- [ ] CI tests are passing
18+
- [ ] README.md has been updated after any changes to variables and outputs. See [docs generation](README.md#doc-generation)
19+
20+
## Semantic Pull Requests
21+
22+
To generate changelog, Pull Requests or Commits must have sementic and must follow conventional specs below:
23+
24+
- `feat:` for new features
25+
- `fix:` for bug fixes
26+
- `improvement:` for enhancements
27+
- `docs:` for documentation and examples
28+
- `refactor:` for code refactoring
29+
- `test:` for tests
30+
- `ci:` for CI purpose
31+
- `chore:` for chores stuff
32+
33+
The `chore` prefix skipped during changelog generation. It can be used for `chore: update changelog` commit message by example.

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Terraform OpenVPN GCP
2+
A terraform module to setup OpenVPN on GCP
3+
4+
5+
## Usage
6+
7+
```hcl
8+
module "openvpn" {
9+
source = "../modules/terraform-openvpn-gcp"
10+
region = var.region
11+
project_id = var.project_id
12+
network = module.vpc.network
13+
subnetwork = module.vpc.public_subnetwork
14+
hostname = "openvpn"
15+
output_dir = "${path.module}/openvpn"
16+
users = ["bob", "alice"]
17+
}
18+
19+
```
20+
21+
## Doc generation
22+
23+
Code formatting and documentation for variables and outputs is generated using [pre-commit-terraform hooks](https://github.com/antonbabenko/pre-commit-terraform) which uses [terraform-docs](https://github.com/segmentio/terraform-docs).
24+
25+
26+
And install `terraform-docs` with
27+
```bash
28+
go get github.com/segmentio/terraform-docs
29+
```
30+
or
31+
```bash
32+
brew install terraform-docs.
33+
```
34+
35+
## Contributing
36+
37+
Report issues/questions/feature requests on in the issues section.
38+
39+
Full contributing guidelines are covered [here](CONTRIBUTIONS.md).
40+
41+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
42+
## Requirements
43+
44+
No requirements.
45+
46+
## Providers
47+
48+
| Name | Version |
49+
|------|---------|
50+
| google | n/a |
51+
| local | n/a |
52+
| null | n/a |
53+
| random | n/a |
54+
| tls | n/a |
55+
56+
## Inputs
57+
58+
| Name | Description | Type | Default | Required |
59+
|------|-------------|------|---------|:--------:|
60+
| disk\_size\_gb | n/a | `string` | `"30"` | no |
61+
| hostname | Hostname of instances | `string` | `"openvpn"` | no |
62+
| image\_family | n/a | `string` | `"ubuntu-2004-lts"` | no |
63+
| labels | Labels, provided as a map | `map` | `{}` | no |
64+
| metadata | Metadata, provided as a map | `map` | `{}` | no |
65+
| network | The name or self\_link of the network to attach this interface to. Use network attribute for Legacy or Auto subnetted networks and subnetwork for custom subnetted networks. | `any` | `null` | no |
66+
| network\_tier | Network network\_tier | `string` | `"STANDARD"` | no |
67+
| output\_dir | Folder to store all user openvpn details | `string` | `"openvpn"` | no |
68+
| project\_id | The GCP Project ID | `any` | `null` | no |
69+
| region | The GCP Project Region | `any` | `null` | no |
70+
| remote\_user | The user to operate as on the VM. SSH Key is generated for this user | `string` | `"ubuntu"` | no |
71+
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account. | <pre>object({<br> email = string,<br> scopes = set(string)<br> })</pre> | <pre>{<br> "email": null,<br> "scopes": []<br>}</pre> | no |
72+
| source\_image\_project | n/a | `string` | `"ubuntu-os-cloud"` | no |
73+
| subnetwork | The name of the subnetwork to attach this interface to. The subnetwork must exist in the same region this instance will be created in. Either network or subnetwork must be provided. | `any` | `null` | no |
74+
| tags | network tags to attach to the instance | `list` | `[]` | no |
75+
| users | list of user to create | `list(string)` | `[]` | no |
76+
77+
## Outputs
78+
79+
| Name | Description |
80+
|------|-------------|
81+
| users | Created Users |
82+
83+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

main.tf

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Uses https://github.com/angristan/openvpn-install to setup VPN in a google VM
2+
# creates and deletes users accordingly
3+
4+
locals {
5+
metadata = merge(var.metadata, {
6+
sshKeys = "${var.remote_user}:${tls_private_key.ssh-key.public_key_openssh}"
7+
})
8+
ssh_tag = ["allow-ssh"]
9+
tags = toset(concat(var.tags, local.ssh_tag))
10+
output_folder = var.output_dir
11+
private_key_file = "private-key.pem"
12+
}
13+
14+
resource "google_compute_firewall" "allow-external-ssh" {
15+
name = "allow-external-ssh"
16+
network = var.network
17+
18+
allow {
19+
protocol = "tcp"
20+
ports = ["22"]
21+
}
22+
23+
source_ranges = ["0.0.0.0/0"]
24+
target_tags = local.ssh_tag
25+
}
26+
27+
resource "google_compute_address" "default" {
28+
name = "global-openvpn-ip"
29+
region = var.region
30+
network_tier = var.network_tier
31+
}
32+
33+
resource "tls_private_key" "ssh-key" {
34+
algorithm = "RSA"
35+
}
36+
37+
38+
// SSH Private Key
39+
resource "local_file" "private_key" {
40+
sensitive_content = tls_private_key.ssh-key.private_key_pem
41+
filename = "${var.output_dir}/${local.private_key_file}"
42+
file_permission = "0400"
43+
}
44+
45+
resource "random_id" "this" {
46+
byte_length = "8"
47+
}
48+
49+
resource "random_id" "password" {
50+
byte_length = "16"
51+
}
52+
53+
module "instance_template" {
54+
source = "terraform-google-modules/vm/google//modules/instance_template"
55+
version = "~>6.0.0"
56+
region = var.region
57+
project_id = var.project_id
58+
network = var.network
59+
subnetwork = var.subnetwork
60+
metadata = local.metadata
61+
service_account = var.service_account
62+
source_image_family = var.image_family
63+
source_image_project = var.source_image_project
64+
disk_size_gb = var.disk_size_gb
65+
66+
startup_script = <<SCRIPT
67+
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh
68+
chmod +x openvpn-install.sh
69+
mv openvpn-install.sh /home/${var.remote_user}/
70+
export AUTO_INSTALL=y
71+
export PASS=1
72+
/home/${var.remote_user}/openvpn-install.sh
73+
SCRIPT
74+
75+
tags = local.tags
76+
labels = var.labels
77+
}
78+
79+
80+
module "openvpn_vm" {
81+
source = "terraform-google-modules/vm/google//modules/compute_instance"
82+
version = "~>6.0.0"
83+
region = var.region
84+
network = var.network
85+
subnetwork = var.subnetwork
86+
hostname = var.hostname
87+
instance_template = module.instance_template.self_link
88+
89+
access_config = [{
90+
nat_ip = google_compute_address.default.address
91+
network_tier = var.network_tier
92+
}]
93+
}
94+
95+
96+
resource "null_resource" "openvpn_update_users_script" {
97+
triggers = {
98+
users = join(", ", var.users)
99+
}
100+
101+
connection {
102+
type = "ssh"
103+
user = var.remote_user
104+
host = google_compute_address.default.address
105+
private_key = tls_private_key.ssh-key.private_key_pem
106+
}
107+
108+
provisioner "file" {
109+
source = "${path.module}/scripts/update_users.sh"
110+
destination = "/home/${var.remote_user}/update_users.sh"
111+
when = create
112+
}
113+
114+
# Create New User with MENU_OPTION=1
115+
provisioner "remote-exec" {
116+
inline = [
117+
"while [ ! -f /etc/openvpn/server.conf ]; do sleep 10; done",
118+
"chmod +x ~${var.remote_user}/update_users.sh",
119+
"sudo ~${var.remote_user}/update_users.sh ${join(" ", var.users)}",
120+
]
121+
when = create
122+
}
123+
124+
depends_on = [module.openvpn_vm, local_file.private_key]
125+
}
126+
127+
# Download user configurations to output_dir
128+
resource "null_resource" "openvpn_download_configurations" {
129+
depends_on = [null_resource.openvpn_update_users_script]
130+
131+
triggers = {
132+
users = join(", ", var.users)
133+
}
134+
135+
# Copy .ovpn config for user from server to var.output_dir
136+
provisioner "local-exec" {
137+
working_dir = var.output_dir
138+
command = <<SCRIPT
139+
scp -i ${local.private_key_file} \
140+
-o StrictHostKeyChecking=no \
141+
-o UserKnownHostsFile=/dev/null \
142+
${var.remote_user}@${google_compute_address.default.address}:/home/${var.remote_user}/*.ovpn .
143+
SCRIPT
144+
when = create
145+
}
146+
}

output.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "users" {
2+
description = "Created Users"
3+
value = var.users
4+
}

0 commit comments

Comments
 (0)