forked from aws/amazon-ecs-agent
/
ssm.go
77 lines (64 loc) · 2.21 KB
/
ssm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package ssm
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/pkg/errors"
)
// GetSecretFromSSM makes the api call to the AWS SSM parameter store to
// retrieve secrets value in batches
func GetSecretsFromSSM(names []string, client SSMClient) (map[string]string, error) {
return getParameters(names, client, true)
}
// GetParametersFromSSM makes the api call to the AWS SSM parameter store to
// retrieve parameter value in batches
func GetParametersFromSSM(names []string, client SSMClient) (map[string]string, error) {
return getParameters(names, client, false)
}
func getParameters(names []string, client SSMClient, withDecryption bool) (map[string]string, error) {
var params []*string
for _, name := range names {
params = append(params, aws.String(name))
}
in := &ssm.GetParametersInput{
Names: params,
WithDecryption: aws.Bool(withDecryption),
}
out, err := client.GetParameters(in)
if err != nil {
return nil, err
}
return extractSSMValues(out)
}
func extractSSMValues(out *ssm.GetParametersOutput) (map[string]string, error) {
if out == nil {
return nil, errors.New(
"empty response")
}
if len(out.InvalidParameters) != 0 {
var stringValues []string
for _, invalid := range out.InvalidParameters {
stringValues = append(stringValues, aws.StringValue(invalid))
}
return nil, fmt.Errorf(
"invalid parameters: %s", strings.Join(stringValues, ","))
}
parameterValues := make(map[string]string)
for _, parameter := range out.Parameters {
parameterValues[aws.StringValue(parameter.Name)] = aws.StringValue(parameter.Value)
}
return parameterValues, nil
}