Skip to content

Commit

Permalink
update aws param store store to use getKey (#1009)
Browse files Browse the repository at this point in the history
## 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 -->
  • Loading branch information
mcalhoun authored Feb 4, 2025
1 parent b404408 commit 2a38feb
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions pkg/store/aws_ssm_param_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package store
import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
Expand Down Expand Up @@ -68,14 +67,12 @@ func NewSSMStore(options SSMStoreOptions) (Store, error) {
return store, nil
}

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

parts := append([]string{s.prefix}, stackParts...)
parts = append(parts, componentParts...)
parts = append(parts, key)
return strings.Join(parts, "/")
return getKey(s.prefix, *s.stackDelimiter, stack, component, key, "/")
}

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

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

// Put the parameter in SSM Parameter Store
_, err := s.client.PutParameter(ctx, &ssm.PutParameterInput{
_, err = s.client.PutParameter(ctx, &ssm.PutParameterInput{
Name: aws.String(paramName),
Value: aws.String(strValue),
Type: types.ParameterTypeString,
Overwrite: aws.Bool(true), // Allow overwriting existing keys
})

if err != nil {
return fmt.Errorf("failed to set parameter '%s': %w", paramName, err)
}
Expand All @@ -131,7 +130,10 @@ func (s *SSMStore) Get(stack string, component string, key string) (interface{},
ctx := context.TODO()

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

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

0 comments on commit 2a38feb

Please sign in to comment.