Skip to content

Commit

Permalink
Public Cloud: Use ed25519 in GCP and AWS
Browse files Browse the repository at this point in the history
  • Loading branch information
pdostal committed Feb 21, 2024
1 parent a758022 commit fb3cc81
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 11 deletions.
6 changes: 5 additions & 1 deletion data/publiccloud/terraform/azure.tf
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ variable "subnet_id" {
default = ""
}

variable "ssh_public_key" {
default = "/root/.ssh/id_ed25519.pub"
}

resource "random_id" "service" {
count = var.instance_count
keepers = {
Expand Down Expand Up @@ -162,7 +166,7 @@ resource "azurerm_linux_virtual_machine" "openqa-vm" {

admin_ssh_key {
username = "azureuser"
public_key = file("/root/.ssh/id_rsa.pub")
public_key = file("${var.ssh_public_key}")
}

os_disk {
Expand Down
7 changes: 6 additions & 1 deletion data/publiccloud/terraform/azure_nfstest.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ variable "subnet_id" {
default = ""
}

variable "ssh_public_key" {
default = "/root/.ssh/id_ed25519.pub"
}


## ---- data ---------------------------------------------------------------- ##

// IP address of the client
Expand Down Expand Up @@ -185,7 +190,7 @@ resource "azurerm_linux_virtual_machine" "openqa-vm" {

admin_ssh_key {
username = "azureuser"
public_key = file("~/.ssh/id_rsa.pub")
public_key = file("${var.ssh_public_key}")
}

os_disk {
Expand Down
6 changes: 5 additions & 1 deletion data/publiccloud/terraform/ec2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ variable "ipv6_address_count" {
default = 0
}

variable "ssh_public_key" {
default = "/root/.ssh/id_ed25519.pub"
}

resource "random_id" "service" {
count = var.instance_count
keepers = {
Expand All @@ -86,7 +90,7 @@ resource "random_id" "service" {

resource "aws_key_pair" "openqa-keypair" {
key_name = "openqa-${element(random_id.service.*.hex, 0)}"
public_key = file("/root/.ssh/id_rsa.pub")
public_key = file("${var.ssh_public_key}")
}

resource "aws_instance" "openqa" {
Expand Down
7 changes: 6 additions & 1 deletion data/publiccloud/terraform/gce.tf
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ variable "vm_create_timeout" {
default = "20m"
}

variable "ssh_public_key" {
default = "/root/.ssh/id_ed25519.pub"
}


resource "random_id" "service" {
count = var.instance_count
keepers = {
Expand Down Expand Up @@ -124,7 +129,7 @@ resource "google_compute_instance" "openqa" {
}

metadata = merge({
sshKeys = "susetest:${file("/root/.ssh/id_rsa.pub")}"
sshKeys = "susetest:${file("${var.ssh_public_key}")}"
openqa_created_by = var.name
openqa_created_date = timestamp()
openqa_created_id = element(random_id.service.*.hex, count.index)
Expand Down
2 changes: 2 additions & 0 deletions lib/publiccloud/azure.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ has container => 'sle-images';
has image_gallery => 'test_image_gallery';
has lease_id => undef;
has storage_region => 'westeurope';
# The ssh_key already exists in parrent class
has ssh_key => '/root/.ssh/id_rsa';

my $default_sku = 'gen2';

Expand Down
15 changes: 9 additions & 6 deletions lib/publiccloud/provider.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ has terraform_applied => 0;
has resource_name => sub { get_var('PUBLIC_CLOUD_RESOURCE_NAME', 'openqa-vm') };
has provider_client => undef;

has ssh_key => '/root/.ssh/id_rsa';
has ssh_key => '/root/.ssh/id_ed25519';

=head1 METHODS
Expand Down Expand Up @@ -154,11 +154,13 @@ Creates an ssh keypair in a given file path by $args{ssh_private_key_file}
=cut

sub create_ssh_key {
my ($self, %args) = @_;
$args{ssh_private_key_file} //= '/root/.ssh/id_rsa';
if (script_run('test -f ' . $args{ssh_private_key_file}) != 0) {
assert_script_run('SSH_DIR=`dirname ' . $args{ssh_private_key_file} . '`; mkdir -p $SSH_DIR');
assert_script_run('ssh-keygen -b 2048 -t rsa -q -N "" -C "" -m pem -f ' . $args{ssh_private_key_file});
my ($self) = @_;
my $alg = $self->ssh_key;
$alg =~ s@[a-z0-9/-_~.]*id_@@;
record_info($alg, "The $alg key will be generated.");
if (script_run('test -f ' . $self->ssh_key) != 0) {
assert_script_run('SSH_DIR=`dirname ' . $self->ssh_key . '`; mkdir -p $SSH_DIR');
assert_script_run('ssh-keygen -t ' . $alg . ' -q -N "" -C "" -m pem -f ' . $self->ssh_key);
}
}

Expand Down Expand Up @@ -510,6 +512,7 @@ sub terraform_apply {
if (get_var('PUBLIC_CLOUD_NVIDIA')) {
$cmd .= "-var gpu=true ";
}
$cmd .= "-var 'ssh_public_key=" . $self->ssh_key . ".pub' ";
$cmd .= "-out myplan";
record_info('TFM cmd', $cmd);

Expand Down
2 changes: 1 addition & 1 deletion tests/publiccloud/aws_cli.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sub run {
record_info("EC2 AMI", "EC2 AMI query: " . $image_id);

my $ssh_key = "openqa-cli-test-key-$job_id";
assert_script_run("aws ec2 import-key-pair --key-name '$ssh_key' --public-key-material fileb://~/.ssh/id_rsa.pub");
assert_script_run("aws ec2 import-key-pair --key-name '$ssh_key' --public-key-material fileb://" . $provider->ssh_key . ".pub");

my $machine_name = "openqa-cli-test-vm-$job_id";
my $security_group_name = "openqa-cli-test-sg-$job_id";
Expand Down

0 comments on commit fb3cc81

Please sign in to comment.