We can deploy cloud Infrastructure on IDrive® Compute with the help of widely used open-source infrastructure as code software Terraform. While we can deploy various virtual resources on IDrive® Compute with Terraform, In this article we will try to concentrate on creating IDrive® Compute SSH-KEYS.
Pre-requisite:
We need to install the following software on your local system.
Source the endpoint for key manager service of IDrive® Compute.
$ export OS_ENDPOINT=https://key-manager.api.idrivecompute.io
Make sure you do not export variable "OS_REGION_NAME" as key-manager service is a global service and not specific to a region.
Terraform code:
Here are the terraform files required for the creation.
1) Providers file:
Create a file named main.tf and write the file with below code.
$ touch main.tf
Append the following configuration to the main.tf file.
# Configure the OpenStack Provider
terraform {
required_providers {
openstack = {
source = "terraform-provider-openstack/openstack"
}
}
}
2) Deployment file: deploy.tf
$ touch deploy.tf
Add the below code to deploy.tf file.
resource "openstack_keymanager_secret_v1" "secret_1" {
algorithm = "aes"
bit_length = 256
mode = "cbc"
name = "mysecret"
payload = "ssh-rsa .... test_server@ubuntu"
payload_content_type = "text/plain"
secret_type = "opaque"
}
Here we are creating a secret called "mysecret" which has a payload that contains the public keys. Make sure the secret type is "opaque" and the algorithm type is "aes".
Using Terraform to create a ssh-keys
we execute the below operations:
- terraform init ( Needs to be executed only once for initializing)
- terraform plan ( To see what's being deployed)
- terraform deploy (creates the requested resources)
Initializing terraform:
$ terraform init
Output:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of terraform-provider-openstack/openstack...
- Installing terraform-provider-openstack/openstack v1.49.0...
- Installed terraform-provider-openstack/openstack v1.49.0 (self-signed, key ID 4F80527A391BEFD2)
Partner and community providers are signed by their developers.
If you'd like to know more about provider signing, you can read about it here:
https://www.terraform.io/docs/cli/plugins/signing.html
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Applying terraform plan:
$ terraform plan
Output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# openstack_keymanager_secret_v1.secret_1 will be created
+ resource "openstack_keymanager_secret_v1" "secret_1" {
+ algorithm = "aes"
+ all_metadata = (known after apply)
+ bit_length = 256
+ content_types = (known after apply)
+ created_at = (known after apply)
+ creator_id = (known after apply)
+ id = (known after apply)
+ mode = "cbc"
+ name = "mysecret"
+ payload = (sensitive value)
+ payload_content_type = "text/plain"
+ region = (known after apply)
+ secret_ref = (known after apply)
+ secret_type = "opaque"
+ status = (known after apply)
+ updated_at = (known after apply)
+ acl {
+ read {
+ created_at = (known after apply)
+ project_access = (known after apply)
+ updated_at = (known after apply)
+ users = (known after apply)
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
Applying the configuration:
$ terraform apply
Output:
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# openstack_keymanager_secret_v1.secret_1 will be created
+ resource "openstack_keymanager_secret_v1" "secret_1" {
+ algorithm = "aes"
+ all_metadata = (known after apply)
+ bit_length = 256
+ content_types = (known after apply)
+ created_at = (known after apply)
+ creator_id = (known after apply)
+ id = (known after apply)
+ mode = "cbc"
+ name = "mysecret"
+ payload = (sensitive value)
+ payload_content_type = "text/plain"
+ region = (known after apply)
+ secret_ref = (known after apply)
+ secret_type = "opaque"
+ status = (known after apply)
+ updated_at = (known after apply)
+ acl {
+ read {
+ created_at = (known after apply)
+ project_access = (known after apply)
+ updated_at = (known after apply)
+ users = (known after apply)
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
openstack_keymanager_secret_v1.secret_1: Creating...
openstack_keymanager_secret_v1.secret_1: Creation complete after 2s [id=005394c5-d264-4109-8b9b-2e17d308aa7f]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Verification
To verify the ssh-keys created, you need to install barbicanclient, the key manager for OpenStack.
$ pip install python-barbicanclient
You can verify the creation of the ssh-keys by the bellow command.
$ openstack secret list -f json
Output:
"Secret href": "https://key-manager.api.idrivecompute.io/v1/secrets/005394c5-d264-4109-8b9b-2e17d308aa7f",
"Name": "mysecret",
"Created": "2023-01-16T08:55:02+00:00",
"Status": "ACTIVE",
"Content types": {
"default": "text/plain"
},
"Algorithm": "aes",
"Bit length": 256,
"Secret type": "opaque",
"Mode": "cbc",
"Expiration": null
}
]
Removing SSH-KEYS
To remove the created SSH-KEYS run the below command.
$ terraform destroy
Output:
openstack_keymanager_secret_v1.secret_1: Refreshing state... [id=005394c5-d264-4109-8b9b-2e17d308aa7f]
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# openstack_keymanager_secret_v1.secret_1 will be destroyed
- resource "openstack_keymanager_secret_v1" "secret_1" {
- algorithm = "aes" -> null
- all_metadata = {} -> null
- bit_length = 256 -> null
- content_types = {
- "default" = "text/plain"
} -> null
- created_at = "2023-01-16T08:55:02Z" -> null
- creator_id = "191931bc755f410784fe42594c8e588d" -> null
- id = "005394c5-d264-4109-8b9b-2e17d308aa7f" -> null
- mode = "cbc" -> null
- name = "mysecret" -> null
- payload = (sensitive value)
- payload_content_type = "text/plain" -> null
- secret_ref = "https://key-manager.api.idrivecompute.io/v1/secrets/005394c5-d264-4109-8b9b-2e17d308aa7f" -> null
- secret_type = "opaque" -> null
- status = "ACTIVE" -> null
- updated_at = "2023-01-16T08:55:02Z" -> null
- acl {
- read {
- created_at = "0001-01-01T00:00:00Z" -> null
- project_access = true -> null
- updated_at = "0001-01-01T00:00:00Z" -> null
- users = [] -> null
}
}
}
Plan: 0 to add, 0 to change, 1 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
openstack_keymanager_secret_v1.secret_1: Destroying... [id=005394c5-d264-4109-8b9b-2e17d308aa7f]
openstack_keymanager_secret_v1.secret_1: Destruction complete after 1s
Destroy complete! Resources: 1 destroyed.