aws-greengrass / aws-greengrass-shadow-manager

A GreengrassV2 Component that provides offline device shadow documents and optional synchronization to the IoT device shadow service.
Apache License 2.0
9 stars 5 forks source link

How to use IoT policy thing variables ? #200

Closed sv3ndk closed 5 months ago

sv3ndk commented 5 months ago

Describe the bug I apologize, this is probably more my own misunderstanding than a bug, although I can't figure out why my setup fails.

When my python component is updating or deleting a named shadow via IPC and when using an IoT policy containing thing policy variables for the iot:UpdateThingShadow action, the shadow manager is forbidden to synchronize up to AWS.

When replacing the thing policy variable with a hardcoded thing name, the shadow manager works successfully.

The python component has been otherwise working successfully on multiple instances for a while though, it's already using IPC to interract with AWS and we already use one single IoT policy for all IoT thing, using thing policy variable, for example when publishing to MQTT via IPC:

{
      "Action": [
        "iot:Publish",
        "iot:Receive",
        "iot:RetainPublish"
      ],
      "Resource": [
        "arn:aws:iot:eu-west-1:111111111111:topic/${iot:Connection.Thing.ThingName}/*",
        "arn:aws:iot:eu-west-1:111111111111:topic/$aws/things/${iot:Connection.Thing.ThingName}*"
      ],
      "Effect": "Allow"
    },

To Reproduce

    {
      "Action": [
        "iot:GetThingShadow",
        "iot:UpdateThingShadow",
        "iot:DeleteThingShadow"
      ],
      "Resource": [
        "arn:aws:iot:eu-west-1:111111111111:thing/${iot:Connection.Thing.ThingName}",
        "arn:aws:iot:eu-west-1:111111111111:thing/${iot:Connection.Thing.ThingName}/*"
      ],
      "Effect": "Allow"
    },
    {
      "Action": [
        "iot:ListNamedShadowsForThing"
      ],
      "Resource": [
        "*"
      ],
      "Effect": "Allow"
    },

Expected behavior

Shadow update should be propagated to AWS

Actual behavior

Service returned error code ForbiddenException (Service: IotDataPlane, Status Code: 403,

Environment

Additional context

When replacing ${iot:Connection.Thing.ThingName} with one hard-coded thing name, the shadow manager succeeds in synchronizing its state to AWS, but of course that would force me to create one IoT policy per IoT thing, which is quickly unpractical.

I'm sure I'm doing something wrong in my config, could you help me spot where? Thanks a lot in advance!

MikeDombo commented 5 months ago

Hello,

IoT connection policy variables are not supported. You may use IoT certificate policy variables or specify the exact/wildcard name. https://docs.aws.amazon.com/iot/latest/developerguide/cert-policy-variables.html

Also see previous answers here: https://repost.aws/questions/QUOvz_sVB1RjOUlJRclfXrOw/secure-iot-policy-for-shadowmanager-shadow-actions#ANAdZ-7t5FRv6mtmync5Egnw.

sv3ndk commented 5 months ago

Thanks for the quick update and the links.

Ok, see, that's quite a pity, once my things are in the wild, I'd rather restrict at maximum their ability to update anything else than their own data (I can't imagine anybody would want otherwise in prod?).

I'll try to find a solution that works for me based on the pointers you sent me.

Thanks again