Debian Codegraph @

integration/hcvault/hcvault_client_test.go

raw
1 // Copyright 2019 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package hcvault_test
16
17 import (
18 "crypto/tls"
19 "log"
20 "net/http"
21
22 "github.com/hashicorp/vault/api"
23 "github.com/tink-crypto/tink-go/v2/aead"
24 "github.com/tink-crypto/tink-go-hcvault/v2/integration/hcvault"
25 )
26
27 func Example() {
28 const keyURI = "hcvault://hcvault.corp.com:8200/transit/keys/key-1"
29
30 httpClient := api.DefaultConfig().HttpClient
31 transport := httpClient.Transport.(*http.Transport)
32 transport.TLSClientConfig = tlsConfig()
33
34 cfg := &api.Config{
35 Address: "https://hcvault.corp.com:8200",
36 HttpClient: httpClient,
37 }
38 apiClient, err := api.NewClient(cfg)
39 if err != nil {
40 log.Fatal(err)
41 }
42 apiClient.SetToken(vaultToken())
43
44 kmsClient, err := hcvault.NewClientWithAEADOptions(keyURI, apiClient.Logical())
45 if err != nil {
46 log.Fatal(err)
47 }
48 kekAEAD, err := kmsClient.GetAEAD(keyURI)
49 if err != nil {
50 log.Fatal(err)
51 }
52 dekTemplate := aead.AES128CTRHMACSHA256KeyTemplate()
53 a := aead.NewKMSEnvelopeAEAD2(dekTemplate, kekAEAD)
54 if err != nil {
55 log.Fatal(err)
56 }
57 if err != nil {
58 log.Fatal(err)
59 }
60
61 plaintext := []byte("plaintext")
62 associatedData := []byte("associatedData")
63
64 ciphertext, err := a.Encrypt(plaintext, associatedData)
65 if err != nil {
66 log.Fatal(err)
67 }
68
69 _, err = a.Decrypt(ciphertext, associatedData)
70 if err != nil {
71 log.Fatal(err)
72 }
73 }
74
75 func tlsConfig() *tls.Config {
76 // Return a TLS configuration used to communicate with Vault server via HTTPS.
77 return nil
78 }
79
80 func vaultToken() string {
81 return "" // Your Vault token.
82 }