Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect all IP addresses on eth0 #457

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions source/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,13 +1026,15 @@ constexpr char PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_TEMPLATE_P
constexpr char PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_CSR_FILE[];
constexpr char PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_DEVICE_KEY[];
constexpr char PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_PUBLISH_SYS_INFO[];
constexpr char PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_NETWORK_INTERFACE[];

constexpr char PlainConfig::FleetProvisioning::JSON_KEY_ENABLED[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_TEMPLATE_NAME[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_TEMPLATE_PARAMETERS[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_CSR_FILE[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_DEVICE_KEY[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_PUBLISH_SYS_INFO[];
constexpr char PlainConfig::FleetProvisioning::JSON_KEY_NETWORK_INTERFACE[];

bool PlainConfig::FleetProvisioning::LoadFromJson(const Crt::JsonView &json)
{
Expand Down Expand Up @@ -1077,6 +1079,7 @@ bool PlainConfig::FleetProvisioning::LoadFromJson(const Crt::JsonView &json)
Config::TAG, "Key {%s} was provided in the JSON configuration file with an empty value", jsonKey);
}
}

jsonKey = JSON_KEY_DEVICE_KEY;
if (json.ValueExists(jsonKey))
{
Expand All @@ -1090,11 +1093,26 @@ bool PlainConfig::FleetProvisioning::LoadFromJson(const Crt::JsonView &json)
Config::TAG, "Key {%s} was provided in the JSON configuration file with an empty value", jsonKey);
}
}

jsonKey = JSON_KEY_PUBLISH_SYS_INFO;
if (json.ValueExists(jsonKey))
{
collectSystemInformation = json.GetBool(jsonKey);
}

jsonKey = JSON_KEY_NETWORK_INTERFACE;
if (json.ValueExists(jsonKey))
{
if (!json.GetString(jsonKey).empty())
{
networkInterface = json.GetString(jsonKey).c_str();
}
else
{
LOGM_WARN(
Config::TAG, "Key {%s} was provided in the JSON configuration file with an empty value", jsonKey);
}
}
}

return true;
Expand Down Expand Up @@ -1129,6 +1147,11 @@ bool PlainConfig::FleetProvisioning::LoadFromCliArgs(const CliArgs &cliArgs)
{
enabled = cliArgs.at(CLI_FLEET_PROVISIONING_PUBLISH_SYS_INFO).compare("true") == 0;
}
if (cliArgs.count(PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_NETWORK_INTERFACE))
{
networkInterface = cliArgs.at(
PlainConfig::FleetProvisioning::CLI_FLEET_PROVISIONING_NETWORK_INTERFACE).c_str();
}

return true;
}
Expand Down Expand Up @@ -1186,6 +1209,16 @@ void PlainConfig::FleetProvisioning::SerializeToObject(Crt::JsonObject &object)
{
object.WithString(JSON_KEY_DEVICE_KEY, deviceKey->c_str());
}

if (collectSystemInformation)
{
object.WithBool(JSON_KEY_PUBLISH_SYS_INFO, true);
}

if (networkInterface.has_value() && networkInterface->c_str())
{
object.WithString(JSON_KEY_NETWORK_INTERFACE, networkInterface->c_str());
}
}

constexpr char PlainConfig::FleetProvisioningRuntimeConfig::JSON_KEY_COMPLETED_FLEET_PROVISIONING[];
Expand Down
3 changes: 3 additions & 0 deletions source/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,23 @@ namespace Aws
static constexpr char CLI_FLEET_PROVISIONING_CSR_FILE[] = "--csr-file";
static constexpr char CLI_FLEET_PROVISIONING_DEVICE_KEY[] = "--device-key";
static constexpr char CLI_FLEET_PROVISIONING_PUBLISH_SYS_INFO[] = "--collect-system-information";
static constexpr char CLI_FLEET_PROVISIONING_NETWORK_INTERFACE[] = "--network-interface";

static constexpr char JSON_KEY_ENABLED[] = "enabled";
static constexpr char JSON_KEY_TEMPLATE_NAME[] = "template-name";
static constexpr char JSON_KEY_TEMPLATE_PARAMETERS[] = "template-parameters";
static constexpr char JSON_KEY_CSR_FILE[] = "csr-file";
static constexpr char JSON_KEY_DEVICE_KEY[] = "device-key";
static constexpr char JSON_KEY_PUBLISH_SYS_INFO[] = "collect-system-information";
static constexpr char JSON_KEY_NETWORK_INTERFACE[] = "network-interface";

