-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
146 lines (127 loc) · 4.73 KB
/
main.tf
File metadata and controls
146 lines (127 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
locals {
web_console_username = "admin"
web_console_default_password = "admin"
security_group_ids = concat(
[for sg in aws_security_group.sg : sg.id],
var.security_group_ids)
public_ip = (var.attach_persistent_public_ip ?
(length(aws_eip.dsf_instance_eip) > 0 ? aws_eip.dsf_instance_eip[0].public_ip : null) :
aws_instance.cipthertrust_manager_instance.public_ip)
public_dns = (var.attach_persistent_public_ip ?
(length(aws_eip.dsf_instance_eip) > 0 ? aws_eip.dsf_instance_eip[0].public_dns : null) :
aws_instance.cipthertrust_manager_instance.public_dns)
private_ip = length(aws_network_interface.eni.private_ips) > 0 ? tolist(aws_network_interface.eni.private_ips)[0] : null
cm_address = coalesce(local.public_ip, local.private_ip)
}
resource "aws_eip" "dsf_instance_eip" {
count = var.attach_persistent_public_ip ? 1 : 0
domain = "vpc"
tags = merge(var.tags, { Name = var.friendly_name })
}
resource "aws_eip_association" "eip_assoc" {
count = var.attach_persistent_public_ip ? 1 : 0
instance_id = aws_instance.cipthertrust_manager_instance.id
allocation_id = aws_eip.dsf_instance_eip[0].id
}
resource "aws_instance" "cipthertrust_manager_instance" {
ami = local.ami_id
instance_type = var.instance_type
key_name = var.key_pair
root_block_device {
volume_size = var.ebs.volume_size
volume_type = var.ebs.volume_type
iops = var.ebs.iops
delete_on_termination = true
}
network_interface {
network_interface_id = aws_network_interface.eni.id
device_index = 0
}
disable_api_termination = true
force_destroy = true
metadata_options {
http_endpoint = "enabled"
http_tokens = "required"
}
tags = merge(var.tags, { Name : var.friendly_name })
volume_tags = merge(var.tags, { Name : var.friendly_name })
lifecycle {
ignore_changes = [ami]
}
depends_on = [
aws_network_interface.eni,
aws_eip.dsf_instance_eip,
data.aws_ami.selected-ami
]
}
resource "aws_network_interface" "eni" {
subnet_id = var.subnet_id
security_groups = local.security_group_ids
tags = var.tags
}
resource "null_resource" "set_password" {
provisioner "local-exec" {
command = <<EOF
#!/bin/bash -x
set -e
# Wait up to 10 minutes for API to respond
echo "Waiting for service API to become reachable..."
for i in {1..60}; do
response=$(curl -k -s --connect-timeout 5 https://${local.cm_address}/api/v1/system/services/status 2>/dev/null) || true
if [ -n "$response" ]; then
echo "CipherTrust Manager API is reachable."
break
fi
echo "[$i] CipherTrust Manager API unreachable, retrying in 10s..."
sleep 10
done
if [ -z "$response" ]; then
echo "ERROR: CipherTrust Manager API did not become reachable in time."
exit 1
fi
# Wait up to 10 minutes for status = "started"
echo "Waiting for CipherTrust Manager services to start..."
for i in {1..60}; do
response=$(curl -k -s --connect-timeout 5 https://${local.cm_address}/api/v1/system/services/status 2>/dev/null) || true
if [ -z "$response" ]; then
echo "[$i] Services status API unreachable, retrying in 10s..."
sleep 10
continue
fi
# Remove carriage returns and newlines from the response since jq does not handle them well
clean_response=$${response//'\r'/}
clean_response=$${clean_response//'\n'/' '}
SERVICE_STATUS=$(echo "$clean_response" | jq -r '.status')
if [ "$SERVICE_STATUS" = "started" ]; then
echo "Service status is 'started'. Proceeding with password change."
response=$(curl -k -s -w "\n%%{http_code}" -X PATCH "https://${local.cm_address}/api/v1/auth/changepw" --header 'Content-Type: application/json' \
--data "{\"username\": \"admin\", \"password\": \"$PASSWORD\", \"new_password\": \"$NEW_PASSWORD\"}")
BODY=$(echo "$response" | sed '$d')
STATUS=$(echo "$response" | tail -n1)
if [ "$STATUS" -ge 200 ] && [ "$STATUS" -lt 300 ]; then
echo "CipherTrust Manager password was set successfully"
exit 0
else
echo "Request failed with HTTP status $STATUS"
echo "$BODY"
exit 1
fi
fi
echo "[$i] Services status: $SERVICE_STATUS... retrying in 10s"
sleep 10
done
echo "ERROR: Services did not start in time."
exit 1
EOF
interpreter = ["bash", "-c"]
environment = {
PASSWORD = local.web_console_default_password
NEW_PASSWORD = nonsensitive(var.cm_password)
}
}
depends_on = [
aws_instance.cipthertrust_manager_instance,
aws_network_interface.eni,
aws_eip_association.eip_assoc
]
}