Skip to content

Commit c6419e1

Browse files
committed
Removed Flags struct, extended with comments and aws_ssm example in README
1 parent e3b6f4a commit c6419e1

8 files changed

Lines changed: 50 additions & 194 deletions

File tree

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ except they explicitly `export` the environment variables. We source these envir
2020
run the development versions of our applications.
2121

2222
It can sometimes be difficult to understand:
23-
1. Whether all the environment variables we *expect* to be defined in production actually have been.
23+
24+
1. Whether all the environment variables we _expect_ to be defined in production actually have been.
2425
2. What the particular value of a production environment actually is.
2526
3. What the differences are between our expectations and the actual environment variables in a running
26-
application process.
27+
application process.
2728

2829
We are building and maintaining `checkenv` to make it easier for us to diagnose and fix issues with
2930
application configuration via environment variables. We stand in solidarity with anyone else who
@@ -40,3 +41,24 @@ binary which supports your needs.
4041

4142
There is currently no need to support runtime plugins. Since doing so would make this program a lot
4243
more complicated, we have decided to forego runtime plugin functionality for now.
44+
45+
## Usage
46+
47+
```bash
48+
./checkenv plugins
49+
```
50+
51+
Available plugins:
52+
53+
- env - Provides the environment variables defined in the checkenv process.
54+
- file - Provides the environment variables defined in the env file with the given path.
55+
- proc - Provides the environment variables set for the process with the given pid.
56+
- aws_ssm - Provides environment variables defined in AWS Systems Manager Parameter Store.
57+
58+
### aws_ssm plugin
59+
60+
In order to fetch parameters with tags `Product` = `test` and `Node` = `true` with `export ` prefix execute following command
61+
62+
```bash
63+
./checkenv show -export aws_ssm+Product:test,Node:true
64+
```

aws_ssm.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// checkenv plugin that provides the environment variables defined in the checkenv process.
1+
// checkenv plugin that provides environment variables defined in AWS System Manager Parameter Store.
22

33
package main
44