bool enabled{false};
Aws::Crt::Optional<std::string> templateName;
Aws::Crt::Optional<std::string> templateParameters;
Aws::Crt::Optional<std::string> csrFile;
Aws::Crt::Optional<std::string> deviceKey;
bool collectSystemInformation{false};
Aws::Crt::Optional<std::string> networkInterface;
};
FleetProvisioning fleetProvisioning;

Expand Down
76 changes: 51 additions & 25 deletions source/fleetprovisioning/FleetProvisioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,19 @@ bool FleetProvisioning::ProvisionDevice(shared_ptr<SharedCrtResourceManager> fpC
LOG_INFO(TAG, "Fleet Provisioning Feature has been started.");
collectSystemInformation = config.fleetProvisioning.collectSystemInformation;

if (collectSystemInformation)
{
if (config.fleetProvisioning.networkInterface.has_value() && config.fleetProvisioning.networkInterface->c_str())
{
networkInterface = config.fleetProvisioning.networkInterface->c_str();
}
else
{
LOG_WARN(TAG, "Network interface not provided, default to eth0");
networkInterface = "eth0";
}
}

bool didSetup = FileUtils::CreateDirectoryWithPermissions(keyDir.c_str(), S_IRWXU) &&
FileUtils::CreateDirectoryWithPermissions(
Config::DEFAULT_CONFIG_DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH | S_IXOTH);
Expand Down Expand Up @@ -812,6 +825,9 @@ bool FleetProvisioning::CollectNetworkInformation()
struct ifaddrs *ifap = nullptr;
char ip[INET6_ADDRSTRLEN];

std::vector<std::string> ipAddrList;
ipAddrList.reserve(2); // normally at most 2 based on observation

if (getifaddrs(&ifap) == -1)
{
LOG_ERROR(TAG, "*** %s: Failed to get network interfaces ***");
Expand All @@ -836,41 +852,51 @@ bool FleetProvisioning::CollectNetworkInformation()
char *name = ifa->ifa_name;

// We only search for addresses on eth0 interface.
if (family == AF_INET && strncmp(name, "eth0", 3) == 0)
if (family == AF_INET && networkInterface == name)
{
struct in_addr addr = (reinterpret_cast<struct sockaddr_in *>(ifa->ifa_addr))->sin_addr;
inet_ntop(AF_INET, &addr, ip, INET_ADDRSTRLEN);
ipAddrList.push_back(ip);
}
}

struct ifreq ifr;
unsigned char *mac;

strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1)
{
close(fd);
freeifaddrs(ifap);
struct ifreq ifr;
unsigned char *mac;

LOG_ERROR(TAG, "*** %s: Failed to get MAC address for interface ***");
return false;
}
mac = reinterpret_cast<unsigned char *>(ifr.ifr_hwaddr.sa_data);
strncpy(ifr.ifr_name, networkInterface.c_str(), IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1)
{
close(fd);
freeifaddrs(ifap);

Aws::Crt::Optional<std::string> params(FormatMessage(
R"({"DeviceIPAddress": "%s", "DeviceMACAddress": "%02x:%02x:%02x:%02x:%02x:%02x"})",
ip,
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]));
MapParameters(params);
LOGM_DEBUG(TAG, "Successfully collected network information: %s", params.value().c_str());
LOGM_ERROR(TAG, "*** %s: Failed to get MAC address for interface %s ***", networkInterface.c_str());
return false;
}
mac = reinterpret_cast<unsigned char *>(ifr.ifr_hwaddr.sa_data);

break;
std::string ipAddrs;
for (size_t i = 0; i < ipAddrList.size(); ++i)
{
if (i > 0)
{
ipAddrs += ",";
}
ipAddrs += ipAddrList[i];
}


Aws::Crt::Optional<std::string> params(FormatMessage(
R"({"DeviceIPAddresses": "%s", "DeviceMACAddress": "%02x:%02x:%02x:%02x:%02x:%02x"})",
ipAddrs.c_str(),
mac[0],
mac[1],
mac[2],
mac[3],
mac[4],
mac[5]));
MapParameters(params);
LOGM_DEBUG(TAG, "Successfully collected network information: %s", params.value().c_str());

close(fd);
freeifaddrs(ifap);
return true;
Expand Down
5 changes: 5 additions & 0 deletions source/fleetprovisioning/FleetProvisioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ namespace Aws
*/
bool collectSystemInformation;

/**
* \brief stores name of network interface for collecting system information.
*/
std::string networkInterface;

/**
* \brief creates a new certificate and private key using the AWS certificate authority
*
Expand Down