Commit 5a68c9c5 authored by Marc Grimme's avatar Marc Grimme Committed by Commit Bot

Introduced location support for the hostname_handler.

HostTemplate in hostname_handler now also supports the ${LOCATION}
variable to be resolved to the location set in the policy.
This will lead to a dhcp hostname supporting the location
autoresolution.

R=marcgrimme

Bug: 1013220
Change-Id: Iac87db9914985ce3a65c34607977cc4b9d663ea3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1915760Reviewed-by: default avatarSergey Poromov <poromov@chromium.org>
Commit-Queue: Marc Grimme <marcgrimme@google.com>
Cr-Commit-Position: refs/heads/master@{#715702}
parent 442de1a3
......@@ -23,6 +23,7 @@ constexpr char kAssetIDPlaceholder[] = "${ASSET_ID}";
constexpr char kMachineNamePlaceholder[] = "${MACHINE_NAME}";
constexpr char kSerialNumPlaceholder[] = "${SERIAL_NUM}";
constexpr char kMACAddressPlaceholder[] = "${MAC_ADDR}";
constexpr char kLocationPlaceholder[] = "${LOCATION}";
// As per RFC 1035, hostname should be 63 characters or less.
const int kMaxHostnameLength = 63;
......@@ -74,13 +75,16 @@ std::string HostnameHandler::FormatHostname(const std::string& name_template,
const std::string& asset_id,
const std::string& serial,
const std::string& mac,
const std::string& machine_name) {
const std::string& machine_name,
const std::string& location) {
std::string result = name_template;
base::ReplaceSubstringsAfterOffset(&result, 0, kAssetIDPlaceholder, asset_id);
base::ReplaceSubstringsAfterOffset(&result, 0, kSerialNumPlaceholder, serial);
base::ReplaceSubstringsAfterOffset(&result, 0, kMACAddressPlaceholder, mac);
base::ReplaceSubstringsAfterOffset(&result, 0, kMachineNamePlaceholder,
machine_name);
base::ReplaceSubstringsAfterOffset(&result, 0, kLocationPlaceholder,
location);
if (!IsValidHostname(result))
return std::string();
......@@ -125,6 +129,10 @@ void HostnameHandler::
->browser_policy_connector_chromeos()
->GetMachineName();
const std::string location = g_browser_process->platform_part()
->browser_policy_connector_chromeos()
->GetDeviceAnnotatedLocation();
chromeos::NetworkStateHandler* handler =
chromeos::NetworkHandler::Get()->network_state_handler();
......@@ -139,8 +147,8 @@ void HostnameHandler::
}
}
handler->SetHostname(
FormatHostname(hostname_template, asset_id, serial, mac, machine_name));
handler->SetHostname(FormatHostname(hostname_template, asset_id, serial, mac,
machine_name, location));
}
} // namespace policy
......@@ -38,7 +38,8 @@ class HostnameHandler : public chromeos::NetworkStateHandlerObserver {
const std::string& asset_id,
const std::string& serial,
const std::string& mac,
const std::string& machine_name);
const std::string& machine_name,
const std::string& location);
void OnDeviceHostnamePropertyChanged();
......
......@@ -17,77 +17,81 @@ class HostnameHandlerTest : public testing::Test {
const std::string& asset_id,
const std::string& serial,
const std::string& mac,
const std::string& machine_name) {
auto result = HostnameHandler::FormatHostname(name_template, asset_id,
serial, mac, machine_name);
const std::string& machine_name,
const std::string& location) {
auto result = HostnameHandler::FormatHostname(
name_template, asset_id, serial, mac, machine_name, location);
ASSERT_EQ(expected, result);
}
};
TEST_F(HostnameHandlerTest, Basic) {
formatAndAssert("name", "name", "asset123", "SER1AL123", "0000deadbeef",
"chrome_machine");
"chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, SubstituteValidAssetId) {
formatAndAssert("chromebook-asset123", "chromebook-${ASSET_ID}", "asset123",
"SER1AL123", "0000deadbeef", "chrome_machine");
"SER1AL123", "0000deadbeef", "chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, SubstituteValidSerial) {
formatAndAssert("chromebook-SER1AL123", "chromebook-${SERIAL_NUM}",
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine");
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine",
"loc123");
}
TEST_F(HostnameHandlerTest, SubstituteValidMAC) {
formatAndAssert("chromebook-0000deadbeef", "chromebook-${MAC_ADDR}",
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine");
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine",
"loc123");
}
TEST_F(HostnameHandlerTest, SubstituteMachineName) {
formatAndAssert("chromebook-chrome_machine", "chromebook-${MACHINE_NAME}",
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine");
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine",
"loc123");
}
TEST_F(HostnameHandlerTest, MixedSubstitution) {
formatAndAssert(
"chromebook-0000deadbeef-SER1AL123-asset123-chrome_machine",
"chromebook-${MAC_ADDR}-${SERIAL_NUM}-${ASSET_ID}-${MACHINE_NAME}",
"asset123", "SER1AL123", "0000deadbeef", "chrome_machine");
TEST_F(HostnameHandlerTest, SubstituteLocation) {
formatAndAssert("chromebook-loc123", "chromebook-${LOCATION}", "asset123",
"SER1AL123", "0000deadbeef", "chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, MultipleSubstitution) {
formatAndAssert("chromebook-asset123-asset123-asset123",
"chromebook-${ASSET_ID}-${ASSET_ID}-${ASSET_ID}", "asset123",
"SER1AL123", "0000deadbeef", "chrome_machine");
TEST_F(HostnameHandlerTest, MixedSubstitution) {
formatAndAssert(
"chromebook-deadbeef-SER1AL123-asset123-chrome_machine-loc123",
"chromebook-${MAC_ADDR}-${SERIAL_NUM}-${ASSET_ID}-${MACHINE_NAME}-"
"${LOCATION}",
"asset123", "SER1AL123", "deadbeef", "chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, SubstituteInvalidSerial) {
formatAndAssert("", "chromebook-${SERIAL_NUM}", "asset123", "Serial number",
"0000deadbeef", "chrome_machine");
"0000deadbeef", "chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, IncorrectTemplateVariable) {
formatAndAssert("", "chromebook-${SERIAL_NUMBER}", "asset123", "SERIAL123",
"0000deadbeef", "chrome_machine");
"0000deadbeef", "chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, InvalidFirstCharacter) {
formatAndAssert("", "-somename", "asset123", "Serial number", "0000deadbeef",
"chrome_machine");
"chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, HostnameTooLong) {
formatAndAssert("", "${ASSET_ID}${ASSET_ID}${ASSET_ID}",
"1234567890123456789012345678901", "serial", "0000deadbeef",
"chrome_machine");
"chrome_machine", "loc123");
}
TEST_F(HostnameHandlerTest, HostnameExactly63Chars) {
formatAndAssert(
"1234567890123456789012345678901-1234567890123456789012345678901",
"${ASSET_ID}-${ASSET_ID}", "1234567890123456789012345678901", "serial",
"0000deadbeef", "chrome_machine");
"0000deadbeef", "chrome_machine", "loc123");
}
} // namespace policy
......@@ -14574,7 +14574,7 @@
If this policy is set to a non empty string, that string will be used as the device hostname during DHCP request.
The string can contain variables <ph name="ASSET_ID_PLACEHOLDER">${ASSET_ID}</ph>, <ph name="SERIAL_NUM_PLACEHOLDER">${SERIAL_NUM}</ph>, <ph name="MAC_ADDR_PLACEHOLDER">${MAC_ADDR}</ph>, <ph name="MACHINE_NAME_PLACEHOLDER">${MACHINE_NAME}</ph> that would be replaced with values on the device before using as a hostname. Resulting substitution should be a valid hostname (per RFC 1035, section 3.1).
The string can contain variables <ph name="ASSET_ID_PLACEHOLDER">${ASSET_ID}</ph>, <ph name="SERIAL_NUM_PLACEHOLDER">${SERIAL_NUM}</ph>, <ph name="MAC_ADDR_PLACEHOLDER">${MAC_ADDR}</ph>, <ph name="MACHINE_NAME_PLACEHOLDER">${MACHINE_NAME}</ph>, <ph name="LOCATION_PLACEHOLDER">${LOCATION}</ph> that would be replaced with values on the device before using as a hostname. Resulting substitution should be a valid hostname (per RFC 1035, section 3.1).
If this policy is not set, or the value after substitution is not a valid hostname, no hostname will be set in DHCP request. '''
},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment