From 8a7406f195e86cfc601483c22aa7472842bb2f68 Mon Sep 17 00:00:00 2001 From: Fabian Fulga Date: Tue, 18 Jun 2024 12:39:46 +0300 Subject: [PATCH] Adding extra-spec SSH Keys --- README.md | 12 ++++++++++-- internal/client/gcp.go | 15 +++++++++++++++ internal/client/gcp_test.go | 9 +++++++++ internal/spec/spec.go | 14 ++++++++++++++ internal/spec/spec_test.go | 2 ++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 756dc6f..27774c6 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,13 @@ To this end, this provider supports the following extra specs schema: "type": "string", "description": "The source snapshot to create this disk." }, + "ssh_keys": { + "type": "array", + "description": "A list of SSH keys to be added to the instance. The format is USERNAME:SSH_KEY", + "items": { + "type": "string" + } + }, "enable_boot_debug": { "type": "boolean", "description": "Enable boot debug on the VM." @@ -155,7 +162,7 @@ To this end, this provider supports the following extra specs schema: } } }, - "additionalProperties": false + "additionalProperties": false } ``` @@ -169,7 +176,8 @@ An example of extra specs json would look like this: "nic_type": "VIRTIO_NET", "custom_labels": {"environment":"production","project":"myproject"}, "network_tags": ["web-server", "production"], - "source_snapshot": "projects/garm-testing/global/snapshots/garm-snapshot" + "source_snapshot": "projects/garm-testing/global/snapshots/garm-snapshot", + "ssh_keys": ["username1:ssh_key1", "username2:ssh_key2"] } ``` diff --git a/internal/client/gcp.go b/internal/client/gcp.go index 6bb9ca2..d35339d 100644 --- a/internal/client/gcp.go +++ b/internal/client/gcp.go @@ -159,6 +159,10 @@ func (g *GcpCli) CreateInstance(ctx context.Context, spec *spec.RunnerSpec) (*co Key: proto.String("runner_name"), Value: proto.String(spec.BootstrapParams.Name), }, + { + Key: proto.String("ssh-keys"), + Value: proto.String(spec.SSHKeys), + }, }, }, Labels: spec.CustomLabels, @@ -171,6 +175,17 @@ func (g *GcpCli) CreateInstance(ctx context.Context, spec *spec.RunnerSpec) (*co inst.NetworkInterfaces[0].AccessConfigs = nil } + if spec.BootstrapParams.OSType == params.Windows && len(spec.SSHKeys) > 0 { + inst.Metadata.Items = append(inst.Metadata.Items, &computepb.Items{ + Key: proto.String("enable-windows-ssh"), + Value: proto.String("TRUE"), + }) + inst.Metadata.Items = append(inst.Metadata.Items, &computepb.Items{ + Key: proto.String("sysprep-specialize-script-cmd"), + Value: proto.String("googet -noconfirm=true install google-compute-engine-ssh"), + }) + } + insertReq := &computepb.InsertInstanceRequest{ Project: g.cfg.ProjectId, Zone: g.cfg.Zone, diff --git a/internal/client/gcp_test.go b/internal/client/gcp_test.go index b6492df..79ee85b 100644 --- a/internal/client/gcp_test.go +++ b/internal/client/gcp_test.go @@ -147,6 +147,7 @@ func TestCreateInstanceWindows(t *testing.T) { CustomLabels: map[string]string{"key1": "value1"}, NetworkTags: []string{"tag1", "tag2"}, SourceSnapshot: "projects/garm-testing/global/snapshots/garm-snapshot", + SSHKeys: "MockSSHKey", BootstrapParams: params.BootstrapInstance{ Name: "garm-instance", Flavor: "n1-standard-1", @@ -168,6 +169,14 @@ func TestCreateInstanceWindows(t *testing.T) { Key: proto.String(windowsStartupScript), Value: proto.String("MockUserData"), }, + { + Key: proto.String("ssh-keys"), + Value: proto.String("MockSSHKey"), + }, + { + Key: proto.String("enable-windows-ssh"), + Value: proto.String("TRUE"), + }, }, }, } diff --git a/internal/spec/spec.go b/internal/spec/spec.go index 00262b0..5bc1d4d 100644 --- a/internal/spec/spec.go +++ b/internal/spec/spec.go @@ -77,6 +77,13 @@ const ( "type": "string", "description": "The source snapshot to create this disk." }, + "ssh_keys": { + "type": "array", + "description": "A list of SSH keys to be added to the instance.", + "items": { + "type": "string" + } + }, "enable_boot_debug": { "type": "boolean", "description": "Enable boot debug on the VM." @@ -181,6 +188,7 @@ type extraSpecs struct { CustomLabels map[string]string `json:"custom_labels,omitempty"` NetworkTags []string `json:"network_tags,omitempty"` SourceSnapshot string `json:"source_snapshot,omitempty"` + SSHKeys []string `json:"ssh_keys,omitempty"` EnableBootDebug *bool `json:"enable_boot_debug"` } @@ -230,6 +238,7 @@ type RunnerSpec struct { CustomLabels map[string]string NetworkTags []string SourceSnapshot string + SSHKeys string EnableBootDebug bool } @@ -255,6 +264,11 @@ func (r *RunnerSpec) MergeExtraSpecs(extraSpecs *extraSpecs) { if extraSpecs.SourceSnapshot != "" { r.SourceSnapshot = extraSpecs.SourceSnapshot } + if len(extraSpecs.SSHKeys) > 0 { + for key := range extraSpecs.SSHKeys { + r.SSHKeys = r.SSHKeys + "\n" + extraSpecs.SSHKeys[key] + } + } if extraSpecs.EnableBootDebug != nil { r.EnableBootDebug = *extraSpecs.EnableBootDebug } diff --git a/internal/spec/spec_test.go b/internal/spec/spec_test.go index f1da448..bef7023 100644 --- a/internal/spec/spec_test.go +++ b/internal/spec/spec_test.go @@ -42,6 +42,7 @@ func TestJsonSchemaValidation(t *testing.T) { }, "network_tags": ["example_tag"], "source_snapshot": "snapshot-id", + "ssh_keys": ["ssh-key", "ssh-key2"], "enable_boot_debug": true, "runner_install_template": "install-template", "extra_context": { @@ -99,6 +100,7 @@ func TestMergeExtraSpecs(t *testing.T) { CustomLabels: map[string]string{"key1": "value1"}, NetworkTags: []string{"tag1", "tag2"}, SourceSnapshot: "projects/garm-testing/global/snapshots/garm-snapshot", + SSHKeys: []string{"ssh-key1", "ssh-key2"}, EnableBootDebug: &enable_boot_debug, }, },