github.com/cornelk/go-cloud@v0.17.1/secrets/example_test.go (about) 1 // Copyright 2019 The Go Cloud Development Kit Authors 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 // https://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 secrets_test 16 17 import ( 18 "context" 19 "fmt" 20 "log" 21 22 "github.com/cornelk/go-cloud/secrets" 23 _ "github.com/cornelk/go-cloud/secrets/gcpkms" 24 "github.com/cornelk/go-cloud/secrets/localsecrets" 25 "google.golang.org/grpc/status" 26 ) 27 28 func Example() { 29 ctx := context.Background() 30 31 // Construct a *secrets.Keeper from one of the secrets subpackages. 32 // This example uses localsecrets. 33 sk, err := localsecrets.NewRandomKey() 34 if err != nil { 35 log.Fatal(err) 36 } 37 keeper := localsecrets.NewKeeper(sk) 38 defer keeper.Close() 39 40 // Now we can use keeper to Encrypt. 41 plaintext := []byte("Go CDK Secrets") 42 ciphertext, err := keeper.Encrypt(ctx, plaintext) 43 if err != nil { 44 log.Fatal(err) 45 } 46 47 // And/or Decrypt. 48 decrypted, err := keeper.Decrypt(ctx, ciphertext) 49 if err != nil { 50 log.Fatal(err) 51 } 52 fmt.Println(string(decrypted)) 53 54 // Output: 55 // Go CDK Secrets 56 } 57 58 func Example_errorAs() { 59 // This example is specific to the gcpkms implementation; it 60 // demonstrates access to the underlying google.golang.org/grpc/status.Status 61 // type. 62 // The types exposed for As by gcpkms are documented in 63 // https://godoc.org/github.com/cornelk/go-cloud/secrets/gcpkms#hdr-As 64 ctx := context.Background() 65 66 const url = "gcpkms://projects/proj/locations/global/keyRings/test/ring/wrongkey" 67 keeper, err := secrets.OpenKeeper(ctx, url) 68 if err != nil { 69 log.Fatal(err) 70 } 71 defer keeper.Close() 72 73 plaintext := []byte("Go CDK secrets") 74 _, err = keeper.Encrypt(ctx, plaintext) 75 if err != nil { 76 var s *status.Status 77 if keeper.ErrorAs(err, &s) { 78 fmt.Println(s.Code()) 79 } 80 } 81 } 82 83 func ExampleKeeper_Encrypt() { 84 // PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored. 85 // PRAGMA: On github.com/cornelk/go-cloud, hide lines until the next blank line. 86 ctx := context.Background() 87 var keeper *secrets.Keeper 88 89 plainText := []byte("Secrets secrets...") 90 cipherText, err := keeper.Encrypt(ctx, plainText) 91 if err != nil { 92 log.Fatal(err) 93 } 94 95 // PRAGMA: On github.com/cornelk/go-cloud, hide the rest of the function. 96 _ = cipherText 97 } 98 99 func ExampleKeeper_Decrypt() { 100 // PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored. 101 // PRAGMA: On github.com/cornelk/go-cloud, hide lines until the next blank line. 102 ctx := context.Background() 103 var keeper *secrets.Keeper 104 105 var cipherText []byte // obtained from elsewhere and random-looking 106 plainText, err := keeper.Decrypt(ctx, cipherText) 107 if err != nil { 108 log.Fatal(err) 109 } 110 111 // PRAGMA: On github.com/cornelk/go-cloud, hide the rest of the function. 112 _ = plainText 113 }