chore: used shared iam formatter

This commit is contained in:
Julian Sommer
2026-08-25 10:35:39 +10:00
parent b62a630989
commit 0d6676ecc8
6 changed files with 23 additions and 7 deletions
+6
View File
@@ -23,6 +23,12 @@ func FormatIAMPath(kind IAMResourceType, accountID, resourcePath, name string) s
return fmt.Sprintf("arn:aws:iam::%s:%s%s%s", accountID, kind, resourcePath, name)
}
// FormatIAMRoot builds the account root ARN, the principal AWS uses for the
// account itself rather than a named IAM resource.
func FormatIAMRoot(accountID string) string {
return fmt.Sprintf("arn:aws:iam::%s:root", accountID)
}
// FormatIAMResource builds an IAM ARN from an already-formed resource
// component, such as an OIDC provider issuer host/path.
func FormatIAMResource(kind IAMResourceType, accountID, resource string) string {
+8
View File
@@ -29,6 +29,14 @@ func TestFormatIAMPath(t *testing.T) {
}
}
func TestFormatIAMRoot(t *testing.T) {
got := arn.FormatIAMRoot("123456789012")
want := "arn:aws:iam::123456789012:root"
if got != want {
t.Fatalf("FormatIAMRoot() = %q, want %q", got, want)
}
}
func TestFormatIAMResource(t *testing.T) {
got := arn.FormatIAMResource(arn.IAMOIDCProvider, "123456789012", "issuer.example/id/cluster")
want := "arn:aws:iam::123456789012:oidc-provider/issuer.example/id/cluster"
@@ -10,6 +10,7 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/mulgadc/spinifex/spinifex/arn"
"github.com/mulgadc/spinifex/spinifex/awserrors"
handlers_iam "github.com/mulgadc/spinifex/spinifex/handlers/iam"
spxtypes "github.com/mulgadc/spinifex/spinifex/types"
@@ -40,7 +41,7 @@ func resolveAndAuthorizeProfile(spec *ec2.IamInstanceProfileSpecification, iamSv
return nil, err
}
if profile.RoleName != "" && passRoleCheck != nil {
roleARN := fmt.Sprintf("arn:aws:iam::%s:role/%s", profile.AccountID, profile.RoleName)
roleARN := arn.FormatIAMPath(arn.IAMRole, profile.AccountID, "/", profile.RoleName)
if err := passRoleCheck(roleARN); err != nil {
return nil, err
}
+4 -4
View File
@@ -2,11 +2,11 @@ package gateway
import (
"errors"
"fmt"
"log/slog"
"net/http"
"github.com/aws/aws-sdk-go/service/sts"
spxarn "github.com/mulgadc/spinifex/spinifex/arn"
"github.com/mulgadc/spinifex/spinifex/awsec2query"
"github.com/mulgadc/spinifex/spinifex/awserrors"
gateway_sts "github.com/mulgadc/spinifex/spinifex/gateway/sts"
@@ -164,16 +164,16 @@ func buildCallerARN(accountID, identity, principalType, assumedRoleARN string) (
}
return assumedRoleARN, nil
case principalTypeRoot:
return fmt.Sprintf("arn:aws:iam::%s:root", accountID), nil
return spxarn.FormatIAMRoot(accountID), nil
case principalTypeUser:
if identity == "root" && accountID == utils.GlobalAccountID {
return fmt.Sprintf("arn:aws:iam::%s:root", accountID), nil
return spxarn.FormatIAMRoot(accountID), nil
}
if identity == "" {
slog.Error("STS_Request: user principal without identity")
return "", errors.New(awserrors.ErrorInternalError)
}
return fmt.Sprintf("arn:aws:iam::%s:user/%s", accountID, identity), nil
return spxarn.FormatIAMPath(spxarn.IAMUser, accountID, "/", identity), nil
default:
slog.Error("STS_Request: unknown principal type", "principalType", principalType)
return "", errors.New(awserrors.ErrorInternalError)
+1 -1
View File
@@ -746,7 +746,7 @@ func (s *IAMServiceImpl) SeedBootstrap(data *BootstrapData) error {
UserName: "root",
UserID: "AIDAAAAAAAAAAAAAAAAA",
AccountID: utils.GlobalAccountID,
ARN: fmt.Sprintf("arn:aws:iam::%s:root", utils.GlobalAccountID),
ARN: arn.FormatIAMRoot(utils.GlobalAccountID),
Path: "/",
CreatedAt: time.Now().UTC().Format(time.RFC3339),
AccessKeys: []string{data.AccessKeyID},
@@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/service/iam"
"github.com/mulgadc/bluebottle/pkg/sigv4"
"github.com/mulgadc/spinifex/spinifex/arn"
"github.com/mulgadc/spinifex/spinifex/awserrors"
handlers_iam "github.com/mulgadc/spinifex/spinifex/handlers/iam"
)
@@ -153,7 +154,7 @@ func (s *STSServiceImpl) resolvePrincipalForVerify(accessKeyID string) (*Presign
userARN := aws.StringValue(userOut.User.Arn)
userID := aws.StringValue(userOut.User.UserId)
if userARN == "" {
userARN = fmt.Sprintf("arn:aws:iam::%s:user/%s", ak.AccountID, ak.UserName)
userARN = arn.FormatIAMPath(arn.IAMUser, ak.AccountID, "/", ak.UserName)
}
return &PresignedCallerIdentity{
AccountID: ak.AccountID,