hostenv: a box's named-units parameter, read and put
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
3 files changed,
+170
−0
internal/hostenv/awsssm.go+16 −0internal/hostenv/namedunits.go+81 −0internal/hostenv/namedunits_test.go+73 −0
diff --git a/internal/hostenv/awsssm.go b/internal/hostenv/awsssm.go| index d0d5375db..f009a6d78 100644 |
| --- a/internal/hostenv/awsssm.go |
| +++ b/internal/hostenv/awsssm.go |
| @@ -167,3 +167,19 @@ func (p *ssmParams) groupByPath(ctx context.Context, prefix string) (map[string] |
| token = page.NextToken |
| } |
| } |
| + |
| +// OpenNamedUnitsStore opens SSM in awsRegion ("" is the SDK's default) for a |
| +// box's named-units parameter. Root gets the IMDS chain; an operator gets |
| +// their own profile (rootaws). |
| +func OpenNamedUnitsStore(ctx context.Context, awsRegion string) (NamedUnitsStore, error) { |
| + var opts []func(*config.LoadOptions) error |
| + if awsRegion != "" { |
| + opts = append(opts, config.WithRegion(awsRegion)) |
| + } |
| + cfg, err := rootaws.LoadDefaultConfig(ctx, opts...) |
| + if err != nil { |
| + return nil, err |
| + } |
| + client := ssm.NewFromConfig(cfg) |
| + return &ssmNamedUnits{get: client.GetParameter, put: client.PutParameter}, nil |
| +} |
diff --git a/internal/hostenv/namedunits.go b/internal/hostenv/namedunits.go| new file mode 100644 |
| index 000000000..5e9af2e81 |
| --- /dev/null |
| +++ b/internal/hostenv/namedunits.go |
| @@ -0,0 +1,81 @@ |
| +package hostenv |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + |
| + "github.com/aws/aws-sdk-go-v2/aws" |
| + "github.com/aws/aws-sdk-go-v2/service/ssm" |
| + ssmtypes "github.com/aws/aws-sdk-go-v2/service/ssm/types" |
| +) |
| + |
| +// NamedUnitsPrefix is where a box's named-units declaration lives in SSM |
| +// Parameter Store: one String parameter per instance ID. |
| +const NamedUnitsPrefix = "/carlos/host/named-units/" |
| + |
| +// ErrNoParameter is a named-units parameter that does not exist. |
| +var ErrNoParameter = errors.New("hostenv: parameter not found") |
| + |
| +// NamedUnitsParameter is the parameter name for a box's instance ID. The ID |
| +// must be a real EC2 instance ID, so no other path can be built from it. |
| +func NamedUnitsParameter(iid string) (string, error) { |
| + if !bindingIID.MatchString(iid) { |
| + return "", fmt.Errorf("hostenv: %q is not an instance ID", iid) |
| + } |
| + return NamedUnitsPrefix + iid, nil |
| +} |
| + |
| +// NamedUnitsStore reads and writes a box's named-units parameter. |
| +type NamedUnitsStore interface { |
| + // Get returns ErrNoParameter for ParameterNotFound, and an error for |
| + // a parameter whose Type is not String. The bytes are as stored. |
| + Get(ctx context.Context, iid string) (raw []byte, version int64, err error) |
| + // Put writes Type=String, Overwrite=true; returns the new version. |
| + Put(ctx context.Context, iid string, raw []byte) (version int64, err error) |
| +} |
| + |
| +// ssmNamedUnits holds method values, not the client, as ssmParams does. |
| +type ssmNamedUnits struct { |
| + get func(context.Context, *ssm.GetParameterInput, ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) |
| + put func(context.Context, *ssm.PutParameterInput, ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) |
| +} |
| + |
| +func (s *ssmNamedUnits) Get(ctx context.Context, iid string) ([]byte, int64, error) { |
| + name, err := NamedUnitsParameter(iid) |
| + if err != nil { |
| + return nil, 0, err |
| + } |
| + out, err := s.get(ctx, &ssm.GetParameterInput{Name: aws.String(name), WithDecryption: aws.Bool(false)}) |
| + var nf *ssmtypes.ParameterNotFound |
| + if errors.As(err, &nf) { |
| + return nil, 0, ErrNoParameter |
| + } |
| + if err != nil { |
| + return nil, 0, err |
| + } |
| + if out == nil || out.Parameter == nil { |
| + return nil, 0, fmt.Errorf("hostenv: %s: empty response", name) |
| + } |
| + if out.Parameter.Type != ssmtypes.ParameterTypeString { |
| + return nil, 0, fmt.Errorf("hostenv: %s is type %s, want String", name, out.Parameter.Type) |
| + } |
| + return []byte(aws.ToString(out.Parameter.Value)), out.Parameter.Version, nil |
| +} |
| + |
| +func (s *ssmNamedUnits) Put(ctx context.Context, iid string, raw []byte) (int64, error) { |
| + name, err := NamedUnitsParameter(iid) |
| + if err != nil { |
| + return 0, err |
| + } |
| + out, err := s.put(ctx, &ssm.PutParameterInput{ |
| + Name: aws.String(name), |
| + Value: aws.String(string(raw)), |
| + Type: ssmtypes.ParameterTypeString, |
| + Overwrite: aws.Bool(true), |
| + }) |
| + if err != nil { |
| + return 0, err |
| + } |
| + return out.Version, nil |
| +} |
diff --git a/internal/hostenv/namedunits_test.go b/internal/hostenv/namedunits_test.go| new file mode 100644 |
| index 000000000..502440439 |
| --- /dev/null |
| +++ b/internal/hostenv/namedunits_test.go |
| @@ -0,0 +1,73 @@ |
| +package hostenv |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "testing" |
| + |
| + "github.com/aws/aws-sdk-go-v2/aws" |
| + "github.com/aws/aws-sdk-go-v2/service/ssm" |
| + "github.com/aws/aws-sdk-go-v2/service/ssm/types" |
| +) |
| + |
| +func TestNamedUnitsGetMapsParameterNotFound(t *testing.T) { |
| + s := &ssmNamedUnits{get: func(context.Context, *ssm.GetParameterInput, ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) { |
| + return nil, &types.ParameterNotFound{} |
| + }} |
| + _, _, err := s.Get(context.Background(), "i-0123456789abcdef0") |
| + if !errors.Is(err, ErrNoParameter) { |
| + t.Fatalf("err = %v, want ErrNoParameter", err) |
| + } |
| +} |
| + |
| +func TestNamedUnitsGetRefusesSecureString(t *testing.T) { |
| + s := &ssmNamedUnits{get: func(context.Context, *ssm.GetParameterInput, ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) { |
| + return &ssm.GetParameterOutput{Parameter: &types.Parameter{Type: types.ParameterTypeSecureString, Value: aws.String("x")}}, nil |
| + }} |
| + _, _, err := s.Get(context.Background(), "i-0123456789abcdef0") |
| + if err == nil || errors.Is(err, ErrNoParameter) { |
| + t.Fatalf("err = %v, want a non-ErrNoParameter error", err) |
| + } |
| +} |
| + |
| +func TestNamedUnitsGetReturnsRawAndVersion(t *testing.T) { |
| + var in *ssm.GetParameterInput |
| + s := &ssmNamedUnits{get: func(_ context.Context, i *ssm.GetParameterInput, _ ...func(*ssm.Options)) (*ssm.GetParameterOutput, error) { |
| + in = i |
| + return &ssm.GetParameterOutput{Parameter: &types.Parameter{Type: types.ParameterTypeString, Value: aws.String(`{"a":1}`), Version: 7}}, nil |
| + }} |
| + raw, v, err := s.Get(context.Background(), "i-0123456789abcdef0") |
| + if err != nil || string(raw) != `{"a":1}` || v != 7 { |
| + t.Fatalf("got %q %d %v", raw, v, err) |
| + } |
| + if aws.ToString(in.Name) != NamedUnitsPrefix+"i-0123456789abcdef0" || aws.ToBool(in.WithDecryption) { |
| + t.Fatalf("bad input %+v", in) |
| + } |
| +} |
| + |
| +func TestNamedUnitsPutSendsStringOverwrite(t *testing.T) { |
| + var in *ssm.PutParameterInput |
| + s := &ssmNamedUnits{put: func(_ context.Context, i *ssm.PutParameterInput, _ ...func(*ssm.Options)) (*ssm.PutParameterOutput, error) { |
| + in = i |
| + return &ssm.PutParameterOutput{Version: 3}, nil |
| + }} |
| + v, err := s.Put(context.Background(), "i-0123456789abcdef0", []byte("raw")) |
| + if err != nil || v != 3 { |
| + t.Fatalf("v=%d err=%v", v, err) |
| + } |
| + if aws.ToString(in.Name) != "/carlos/host/named-units/i-0123456789abcdef0" || |
| + in.Type != types.ParameterTypeString || !aws.ToBool(in.Overwrite) || aws.ToString(in.Value) != "raw" { |
| + t.Fatalf("bad input %+v", in) |
| + } |
| +} |
| + |
| +func TestNamedUnitsParameterRefusesBadInstanceID(t *testing.T) { |
| + for _, iid := range []string{"i-xyz", "../x", "", "i-0123456789abcdef0/x"} { |
| + if _, err := NamedUnitsParameter(iid); err == nil { |
| + t.Errorf("%q accepted", iid) |
| + } |
| + } |
| + if n, err := NamedUnitsParameter("i-0123abcd"); err != nil || n != NamedUnitsPrefix+"i-0123abcd" { |
| + t.Fatalf("%q %v", n, err) |
| + } |
| +} |