forked from mkhanal1/connector_terraform_templates
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaws_connector_terraform.tf
107 lines (93 loc) · 3.64 KB
/
aws_connector_terraform.tf
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
##################################
# THIS SCRIPT IS PROVIDED TO YOU "AS IS." TO THE EXTENT PERMITTED BY LAW, QUALYS HEREBY DISCLAIMS ALL WARRANTIES AND LIABILITY
# FOR THE PROVISION OR USE OF THIS SCRIPT. IN NO EVENT SHALL THESE SCRIPTS BE DEEMED TO BE CLOUD SERVICES AS PROVIDED BY QUALYS
#
# Author: Mikesh Khanal
#
# INPUT THE FOLLOWING PARAMETERS
#
# username: Username to login to Qualys CloudView
# Password: Password to login to Qualys CloudView
# baseurl: Qualys CloudView URL
##################################
variable "create_assetview_cloudview_connector" {
type = bool
description = "If set to true, creates cloudview and AssetView Connector; If set to false, creates AssetView Connector"
}
variable "username" {
type = string
description = "The username for Qualys CloudView."
}
variable "baseurl" {
type = string
description = "The API server for Qualys CloudView eg:https://qualysapi.qg2.apps.qualys.com"
}
variable "password" {
type = string
description = "The password for Qualys CloudView."
}
variable "externalId" {
type = string
description = "The external Id for the assume role."
}
#############################
# Initializing the provider
##############################
provider "aws" {
version = "~> 2.0"
region = "us-east-1"
}
################################################
# Creating an IAM role with assume_role policy
################################################
resource "aws_iam_role" "Qualys_role" {
name = "qualys_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::805950163170:root"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": ${var.externalId}
}
}
}
]
}
EOF
tags = {
tag-key = "tag-value"
}
}
###################################################
# Attaching SecurityAudit Policy to the Qualys role
###################################################
resource "aws_iam_role_policy_attachment" "role_attach" {
role = "${aws_iam_role.Qualys_role.name}"
policy_arn = "arn:aws:iam::aws:policy/SecurityAudit"
}
#################################################################
# Qualys API Call to create CloudView AWS Connector
#################################################################
resource "local_file" "authentication_key" {
content = "<ServiceRequest><data><AwsAssetDataConnector><name>AWS-connector-\"${substr("${aws_iam_role.Qualys_role.arn}",13,12)}\"</name><activation><set><ActivationModule>VM</ActivationModule><ActivationModule>PC</ActivationModule></set></activation><arn>${aws_iam_role.Qualys_role.arn}</arn><externalId>${var.externalId}</externalId><allRegions>true</allRegions><useForCloudView>${var.create_cloudview_connector}</useForCloudView></AwsAssetDataConnector></data></ServiceRequest>"
filename = "${path.module}/file.xml"
depends_on = [aws_iam_role_policy_attachment.role_attach]
}
module "QualysCloudViewAssetViewConnector" {
source = "matti/resource/shell"
command = "curl -u '${var.username}:${var.password}' --header 'Content-type: text/xml' -X POST --data-binary @- ${var.baseurl}/qps/rest/2.0/create/am/awsassetdataconnector < file.xml"
depends = [aws_iam_role_policy_attachment.role_attach]
}
#######################################################
# Outputs
#######################################################
output "ROLE_ARN" { value = aws_iam_role.Qualys_role.arn}
output "CLOUDVIEW-OUTPUT" { value = module.QualysCloudViewAssetViewConnector.stdout }
output "CLOUDVIEW-EXIT-STATUS" { value = module.QualysCloudViewAssetViewConnector.exitstatus }