mirror of
https://github.com/mulgadc/spinifex.git
synced 2026-10-21 03:36:35 +00:00
chore: remove functions with stdlib alternatives
This commit is contained in:
@@ -2,6 +2,7 @@ package handlers_ecs
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
@@ -242,9 +243,9 @@ func (s *Service) releaseReservation(kv nats.KeyValue, cluster, instanceID, task
|
||||
if uerr := json.Unmarshal(entry.Value(), &rec); uerr != nil {
|
||||
return uerr
|
||||
}
|
||||
rec.ReservedCPU = maxZero(rec.ReservedCPU - cpu)
|
||||
rec.ReservedMemoryMiB = maxZero(rec.ReservedMemoryMiB - mem)
|
||||
rec.PlacedTasks = removeString(rec.PlacedTasks, taskID)
|
||||
rec.ReservedCPU = max(rec.ReservedCPU-cpu, 0)
|
||||
rec.ReservedMemoryMiB = max(rec.ReservedMemoryMiB-mem, 0)
|
||||
rec.PlacedTasks = slices.DeleteFunc(rec.PlacedTasks, func(v string) bool { return v == taskID })
|
||||
data, merr := json.Marshal(&rec)
|
||||
if merr != nil {
|
||||
return merr
|
||||
@@ -255,20 +256,3 @@ func (s *Service) releaseReservation(kv nats.KeyValue, cluster, instanceID, task
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func maxZero(v int) int {
|
||||
if v < 0 {
|
||||
return 0
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func removeString(in []string, s string) []string {
|
||||
out := in[:0]
|
||||
for _, v := range in {
|
||||
if v != s {
|
||||
out = append(out, v)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -87,18 +87,10 @@ func catalogSpecs() []AddonSpec {
|
||||
for n := range addonCatalog {
|
||||
names = append(names, n)
|
||||
}
|
||||
insertionSortStrings(names)
|
||||
slices.Sort(names)
|
||||
out := make([]AddonSpec, 0, len(names))
|
||||
for _, n := range names {
|
||||
out = append(out, addonCatalog[n])
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func insertionSortStrings(s []string) {
|
||||
for i := 1; i < len(s); i++ {
|
||||
for j := i; j > 0 && s[j-1] > s[j]; j-- {
|
||||
s[j-1], s[j] = s[j], s[j-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ func (s *IAMServiceImpl) CreateOpenIDConnectProvider(accountID string, input *ia
|
||||
return nil, fmt.Errorf("store OIDC provider: %w", err)
|
||||
}
|
||||
|
||||
arn := OIDCProviderARN(accountID, stripIssuerScheme(issuer))
|
||||
arn := OIDCProviderARN(accountID, strings.TrimPrefix(issuer, "https://"))
|
||||
slog.Info("IAM OIDC provider created", "accountID", accountID, "url", issuer, "arn", arn)
|
||||
return &iam.CreateOpenIDConnectProviderOutput{
|
||||
OpenIDConnectProviderArn: aws.String(arn),
|
||||
@@ -145,7 +145,7 @@ func (s *IAMServiceImpl) GetOpenIDConnectProvider(accountID string, input *iam.G
|
||||
return nil, err
|
||||
}
|
||||
return &iam.GetOpenIDConnectProviderOutput{
|
||||
Url: aws.String(stripIssuerScheme(record.Url)),
|
||||
Url: aws.String(strings.TrimPrefix(record.Url, "https://")),
|
||||
ClientIDList: aws.StringSlice(record.ClientIDList),
|
||||
ThumbprintList: aws.StringSlice(record.ThumbprintList),
|
||||
CreateDate: aws.Time(parseCreatedAt(record.CreatedAt)),
|
||||
@@ -193,7 +193,7 @@ func (s *IAMServiceImpl) ListOpenIDConnectProviders(accountID string, _ *iam.Lis
|
||||
}
|
||||
out.OpenIDConnectProviderList = append(out.OpenIDConnectProviderList,
|
||||
&iam.OpenIDConnectProviderListEntry{
|
||||
Arn: aws.String(OIDCProviderARN(accountID, stripIssuerScheme(record.Url))),
|
||||
Arn: aws.String(OIDCProviderARN(accountID, strings.TrimPrefix(record.Url, "https://"))),
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
@@ -255,12 +255,6 @@ func (s *IAMServiceImpl) getOIDCProvider(accountID, arn string) (*OIDCProviderRe
|
||||
return &record, nil
|
||||
}
|
||||
|
||||
// stripIssuerScheme drops the https:// prefix to produce the scheme-less
|
||||
// host/path form AWS uses in the oidc-provider ARN suffix.
|
||||
func stripIssuerScheme(issuer string) string {
|
||||
return strings.TrimPrefix(issuer, "https://")
|
||||
}
|
||||
|
||||
// tagsToSDK converts stored Tags into the SDK shape.
|
||||
func tagsToSDK(tags []Tag) []*iam.Tag {
|
||||
if len(tags) == 0 {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package handlers_iam
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
@@ -22,7 +23,7 @@ func TestCreateOpenIDConnectProvider(t *testing.T) {
|
||||
t.Fatalf("CreateOpenIDConnectProvider: %v", err)
|
||||
}
|
||||
|
||||
wantARN := OIDCProviderARN(acct, stripIssuerScheme(testIssuer))
|
||||
wantARN := OIDCProviderARN(acct, strings.TrimPrefix(testIssuer, "https://"))
|
||||
if aws.StringValue(out.OpenIDConnectProviderArn) != wantARN {
|
||||
t.Fatalf("ARN = %q, want %q", aws.StringValue(out.OpenIDConnectProviderArn), wantARN)
|
||||
}
|
||||
@@ -79,8 +80,8 @@ func TestGetOpenIDConnectProvider(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if aws.StringValue(got.Url) != stripIssuerScheme(testIssuer) {
|
||||
t.Fatalf("Url = %q, want %q", aws.StringValue(got.Url), stripIssuerScheme(testIssuer))
|
||||
if aws.StringValue(got.Url) != strings.TrimPrefix(testIssuer, "https://") {
|
||||
t.Fatalf("Url = %q, want %q", aws.StringValue(got.Url), strings.TrimPrefix(testIssuer, "https://"))
|
||||
}
|
||||
if len(got.ClientIDList) != 1 || aws.StringValue(got.ClientIDList[0]) != "sts.amazonaws.com" {
|
||||
t.Fatalf("ClientIDList = %v", aws.StringValueSlice(got.ClientIDList))
|
||||
@@ -125,8 +126,8 @@ func TestListOpenIDConnectProviders(t *testing.T) {
|
||||
t.Fatalf("list = %d entries, want 2", len(out.OpenIDConnectProviderList))
|
||||
}
|
||||
want := map[string]bool{
|
||||
OIDCProviderARN(acct, stripIssuerScheme(issuers[0])): true,
|
||||
OIDCProviderARN(acct, stripIssuerScheme(issuers[1])): true,
|
||||
OIDCProviderARN(acct, strings.TrimPrefix(issuers[0], "https://")): true,
|
||||
OIDCProviderARN(acct, strings.TrimPrefix(issuers[1], "https://")): true,
|
||||
}
|
||||
for _, e := range out.OpenIDConnectProviderList {
|
||||
if !want[aws.StringValue(e.Arn)] {
|
||||
@@ -159,7 +160,7 @@ func TestDeleteOpenIDConnectProvider(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestIssuerProviderARNRoundTrip(t *testing.T) {
|
||||
arn := OIDCProviderARN("000000000001", stripIssuerScheme(testIssuer))
|
||||
arn := OIDCProviderARN("000000000001", strings.TrimPrefix(testIssuer, "https://"))
|
||||
got, err := issuerFromOIDCProviderARN(arn)
|
||||
if err != nil {
|
||||
t.Fatalf("reverse: %v", err)
|
||||
|
||||
@@ -161,9 +161,7 @@ func (s *STSServiceImpl) assumeRoleForCaller(callerAccountID, callerARN, princip
|
||||
if effectiveMax == 0 {
|
||||
effectiveMax = defaultDurationSeconds
|
||||
}
|
||||
if effectiveMax > maxDurationSeconds {
|
||||
effectiveMax = maxDurationSeconds
|
||||
}
|
||||
effectiveMax = min(effectiveMax, maxDurationSeconds)
|
||||
if duration < minDurationSeconds || duration > effectiveMax {
|
||||
return nil, errors.New(awserrors.ErrorValidationError)
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (s *STSServiceImpl) AssumeRoleWithWebIdentity(input *sts.AssumeRoleWithWebI
|
||||
if claims.Issuer == "" || claims.Subject == "" {
|
||||
return nil, errors.New(awserrors.ErrorInvalidIdentityToken)
|
||||
}
|
||||
if !claimAudienceContains(claims.Audience, irsaExpectedAudience) {
|
||||
if !slices.Contains(claims.Audience, irsaExpectedAudience) {
|
||||
slog.Warn("AssumeRoleWithWebIdentity: aud claim missing sts.amazonaws.com",
|
||||
"role_arn", roleARN, "aud", []string(claims.Audience))
|
||||
return nil, errors.New(awserrors.ErrorInvalidIdentityToken)
|
||||
@@ -93,14 +93,12 @@ func (s *STSServiceImpl) AssumeRoleWithWebIdentity(input *sts.AssumeRoleWithWebI
|
||||
if effectiveMax == 0 {
|
||||
effectiveMax = defaultDurationSeconds
|
||||
}
|
||||
if effectiveMax > maxDurationSeconds {
|
||||
effectiveMax = maxDurationSeconds
|
||||
}
|
||||
effectiveMax = min(effectiveMax, maxDurationSeconds)
|
||||
if duration < minDurationSeconds || duration > effectiveMax {
|
||||
return nil, errors.New(awserrors.ErrorValidationError)
|
||||
}
|
||||
|
||||
issuerHostPath := stripIssuerScheme(claims.Issuer)
|
||||
issuerHostPath := strings.TrimPrefix(claims.Issuer, "https://")
|
||||
federatedARN := handlers_iam.OIDCProviderARN(roleAccountID, issuerHostPath)
|
||||
ctx := webIdentityContext{
|
||||
federatedPrincipalARN: federatedARN,
|
||||
@@ -242,14 +240,3 @@ func jwkToECDSAPublicKey(jwk *JWK) (*ecdsa.PublicKey, error) {
|
||||
}
|
||||
return pub, nil
|
||||
}
|
||||
|
||||
// claimAudienceContains tests membership of want in the JWT aud claim.
|
||||
func claimAudienceContains(aud jwt.ClaimStrings, want string) bool {
|
||||
return slices.Contains(aud, want)
|
||||
}
|
||||
|
||||
// stripIssuerScheme drops the https:// prefix to produce the scheme-less form
|
||||
// AWS uses in oidc-provider ARN suffixes.
|
||||
func stripIssuerScheme(issuer string) string {
|
||||
return strings.TrimPrefix(issuer, "https://")
|
||||
}
|
||||
|
||||
@@ -598,10 +598,3 @@ func TestJWKToECDSAPublicKey_RejectsUnsupportedShapes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClaimAudienceContains(t *testing.T) {
|
||||
assert.True(t, claimAudienceContains(jwt.ClaimStrings{irsaExpectedAudience}, irsaExpectedAudience))
|
||||
assert.True(t, claimAudienceContains(jwt.ClaimStrings{"x", irsaExpectedAudience}, irsaExpectedAudience))
|
||||
assert.False(t, claimAudienceContains(jwt.ClaimStrings{"x"}, irsaExpectedAudience))
|
||||
assert.False(t, claimAudienceContains(jwt.ClaimStrings{}, irsaExpectedAudience))
|
||||
}
|
||||
|
||||
+1
-11
@@ -71,7 +71,7 @@ func (a *DHCPGatewayLRPAllocator) Allocate(ctx context.Context, vpcID string, po
|
||||
return "", 0, "", false, fmt.Errorf("dhcp gw-lrp allocator: pool %q: %w", pool.Name, err)
|
||||
}
|
||||
|
||||
prefix := maskToPrefix(entry.Lease.SubnetMask)
|
||||
prefix, _ := entry.Lease.SubnetMask.Size()
|
||||
if prefix <= 0 {
|
||||
prefix = pool.PrefixLen
|
||||
}
|
||||
@@ -100,13 +100,3 @@ func (a *DHCPGatewayLRPAllocator) Release(ctx context.Context, vpcID string) err
|
||||
}
|
||||
return a.mgr.handleRelease(ctx, GatewayLRPClientID(vpcID))
|
||||
}
|
||||
|
||||
// maskToPrefix returns the prefix length encoded in an IPv4 subnet mask.
|
||||
// Empty mask → 0 (caller falls back to pool.PrefixLen).
|
||||
func maskToPrefix(m net.IPMask) int {
|
||||
if len(m) == 0 {
|
||||
return 0
|
||||
}
|
||||
ones, _ := m.Size()
|
||||
return ones
|
||||
}
|
||||
|
||||
Vendored
+2
-2
@@ -1,12 +1,12 @@
|
||||
package external
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"net"
|
||||
)
|
||||
|
||||
func ipv4ToUint32(ip net.IP) uint32 {
|
||||
v := ip.To4()
|
||||
return uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
|
||||
return binary.BigEndian.Uint32(ip.To4())
|
||||
}
|
||||
|
||||
func uint32ToIPv4(n uint32) net.IP {
|
||||
|
||||
Reference in New Issue
Block a user