【问题标题】:Golang mongodb field level encryptionGolang mongodb 字段级加密
【发布时间】:2019-12-13 05:22:14
【问题描述】:

我正在尝试使用 mgo 和 Golang 加密 mongodb 中的某些字段。由于 mongodb 不支持字段级加密,所以我在考虑对数据进行加密和解密,同时对数据进行编组和解组。

例如,在下面的结构中我要加密 Name 和 Gender

如果我的结构是这样的

type User struct {
UserID      string `json:"userID,omitempty" bson:"userID,omitempty"`
UserName    string `json:"userName,omitempty" bson:"userName,omitempty"`
UserAge   string `json:"userAge,omitempty" bson:"userAge,omitempty"`
UserGender string `json:"userGender,omitempty" bson:"userGender,omitempty"`
}

解决此问题的一种方法是在编组数据之后,在保存到数据库之前加密数据,以及将数据发送回 UI 的类似方法。在对数据进行编组和解组之前,是否有我可以收听的事件?或者有更好的方法吗?

【问题讨论】:

  • “事件”是对GetBSON() 和SetBSON() 方法的实际调用。

标签: mongodb go encryption


【解决方案1】:

由于 mongodb 不支持字段级加密

MongoDB v4.2 现在支持Client-Side Field Level Encryption,另见Field Level Encryption is GA。有两种支持的类型:

查看您的要求,您似乎需要automatic 加密/解密。您可以使用免费层 Atlas 集群尝试此操作,请参阅:Getting Started with Atlas。

使用 mgo 和 Golang 加密 mongodb 中的某些字段

不幸的是,mgo 上不提供此功能,但mongo-go-driver v1.2+ 上提供。您可以在MongoDB Go driver: Client Side Encryption 上查看一些示例。创建配置自动加密的MongoClient 的示例如下:

func createEncryptedClient() *mongo.Client {
        // create a client with auto encryption
        schemaMap := map[string]interface{}{
                "foo.bar": readJSONFile("collection_schema.json"),
        }
        autoEncOpts := options.AutoEncryption().
                SetKeyVaultNamespace("keyvault.__datakeys").
                SetKmsProviders(kmsProviders).
                SetSchemaMap(schemaMap)

        clientOpts := options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncOpts)
        autoEncryptionClient, err := mongo.Connect(ctx, clientOpts)
        if err != nil {
                log.Fatalf("Connect error for client with automatic encryption: %v", err)
        }
        return autoEncryptionClient
}

另见github.com/mongodb-labs/field-level-encryption-sandbox

【讨论】:

    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 2021-03-07
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多