Debian Codegraph @

internal/website/content/howto/secrets/_index.md

raw
1 ---
2 title: "Secrets"
3 date: 2019-03-21T17:42:18-07:00
4 lastmod: 2019-07-29T12:00:00-07:00
5 showInSidenav: true
6 toc: true
7 ---
8
9 The [`secrets` package][] provides access to key management services in a
10 portable way. This guide shows how to work with secrets in the Go CDK.
11
12 <!--more-->
13
14 Cloud applications frequently need to store sensitive information like web
15 API credentials or encryption keys in a medium that is not fully secure. For
16 example, an application that interacts with GitHub needs to store its OAuth2
17 client secret and use it when obtaining end-user credentials. If this
18 information was compromised, it could allow someone else to impersonate the
19 application. In order to keep such information secret and secure, you can
20 encrypt the data, but then you need to worry about rotating the encryption
21 keys and distributing them securely to all of your application servers.
22 Most Cloud providers include a key management service to perform these tasks,
23 usually with hardware-level security and audit logging.
24
25 The [`secrets` package][] supports encryption and decryption operations.
26
27 Subpackages contain driver implementations of secrets for various services,
28 including Cloud and on-prem solutions. You can develop your application
29 locally using [`localsecrets`][], then deploy it to multiple Cloud providers with
30 minimal initialization reconfiguration.
31
32 [`secrets` package]: https://godoc.org/gocloud.dev/secrets
33 [`localsecrets`]: https://godoc.org/gocloud.dev/secrets/localsecrets
34
35 ## Opening a SecretsKeeper {#opening}
36
37 The first step in working with your secrets is
38 to instantiate a portable [`*secrets.Keeper`][] for your service.
39
40 The easiest way to do so is to use [`secrets.OpenKeeper`][] and a service-specific
41 URL pointing to the keeper, making sure you ["blank import"][] the driver
42 package to link it in.
43
44 ```go
45 import (
46 "gocloud.dev/secrets"
47 _ "gocloud.dev/secrets/<driver>"
48 )
49 ...
50 keeper, err := secrets.OpenKeeper(context.Background(), "<driver-url>")
51 if err != nil {
52 return fmt.Errorf("could not open keeper: %v", err)
53 }
54 defer keeper.Close()
55 // keeper is a *secrets.Keeper; see usage below
56 ...
57 ```
58
59 See [Concepts: URLs][] for general background and the [guide below][]
60 for URL usage for each supported service.
61
62 Alternatively, if you need
63 fine-grained control over the connection settings, you can call the constructor
64 function in the driver package directly (like `awskms.OpenKeeper`).
65
66 ```go
67 import "gocloud.dev/secrets/<driver>"
68 ...
69 keeper, err := <driver>.OpenKeeper(...)
70 ...
71 ```
72
73 You may find the [`wire` package][] useful for managing your initialization code
74 when switching between different backing services.
75
76 See the [guide below][] for constructor usage for each supported service.
77
78 [`*secrets.Keeper`]: https://godoc.org/gocloud.dev/secrets#Keeper
79 [`secrets.OpenKeeper`]:
80 https://godoc.org/gocloud.dev/secrets#OpenKeeper
81 ["blank import"]: https://golang.org/doc/effective_go.html#blank_import
82 [Concepts: URLs]: {{< ref "/concepts/urls.md" >}}
83 [guide below]: {{< ref "#services" >}}
84 [`wire` package]: http://github.com/google/wire
85
86 ## Using a SecretsKeeper {#using}
87
88 Once you have [opened a secrets keeper][] for the secrets provider you want,
89 you can encrypt and decrypt small messages using the keeper.
90
91 [opened a secrets keeper]: {{< ref "#opening" >}}
92
93 ### Encrypting Data {#encrypt}
94
95 To encrypt data with a keeper, you call `Encrypt` with the byte slice you
96 want to encrypt.
97
98 {{< goexample src="gocloud.dev/secrets.ExampleKeeper_Encrypt" imports="0" >}}
99
100 ### Decrypting Data {#decrypt}
101
102 To decrypt data with a keeper, you call `Decrypt` with the byte slice you
103 want to decrypt. This should be data that you obtained from a previous call
104 to `Encrypt` with a keeper that uses the same secret material (e.g. two AWS
105 KMS keepers created with the same customer master key ID). The `Decrypt`
106 method will return an error if the input data is corrupted.
107
108 {{< goexample src="gocloud.dev/secrets.ExampleKeeper_Decrypt" imports="0" >}}
109
110 ### Large Messages {#large-messages}
111
112 The secrets keeper API is designed to work with small messages (i.e. <10 KiB
113 in length.) Cloud key management services are high latency; using them for
114 encrypting or decrypting large amounts of data is prohibitively slow (and in
115 some providers not permitted). If you need your application to encrypt or
116 decrypt large amounts of data, you should:
117
118 1. Generate a key for the encryption algorithm (16KiB chunks with
119 [`secretbox`][] is a reasonable approach).
120 2. Encrypt the key with secret keeper.
121 3. Store the encrypted key somewhere accessible to the application.
122
123 When your application needs to encrypt or decrypt a large message:
124
125 1. Decrypt the key from storage using the secret keeper
126 2. Use the decrypted key to encrypt or decrypt the message inside your
127 application.
128
129 [`secretbox`]: https://godoc.org/golang.org/x/crypto/nacl/secretbox
130
131 ### Keep Secrets in Configuration {#runtimevar}
132
133 Once you have [opened a secrets keeper][] for the secrets provider you want,
134 you can use a secrets keeper to access sensitive configuration stored in an
135 encrypted `runtimevar`.
136
137 First, you create a [`*runtimevar.Decoder`][] configured to use your secrets
138 keeper using [`runtimevar.DecryptDecode`][]. In this example, we assume the
139 data is a plain string, but the configuration could be a more structured
140 type.
141
142 {{< goexample src="gocloud.dev/runtimevar.ExampleDecryptDecode" imports="0" >}}
143
144 Then you can pass the decoder to the runtime configuration provider of your
145 choice. See the [Runtime Configuration How-To Guide][] for more on how to set up
146 runtime configuration.
147
148 [opened a secrets keeper]: {{< ref "#opening" >}}
149 [Runtime Configuration How-To Guide]: {{< ref "/howto/runtimevar/_index.md" >}}
150 [`*runtimevar.Decoder`]: https://godoc.org/gocloud.dev/runtimevar#Decoder
151 [`runtimevar.DecryptDecode`]: https://godoc.org/gocloud.dev/runtimevar#DecryptDecode
152
153 ## Other Usage Samples
154
155 * [CLI Sample](https://github.com/google/go-cloud/tree/master/samples/gocdk-secrets)
156 * [Secrets package examples](https://godoc.org/gocloud.dev/secrets#example-package)
157
158 ## Supported Services {#services}
159
160 ### Google Cloud Key Management Service {#gcp}
161
162 The Go CDK can use keys from Google Cloud Platform's [Key Management
163 Service][GCP KMS] (GCP KMS) to keep information secret. GCP KMS URLs are
164 similar to [key resource IDs][].
165
166 [GCP KMS]: https://cloud.google.com/kms/
167 [key resource IDs]: https://cloud.google.com/kms/docs/object-hierarchy#key
168
169 `secrets.OpenKeeper` will use Application Default Credentials; if you have
170 authenticated via [`gcloud auth application-default login`][], it will use those credentials. See
171 [Application Default Credentials][GCP creds] to learn about authentication
172 alternatives, including using environment variables.
173
174 [GCP creds]: https://cloud.google.com/docs/authentication/production
175 [`gcloud auth application-default login`]: https://cloud.google.com/sdk/gcloud/reference/auth/application-default/login
176
177 {{< goexample "gocloud.dev/secrets/gcpkms.Example_openFromURL" >}}
178
179 #### GCP Constructor {#gcp-ctor}
180
181 The [`gcpkms.OpenKeeper`][] constructor opens a GCP KMS key. You must first
182 obtain [GCP credentials][GCP creds] and then create a gRPC connection to GCP KMS.
183
184 {{< goexample "gocloud.dev/secrets/gcpkms.ExampleOpenKeeper" >}}
185
186 [`gcpkms.OpenKeeper`]: https://godoc.org/gocloud.dev/secrets/gcpkms#OpenKeeper
187
188 ### AWS Key Management Service {#aws}
189
190 The Go CDK can use customer master keys from Amazon Web Service's [Key
191 Management Service][AWS KMS] (AWS KMS) to keep information secret. AWS KMS
192 URLs can use the key's ID, alias, or Amazon Resource Name (ARN) to identify
193 the key. You should specify the `region` query parameter to ensure your
194 application connects to the correct region.
195
196 [AWS KMS]: https://aws.amazon.com/kms/
197
198 It will create an AWS Config based on the AWS SDK V2; see [AWS V2 Config][] to learn more.
199
200 [AWS V2 Config]: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/
201
202 {{< goexample "gocloud.dev/secrets/awskms.Example_openFromURL" >}}
203
204 #### AWS Constructor {#aws-ctor}
205
206 The [`awskms.OpenKeeper`][] constructor opens a customer master key. You must
207 first create an AWS Config with the same region as your key and then
208 connect to KMS:
209
210 {{< goexample "gocloud.dev/secrets/awskms.ExampleOpenKeeper" >}}
211
212 [`awskms.OpenKeeper`]: https://godoc.org/gocloud.dev/secrets/awskms#OpenKeeper
213
214 ### Azure KeyVault {#azure}
215
216 The Go CDK can use keys from [Azure KeyVault][] to keep information secret.
217 `secrets.OpenKeeper` will use [default credentials from the environment][Azure
218 Environment Auth], unless you set the environment variable
219 `AZURE_KEYVAULT_AUTH_VIA_CLI` to `true`, in which case it will use
220 credentials from the `az` command line.
221
222 Azure KeyVault URLs are based on the [Azure Key object identifer][Azure Key ID]:
223
224 {{< goexample "gocloud.dev/secrets/azurekeyvault.Example_openFromURL" >}}
225
226 [Azure KeyVault]: https://azure.microsoft.com/en-us/services/key-vault/
227 [Azure Environment Auth]: https://docs.microsoft.com/en-us/go/azure/azure-sdk-go-authorization#use-environment-based-authentication
228 [Azure Key ID]: https://docs.microsoft.com/en-us/azure/key-vault/about-keys-secrets-and-certificates
229
230 #### Azure Constructor {#azure-ctor}
231
232 The [`azurekeyvault.OpenKeeper`][] constructor opens an Azure KeyVault key.
233
234 {{< goexample "gocloud.dev/secrets/azurekeyvault.ExampleOpenKeeper" >}}
235
236 [`azurekeyvault.OpenKeeper`]: https://godoc.org/gocloud.dev/secrets/azurekeyvault#OpenKeeper
237
238 ### HashiCorp Vault {#vault}
239
240 The Go CDK can use the [transit secrets engine][] in [Vault][] to keep
241 information secret. Vault URLs only specify the key ID. The Vault server
242 endpoint and authentication token are specified using the environment
243 variables `VAULT_SERVER_URL` and `VAULT_SERVER_TOKEN`, respectively.
244
245 {{< goexample "gocloud.dev/secrets/hashivault.Example_openFromURL" >}}
246
247 [Vault]: https://www.vaultproject.io/
248 [transit secrets engine]: https://www.vaultproject.io/docs/secrets/transit/index.html
249
250 #### HashiCorp Vault Constructor {#vault-ctor}
251
252 The [`hashivault.OpenKeeper`][] constructor opens a transit secrets engine
253 key. You must first connect to your Vault instance.
254
255 {{< goexample "gocloud.dev/secrets/hashivault.ExampleOpenKeeper" >}}
256
257 [`hashivault.OpenKeeper`]: https://godoc.org/gocloud.dev/secrets/hashivault#OpenKeeper
258
259 ### Local Secrets {#local}
260
261 The Go CDK can use local encryption for keeping secrets. Internally, it uses
262 the [NaCl secret box][] algorithm to perform encryption and authentication.
263
264 {{< goexample "gocloud.dev/secrets/localsecrets.Example_openFromURL" >}}
265
266 [NaCl secret box]: https://godoc.org/golang.org/x/crypto/nacl/secretbox
267
268 #### Local Secrets Constructor {#local-ctor}
269
270 The [`localsecrets.NewKeeper`][] constructor takes in its secret material as
271 a `[]byte`.
272
273 {{< goexample "gocloud.dev/secrets/localsecrets.ExampleNewKeeper" >}}
274
275 [`localsecrets.NewKeeper`]: https://godoc.org/gocloud.dev/secrets/localsecrets#NewKeeper
276