forked from tidbyt/pixlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.go
60 lines (50 loc) · 1.46 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"log"
"github.com/spf13/cobra"
"go.starlark.net/starlark"
"tidbyt.dev/pixlet/runtime"
)
const PublicKeysetJSON = `{
"primaryKeyId": 1589560679,
"key": [
{
"keyData": {
"typeUrl": "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPublicKey",
"value": "ElwKBAgCEAMSUhJQCjh0eXBlLmdvb2dsZWFwaXMuY29tL2dvb2dsZS5jcnlwdG8udGluay5BZXNDdHJIbWFjQWVhZEtleRISCgYKAggQEBASCAoECAMQEBAgGAEYARogLGtas20og5yP8/g9mCNLNCWTDeLUdcHH7o9fbzouOQoiIBIth4hdVF5A2sztwfW+hNoZ0ht/HNH3dDTEBPW3GXA2",
"keyMaterialType": "ASYMMETRIC_PUBLIC"
},
"status": "ENABLED",
"keyId": 1589560679,
"outputPrefixType": "TINK"
}
]
}`
func init() {
rootCmd.AddCommand(encryptCmd)
}
var encryptCmd = &cobra.Command{
Use: "encrypt [app name] [secret value]...",
Short: "Encrypts secrets for use in an app that will be submitted to the Tidbyt community repo",
Example: "encrypt weather my-top-secretweather-api-key-123456",
Args: cobra.MinimumNArgs(2),
Run: encrypt,
}
func encrypt(cmd *cobra.Command, args []string) {
sek := &runtime.SecretEncryptionKey{
PublicKeysetJSON: []byte(PublicKeysetJSON),
}
appName := args[0]
encrypted := make([]string, len(args)-1)
for i, val := range args[1:] {
var err error
encrypted[i], err = sek.Encrypt(appName, val)
if err != nil {
log.Fatalf("encrypting value: %v", err)
}
}
for _, val := range encrypted {
fmt.Println(starlark.String(val).String())
}
}