It is common for short-lived nodes to be created in AWS, and manually adding these nodes into UpGuard is daunting. This article walks through a solution using the UpGuard API and AWS user-data to do this automatically

Prerequisites

Goals

  • For any new server created in AWS, that node should also be added to UpGuard for scanning.

Setup

To achieve the automated adding of an AWS node, we will use the User data field.

If you are setting up a new AWS instance, you can access the User data field by clicking on Next: Configure Instance Details and looking in the Advanced section at the bottom.

Script

You can use the following script to add the node to UpGuard.

If you are adding a Linux server, use the following script (you will need to populate the variables. For field reference, see bulk adding nodes):

#!/bin/bash

apikey=""
secretkey=""
instance="https://appliance.upguard.org"

name=$(hostname)
short_description=""
node_type="SV"
operating_system_family_id=2
operating_system_id=245
medium_type=3
medium_hostname=$(hostname)
medium_username=""
medium_password=""
connection_manager_group_id=0

curl -w '%{http_code}\n' -X POST -s -k -H "Authorization: Token token=\"$apikey$secretkey\"" -H "Accept: application/json" -H "Content-Type: application/json" -d "{\"node\": {\"name\": \"$name\", \"short_description\": \"$short_description\", \"node_type\": \"$node_type\", \"operating_system_family_id\": $operating_system_family_id, \"operating_system_id\": $operating_system_id, \"medium_type\": $medium_type, \"medium_username\": \"$medium_username\", \"medium_hostname\": \"$medium_hostname\", \"medium_password\": \"$medium_password\", \"connection_manager_group_id\": $connection_manager_group_id}}" $instance/api/v1/nodes

If you are adding a Windows server, use the folliwng script (you will need to populate the variables. For field reference, see bulk adding nodes):

<powershell>
$instance = "https://appliance.upguard.org"
$apiKey = ""
$secretKey = ""

$node = @{
    # 'name' = $(Hostname);
    'name' = 'test-system';
    'node_type' = 'SV';
    'medium_type' = 7;
    'medium_username' = 'username';
    'medium_password' = '';
    'medium_hostname' = $(Hostname);
    'medium_port' = 5985;
    'connection_manager_group_id' = 1
}


$headers = @{'Authorization' = 'Token token="' + $apiKey + $secretKey + '"';}

$body = ''
foreach($kvp in $node.GetEnumerator()) {$body += 'node[' + $kvp.Key + ']=' + $kvp.Value + '&'}

$body = $body.TrimEnd('&')

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$req = Invoke-WebRequest "$($instance)/api/v1/nodes.json" -Method Post -Headers $headers -Body $body
</powershell>

Further Reading

Tags: nodes