Skip to content

Commit bad0668

Browse files
authored
fix(public_ssh_keys): support single and multiple keys, as string or file (#73)
1 parent fc66206 commit bad0668

File tree

15 files changed

+146
-97
lines changed

15 files changed

+146
-97
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ If you are running into one of these scenarios, we recommend opening an issue in
2828

2929
### Terraform Version and Provider Version
3030

31-
<!--- Please run `terraform -v` to show the Terraform core version and provider version(s).
31+
<!--- Please run `terraform -v` to show the Terraform core version and provider version(s).
3232
If you are using a local copy of the Terraform Oracle Cloud Infrastructure Provider, run the plugin directly to get the version: `<path-to-plugin>/terraform-provider-oci`
3333
If you are not running the latest version of Terraform or the provider, please upgrade because your issue may have already been fixed. [Terraform documentation on provider versioning](https://www.terraform.io/docs/configuration/providers.html#provider-versions). --->
3434

@@ -43,8 +43,8 @@ If you are not running the latest version of Terraform or the provider, please u
4343

4444
```hcl
4545
# Copy-paste your Terraform configurations here - for large Terraform configs,
46-
# please use a service like Dropbox and share a link to the ZIP file.
47-
# Please remove any sensitive information from configuration files before sharing them.
46+
# please use a service like Dropbox and share a link to the ZIP file.
47+
# Please remove any sensitive information from configuration files before sharing them.
4848
```
4949

5050
### Debug Output
@@ -59,8 +59,8 @@ Github Gist: https://gist.github.com/
5959

6060
### Panic Output
6161

62-
<!---
63-
If Terraform produced a panic, please provide a link to a GitHub Gist containing the output of the `crash.log`.
62+
<!---
63+
If Terraform produced a panic, please provide a link to a GitHub Gist containing the output of the `crash.log`.
6464
6565
Github Gist: https://gist.github.com/
6666
--->

.github/ISSUE_TEMPLATE/feature_request.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ labels: enhancement
2828
<!--- Information about code formatting: https://help.github.com/articles/basic-writing-and-formatting-syntax/#quoting-code --->
2929

3030
```hcl
31-
# Copy-paste any Terraform configurations for how the requested feature may be used.
31+
# Copy-paste any Terraform configurations for how the requested feature may be used.
3232
```
3333

3434
### References

.pre-commit-config.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

CHANGELOG.adoc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ Given a version number MAJOR.MINOR.PATCH:
1818

1919
=== Deprecated
2020

21-
* `var.ssh_authorized_keys` is deprecated. Use `var.ssh_public_key` add a single public key or `var.ssh_public_key_path` for a file containing mutiple public keys.
21+
* `var.ssh_authorized_keys` is deprecated. Use `var.ssh_public_keys`.
2222

2323
=== New features
2424

2525
* Add support for freeform and defined tags for instances, vnics and block volumes (Fix #10, #11, #12, #13, #18, #20)
2626
* Add "module watermark" freeform tags: module defined and user defined freeform tags are merged on the final resource
27-
* Add support to provide the `ssh_authorized_keys` argument as a string or as a file (Fix #67)
27+
* Add support to provide the `ssh_authorized_keys` argument as a string or as a file (Fix #67 #70)
28+
29+
=== Documentation
30+
31+
* Add add instructions on how to provide one or more public ssh keys using string, variable or file.
2832

2933
== 2.1.0 - 2021-03-02
3034

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,6 @@ Compute Instances are also a perfect place to illustrate [module composition pri
7171

7272
- For guidance configuring iSCSI on a Linux platform, see [iSCSI Commands and Information](https://docs.cloud.oracle.com/iaas/Content/Block/Concepts/iscsiinformation.htm). See also this example of inline iSCSI commands execution using `iscsiadm` CLI called from terraform file: [instance.tf](https://github.com/terraform-providers/terraform-provider-oci/blob/master/examples/compute/instance/instance.tf).
7373

74-
## Windows remote scripts execution
75-
76-
Terraform supports using Windows Remote Management (WinRM) for connecting to Windows instances. Ensure that your Windows image has WinRM properly configured to allow remote access. Following is a sample WinRM configuration:
77-
78-
```HCL
79-
winrm quickconfig -q
80-
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}’'
81-
winrm set winrm/config ‘@{MaxTimeoutms=”1800000″}’
82-
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
83-
winrm set winrm/config/service/auth '@{Basic="true"}'
84-
85-
netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
86-
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow
87-
88-
net stop winrm
89-
sc.exe config winrm start=auto
90-
net start winrm
91-
```
92-
9374
## Contributing
9475

9576
This project is open source. Oracle appreciates any contributions that are made by the open source community: raising issues, improving documentation, fixing bugs, or adding new features.

docs/instance_ssh_keys.adoc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
= Instance SSH Keys
2+
ifdef::env-github[]
3+
:tip-caption: :bulb:
4+
:note-caption: :information_source:
5+
:important-caption: :heavy_exclamation_mark:
6+
:caution-caption: :fire:
7+
:warning-caption: :warning:
8+
endif::[]
9+
:idprefix:
10+
:idseparator: -
11+
:sectlinks:
12+
:toc:
13+
14+
:uri-repo: https://github.com/oracle-terraform-modules/terraform-oci-vcn
15+
:uri-rel-file-base: link:{uri-repo}/blob/main
16+
:uri-rel-tree-base: link:{uri-repo}/tree/main
17+
:uri-docs: {uri-rel-file-base}/docs
18+
:uri-oci-security-credentials: https://docs.oracle.com/en-us/iaas/Content/General/Concepts/credentials.htm#Instance
19+
:uri-oci-core-instance: https://registry.terraform.io/providers/hashicorp/oci/latest/docs/resources/core_instance#metadata
20+
:uri-tf-heredoc: https://www.terraform.io/docs/language/expressions/strings.html#heredoc-strings
21+
:uri-tf-file-function: https://www.terraform.io/docs/language/functions/file.html
22+
23+
This document explains how to provide one or more Instance public SSH keys for Linux instances using this module. See {uri-oci-security-credentials}[Instance SSH key] on OCI documentation for more details.
24+
25+
Linux public keys are controlled by the `ssh_authorized_keys` argument in the `metadata` block of the {uri-oci-core-instance}[oci_core_instance] resource at the provider level.
26+
27+
The corresponding feature on this module uses the module Input Variable `var.ssh_public_keys`.
28+
29+
== Providing one public key
30+
31+
Providing one public ssh key is as simple as directly pasting it as a string in your module block.
32+
33+
[source,hcl]
34+
----
35+
module "instance" {
36+
source = "oracle-terraform-modules/compute-instance/oci"
37+
...
38+
ssh_public_keys = "<your public ssh key>"
39+
}
40+
----
41+
42+
Alternatively, you can also declare a variable in your configuration and feed the module with it.
43+
44+
[source,hcl]
45+
----
46+
module "instance" {
47+
source = "oracle-terraform-modules/compute-instance/oci"
48+
...
49+
ssh_public_keys = var.my_public_ssh_key
50+
}
51+
52+
variable "my_public_ssh_key" {
53+
type = string
54+
default = "<ssh public key>"
55+
}
56+
----
57+
58+
== Providing multiple public keys
59+
60+
To inject multiple public SSH keys when launching an instance, you must provide one public key per line. Terraform allows multi-line strings using {uri-tf-heredoc}[Heredoc strings] and this is maybe the most straight-forward solution.
61+
62+
[source,hcl]
63+
----
64+
module "instance" {
65+
source = "oracle-terraform-modules/compute-instance/oci"
66+
...
67+
ssh_public_keys = var.my_public_ssh_key
68+
}
69+
70+
variable "my_public_ssh_key" {
71+
type = string
72+
default = <<EOT
73+
<ssh public key 1>
74+
<ssh public key 2>
75+
...
76+
<ssh public key n>
77+
EOT
78+
}
79+
----
80+
81+
Alternatively, you can also source your public ssh keys from a file, using the {uri-tf-file-function}[Terraform File function].
82+
83+
[source,hcl]
84+
----
85+
module "instance" {
86+
source = "oracle-terraform-modules/compute-instance/oci"
87+
...
88+
ssh_public_keys = file("/path/to/your/file")
89+
}
90+
----
91+
92+
== Examples
93+
94+
For example configuration using this feature, see link:../examples/instances_flex_shape/README.md[examples/instances_flex_shape]

docs/terraformoptions.adoc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[cols="a,a",options="header,autowidth"]
1313
|===
1414
|Name |Version
15-
|[[provider_oci]] <<provider_oci,oci>> |>= 3.27
15+
|[[provider_oci]] <<provider_oci,oci>> |4.17.0
1616
|===
1717

1818
== Modules
@@ -177,19 +177,13 @@ No modules.
177177
|no
178178

179179
|[[input_ssh_authorized_keys]] <<input_ssh_authorized_keys,ssh_authorized_keys>>
180-
|DEPRECATED: use ssh_public_key or ssh_public_key_path instead. Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance.
180+
|DEPRECATED: use ssh_public_keys instead. Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance.
181181
|`string`
182182
|`null`
183183
|no
184184

185-
|[[input_ssh_public_key]] <<input_ssh_public_key,ssh_public_key>>
186-
|The content of the ssh public key used to access the compute instance. Set this, or ssh_public_key_path to add multiple keys at once.
187-
|`string`
188-
|`null`
189-
|no
190-
191-
|[[input_ssh_public_key_path]] <<input_ssh_public_key_path,ssh_public_key_path>>
192-
|Path to file containing one or more ssh public keys used to access the compute instance. One key per line. Set this or the ssh_public_key.
185+
|[[input_ssh_public_keys]] <<input_ssh_public_keys,ssh_public_keys>>
186+
|Public SSH keys to be included in the ~/.ssh/authorized_keys file for the default user on the instance. To provide multiple keys, see docs/instance_ssh_keys.adoc.
193187
|`string`
194188
|`null`
195189
|no

examples/instances_fixed_shape/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module "instance_nonflex" {
3232
source_ocid = var.source_ocid
3333
source_type = var.source_type
3434
# operating system parameters
35-
ssh_authorized_keys = var.ssh_authorized_keys
35+
ssh_public_keys = var.ssh_public_keys
3636
# networking parameters
3737
assign_public_ip = var.assign_public_ip
3838
subnet_ocids = var.subnet_ocids
@@ -63,7 +63,7 @@ module "instance_nonflex_custom" {
6363
instance_flex_memory_in_gbs = 8 # only used if shape is Flex type
6464
instance_flex_ocpus = 1 # only used if shape is Flex type
6565
# operating system parameters
66-
ssh_authorized_keys = var.ssh_authorized_keys
66+
ssh_public_keys = var.ssh_public_keys
6767
# networking parameters
6868
assign_public_ip = var.assign_public_ip
6969
subnet_ocids = var.subnet_ocids

examples/instances_fixed_shape/terraform.tfvars.example

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2020 Oracle Corporation and/or affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2021 Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

44
/*----------------------------------------------------------------------------
@@ -27,7 +27,11 @@ source_ocid = "<The OCID of an image or a boot volume>"
2727

2828
# operating system parameters
2929

30-
ssh_authorized_keys = "<path to the instance's public key>"
30+
ssh_public_keys = <<EOT
31+
<ssh_public_key_1>
32+
<ssh_public_key_2>
33+
<ssh_public_key_3>
34+
EOT
3135

3236
# networking parameters
3337

examples/instances_fixed_shape/variables.tf

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ variable "source_type" {
104104

105105
# operating system parameters
106106

107-
variable "ssh_authorized_keys" {
108-
description = "Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance."
107+
variable "ssh_public_keys" {
108+
description = "Public SSH keys to be included in the ~/.ssh/authorized_keys file for the default user on the instance. To provide multiple keys, see docs/instance_ssh_keys.adoc."
109109
type = string
110+
default = null
110111
}
111112

112113
# networking parameters
@@ -129,12 +130,3 @@ variable "block_storage_sizes_in_gbs" {
129130
type = list(string)
130131
default = [50]
131132
}
132-
133-
134-
135-
136-
137-
138-
139-
140-

examples/instances_flex_shape/main.tf

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ module "instance_flex" {
2626
freeform_tags = var.freeform_tags
2727
defined_tags = var.defined_tags
2828
# compute instance parameters
29-
ad_number = var.instance_ad_number
30-
instance_count = var.instance_count
31-
instance_display_name = var.instance_display_name
32-
shape = var.shape
33-
source_ocid = var.source_ocid
34-
source_type = var.source_type
29+
ad_number = var.instance_ad_number
30+
instance_count = var.instance_count
31+
instance_display_name = var.instance_display_name
32+
shape = var.shape
33+
source_ocid = var.source_ocid
34+
source_type = var.source_type
35+
instance_flex_memory_in_gbs = 1 # only used if shape is Flex type
36+
instance_flex_ocpus = 1 # only used if shape is Flex type
3537
# operating system parameters
36-
ssh_public_key = var.ssh_public_key
38+
ssh_public_keys = var.ssh_public_keys
3739
# networking parameters
3840
assign_public_ip = var.assign_public_ip
3941
subnet_ocids = var.subnet_ocids

examples/instances_flex_shape/terraform.tfvars.example

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019, 2020 Oracle Corporation and/or affiliates. All rights reserved.
1+
# Copyright (c) 2019, 2021 Oracle Corporation and/or affiliates. All rights reserved.
22
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
33

44
/*----------------------------------------------------------------------------
@@ -27,7 +27,11 @@ source_ocid = "<The OCID of an image or a boot volume>"
2727

2828
# operating system parameters
2929

30-
ssh_authorized_keys = "<path to the instance's public key>"
30+
ssh_public_keys = <<EOT
31+
<ssh_public_key_1>
32+
<ssh_public_key_2>
33+
<ssh_public_key_3>
34+
EOT
3135

3236
# networking parameters
3337

examples/instances_flex_shape/variables.tf

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,14 @@ variable "source_type" {
105105
# operating system parameters
106106

107107
variable "ssh_authorized_keys" {
108-
description = "Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance."
108+
#! Deprecation notice: Please use `ssh_public_keys` instead
109+
description = "DEPRECATED: use ssh_public_keys instead. Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance."
109110
type = string
110111
default = null
111112
}
112113

113-
variable "ssh_public_key" {
114-
description = "The content of the ssh public key used to access the compute instance. Set this, or ssh_public_key_path to add multiple keys at once."
115-
type = string
116-
default = null
117-
}
118-
variable "ssh_public_key_path" {
119-
description = "Path to file containing one or more ssh public keys used to access the compute instance. One key per line. Set this or the ssh_public_key."
114+
variable "ssh_public_keys" {
115+
description = "Public SSH keys to be included in the ~/.ssh/authorized_keys file for the default user on the instance. To provide multiple keys, see docs/instance_ssh_keys.adoc."
120116
type = string
121117
default = null
122118
}

main.tf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ resource "oci_core_instance" "this" {
9090
}
9191

9292
metadata = {
93-
ssh_authorized_keys = (var.ssh_public_key != "" ? var.ssh_public_key :
94-
(var.ssh_public_key_path != "" ? file(var.ssh_public_key_path) :
95-
(var.ssh_authorized_keys != "" ? file(var.ssh_authorized_keys) : "")))
96-
user_data = var.user_data
93+
ssh_authorized_keys = var.ssh_public_keys != null ? var.ssh_public_keys : file(var.ssh_authorized_keys)
94+
user_data = var.user_data
9795
}
9896

9997
source_details {

variables.tf

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,14 @@ variable "resource_platform" {
105105
}
106106

107107
variable "ssh_authorized_keys" {
108-
#! Deprecation notice: Please use `ssh_public_key` or `ssh_public_key_path` instead
109-
description = "DEPRECATED: use ssh_public_key or ssh_public_key_path instead. Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance."
108+
#! Deprecation notice: Please use `ssh_public_keys` instead
109+
description = "DEPRECATED: use ssh_public_keys instead. Public SSH keys path to be included in the ~/.ssh/authorized_keys file for the default user on the instance."
110110
type = string
111111
default = null
112112
}
113113

114-
variable "ssh_public_key" {
115-
description = "The content of the ssh public key used to access the compute instance. Set this, or ssh_public_key_path to add multiple keys at once."
116-
type = string
117-
default = null
118-
}
119-
variable "ssh_public_key_path" {
120-
description = "Path to file containing one or more ssh public keys used to access the compute instance. One key per line. Set this or the ssh_public_key."
114+
variable "ssh_public_keys" {
115+
description = "Public SSH keys to be included in the ~/.ssh/authorized_keys file for the default user on the instance. To provide multiple keys, see docs/instance_ssh_keys.adoc."
121116
type = string
122117
default = null
123118
}

0 commit comments

Comments
 (0)