Skip to content

Commit 2a38feb

Browse files
authored
update aws param store store to use getKey (#1009)
## what - Refactored the `getKey` method in AWS SSM Parameter Store to handle nil stack delimiters - Added error handling for cases where stack delimiter is not properly set - Improved error propagation in `Set` and `Get` operations ## why - Prevents potential nil pointer dereferences when stack delimiter is not initialized - Makes the code more robust by explicitly handling error cases - Improves debugging capabilities by providing more specific error messages ## references - Related to AWS SSM Parameter Store documentation: https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved error reporting to promptly capture and inform users of configuration issues. - **Refactor** - Streamlined the process for constructing actual configuration keys, enhancing overall consistency and system reliability. - Enhanced error handling in key retrieval processes for improved robustness. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent b404408 commit 2a38feb

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

pkg/store/aws_ssm_param_store.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package store
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

87
"github.com/aws/aws-sdk-go-v2/aws"
98
"github.com/aws/aws-sdk-go-v2/config"
@@ -68,14 +67,12 @@ func NewSSMStore(options SSMStoreOptions) (Store, error) {
6867
return store, nil
6968
}
7069

71-
func (s *SSMStore) getKey(stack string, component string, key string) string {
72-
stackParts := strings.Split(stack, *s.stackDelimiter)
73-
componentParts := strings.Split(component, "/")
70+
func (s *SSMStore) getKey(stack string, component string, key string) (string, error) {
71+
if s.stackDelimiter == nil {
72+
return "", fmt.Errorf("stack delimiter is not set")
73+
}
7474

75-
parts := append([]string{s.prefix}, stackParts...)
76-
parts = append(parts, componentParts...)
77-
parts = append(parts, key)
78-
return strings.Join(parts, "/")
75+
return getKey(s.prefix, *s.stackDelimiter, stack, component, key, "/")
7976
}
8077

8178
// Set stores a key-value pair in AWS SSM Parameter Store.
@@ -99,16 +96,18 @@ func (s *SSMStore) Set(stack string, component string, key string, value interfa
9996
}
10097

10198
// Construct the full parameter name using getKey
102-
paramName := s.getKey(stack, component, key)
99+
paramName, err := s.getKey(stack, component, key)
100+
if err != nil {
101+
return fmt.Errorf("failed to get key: %w", err)
102+
}
103103

104104
// Put the parameter in SSM Parameter Store
105-
_, err := s.client.PutParameter(ctx, &ssm.PutParameterInput{
105+
_, err = s.client.PutParameter(ctx, &ssm.PutParameterInput{
106106
Name: aws.String(paramName),
107107
Value: aws.String(strValue),
108108
Type: types.ParameterTypeString,
109109
Overwrite: aws.Bool(true), // Allow overwriting existing keys
110110
})
111-
112111
if err != nil {
113112
return fmt.Errorf("failed to set parameter '%s': %w", paramName, err)
114113
}
@@ -131,7 +130,10 @@ func (s *SSMStore) Get(stack string, component string, key string) (interface{},
131130
ctx := context.TODO()
132131

133132
// Construct the full parameter name using getKey
134-
paramName := s.getKey(stack, component, key)
133+
paramName, err := s.getKey(stack, component, key)
134+
if err != nil {
135+
return nil, fmt.Errorf("failed to get key: %w", err)
136+
}
135137

136138
// Get the parameter from SSM Parameter Store
137139
result, err := s.client.GetParameter(ctx, &ssm.GetParameterInput{

0 commit comments

Comments
 (0)