| 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 |
// gocdk-secrets demonstrates the use of the Go CDK secrets package in a |
| 16 |
// simple command-line application. |
| 17 |
package main |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/base64" |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"log" |
| 25 |
"os" |
| 26 |
|
| 27 |
"github.com/google/subcommands" |
| 28 |
"gocloud.dev/secrets" |
| 29 |
|
| 30 |
// Import the secrets driver packages we want to be able to open. |
| 31 |
_ "gocloud.dev/secrets/awskms" |
| 32 |
_ "gocloud.dev/secrets/azurekeyvault" |
| 33 |
_ "gocloud.dev/secrets/gcpkms" |
| 34 |
_ "gocloud.dev/secrets/hashivault" |
| 35 |
_ "gocloud.dev/secrets/localsecrets" |
| 36 |
) |
| 37 |
|
| 38 |
const helpSuffix = ` |
| 39 |
|
| 40 |
See https://gocloud.dev/concepts/urls/ for more background on |
| 41 |
Go CDK URLs, and sub-packages under gocloud.dev/secrets |
| 42 |
(https://godoc.org/gocloud.dev/secrets#pkg-subdirectories) |
| 43 |
for details on the secrets.Keeper URL format. |
| 44 |
` |
| 45 |
|
| 46 |
func main() { |
| 47 |
os.Exit(run()) |
| 48 |
} |
| 49 |
|
| 50 |
func run() int { |
| 51 |
subcommands.Register(subcommands.HelpCommand(), "") |
| 52 |
subcommands.Register(&decryptCmd{}, "") |
| 53 |
subcommands.Register(&encryptCmd{}, "") |
| 54 |
log.SetFlags(0) |
| 55 |
log.SetPrefix("gocdk-secrets: ") |
| 56 |
flag.Parse() |
| 57 |
return int(subcommands.Execute(context.Background())) |
| 58 |
} |
| 59 |
|
| 60 |
type decryptCmd struct { |
| 61 |
base64in bool |
| 62 |
base64out bool |
| 63 |
} |
| 64 |
|
| 65 |
func (*decryptCmd) Name() string { return "decrypt" } |
| 66 |
func (*decryptCmd) Synopsis() string { return "Decrypt data" } |
| 67 |
func (*decryptCmd) Usage() string { |
| 68 |
return `decrypt [-base64in] [-base64out] <keeper URL> <ciphertext> |
| 69 |
|
| 70 |
Decrypt the ciphertext using <keeper URL> and print the result to stdout. |
| 71 |
|
| 72 |
Example: |
| 73 |
gocdk-secrets decrypt stringkey://mykey nzam9AJHqH1sqeEr1ZLMbWOf4pp5NRHKYBx/h8loARL83+CBc0WPh8dYzHfccQYFUQ==` + helpSuffix |
| 74 |
} |
| 75 |
|
| 76 |
func (cmd *decryptCmd) SetFlags(f *flag.FlagSet) { |
| 77 |
f.BoolVar(&cmd.base64in, "base64in", true, "the ciphertext is base64 encoded") |
| 78 |
f.BoolVar(&cmd.base64out, "base64out", false, "the resulting plaintext should be base64 encoded before printing it out") |
| 79 |
} |
| 80 |
|
| 81 |
func (cmd *decryptCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 82 |
if f.NArg() != 2 { |
| 83 |
f.Usage() |
| 84 |
return subcommands.ExitUsageError |
| 85 |
} |
| 86 |
keeperURL := f.Arg(0) |
| 87 |
ciphertext := f.Arg(1) |
| 88 |
|
| 89 |
cipher := []byte(ciphertext) |
| 90 |
if cmd.base64in { |
| 91 |
var err error |
| 92 |
cipher, err = base64.StdEncoding.DecodeString(ciphertext) |
| 93 |
if err != nil { |
| 94 |
log.Printf("Failed to base64 decode ciphertext: %v\n", err) |
| 95 |
return subcommands.ExitFailure |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
// Open a *secrets.Keeper using the keeperURL. |
| 100 |
keeper, err := secrets.OpenKeeper(ctx, keeperURL) |
| 101 |
if err != nil { |
| 102 |
log.Printf("Failed to open keeper: %v\n", err) |
| 103 |
return subcommands.ExitFailure |
| 104 |
} |
| 105 |
defer keeper.Close() |
| 106 |
|
| 107 |
plain, err := keeper.Decrypt(ctx, cipher) |
| 108 |
if err != nil { |
| 109 |
log.Printf("Failed to decrypt: %v\n", err) |
| 110 |
return subcommands.ExitFailure |
| 111 |
} |
| 112 |
|
| 113 |
plaintext := string(plain) |
| 114 |
if cmd.base64out { |
| 115 |
plaintext = base64.StdEncoding.EncodeToString(plain) |
| 116 |
} |
| 117 |
fmt.Println(plaintext) |
| 118 |
|
| 119 |
return subcommands.ExitSuccess |
| 120 |
} |
| 121 |
|
| 122 |
type encryptCmd struct { |
| 123 |
base64in bool |
| 124 |
base64out bool |
| 125 |
} |
| 126 |
|
| 127 |
func (*encryptCmd) Name() string { return "encrypt" } |
| 128 |
func (*encryptCmd) Synopsis() string { return "Encrypt data" } |
| 129 |
func (*encryptCmd) Usage() string { |
| 130 |
return `encrypt [-base64in] [-base64out] <keeper URL> <plaintext> |
| 131 |
|
| 132 |
Encrypt the plaintext using <keeper URL> and print the result to stdout. |
| 133 |
|
| 134 |
Example: |
| 135 |
gocdk-secrets encrypt --base64out stringkey://mykey my-plaintext` + helpSuffix |
| 136 |
} |
| 137 |
|
| 138 |
func (cmd *encryptCmd) SetFlags(f *flag.FlagSet) { |
| 139 |
f.BoolVar(&cmd.base64in, "base64in", false, "the plaintext is base64-encoded") |
| 140 |
f.BoolVar(&cmd.base64out, "base64out", true, "the resulting ciphertext should be base64-encoded before printing it out") |
| 141 |
} |
| 142 |
|
| 143 |
func (cmd *encryptCmd) Execute(ctx context.Context, f *flag.FlagSet, _ ...any) subcommands.ExitStatus { |
| 144 |
if f.NArg() != 2 { |
| 145 |
f.Usage() |
| 146 |
return subcommands.ExitUsageError |
| 147 |
} |
| 148 |
keeperURL := f.Arg(0) |
| 149 |
plaintext := f.Arg(1) |
| 150 |
|
| 151 |
plain := []byte(plaintext) |
| 152 |
if cmd.base64in { |
| 153 |
var err error |
| 154 |
plain, err = base64.StdEncoding.DecodeString(plaintext) |
| 155 |
if err != nil { |
| 156 |
log.Printf("Failed to base64 decode plaintext: %v\n", err) |
| 157 |
return subcommands.ExitFailure |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Open a *secrets.Keeper using the keeperURL. |
| 162 |
keeper, err := secrets.OpenKeeper(ctx, keeperURL) |
| 163 |
if err != nil { |
| 164 |
log.Printf("Failed to open keeper: %v\n", err) |
| 165 |
return subcommands.ExitFailure |
| 166 |
} |
| 167 |
defer keeper.Close() |
| 168 |
|
| 169 |
cipher, err := keeper.Encrypt(ctx, plain) |
| 170 |
if err != nil { |
| 171 |
log.Printf("Failed to encrypt: %v\n", err) |
| 172 |
return subcommands.ExitFailure |
| 173 |
} |
| 174 |
|
| 175 |
ciphertext := string(cipher) |
| 176 |
if cmd.base64out { |
| 177 |
ciphertext = base64.StdEncoding.EncodeToString(cipher) |
| 178 |
} |
| 179 |
fmt.Println(ciphertext) |
| 180 |
|
| 181 |
return subcommands.ExitSuccess |
| 182 |
} |