github.com/thiagoyeds/go-cloud@v0.26.0/secrets/awskms/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 awskms_test 16 17 import ( 18 "context" 19 "log" 20 21 awsv2cfg "github.com/aws/aws-sdk-go-v2/config" 22 "github.com/aws/aws-sdk-go/aws/session" 23 "gocloud.dev/secrets" 24 "gocloud.dev/secrets/awskms" 25 ) 26 27 func ExampleOpenKeeper() { 28 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 29 30 // Establish an AWS session. 31 // See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more info. 32 sess, err := session.NewSession(nil) 33 if err != nil { 34 log.Fatal(err) 35 } 36 37 // Get a client to use with the KMS API. 38 client, err := awskms.Dial(sess) 39 if err != nil { 40 log.Fatal(err) 41 } 42 43 // Construct a *secrets.Keeper. 44 keeper := awskms.OpenKeeper(client, "alias/test-secrets", nil) 45 defer keeper.Close() 46 } 47 48 func ExampleOpenKeeperV2() { 49 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 50 51 // Establish a AWS V2 Config. 52 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info. 53 ctx := context.Background() 54 cfg, err := awsv2cfg.LoadDefaultConfig(ctx) 55 if err != nil { 56 log.Fatal(err) 57 } 58 59 // Get a client to use with the KMS API. 60 client, err := awskms.DialV2(cfg) 61 if err != nil { 62 log.Fatal(err) 63 } 64 65 // Construct a *secrets.Keeper. 66 keeper := awskms.OpenKeeperV2(client, "alias/test-secrets", nil) 67 defer keeper.Close() 68 } 69 70 func Example_openFromURL() { 71 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. 72 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/secrets/awskms" 73 // PRAGMA: On gocloud.dev, hide lines until the next blank line. 74 ctx := context.Background() 75 76 // Use one of the following: 77 78 // 1. By ID. 79 keeperByID, err := secrets.OpenKeeper(ctx, 80 "awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1") 81 if err != nil { 82 log.Fatal(err) 83 } 84 defer keeperByID.Close() 85 86 // 2. By alias. 87 keeperByAlias, err := secrets.OpenKeeper(ctx, 88 "awskms://alias/ExampleAlias?region=us-east-1") 89 if err != nil { 90 log.Fatal(err) 91 } 92 defer keeperByAlias.Close() 93 94 // 3. By ARN. 95 const arn = "arn:aws:kms:us-east-1:111122223333:key/" + 96 "1234abcd-12ab-34bc-56ef-1234567890ab" 97 keeperByARN, err := secrets.OpenKeeper(ctx, 98 "awskms://"+arn+"?region=us-east-1") 99 if err != nil { 100 log.Fatal(err) 101 } 102 defer keeperByARN.Close() 103 104 // Use "awssdk=v1" or "v2" to force a specific AWS SDK version. 105 keeperUsingV2, err := secrets.OpenKeeper(ctx, 106 "awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1&awssdk=v2") 107 if err != nil { 108 log.Fatal(err) 109 } 110 defer keeperUsingV2.Close() 111 }