-
Notifications
You must be signed in to change notification settings - Fork 2
/
structured_encrypt.go
63 lines (54 loc) · 1.53 KB
/
structured_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
61
62
63
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/pangea"
"github.com/pangeacyber/pangea-go/pangea-sdk/v4/service/vault"
)
func main() {
token := os.Getenv("PANGEA_VAULT_TOKEN")
if token == "" {
log.Fatal("missing PANGEA_VAULT_TOKEN environment variable")
}
domain := os.Getenv("PANGEA_DOMAIN")
if domain == "" {
log.Fatal("missing PANGEA_DOMAIN environment variable")
}
vaultClient := vault.New(&pangea.Config{
Token: token,
Domain: domain,
})
ctx := context.Background()
// First create an encryption key, either from the Pangea Console or
// programmatically as below.
generateInput := &vault.SymmetricGenerateRequest{
Algorithm: vault.SYAaes256_cfb,
Purpose: vault.KPencryption,
CommonGenerateRequest: vault.CommonGenerateRequest{
Name: "Go structured encrypt example " + time.Now().Format(time.RFC3339),
},
}
generateResponse, err := vaultClient.SymmetricGenerate(ctx, generateInput)
if err != nil {
log.Fatal(err)
}
encryptionKeyId := generateResponse.Result.ID
// Structured data that we'll encrypt.
data := map[string]interface{}{
"foo": [4]interface{}{1, 2, "true", "false"},
"some": "thing",
}
encryptInput := &vault.EncryptStructuredRequest{
ID: encryptionKeyId,
StructuredData: data,
Filter: "$.foo[2:4]",
}
encryptResponse, err := vaultClient.EncryptStructured(ctx, encryptInput)
if err != nil {
log.Fatal(err)
}
fmt.Println("Encrypted result:", pangea.Stringify(encryptResponse.Result.StructuredData))
}