| 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 |
"gocloud.dev/secrets" |
| 23 |
_ "gocloud.dev/secrets/localsecrets" |
| 24 |
) |
| 25 |
|
| 26 |
func Example_openFromURL() { |
| 27 |
ctx := context.Background() |
| 28 |
|
| 29 |
// Create a Keeper using a URL. |
| 30 |
// This example uses "localsecrets", the in-memory implementation. |
| 31 |
// We need to add a blank import line to register the localsecrets driver's |
| 32 |
// URLOpener, which implements secrets.KeeperURLOpener: |
| 33 |
// import _ "gocloud.dev/secrets/localsecrets" |
| 34 |
// localsecrets registers for the "base64key" scheme. |
| 35 |
// All secrets.OpenKeeper URLs also work with "secrets+" or "secrets+keeper+" prefixes, |
| 36 |
// e.g., "secrets+base64key://..." or "secrets+variable+base64key://...". |
| 37 |
// All secrets URLs also work with the "secrets+" prefix, e.g., "secrets+base64key://". |
| 38 |
k, err := secrets.OpenKeeper(ctx, "base64key://smGbjm71Nxd1Ig5FS0wj9SlbzAIrnolCz9bQQ6uAhl4=") |
| 39 |
if err != nil { |
| 40 |
log.Fatal(err) |
| 41 |
} |
| 42 |
defer k.Close() |
| 43 |
|
| 44 |
// Now we can use k to encrypt/decrypt. |
| 45 |
plaintext := []byte("Go CDK Secrets") |
| 46 |
ciphertext, err := k.Encrypt(ctx, plaintext) |
| 47 |
if err != nil { |
| 48 |
log.Fatal(err) |
| 49 |
} |
| 50 |
decrypted, err := k.Decrypt(ctx, ciphertext) |
| 51 |
if err != nil { |
| 52 |
log.Fatal(err) |
| 53 |
} |
| 54 |
fmt.Println(string(decrypted)) |
| 55 |
|
| 56 |
// Output: |
| 57 |
// Go CDK Secrets |
| 58 |
} |