Skip to content

Commit 20cdafd

Browse files
authored
Merge pull request #55 from ibm-hyper-protect/create-contract-dynamic-registry
Add create-contract-dynamic-registry example
2 parents 1be22d6 + a84cc83 commit 20cdafd

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
## Contract generation example with support for dynamic container registry definition
2+
3+
This sample creates an encrypted and signed contract and stores it locally in a file. You can later use the contract to provision a HPVS for VPC instance.
4+
The contract will define the container registry and the credentials for pulling your workload container image.
5+
6+
### Prerequisite
7+
8+
Prepare your environment according to [these steps](../README.md)
9+
10+
### Define your settings
11+
12+
Define your settings:
13+
- logdna_ingestion_hostname: The ingestion host name of your Log instance which you provisioned previously
14+
- logdna_ingestion_key: The ingestion key of your Log instance
15+
- registry: The container registry to pull your workload container image from
16+
- pull_username: The container registry username for pulling your workload container image
17+
- pull_password: The container registry password for pulling your workload container image
18+
19+
The settings are defined in form of Terraform variables.
20+
21+
Use one of the following options to define the variables:
22+
23+
#### Define the variables in a template file
24+
25+
1. `cp my-settings.auto.tfvars-template my-settings.auto.tfvars`
26+
2. Fill the values in `my-settings.auto.tfvars`
27+
28+
#### Define environment variables
29+
30+
Set the following environment variables:
31+
32+
```text
33+
TF_VAR_logdna_ingestion_key=
34+
TF_VAR_logdna_ingestion_hostname=
35+
TF_VAR_registry=
36+
TF_VAR_pull_username=
37+
TF_VAR_pull_password=
38+
```
39+
40+
### Define your workload
41+
42+
Create the file `compose\docker-compose.yml` for your workload. Specify at least the container image digest and use the `${REGISTRY}` variable to reference the container registry defined in your settings, e.g.:
43+
44+
```
45+
services:
46+
helloworld:
47+
image: ${REGISTRY}/hpse-docker-hello-world-s390x@sha256:43c500c5f85fc450060b804851992314778e35cadff03cb63042f593687b7347
48+
```
49+
50+
### Run the Example
51+
52+
Initialize terraform:
53+
54+
```bash
55+
terraform init
56+
```
57+
58+
Deploy the example:
59+
60+
```bash
61+
terraform apply
62+
```
63+
64+
### Further steps
65+
66+
The contract will be written to the file `build/contract.yml` and can now be used for e.g. provisining a HPVS for VPC instance.
67+
68+
Note that you will need to create a public gateway in your VPC before creating the HPVS for VPC instance. This is necessary to allow the HPVS for VPC instance to reach your Log instance through the public gateway.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
helloworld:
3+
image: ${REGISTRY}/hpse-docker-hello-world-s390x@sha256:43c500c5f85fc450060b804851992314778e35cadff03cb63042f593687b7347
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
logdna_ingestion_key="Your LogDNA ingestion key".
2+
logdna_ingestion_hostname="rsyslog endpoint of IBM Log Analysis instance" # Example: "syslog-a.<log_region>.logging.cloud.ibm.com".
3+
registry="Prefix for the dynamic registry" # e.g. docker.io/library or us.icr.io
4+
pull_username="Username for registry" # Username with read access to the container registry
5+
pull_password="Password for registry" # Password with read access to the container registry
6+
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
terraform {
2+
required_providers {
3+
hpcr = {
4+
source = "ibm-hyper-protect/hpcr"
5+
version = ">= 0.1.1"
6+
}
7+
}
8+
}
9+
10+
# archive of the folder containing docker-compose file. This folder could create additional resources such as files
11+
# to be mounted into containers, environment files etc. This is why all of these files get bundled in a tgz file (base64 encoded)
12+
resource "hpcr_tgz" "contract" {
13+
folder = "compose"
14+
}
15+
16+
locals {
17+
# contract in clear text
18+
contract = yamlencode({
19+
"env" : {
20+
"type" : "env",
21+
"logging" : {
22+
"logDNA" : {
23+
"ingestionKey" : var.logdna_ingestion_key,
24+
"hostname" : var.logdna_ingestion_hostname,
25+
}
26+
},
27+
"auths" : {
28+
(var.registry) : {
29+
"username" : var.pull_username,
30+
"password" : var.pull_password
31+
}
32+
},
33+
"env" : {
34+
"REGISTRY" : var.registry
35+
}
36+
},
37+
"workload" : {
38+
"type" : "workload",
39+
"compose" : {
40+
"archive" : hpcr_tgz.contract.rendered
41+
}
42+
}
43+
})
44+
}
45+
46+
# In this step we encrypt the fields of the contract and sign the env and workload field. The certificate to execute the
47+
# encryption it built into the provider and matches the latest HPCR image. If required it can be overridden.
48+
# We use a temporary, random keypair to execute the signature. This could also be overriden.
49+
resource "hpcr_contract_encrypted" "contract" {
50+
contract = local.contract
51+
}
52+
53+
resource "local_file" "contract" {
54+
content = hpcr_contract_encrypted.contract.rendered
55+
filename = "${path.module}/build/contract.yml"
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 "registry" {
22+
type = string
23+
description = <<-DESC
24+
Prefix of the container registry used to pull the image
25+
DESC
26+
}
27+
28+
variable "pull_username" {
29+
type = string
30+
description = <<-DESC
31+
Username to pull from the above registry
32+
DESC
33+
}
34+
35+
variable "pull_password" {
36+
type = string
37+
description = <<-DESC
38+
Password to pull from the above registry
39+
DESC
40+
}

0 commit comments

Comments
 (0)