Skip to content

Commit

Permalink
Merge pull request #24 from fabi200123/add-ssh-keys
Browse files Browse the repository at this point in the history
Adding extra-spec SSH Keys
  • Loading branch information
gabriel-samfira authored Jun 20, 2024
2 parents bec8b51 + 3c7bb05 commit eb6c0e5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -155,7 +162,7 @@ To this end, this provider supports the following extra specs schema:
}
}
},
"additionalProperties": false
"additionalProperties": false
}
```

Expand All @@ -169,12 +176,15 @@ 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"]
}
```

**NOTE**: The `custom_labels` and `network_tags` must meet the [GCP requirements for labels](https://cloud.google.com/compute/docs/labeling-resources#requirements) and the [GCP requirements for network tags](https://cloud.google.com/vpc/docs/add-remove-network-tags#restrictions)!

**NOTE**: The `ssh_keys` add the option to [connect to an instance via SSH](https://cloud.google.com/compute/docs/instances/ssh) (either Linux or Windows). After you added the key as `username:ssh_public_key`, you can use the `private_key` to connect to the Linux/Windows instance via `ssh -i private_rsa username@instance_ip`. For **Windows** instances, the provider installs on the instance `google-compute-engine-ssh` and `enables ssh` if a `ssh_key` is added to extra-specs.

To set it on an existing pool, simply run:

```bash
Expand Down
15 changes: 15 additions & 0 deletions internal/client/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions internal/client/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"),
},
},
},
}
Expand Down
14 changes: 14 additions & 0 deletions internal/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down Expand Up @@ -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"`
}

Expand Down Expand Up @@ -230,6 +238,7 @@ type RunnerSpec struct {
CustomLabels map[string]string
NetworkTags []string
SourceSnapshot string
SSHKeys string
EnableBootDebug bool
}

Expand All @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions internal/spec/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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,
},
},
Expand Down

0 comments on commit eb6c0e5

Please sign in to comment.