-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
192 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package paramcopy | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ssm" | ||
) | ||
|
||
var service = createSSMService() | ||
|
||
var usage = fmt.Sprintf("Usage: \n %s paramter_name", path.Base(os.Args[0])) | ||
|
||
func getName() string { | ||
if flag.NArg() < 1 { | ||
exitErrorf("Parameter name required.\n%s", usage) | ||
} else if flag.NArg() > 1 { | ||
exitErrorf("Too many parameters.\n%s", usage) | ||
} | ||
|
||
return flag.Arg(0) | ||
} | ||
|
||
func GetDecryptedParameter(name string) string { | ||
output, err := service.GetParameter(&ssm.GetParameterInput{ | ||
Name: aws.String(name), | ||
WithDecryption: aws.Bool(true), | ||
}) | ||
if err != nil { | ||
exitErrorf("Unable to describe parameters, %v", err) | ||
} | ||
return *output.Parameter.Value | ||
} | ||
|
||
func createSSMService() *ssm.SSM { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String("eu-west-1")}, | ||
) | ||
|
||
if err != nil { | ||
exitErrorf("Unable to describe parameters, %v", err) | ||
} | ||
|
||
// Create SSM service client | ||
return ssm.New(sess) | ||
} | ||
|
||
func exitErrorf(msg string, args ...interface{}) { | ||
fmt.Fprintf(os.Stderr, msg+"\n", args...) | ||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package paramlist | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ssm" | ||
"github.com/droundy/goopt" | ||
"os" | ||
"sort" | ||
) | ||
|
||
var service = createSSMService() | ||
|
||
func createSSMService() *ssm.SSM { | ||
sess, err := session.NewSession(&aws.Config{ | ||
Region: aws.String("eu-west-1")}, | ||
) | ||
|
||
if err != nil { | ||
exitErrorf("Unable to describe parameters, %v", err) | ||
} | ||
|
||
// Create SSM service client | ||
return ssm.New(sess) | ||
} | ||
|
||
func initGoOpt() { | ||
goopt.Description = func() string { | ||
return "Get parameter names from AWS SSM with optional prefixes." | ||
} | ||
goopt.Version = version | ||
goopt.Summary = "paramlist [prefixes]" | ||
goopt.Parse(nil) | ||
} | ||
|
||
func DescribeParameters(prefixes []string) []string { | ||
paramNames := []string{} | ||
if len(prefixes) <= 0 { | ||
paramNames = getAllParamNames() | ||
} else { | ||
for _, prefix := range prefixes { | ||
for _, name := range getParamNames(prefix) { | ||
paramNames = appendIfMissing(paramNames, name) | ||
} | ||
} | ||
} | ||
sort.Strings(paramNames) | ||
return paramNames | ||
} | ||
|
||
func getParamNames(prefix string) []string { | ||
// Fix passing empty string to SSM API | ||
if len(prefix) < 1 { | ||
return getAllParamNames() | ||
} | ||
|
||
filters := []*ssm.ParametersFilter{&ssm.ParametersFilter{ | ||
Key: aws.String("Name"), | ||
Values: []*string{aws.String(prefix)}, | ||
}} | ||
paramNames := []string{} | ||
err := service.DescribeParametersPages(&ssm.DescribeParametersInput{ | ||
Filters: filters}, | ||
func(page *ssm.DescribeParametersOutput, lastPage bool) bool { | ||
for _, parameter := range page.Parameters { | ||
paramNames = append(paramNames, aws.StringValue(parameter.Name)) | ||
} | ||
return true | ||
}) | ||
|
||
if err != nil { | ||
exitErrorf("Unable to describe parameters, %v", err) | ||
} | ||
|
||
return paramNames | ||
} | ||
|
||
func getAllParamNames() []string { | ||
return getParamNames(" ") | ||
} | ||
|
||
func appendIfMissing(slice []string, s string) []string { | ||
for _, ele := range slice { | ||
if ele == s { | ||
return slice | ||
} | ||
} | ||
return append(slice, s) | ||
} | ||
|
||
func exitErrorf(msg string, args ...interface{}) { | ||
fmt.Fprintf(os.Stderr, msg+"\n", args...) | ||
os.Exit(1) | ||
} | ||
|
||
var version string = "πππ―π₯π₯π" |