Skip to content

Commit 65c6e34

Browse files
authored
Merge pull request #56 from ibm-hyper-protect/cleue-contract-sample-with-download
fix: add sample how to select the correct encryption certificate base…
2 parents 20cdafd + 80acb8a commit 65c6e34

File tree

5 files changed

+180
-0
lines changed

5 files changed

+180
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## Contract generation example
2+
3+
This sample creates an encrypted and signed contract and stores it locally in a file. In addition this example identifies
4+
the latest version of HPCR in the VPC cloud and then downloads the matching encryption certifcicate.
5+
6+
### Prerequisite
7+
8+
Prepare your environment according to [these steps](../README.md)
9+
10+
### Settings
11+
12+
Use one of the following options to set you settings:
13+
14+
#### Template file
15+
16+
1. `cp my-settings.auto.tfvars-template my-settings.auto.tfvars`
17+
2. Fill the values in `my-settings.auto.tfvars`
18+
19+
#### Environment variables
20+
21+
Set the following environment variables:
22+
23+
```text
24+
TF_VAR_logdna_ingestion_key=
25+
TF_VAR_logdna_ingestion_hostname=
26+
```
27+
28+
### Run the Example
29+
30+
Initialize terraform:
31+
32+
```bash
33+
terraform init
34+
```
35+
36+
Deploy the example:
37+
38+
```bash
39+
terraform apply
40+
```
41+
42+
The contract will be persisted in the `build/contract.yml` folder for further use.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
helloworld:
3+
image: docker.io/library/hello-world@sha256:53f1bbee2f52c39e41682ee1d388285290c5c8a76cc92b42687eecf38e0af3f0
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
logdna_ingestion_key="Your LogDNA ingestion key". # You can find this in "Linux/ubuntu" section of `Logging sources` tab of "IBM Log Analysis" instance in [cloud.ibm.com](https://cloud.ibm.com)
2+
logdna_ingestion_hostname="rsyslog endpoint of IBM Log Analysis instance"
3+
# Example: "syslog-a.<log_region>.logging.cloud.ibm.com". Where <log_region> is
4+
# the region on which IBM Log Analysis is deployed
5+
# Any other variable you want to set (see variables.tf)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
terraform {
2+
required_providers {
3+
hpcr = {
4+
source = "ibm-hyper-protect/hpcr"
5+
version = ">= 0.1.12"
6+
}
7+
ibm = {
8+
source = "IBM-Cloud/ibm"
9+
version = ">= 1.37.1"
10+
}
11+
}
12+
}
13+
14+
# make sure to target the correct region and zone
15+
provider "ibm" {
16+
region = var.region
17+
zone = "${var.region}-${var.zone}"
18+
ibmcloud_api_key = var.ibmcloud_api_key
19+
}
20+
21+
# archive of the folder containing docker-compose file. This folder could create additional resources such as files
22+
# to be mounted into containers, environment files etc. This is why all of these files get bundled in a tgz file (base64 encoded)
23+
resource "hpcr_tgz" "contract" {
24+
folder = "compose"
25+
}
26+
27+
# locate all public image
28+
data "ibm_is_images" "hyper_protect_images" {
29+
visibility = "public"
30+
status = "available"
31+
}
32+
33+
# locate the latest hyper protect image from the list of available images
34+
data "hpcr_image" "hyper_protect_image" {
35+
images = jsonencode(data.ibm_is_images.hyper_protect_images.images)
36+
}
37+
38+
# load the certificate for the selected image versions
39+
# in this case we only download the certificate for the selected version of the image
40+
data "hpcr_encryption_certs" "enc_certs" {
41+
versions = [data.hpcr_image.hyper_protect_image.version]
42+
}
43+
44+
locals {
45+
# contract in clear text
46+
contract = yamlencode({
47+
"env" : {
48+
"type" : "env",
49+
"logging" : {
50+
"logDNA" : {
51+
"ingestionKey" : var.logdna_ingestion_key,
52+
"hostname" : var.logdna_ingestion_hostname,
53+
}
54+
}
55+
},
56+
"workload" : {
57+
"type" : "workload",
58+
"compose" : {
59+
"archive" : hpcr_tgz.contract.rendered
60+
}
61+
}
62+
})
63+
}
64+
65+
# In this step we encrypt the fields of the contract and sign the env and workload field. The certificate to execute the
66+
# encryption it built into the provider and matches the latest HPCR image. If required it can be overridden.
67+
# We use a temporary, random keypair to execute the signature. This could also be overriden.
68+
resource "hpcr_contract_encrypted" "contract" {
69+
contract = local.contract
70+
cert = data.hpcr_encryption_certs.enc_certs.certs[data.hpcr_image.hyper_protect_image.version]
71+
}
72+
73+
resource "local_file" "contract" {
74+
content = hpcr_contract_encrypted.contract.rendered
75+
filename = "${path.module}/build/contract.yml"
76+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
variable "logdna_ingestion_key" {
2+
type = string
3+
sensitive = true
4+
description = <<-DESC
5+
Ingestion key for IBM Log Analysis instance. This can be
6+
obtained from "Linux/Ubuntu" section of "Logging resource"
7+
tab of IBM Log Analysis instance
8+
DESC
9+
}
10+
11+
variable "logdna_ingestion_hostname" {
12+
type = string
13+
description = <<-DESC
14+
rsyslog endpoint of IBM Log Analysis instance.
15+
Don't include the port. Example:
16+
syslog-a.<log_region>.logging.cloud.ibm.com
17+
log_region is the region where IBM Log Analysis is deployed
18+
DESC
19+
}
20+
21+
variable "ibmcloud_api_key" {
22+
description = <<-DESC
23+
Enter your IBM Cloud API Key, you can get your IBM Cloud API key using:
24+
https://cloud.ibm.com/iam#/apikeys
25+
DESC
26+
sensitive = true
27+
}
28+
29+
variable "region" {
30+
type = string
31+
description = "Region to deploy to, e.g. eu-gb"
32+
33+
validation {
34+
condition = (var.region == "eu-gb" ||
35+
var.region == "br-sao" ||
36+
var.region == "ca-tor" ||
37+
var.region == "jp-tok" ||
38+
var.region == "us-east")
39+
error_message = "Value of region must be one of eu-gb/br-sao/ca-tor/jp-tok/us-east."
40+
}
41+
}
42+
43+
variable "zone" {
44+
type = string
45+
default = "2"
46+
description = "Zone to deploy to, e.g. 2."
47+
48+
validation {
49+
condition = (var.zone == "1" ||
50+
var.zone == "2" ||
51+
var.zone == "3")
52+
error_message = "Value of zone must be one of 1/2/3."
53+
}
54+
}

0 commit comments

Comments
 (0)