fix(iot): resolve connect policy variables through the IAM resolver (#5827)

IoT carried its own policy-variable pass. Before handing the attached documents
to the evaluator, IotService walked each statement's parsed tree, replaced the
connection's ${iot:...} values in every string it found, and dropped any
statement that still held a ${...}. That was IoT's copy of the mechanism the
evaluator gained for every other policy Floci reads, so the documents now go in
as written and PolicyVariableResolver does the substituting, from the same
condition context the connection already supplies.

The developer guide gives IoT no policy language of its own to justify a second
reader: AWS IoT Core policies "are JSON documents. They follow the same
conventions as IAM policies", they "follow the same policy evaluation logic as
IAM policies", and a policy variable is one "that can be used in AWS IoT Core
policies in the Resource or Condition block". The predefined ${*}, ${?} and
${$} are documented by pointing at IAM's own variables page.

Five corners move, each of them onto what those pages say:

- A ${...} outside Resource and Condition is text. The text pass substituted
  into every string of the statement, Action and Sid included, where the guide
  gives a variable meaning in those two elements only.
- ${*}, ${?} and ${$} resolve to the characters they stand for. Any statement
  holding one used to be dropped, the unresolved-variable scan being a plain
  \$\{[^}]*\} that matched them too.
- A variable resolves whatever case its key is written in, the variables page
  being explicit that "Key names are case-insensitive. For example,
  aws:CurrentTime is equivalent to AWS:currenttime". A thing attribute spelled
  two ways in one registry now collides on that reading, which is the price of
  the IAM rule and not a surprise unique to IoT.
- An unresolvable variable in a Condition value leaves the value null rather
  than taking the whole statement away, so a positive operator does not match
  and a negated one does. A Resource pattern holding one still matches no
  resource, which is what dropping the statement amounted to.
- Variables need the version that introduced them. A document that does not
  declare "Version": "2012-10-17" keeps ${iot:ClientId} as literal text, the
  guide's answer being that "if you don't set the Version to 2012-10-17 or
  later, variables like ${aws:username} are treated as literal strings in the
  policy". Every policy example in the IoT guide declares it.

A connection no longer rewrites the document it evaluates, which the evaluator's
parse cache notices: it is keyed by document text, so it held one entry per
client id where it now holds one per policy.

Five tests in IotServiceDeviceAuthTest cover the five corners, and the
unresolved-variable, JSON-syntax and thing-attribute tests already there cover
what has to stay put. docs/services/iot.md says what resolves where.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-16 17:25:46 -05:00
1 parent 49a3ff9a10
commit caa606ba7d
3 files changed
+59 -69

No files matched your search

@@ -377,6 +377,52 @@ class IotServiceDeviceAuthTest {
assertFalse(connect(device, "${iot:Certificate.Subject.CommonName}"));
}
@Test
void aVariableOutsideResourceAndConditionIsNotAVariable() {
RegisteredDevice device = registeredDevice(true);
createAndAttach("p", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"${iot:Certificate.Subject.CommonName}\","
+ "\"Effect\":\"Allow\",\"Action\":\"iot:Connect\",\"Resource\":\"arn:aws:iot:*:*:client/*\"}]}", device);
assertTrue(connect(device, "sensor-1"),
"a variable has meaning in the Resource or Condition block, so a Sid is text like any other");
}
@Test
void theAsteriskVariableStandsForALiteralAsterisk() {
RegisteredDevice device = registeredDevice(true);
createAndAttach("p", statement("Allow", "arn:aws:iot:*:*:client/${*}", null), device);
assertTrue(connect(device, "*"), "the one client id the pattern names");
assertFalse(connect(device, "sensor-1"), "the predefined variable is the character, not a wildcard");
}
@Test
void aVariableResolvesWhateverCaseItsKeyIsWrittenIn() {
RegisteredDevice device = registeredDevice(true);
createAndAttach("p", statement("Allow", "arn:aws:iot:*:*:client/${IOT:clientid}", null), device);
assertTrue(connect(device, "sensor-1"), "key names are case-insensitive, aws:CurrentTime being AWS:currenttime");
}
@Test
void aDocumentWithoutTheVersionThatIntroducedVariablesReadsThemLiterally() {
RegisteredDevice device = registeredDevice(true);
createAndAttach("p", "{\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"iot:Connect\","
+ "\"Resource\":\"arn:aws:iot:*:*:client/${iot:ClientId}\"}]}", device);
assertFalse(connect(device, "sensor-1"), "variables arrived with 2012-10-17 and are literal text without it");
assertTrue(connect(device, "${iot:ClientId}"), "which leaves the pattern naming that client id and no other");
}
@Test
void anUnresolvedConditionValueIsNullRatherThanAStatementThatVanishes() {
RegisteredDevice device = registeredDevice(true);
createAndAttach("p", statement("Allow", "arn:aws:iot:*:*:client/*",
"{\"StringNotEquals\":{\"iot:ClientId\":\"${iot:Connection.Thing.ThingName}\"}}"), device);
assertTrue(connect(device, "sensor-1"), "no thing is attached, so the client id is neither equal to nor like a null");
}
@Test
void aClientIdCarryingJsonSyntaxIsDataNotPolicy() {
RegisteredDevice device = registeredDevice(true);