github.com/cornelk/go-cloud@v0.17.1/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  	"github.com/aws/aws-sdk-go/aws/session"
    22  	"github.com/cornelk/go-cloud/secrets"
    23  	"github.com/cornelk/go-cloud/secrets/awskms"
    24  )
    25  
    26  func ExampleOpenKeeper() {
    27  	// PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
    28  
    29  	// Establish an AWS session.
    30  	// See https://docs.aws.amazon.com/sdk-for-go/api/aws/session/ for more info.
    31  	sess, err := session.NewSession(nil)
    32  	if err != nil {
    33  		log.Fatal(err)
    34  	}
    35  
    36  	// Get a client to use with the KMS API.
    37  	client, err := awskms.Dial(sess)
    38  	if err != nil {
    39  		log.Fatal(err)
    40  	}
    41  
    42  	// Construct a *secrets.Keeper.
    43  	keeper := awskms.OpenKeeper(client, "alias/test-secrets", nil)
    44  	defer keeper.Close()
    45  }
    46  
    47  func Example_openFromURL() {
    48  	// PRAGMA: This example is used on github.com/cornelk/go-cloud; PRAGMA comments adjust how it is shown and can be ignored.
    49  	// PRAGMA: On github.com/cornelk/go-cloud, add a blank import: _ "github.com/cornelk/go-cloud/secrets/awskms"
    50  	// PRAGMA: On github.com/cornelk/go-cloud, hide lines until the next blank line.
    51  	ctx := context.Background()
    52  
    53  	// Use one of the following:
    54  
    55  	// 1. By ID.
    56  	keeperByID, err := secrets.OpenKeeper(ctx,
    57  		"awskms://1234abcd-12ab-34cd-56ef-1234567890ab?region=us-east-1")
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  	defer keeperByID.Close()
    62  
    63  	// 2. By alias.
    64  	keeperByAlias, err := secrets.OpenKeeper(ctx,
    65  		"awskms://alias/ExampleAlias?region=us-east-1")
    66  	if err != nil {
    67  		log.Fatal(err)
    68  	}
    69  	defer keeperByAlias.Close()
    70  
    71  	// 3. By ARN.
    72  	const arn = "arn:aws:kms:us-east-1:111122223333:key/" +
    73  		"1234abcd-12ab-34bc-56ef-1234567890ab"
    74  	keeperByARN, err := secrets.OpenKeeper(ctx,
    75  		"awskms://"+arn+"?region=us-east-1")
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	defer keeperByARN.Close()
    80  }