Skip to content

Commit c719469

Browse files
committed
add ssh key for test-user in windows
1 parent 6e18c8c commit c719469

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

Diff for: test_code/AZURE_MACHINES/main.tf

+9-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ locals {
1212

1313
}
1414

15+
data "template_file" "init" {
16+
template = "${file("${path.module}/azure_windows.ps.tpl")}"
17+
vars = {
18+
public_key = (var.CI) ? var.PUBLIC_KEY : file(var.public_key_path)
19+
}
20+
}
21+
1522
resource "azurerm_resource_group" "linux_host_test" {
1623
name = format(var.name_format, "linux-host-test-resources")
1724
location = var.location
@@ -52,7 +59,7 @@ resource "azurerm_linux_virtual_machine" "linux_host_test" {
5259
custom_data = filebase64(each.value.user_data)
5360
}
5461

55-
resource "azurerm_windows_virtual_machine" "windows_host_test" {
62+
resource "azurerm_windows_virtual_machine" "linux_host_test" {
5663
# https://azapril.dev/2020/05/12/terraform-depends_on/
5764
depends_on = [
5865
azurerm_network_interface_security_group_association.linux_host_test
@@ -81,5 +88,5 @@ resource "azurerm_windows_virtual_machine" "windows_host_test" {
8188
version = each.value.source_image_reference.version
8289
}
8390

84-
custom_data = filebase64(each.value.user_data)
91+
custom_data = filebase64(data.template_file.init.rendered)
8592
}

Diff for: test_code/user_data/azure_windows.ps.tpl

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<powershell>
2+
# Execute it with elevated permissions
3+
# Description:
4+
# This script install automatically the open-ssh feature and enable it
5+
6+
# enable tls1.2 for downloads
7+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
8+
9+
# creating openssh folder and download the zip
10+
mkdir c:\openssh-install
11+
cd c:\openssh-install
12+
13+
#update the last version if you want the last release
14+
Invoke-WebRequest -Uri "https://github.com/PowerShell/Win32-OpenSSH/releases/download/V8.6.0.0p1-Beta/OpenSSH-Win64.zip" -OutFile .\openssh.zip
15+
Expand-Archive .\openssh.zip -DestinationPath .\openssh\
16+
cd .\openssh\OpenSSH-Win64\
17+
18+
# required for enable the service
19+
setx PATH "$env:path;c:\openssh-install\openssh\OpenSSH-Win64\" -m
20+
21+
# required for install the service
22+
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
23+
24+
# required for execute remote connections
25+
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
26+
27+
net start sshd
28+
29+
# auto enable for each restart machine
30+
Set-Service sshd -StartupType Automatic
31+
32+
#Set default shell to powershell
33+
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -PropertyType String -Force
34+
35+
# Configure SSH for the specific user "test-user" with public key "12345"
36+
$sshUser = "test-user"
37+
$sshPublicKey = "${var.public_key}"
38+
$sshUserPath = "C:\ProgramData\ssh\administrators_authorized_keys"
39+
40+
# Append the public key to the authorized_keys file for the user
41+
Add-Content -Path $sshUserPath -Value "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC12345"
42+
43+
# Restart the sshd service to apply the changes
44+
Restart-Service sshd
45+
</powershell>
46+
<persist>true</persist>

0 commit comments

Comments
 (0)