Skip to content

Commit 6e3f243

Browse files
authored
Merge branch 'master' into doug-sts-gov2
2 parents d81e7d3 + 8a6873b commit 6e3f243

File tree

307 files changed

+14065
-12467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

307 files changed

+14065
-12467
lines changed

README.rst

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,21 @@
77
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
88
either express or implied. See the License for the specific language governing permissions and
99
limitations under the License.
10+
1011
Welcome
1112
=======
1213
This is the repository for code examples used in the public
13-
`AWS documentation <https://docs.aws.amazon.com>`_. The examples use the AWS SDKs for the supported programming languages. For more information, see `Tools to Build on
14-
AWS <https://aws.amazon.com/getting-started/tools-sdks/>`_.
14+
`AWS documentation <https://docs.aws.amazon.com>`_. The examples use the AWS SDKs for the supported
15+
programming languages. For more information, see
16+
`Tools to Build on AWS <https://aws.amazon.com/getting-started/tools-sdks/>`_.
1517

1618
Finding code examples
1719
=====================
1820

19-
The code examples are organized by programming language. For example, all of the code examples for the V1 version of the
20-
`AWS SDK for Java Developer Guide <https://www.github.com/awsdocs/aws-java-developer-guide>`_ are
21-
kept in the `java <java>`_ directory (the V2 versions are in the `java2 <java2>`_ directory).
21+
The code examples are organized by programming language. For example, all of the code examples for
22+
the latest version of the `AWS SDK for Java 2.x Developer Guide <https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide>`_
23+
are kept in the `javav2 <javav2>`_ directory (the version 1.x examples are in the `java <java>`_
24+
directory).
2225

2326
You can also try using the preview of our `use case index of examples <code-index.md>`_ (and let us
2427
know what you think about it).
@@ -42,11 +45,11 @@ request <https://github.com/awsdocs/aws-doc-sdk-examples/issues/new?assignees=&l
4245
The AWS documentation team wants to produce code examples that cover broader scenarios and use
4346
cases, versus simple code snippets that cover only individual API calls.
4447

45-
From time to time, the AWS documentation team will select some of these proposals to begin working on.
46-
To view their decisions, see the `code examples roadmap <https://github.com/awsdocs/aws-doc-sdk-examples/projects/2>`_.
47-
If you feel strongly about wanting to accelerate the timeline of a particular proposal or expand or focus the scope of a
48-
particular proposal, be sure to +1 the related issue, add comments to it, or both,
49-
and the team will consider it.
48+
From time to time, the AWS documentation team will select some of these proposals to begin working
49+
on. To view their decisions, see the `code examples roadmap <https://github.com/awsdocs/aws-doc-sdk-examples/projects/2>`_.
50+
If you feel strongly about wanting to accelerate the timeline of a particular proposal or expand
51+
or focus the scope of a particular proposal, be sure to +1 the related issue, add comments to it,
52+
or both, and the team will consider it.
5053

