Hi Jinjun,
Thank you for your reply.
It looks like my questions were misunderstood. I apologize for this. Let me rephrase:
I do not intend to share device id among multiple devices, the opposite - my intent is to actively detect and prevent this from happening accidentally or maliciously.
The private key is not meant to be shared, but it is possible that it is accidentally used in 2 different devices during their setup or stolen by a 3rd party.
It seems like there should be a device registration process whereby during the initial connection, the device receives a unique id from Cloud IoT and uses it to identify itself during all communications, possibly by embedding it in the JWT token. Other devices would not be able to register and receive unique ids.
Regarding device metadata, device state topic already has some metadata posted as message attributes: projectId, location, registryId, deviceId, deviceNum and subfolder. (It is mentioned here and I have verified that it's included for not only telemetry messages but also state messages:
https://cloud.google.com/iot/docs/how-tos/mqtt-bridge)
To retrieve custom metadata, I use googleapiclient.discovery to retrieve the client for the cloudiot API, then client.projects().locations().registries().devices() to get the device by name, then I retrieve metadata with device.get('metadata').
This is done per state message. Ideally, there should be a way to embed device metadata as PubSub message attributes.