Skip to content

Commit db09cb9

Browse files
committed
add tests and correct regex for assume role session name
1 parent 577e1b6 commit db09cb9

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

pkg/aws/cloud.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"k8s.io/apimachinery/pkg/util/cache"
77
"net"
88
"os"
9-
"regexp"
109
"strings"
1110
"sync"
1211
"time"
@@ -40,8 +39,6 @@ const (
4039
cacheTTLBufferTime = 30 * time.Second
4140
)
4241

43-
var illegalValuesInSessionName = regexp.MustCompile(`[^a-zA-Z0-9=,.@-]+`)
44-
4542
// NewCloud constructs new Cloud implementation.
4643
func NewCloud(cfg CloudConfig, clusterName string, metricsCollector *aws_metrics.Collector, logger logr.Logger, awsClientsProvider provider.AWSClientsProvider) (services.Cloud, error) {
4744
hasIPv4 := true
@@ -261,7 +258,7 @@ func (c *defaultCloud) GetAssumedRoleELBV2(ctx context.Context, assumeRoleArn st
261258
sourceAccount := sts.NewFromConfig(*existingAwsConfig)
262259
response, err := sourceAccount.AssumeRole(ctx, &sts.AssumeRoleInput{
263260
RoleArn: aws.String(assumeRoleArn),
264-
RoleSessionName: aws.String(c.makeClusterNameSessionNameSafe()),
261+
RoleSessionName: aws.String(generateAssumeRoleSessionName(c.clusterName)),
265262
ExternalId: aws.String(externalId),
266263
})
267264
if err != nil {
@@ -286,11 +283,6 @@ func (c *defaultCloud) GetAssumedRoleELBV2(ctx context.Context, assumeRoleArn st
286283
return elbv2WithAssumedRole, nil
287284
}
288285

289-
func (c *defaultCloud) makeClusterNameSessionNameSafe() string {
290-
safeClusterName := illegalValuesInSessionName.ReplaceAllString(c.clusterName, "")
291-
return fmt.Sprintf("AWS-LBC-%s", safeClusterName)
292-
}
293-
294286
func (c *defaultCloud) EC2() services.EC2 {
295287
return c.ec2
296288
}

pkg/aws/cloud_util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
)
7+
8+
const (
9+
sessionNamePrefix = "AWS-LBC-"
10+
maxSessionNameLength = 2047
11+
)
12+
13+
var illegalValuesInSessionName = regexp.MustCompile(`[^a-zA-Z0-9=,.@\-_]+`)
14+
15+
func generateAssumeRoleSessionName(clusterName string) string {
16+
safeClusterName := illegalValuesInSessionName.ReplaceAllString(clusterName, "")
17+
18+
sessionName := fmt.Sprintf("%s%s", sessionNamePrefix, safeClusterName)
19+
20+
if len(sessionName) > maxSessionNameLength {
21+
return sessionName[:maxSessionNameLength]
22+
}
23+
24+
return sessionName
25+
}

pkg/aws/cloud_util_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package aws
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestUpdateTrackedTargets(t *testing.T) {
9+
testCases := []struct {
10+
name string
11+
clusterName string
12+
expectedSessionName string
13+
}{
14+
{
15+
name: "no mods",
16+
clusterName: "my-cluster-name",
17+
expectedSessionName: "AWS-LBC-my-cluster-name",
18+
},
19+
{
20+
name: "mix lower and upper case",
21+
clusterName: "My-ClUsTeR-name",
22+
expectedSessionName: "AWS-LBC-My-ClUsTeR-name",
23+
},
24+
{
25+
name: "with legal characters",
26+
clusterName: "my_cluster-name=foo,something@here.",
27+
expectedSessionName: "AWS-LBC-my_cluster-name=foo,something@here.",
28+
},
29+
{
30+
name: "with illegal characters",
31+
clusterName: "my&*&*cluster()!(&name",
32+
expectedSessionName: "AWS-LBC-myclustername",
33+
},
34+
}
35+
36+
for _, tc := range testCases {
37+
t.Run(tc.name, func(t *testing.T) {
38+
result := generateAssumeRoleSessionName(tc.clusterName)
39+
assert.Equal(t, tc.expectedSessionName, result)
40+
})
41+
}
42+
}

0 commit comments

Comments
 (0)