@@ -11,19 +11,20 @@ import (
1111
func AWSSystemsManagerParameterStoreProvider(filter string) (map[string]string, error) {
1212
environment := make(map[string]string)
1313

14-
AWSSystemsManagerFlags := aws_ssm.Flags{
15-
Export: false,
16-
MaxResults: 10,
17-
Outfile: "",
18-
Update: false,
19-
}
20-
AWSSystemsManagerFlags.FilterTags = aws_ssm.ParseFilterTags(filter)
14+
// Convert string of tags for filter to key:value structure
15+
filterTags := aws_ssm.ParseFilterTags(filter)
2116

2217
ctx := context.Background()
18+
2319
api := aws_ssm.InitAWSClient(ctx)
24-
keys := aws_ssm.FetchKeysOfParameters(ctx, api, AWSSystemsManagerFlags)
20+
21+
keys := aws_ssm.FetchKeysOfParameters(ctx, api, filterTags)
22+
23+
// Split slice of parameter keys to chunks by 10 (max len allowed by AWS)
24+
// and fetch values for required parameters
2525
keyChunks := aws_ssm.GenerateChunks(keys, 10)
26-
parameters := aws_ssm.FetchParameters(ctx, api, keyChunks, AWSSystemsManagerFlags)
26+
parameters := aws_ssm.FetchParameters(ctx, api, keyChunks)
27+
2728
for _, parameter := range parameters {
2829
environment[parameter.Name] = parameter.Value
2930
}

aws_ssm/aws_ssm.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
"github.com/aws/aws-sdk-go-v2/service/ssm"
1212
)
1313

14-
type AWSSystemsManagerParameterStore interface {
14+
// AWSSystemsManagerParameterStoreAPI defines the interface
15+
// for the GetParameters and DescribeParameters function.
16+
// We use this interface to test the function using a mocked service
17+
type AWSSystemsManagerParameterStoreAPI interface {
1518
GetParameters(
1619
ctx context.Context,
1720
params *ssm.GetParametersInput,
@@ -33,11 +36,11 @@ type AWSSystemsManagerParameterStore interface {
3336
// Output:
3437
// If success, a GetParametersOutput object containing the result of the service call and nil
3538
// Otherwise, nil and an error from the call to GetParameters
36-
func ExecGetParameters(c context.Context, api AWSSystemsManagerParameterStore, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
39+
func ExecGetParameters(c context.Context, api AWSSystemsManagerParameterStoreAPI, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
3740
return api.GetParameters(c, input)
3841
}
3942

40-
func ExecDescribeParameters(c context.Context, api AWSSystemsManagerParameterStore, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
43+
func ExecDescribeParameters(c context.Context, api AWSSystemsManagerParameterStoreAPI, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
4144
return api.DescribeParameters(c, input)
4245
}
4346

aws_ssm/aws_ssm_test.go

Lines changed: 0 additions & 129 deletions
This file was deleted.

aws_ssm/data.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,12 @@ package aws_ssm
22

33
// Parameter structure for storing final result from AWS SSM
44
type Parameter struct {
5-
Name string
6-
Value string
7-
Export string
5+
Name string
6+
Value string
87
}
98

109
// Tags for filter defined by user
1110
type FilterTag struct {
1211
Name string
1312
Value string
1413
}
15-
16-
// Contains command-line flags defined by user
17-
type Flags struct {
18-
Export bool
19-
MaxResults int
20-
Outfile string
21-
FilterTags []FilterTag
22-
Update bool
23-
}

aws_ssm/parameters.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"os"
87
"strings"
98

109
"github.com/aws/aws-sdk-go-v2/service/ssm"
@@ -14,7 +13,7 @@ import (
1413
// Fetch values for parameters
1514
// Inputs:
1615
// chunks: list of lists with parameter key values
17-
func FetchParameters(ctx context.Context, api AWSSystemsManagerParameterStore, chunks [][]string, flags Flags) []Parameter {
16+
func FetchParameters(ctx context.Context, api AWSSystemsManagerParameterStoreAPI, chunks [][]string) []Parameter {
1817
var parameters []Parameter
1918

2019
for _, chunk := range chunks {
@@ -30,9 +29,6 @@ func FetchParameters(ctx context.Context, api AWSSystemsManagerParameterStore, c
3029
parameter := Parameter{
3130
Name: *p.Name, Value: *p.Value,
3231
}
33-
if flags.Export {
34-
parameter.Export = "export "
35-
}
3632
parameters = append(parameters, parameter)
3733
}
3834
}
@@ -44,22 +40,22 @@ func FetchParameters(ctx context.Context, api AWSSystemsManagerParameterStore, c
4440
// Fetch list of parameter keys from AWS with defined filters
4541
func FetchKeysOfParameters(
4642
ctx context.Context,
47-
api AWSSystemsManagerParameterStore,
48-
flags Flags,
43+
api AWSSystemsManagerParameterStoreAPI,
44+
filterTags []FilterTag,
4945
) []string {
5046
var parameters []string
5147

5248
// Set parameter filters
5349
parameterFilters := []types.ParameterStringFilter{}
54-
for _, ft := range flags.FilterTags {
50+
for _, ft := range filterTags {
5551
filterKey := fmt.Sprintf("tag:%s", ft.Name)
5652
parameterFilters = append(parameterFilters, types.ParameterStringFilter{
5753
Key: &filterKey,
5854
Values: []string{ft.Value},
5955
})
6056
}
6157
describeInput := &ssm.DescribeParametersInput{
62-
MaxResults: int32(flags.MaxResults),
58+
MaxResults: 10,
6359
ParameterFilters: parameterFilters,
6460
}
6561
n := 0
@@ -126,30 +122,3 @@ func ParseFilterTags(filterTagsStr string) []FilterTag {
126122

127123
return filterTags
128124
}
129-
130-
// WriteToFile generate or update existing file and
131-
// flash to it environment variables
132-
func WriteToFile(parameters []Parameter, outfile string, update bool, export bool) {
133-
flag := os.O_TRUNC | os.O_CREATE | os.O_WRONLY
134-
if update {
135-
flag = os.O_APPEND | os.O_CREATE | os.O_WRONLY
136-
}
137-
138-
f, err := os.OpenFile(
139-
outfile,
140-
flag,
141-
0644,
142-
)
143-
if err != nil {
144-
log.Fatalf("Unable to open file %s, error: %s", outfile, err)
145-
}
146-
defer f.Close()
147-
148-
parametersStr := ""
149-
for _, p := range parameters {
150-
parametersStr += fmt.Sprintf("%s%s=%s\n", p.Export, p.Name, p.Value)
151-
}
152-
if _, err := f.WriteString(parametersStr); err != nil {
153-
log.Fatalf("Unable to write to file %s, error: %s", outfile, err)
154-
}
155-
}

checkenv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ func main() {
108108
}
109109

110110
for providerSpec := range spec.providersFull {
111-
fmt.Printf("# %s - all variables:\n", providerSpec)
111+
fmt.Printf("# Generated with %s - all variables:\n", providerSpec)
112112
for k, v := range providedVars[providerSpec] {
113113
fmt.Printf("%s%s=%s\n", exportPrefix, k, v)
114114
}
115115
}
116116
for providerSpec, queriedVars := range spec.providerVars {
117-
fmt.Printf("# %s - specific variables:\n", providerSpec)
117+
fmt.Printf("# Generated with %s - specific variables:\n", providerSpec)
118118
definedVars := providedVars[providerSpec]
119119
for k := range queriedVars {
120120
v, ok := definedVars[k]

0 commit comments

Comments
 (0)