Skip to content
Snippets Groups Projects
Commit cee0826d authored by Goik Martin's avatar Goik Martin
Browse files

Generated server authenticating known_hosts file

parent 6133a6a1
No related branches found
No related tags found
No related merge requests found
Showing
with 115 additions and 0 deletions
# Cloud-init creating a simple http static content server
- File `userData` connecting Terraform and Cloud-init
- Package Nginx + static content
- Firewall by `network.tf` allowing incoming 80 and 443 (yet unused) connections
......@@ -27,4 +27,5 @@ resource "hcloud_firewall" "wwwFw" {
"::/0"
]
}
}
# Improving ssh handling and security
- Creating local `bin/ssh` and related `gen/known_hosts` file from templates
- Enhancing `sshd` security settings
*
!.gitignore
\ No newline at end of file
*
!.gitignore
\ No newline at end of file
terraform {
required_providers {
hcloud = {
source = "hetznercloud/hcloud"
}
}
required_version = ">= 0.13"
}
provider "hcloud" {
token = var.hcloud_token
}
resource "tls_private_key" "host" {
algorithm = "ED25519"
}
resource "hcloud_ssh_key" "loginUser" {
name = "goik@hdm-stuttgart.de"
public_key = file("~/.ssh/id_ed25519.pub")
}
resource "hcloud_server" "helloServer" {
name = "hello"
image = "debian-12"
server_type = "cx11"
user_data = templatefile("tpl/userData.yml", {
host_ed25519_private = indent(4, tls_private_key.host.private_key_openssh) # yaml format parsing quirk, sigh!
host_ed25519_public = tls_private_key.host.public_key_openssh
devopsSshPublicKey = hcloud_ssh_key.loginUser.public_key
})
ssh_keys = [hcloud_ssh_key.loginUser.id]
firewall_ids = [hcloud_firewall.sshFw.id]
}
resource "local_file" "known_hosts" {
content = "${hcloud_server.helloServer.ipv4_address} ${tls_private_key.host.public_key_openssh}"
filename = "gen/known_hosts"
file_permission = "644"
}
resource "local_file" "ssh_script" {
content = templatefile("tpl/ssh.sh", {
ip = hcloud_server.helloServer.ipv4_address
})
filename = "bin/ssh"
file_permission = "700"
depends_on = [local_file.known_hosts]
}
resource "hcloud_firewall" "sshFw" {
name = "www-firewall"
rule {
direction = "in"
protocol = "tcp"
port = "22"
source_ips = [
"0.0.0.0/0",
"::/0"
]
}
}
output "hello_ip_addr" {
value = hcloud_server.helloServer.ipv4_address
description = "The server's IPv4 address"
}
output "hello_datacenter" {
value = hcloud_server.helloServer.datacenter
description = "The server's datacenter"
}
hcloud_token="your_api_token_goes_here"
#!/usr/bin/env bash
GEN_DIR=$(dirname "$0")/../gen
ssh -o UserKnownHostsFile="$GEN_DIR/known_hosts" devops@${ip} "$@"
#cloud-config
ssh_keys:
ed25519_private: |
${host_ed25519_private}
ed25519_public: ${host_ed25519_public}
users:
- name: devops
groups: users, admin
sudo: ALL=(ALL) NOPASSWD:ALL
shell: /bin/bash
ssh_authorized_keys:
- ${devopsSshPublicKey}
runcmd:
- sed -ie '/^PermitRootLogin/s/^.*$/PermitRootLogin no/' /etc/ssh/sshd_config
- sed -ie '/^PasswordAuthentication/s/^.*$/PasswordAuthentication no/' /etc/ssh/sshd_config
- sed -ie '/^#AuthorizedKeysFile/s/^.*$/AuthorizedKeysFile .ssh/authorized_keys/' /etc/ssh/sshd_config
- sed -i '$a AllowUsers devops' /etc/ssh/sshd_config
- systemctl restart ssh
variable "hcloud_token" { # See secret.auto.tfvars
nullable = false
sensitive = true
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment