Protobuf动态消息模式验证

huangapple go评论85阅读模式
英文:

Protobuf dynamic message schema verification

问题

我正在尝试实现数据合约逻辑。
我有两个服务彼此之间切换消息。
服务A以某种格式发送消息+用于生成它的.proto文件的文件描述符proto。服务B同时获取消息和文件描述符proto,并确保消息不会破坏模式定义。
到目前为止,我所做的是使用描述符proto创建一个动态消息实例,并尝试将消息解组到动态消息实例中,如果在解组过程中没有发生错误,则视为成功(消息不会破坏模式)。

  1. 是否可以依赖解组函数来判断消息是否正常?
  2. 我注意到,即使我发送具有完全不同模式的消息,解组也会成功。我发现唯一导致解组失败的方法是发送缺少必需字段的proto2消息。
    因此,每个消息都可以使用完全不同的模式定义进行解组,这是设计上的吗?
  • 我正在使用官方的Go protobuf库。
英文:

I'm trying to implement data contract logic.
I've 2 services switching messages with each other.
Service A send messages in some format + the file descriptor proto of the .proto file used to generate it. Service B gets them both, the message and the file descriptor proto and has to ensure the message doesn't break the schema defenition.
What I've did until now is to create a dynamic message instance using the descriptor proto and tried to unmarshal the message into the dynamic message instance, and in case no error occurred during the unmarshal process it counts as a success (message doesn't break the schema).

  1. Is it ok to rely on the unmarshal function in order to decide whether the message is ok?
  2. I noticed that even in case I'm sending messages with totally different schemas the unmarshal succeed. the only way I found to cause the unmarshal to fail is by sending proto2 messages with missing required fields.
    So is it by design that every message can be unmarshled using a totally different schema definition?
  • I'm using the official Go protobuf library

答案1

得分: 1

  1. 是的,可以使用unmarshal函数来检查您的proto是否有效。
  2. 不,它不是设计用来解组任意消息的,而是设计用来解组仅在您的protobuf消息中存在的字段。例如,您有以下结构的proto消息:
message MyMessage {
    uint64 id = 1;
    string name = 2;
    string surname = 3;
}

如果您的服务器接收到只包含idname的消息,并且您的服务器尝试解组这条消息,idname字段将被解组,而您结构中的surname字段将为空。这种方法也适用于JSON。

英文:
  1. Yes, it is ok to use unmarshal function to check if your proto is valid or not.
  2. No, it's not design to unmarshal arbitrary message but it is designed to unmarshal only that fields which are present in your protobuf message. For example, you have proto message with structure as follow:
message MyMessage {
    uint64 id = 1;
    string name = 2;
    string surname = 3;
}

If your server receives message that contains just id and name and your server is trying to unmarshal this message, id and name fields would be unmarshaled while surname field in your structure would be empty.
This approach appliable for JSONs too.

huangapple
  • 本文由 发表于 2022年10月29日 13:42:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74243073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定