github.com/yorinasub17/go-cloud@v0.27.40/secrets/localsecrets/example_test.go (about)

     1  // Copyright 2018 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 localsecrets_test
    16  
    17  import (
    18  	"context"
    19  	"log"
    20  
    21  	"gocloud.dev/secrets"
    22  	"gocloud.dev/secrets/localsecrets"
    23  )
    24  
    25  func ExampleNewKeeper() {
    26  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    27  
    28  	secretKey, err := localsecrets.NewRandomKey()
    29  	if err != nil {
    30  		log.Fatal(err)
    31  	}
    32  	keeper := localsecrets.NewKeeper(secretKey)
    33  	defer keeper.Close()
    34  }
    35  
    36  func Example_openFromURL() {
    37  	// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored.
    38  
    39  	// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/secrets/localsecrets"
    40  
    41  	// PRAGMA: On gocloud.dev, hide lines until the next blank line.
    42  	ctx := context.Background()
    43  
    44  	// Using "base64key://", a new random key will be generated.
    45  	randomKeyKeeper, err := secrets.OpenKeeper(ctx, "base64key://")
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  	defer randomKeyKeeper.Close()
    50  
    51  	// Otherwise, the URL hostname must be a base64-encoded key, of length 32 bytes when decoded.
    52  	// Note that base64.URLEncode should be used, to avoid URL-unsafe characters.
    53  	savedKeyKeeper, err := secrets.OpenKeeper(ctx, "base64key://smGbjm71Nxd1Ig5FS0wj9SlbzAIrnolCz9bQQ6uAhl4=")
    54  	if err != nil {
    55  		log.Fatal(err)
    56  	}
    57  	defer savedKeyKeeper.Close()
    58  }