如何使用 SOPS(Secrets OPerationS)和 Go 加密从 JSON 文件导入的值?
How to encrypt a value imported from a JSON file using SOPS (Secrets OPerationS) and Go?
我有一个 JSON 文件如下。
secret.json:
{
"secret": "strongPassword"
}
我想打印出密钥 "secret" 的加密值。
到目前为止我已经尝试过如下。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
file, _ := ioutil.ReadFile("secret.json")
getSecretValue := secretValue{}
_ = json.Unmarshal([]byte(file), &getSecretValue)
encryptedValue, err := sops.Tree.Encrypt([]byte(getSecretValue.Value), file)
if err != nil {
panic(err)
}
fmt.Println(encryptedValue)
}
您可能已经猜到了,我是 Go 的新手,上面的代码不起作用。
如何改进代码以打印出加密值?
请注意,我编写这样的代码只是为了了解 SOPS 如何使用 Go 工作。我不会在生产中打印出这样的秘密值。
编辑:
我认为问题出在加密函数的参数上。根据文档,它应该采用 []byte key 和 Cipher 参数,但我不知道我是否正确设置了 []byte key 或者 Cipher 来自哪里。是否来自 crypto/cipher 包裹?
编辑 2:
感谢@HolaYang 的精彩回答。
我试图让你的答案与外部 JSON 文件一起工作,如下所示,但它给了我一条错误消息,说 cannot use fileContent (type secretValue) as type []byte in argument to (&"go.mozilla.org/sops/stores/json".Store literal).LoadPlainFile
.
package main
import (
hey "encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
// fileContent := []byte(`{
// "secret": "strongPassword"
// }`)
file, _ := ioutil.ReadFile("secret.json")
fileContent := secretValue{}
//_ = json.Unmarshal([]byte(file), &fileContent)
_ = hey.Unmarshal([]byte(file), &fileContent)
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}
让我们看看 sops.Tree.Encrypt
(代码中的错字) 的函数声明。
根据代码,我们应该按照这几步去做。
- 使用 json 文件构造一个
sops.Tree
实例。
- 使用特定的
Cipher
进行加密。
请自己尝试一下。
下面的代码演示,以AES为Cipher,sops只能用源代码接口加密total tree
package main
import (
"fmt"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
func main() {
/*
fileContent := []byte(`{
"secret": "strongPassword"
}`)
*/
fileContent, _ := ioutil.ReadFile("xxx.json")
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}
我有一个 JSON 文件如下。
secret.json:
{
"secret": "strongPassword"
}
我想打印出密钥 "secret" 的加密值。
到目前为止我已经尝试过如下。
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
file, _ := ioutil.ReadFile("secret.json")
getSecretValue := secretValue{}
_ = json.Unmarshal([]byte(file), &getSecretValue)
encryptedValue, err := sops.Tree.Encrypt([]byte(getSecretValue.Value), file)
if err != nil {
panic(err)
}
fmt.Println(encryptedValue)
}
您可能已经猜到了,我是 Go 的新手,上面的代码不起作用。
如何改进代码以打印出加密值?
请注意,我编写这样的代码只是为了了解 SOPS 如何使用 Go 工作。我不会在生产中打印出这样的秘密值。
编辑:
我认为问题出在加密函数的参数上。根据文档,它应该采用 []byte key 和 Cipher 参数,但我不知道我是否正确设置了 []byte key 或者 Cipher 来自哪里。是否来自 crypto/cipher 包裹?
编辑 2:
感谢@HolaYang 的精彩回答。
我试图让你的答案与外部 JSON 文件一起工作,如下所示,但它给了我一条错误消息,说 cannot use fileContent (type secretValue) as type []byte in argument to (&"go.mozilla.org/sops/stores/json".Store literal).LoadPlainFile
.
package main
import (
hey "encoding/json"
"fmt"
"io/ioutil"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
type secretValue struct {
Value string `json:"secret"`
}
func main() {
// fileContent := []byte(`{
// "secret": "strongPassword"
// }`)
file, _ := ioutil.ReadFile("secret.json")
fileContent := secretValue{}
//_ = json.Unmarshal([]byte(file), &fileContent)
_ = hey.Unmarshal([]byte(file), &fileContent)
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}
让我们看看 sops.Tree.Encrypt
(代码中的错字) 的函数声明。
根据代码,我们应该按照这几步去做。
- 使用 json 文件构造一个
sops.Tree
实例。 - 使用特定的
Cipher
进行加密。
请自己尝试一下。
下面的代码演示,以AES为Cipher,sops只能用源代码接口加密total tree
package main
import (
"fmt"
"go.mozilla.org/sops"
"go.mozilla.org/sops/aes"
"go.mozilla.org/sops/stores/json"
)
func main() {
/*
fileContent := []byte(`{
"secret": "strongPassword"
}`)
*/
fileContent, _ := ioutil.ReadFile("xxx.json")
encryptKey := []byte("0123456789012345") // length 16
branches, _ := (&json.Store{}).LoadPlainFile(fileContent)
tree := sops.Tree{Branches: branches}
r, err := tree.Encrypt(encryptKey, aes.NewCipher())
if err != nil {
panic(err)
}
fmt.Println(r)
}