Debian Codegraph @

secrets/awskms/example_test.go

raw
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 "github.com/aws/aws-sdk-go-v2/config"
22 "gocloud.dev/secrets"
23 "gocloud.dev/secrets/awskms"
24 )
25
26 func ExampleOpenKeeper() {
27 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
28
29 // Establish a AWS V2 Config.
30 // See https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/ for more info.
31 ctx := context.Background()
32 cfg, err := config.LoadDefaultConfig(ctx)
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(cfg)
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 Example_openFromURL() {
49 // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
50 // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/secrets/awskms"
51 // PRAGMA: On gocloud.dev, hide lines until the next blank line.
52 ctx := context.Background()
53
54 // Use one of the following:
55
56 // 1. By ID.
57 keeperByID, err := secrets.OpenKeeper(ctx,
58 "awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1")
59 if err != nil {
60 log.Fatal(err)
61 }
62 defer keeperByID.Close()
63
64 // 2. By alias.
65 keeperByAlias, err := secrets.OpenKeeper(ctx,
66 "awskms://alias/ExampleAlias?region=us-east-1")
67 if err != nil {
68 log.Fatal(err)
69 }
70 defer keeperByAlias.Close()
71
72 // 3. By ARN. Note that ARN may contain ":" characters, which cannot be escaped
73 // in the Host part of a URL, so the "awskms:///<ARN>" form should be used.
74 const arn = "arn:aws:kms:us-east-1:111122223333:key/" +
75 "1234abcd-12ab-34bc-56ef-1234567890ab"
76 keeperByARN, err := secrets.OpenKeeper(ctx,
77 "awskms:///"+arn+"?region=us-east-1")
78 if err != nil {
79 log.Fatal(err)
80 }
81 defer keeperByARN.Close()
82 }