5154
The AWS documentation team typically moves accepted proposals in the roadmp from the **Wish List**
5255
stage to the **Backlog** stage, then to **In Progress**, and finally to **Recently
@@ -62,8 +65,9 @@ If you plan to contribute examples for use in the documentation (the purpose of
6265
read this section carefully so that we can work together effectively.
6366
For process instructions and additional guidance, see the `Guidelines for contributing <CONTRIBUTING.md>`_.
6467

65-
* **Make sure that the code you want to contribute builds and runs**. There's nothing more frustrating in developer
66-
documentation than code examples that don't work. Build the code and test it before submitting it!
68+
* **Make sure that the code you want to contribute builds and runs**. There's nothing more
69+
frustrating in developer documentation than code examples that don't work. Build the code and
70+
test it before submitting it!
6771

6872
* **Don't include personal account data, keys, or IDs in your examples**. Code should obtain access
6973
keys from the standard SDK credentials and config files, use environment variables or external
@@ -74,10 +78,10 @@ For process instructions and additional guidance, see the `Guidelines for contri
7478
unreadable. If your code includes long text strings, consider breaking these into smaller chunks
7579
and concatenating them.
7680

77-
* **Use short(er) variable names**. To aid in readability and to help keep line length to 80 characters, use
78-
*short yet descriptive* names for variables. Do *not* mimic class names when creating
79-
variables that represent an object of that class. It nearly always results in excessively long
80-
variable names, making it difficult to keep code lines to 80 characters.
81+
* **Use short(er) variable names**. To aid in readability and to help keep line length to 80
82+
characters, use *short yet descriptive* names for variables. Do *not* mimic class names when
83+
creating variables that represent an object of that class. It nearly always results in excessively
84+
long variable names, making it difficult to keep code lines to 80 characters.
8185

8286
* **Use spaces, not tabs, for indentation**. Tabs are variable length in most editors, but will
8387
usually render as 8 characters wide in printed documentation. *Always use spaces* to ensure

gov2/kms/CreateKey/CreateKeyv2.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
// snippet-start:[kms-go-v2.CreateKey]
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
11+
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/service/kms"
13+
"github.com/aws/aws-sdk-go-v2/service/kms/types"
14+
)
15+
16+
// KMSCreateKeyAPI defines the interface for the CreateKey function.
17+
// We use this interface to test the function using a mocked service.
18+
type KMSCreateKeyAPI interface {
19+
CreateKey(ctx context.Context,
20+
params *kms.CreateKeyInput,
21+
optFns ...func(*kms.Options)) (*kms.CreateKeyOutput, error)
22+
}
23+
24+
// MakeKey creates an AWS Key Management Service (AWS KMS) customer master key (CMK).
25+
// Inputs:
26+
// c is the context of the method call, which includes the AWS Region.
27+
// api is the interface that defines the method call.
28+
// input defines the input arguments to the service call.
29+
// Output:
30+
// If success, a CreateKeyOutput object containing the result of the service call and nil.
31+
// Otherwise, nil and an error from the call to CreateKey.
32+
func MakeKey(c context.Context, api KMSCreateKeyAPI, input *kms.CreateKeyInput) (*kms.CreateKeyOutput, error) {
33+
result, err := api.CreateKey(c, input)
34+
35+
return result, err
36+
}
37+
38+
func main() {
39+
key := flag.String("k", "", "The KMS key name")
40+
value := flag.String("v", "", "The value of the KMS key")
41+
flag.Parse()
42+
43+
if *key == "" || *value == "" {
44+
fmt.Println("You must supply a KMS key name and value (-k KEY-NAME -v KEY-VALUE)")
45+
return
46+
}
47+
48+
cfg, err := config.LoadDefaultConfig()
49+
if err != nil {
50+
panic("configuration error, " + err.Error())
51+
}
52+
53+
client := kms.NewFromConfig(cfg)
54+
55+
input := &kms.CreateKeyInput{
56+
Tags: []*types.Tag{
57+
{
58+
TagKey: key,
59+
TagValue: value,
60+
},
61+
},
62+
}
63+
64+
result, err := MakeKey(context.Background(), client, input)
65+
if err != nil {
66+
fmt.Println("Got error creating key:")
67+
fmt.Println(err)
68+
return
69+
}
70+
71+
fmt.Println(*result.KeyMetadata.KeyId)
72+
}
73+
74+
// snippet-end:[kms-go-v2.CreateKey]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io/ioutil"
9+
"testing"
10+
"time"
11+
12+
"github.com/aws/aws-sdk-go-v2/service/kms"
13+
"github.com/aws/aws-sdk-go-v2/service/kms/types"
14+
"github.com/aws/aws-sdk-go/aws"
15+
)
16+
17+
type KMSCreateKeyImpl struct{}
18+
19+
func (dt KMSCreateKeyImpl) CreateKey(ctx context.Context,
20+
params *kms.CreateKeyInput,
21+
optFns ...func(*kms.Options)) (*kms.CreateKeyOutput, error) {
22+
23+
if len(params.Tags) == 0 {
24+
return nil, errors.New("You must supply at least one tag value")
25+
}
26+
27+
keyMetadata := &types.KeyMetadata{KeyId: aws.String("aws-docs-example-kmskeyID")}
28+
29+
output := &kms.CreateKeyOutput{
30+
KeyMetadata: keyMetadata,
31+
}
32+
33+
return output, nil
34+
}
35+
36+
type Config struct {
37+
Key string `json:"Key"`
38+
Value string `json:"Value"`
39+
}
40+
41+
var configFileName = "config.json"
42+
43+
var globalConfig Config
44+
45+
func populateConfiguration(t *testing.T) error {
46+
content, err := ioutil.ReadFile(configFileName)
47+
if err != nil {
48+
return err
49+
}
50+
51+
text := string(content)
52+
53+
err = json.Unmarshal([]byte(text), &globalConfig)
54+
if err != nil {
55+
return err
56+
}
57+
58+
if globalConfig.Key == "" || globalConfig.Value == "" {
59+
msg := "You must supply a value for Key and Value in " + configFileName
60+
return errors.New(msg)
61+
}
62+
63+
return nil
64+
}
65+
66+
func TestCreateKey(t *testing.T) {
67+
thisTime := time.Now()
68+
nowString := thisTime.Format("2006-01-02 15:04:05 Monday")
69+
t.Log("Starting unit test at " + nowString)
70+
71+
err := populateConfiguration(t)
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
api := &KMSCreateKeyImpl{}
77+
78+
input := &kms.CreateKeyInput{
79+
Tags: []*types.Tag{
80+
{
81+
TagKey: &globalConfig.Key,
82+
TagValue: &globalConfig.Value,
83+
},
84+
},
85+
}
86+
87+
result, err := MakeKey(context.Background(), api, input)
88+
if err != nil {
89+
fmt.Println("Got error creating key:")
90+
fmt.Println(err)
91+
return
92+
}
93+
94+
t.Log("KeyID: " + *result.KeyMetadata.KeyId)
95+
}

gov2/kms/CreateKey/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### CreateKeyv2.go
2+
3+
This example creates an AWS Key Management Service (AWS KMS) customer master key (CMK).
4+
5+
`go run CreateKeyv2.go -k KEY -v VALUE`
6+
7+
- _KEY_ is the name of the key to create.
8+
9+
The unit test accepts similar values in _config.json_.

gov2/kms/CreateKey/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Key": "aws-docs-example-kmskey-key",
3+
"Value": "aws-docs-example-kmskey-value"
4+
}

gov2/kms/DecryptData/DecryptDatav2.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX - License - Identifier: Apache - 2.0
3+
// snippet-start:[kms.go-v2.DecryptData]
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
11+
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/service/kms"
13+
)
14+
15+
// KMSDecryptAPI defines the interface for the Decrypt function.
16+
// We use this interface to test the function using a mocked service.
17+
type KMSDecryptAPI interface {
18+
Decrypt(ctx context.Context,
19+
params *kms.DecryptInput,
20+
optFns ...func(*kms.Options)) (*kms.DecryptOutput, error)
21+
}
22+
23+
// DecodeData decrypts some text that was encrypted with an AWS Key Management Service (AWS KMS) customer master key (CMK).
24+
// Inputs:
25+
// c is the context of the method call, which includes the AWS Region.
26+
// api is the interface that defines the method call.
27+
// input defines the input arguments to the service call.
28+
// Output:
29+
// If success, a METHODOutput object containing the result of the service call and nil.
30+
// Otherwise, nil and an error from the call to Decrypt.
31+
func DecodeData(c context.Context, api KMSDecryptAPI, input *kms.DecryptInput) (*kms.DecryptOutput, error) {
32+
result, err := api.Decrypt(c, input)
33+
34+
return result, err
35+
}
36+
37+
func main() {
38+
data := flag.String("d", "", "The encrypted data, as a string")
39+
flag.Parse()
40+
41+
if *data == "" {
42+
fmt.Println("You must supply the encrypted data as a string")
43+
fmt.Println("-d DATA")
44+
return
45+
}
46+
47+
cfg, err := config.LoadDefaultConfig()
48+
if err != nil {
49+
panic("configuration error, " + err.Error())
50+
}
51+
52+
client := kms.NewFromConfig(cfg)
53+
54+
blob := []byte(*data)
55+
56+
input := &kms.DecryptInput{
57+
CiphertextBlob: blob,
58+
}
59+
60+
result, err := DecodeData(context.Background(), client, input)
61+
if err != nil {
62+
fmt.Println("Got error decrypting data: ", err)
63+
return
64+
}
65+
66+
fmt.Println(string(result.Plaintext))
67+
}
68+
69+
// snippet-end:[kms.go-v2.DecryptData]

0 commit comments

Comments
 (0)