Skip to content

Commit 721d49a

Browse files
authored
Add structured encrypt example (GEA-11385) (#158)
1 parent 0d319e6 commit 721d49a

File tree

6 files changed

+109
-30
lines changed

6 files changed

+109
-30
lines changed

examples/README.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
# Pangea Go SDK examples
22

3-
This is a quick example about how you use Pangea Go SDK, set up and run it.
3+
This directory contains full examples on how to use the Pangea Go SDK.
44

55
## Setup
66

7-
Set up environment variables ([Instructions](https://pangea.cloud/docs/getting-started/integrate/#set-environment-variables)) `PANGEA_AUDIT_TOKEN` and `PANGEA_DOMAIN` with your project token configured on Pangea User Console (token should have access to Audit service [Instructions](https://pangea.cloud/docs/getting-started/configure-services/#configure-a-pangea-service)) and with your pangea domain.
7+
Each example requires certain environment variables to be set. `PANGEA_DOMAIN`
8+
must be set to a Pangea domain (e.g. `aws.us.pangea.cloud`). Then, a token
9+
variable must be set as well. This is typically in the format of
10+
`PANGEA_{SERVICE_NAME}_TOKEN` (so: `PANGEA_REDACT_TOKEN` for the Redact service,
11+
`PANGEA_VAULT_TOKEN` for the Vault service, etc.). Finally, note that some
12+
examples require additional variables, so check out the example's source code
13+
and look out for what environment variables it loads at the beginning.
814

915
## Run
1016

11-
To run examples, move to service folder:
12-
```
13-
cd examples/intel
14-
go mod tidy
15-
```
17+
To run an example, navigate to its directory and use `go run`:
1618

17-
and from service folder run:
18-
19-
```
20-
go run url/reputation.go
19+
```bash
20+
$ cd examples/intel
21+
$ go run url/reputation.go
2122
```

examples/vault/go.mod

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ require github.com/pangeacyber/pangea-go/pangea-sdk/v3 v3.5.0
66

77
require (
88
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
9-
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
10-
github.com/mattn/go-colorable v0.1.12 // indirect
11-
github.com/mattn/go-isatty v0.0.14 // indirect
12-
github.com/rs/zerolog v1.29.1 // indirect
13-
golang.org/x/sys v0.11.0 // indirect
9+
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
10+
github.com/mattn/go-colorable v0.1.13 // indirect
11+
github.com/mattn/go-isatty v0.0.20 // indirect
12+
github.com/rs/zerolog v1.31.0 // indirect
13+
github.com/stretchr/testify v1.8.4 // indirect
14+
golang.org/x/sys v0.15.0 // indirect
1415
)

examples/vault/go.sum

+18-14
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
66
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
77
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
88
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
9-
github.com/hashicorp/go-retryablehttp v0.7.2 h1:AcYqCvkpalPnPF2pn0KamgwamS42TqUDDYFRKq/RAd0=
10-
github.com/hashicorp/go-retryablehttp v0.7.2/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
11-
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
12-
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
13-
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
14-
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
9+
github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT9yvm0e+Nd5M=
10+
github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
11+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
12+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
13+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
14+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
15+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
16+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
1517
github.com/pangeacyber/pangea-go/pangea-sdk/v3 v3.5.0 h1:RSDC/t1klmMXp9C6QZwDXqlGOVf9L/V5/gNfKXZ+PPo=
1618
github.com/pangeacyber/pangea-go/pangea-sdk/v3 v3.5.0/go.mod h1:Mza25gc1H5EwnRN3Qg3G8b192nOMQ8c+UOl0zhD+dGE=
1719
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1820
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1921
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
20-
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
21-
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
22-
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
22+
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
23+
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
24+
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
2325
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
24-
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
25-
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26-
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27-
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
28-
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
27+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
28+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
29+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
30+
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
31+
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
32+
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2933
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

examples/vault/structured_encrypt.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"log"
7+
"os"
8+
"time"
9+
10+
"github.com/pangeacyber/pangea-go/pangea-sdk/v3/pangea"
11+
"github.com/pangeacyber/pangea-go/pangea-sdk/v3/service/vault"
12+
)
13+
14+
func main() {
15+
token := os.Getenv("PANGEA_VAULT_TOKEN")
16+
if token == "" {
17+
log.Fatal("missing PANGEA_VAULT_TOKEN environment variable")
18+
}
19+
20+
domain := os.Getenv("PANGEA_DOMAIN")
21+
if domain == "" {
22+
log.Fatal("missing PANGEA_DOMAIN environment variable")
23+
}
24+
25+
vaultClient := vault.New(&pangea.Config{
26+
Token: token,
27+
Domain: domain,
28+
})
29+
30+
ctx := context.Background()
31+
32+
// First create an encryption key, either from the Pangea Console or
33+
// programmatically as below.
34+
generateInput := &vault.SymmetricGenerateRequest{
35+
Algorithm: vault.SYAaes256_cfb,
36+
Purpose: vault.KPencryption,
37+
CommonGenerateRequest: vault.CommonGenerateRequest{
38+
Name: "Go encrypt example " + time.Now().Format(time.RFC3339),
39+
},
40+
}
41+
generateResponse, err := vaultClient.SymmetricGenerate(ctx, generateInput)
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
encryptionKeyId := generateResponse.Result.ID
46+
47+
// Structured data that we'll encrypt.
48+
data := map[string]interface{}{
49+
"foo": [4]interface{}{1, 2, "true", "false"},
50+
"some": "thing",
51+
}
52+
53+
encryptInput := &vault.EncryptStructuredRequest{
54+
ID: encryptionKeyId,
55+
StructuredData: data,
56+
Filter: "$.foo[2:4]",
57+
}
58+
encryptResponse, err := vaultClient.EncryptStructured(ctx, encryptInput)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
fmt.Println("Encrypted result:", pangea.Stringify(encryptResponse.Result.StructuredData))
63+
}

go.work

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
go 1.18
2+
3+
use (
4+
./examples/vault
5+
./pangea-sdk/v2
6+
./pangea-sdk/v3
7+
)

go.work.sum

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
2+
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
3+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

0 commit comments

Comments
 (0)