Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster details to the sts through headers #649

Merged
merged 5 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pkg/providers/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ const volumeAttachmentStuck = "VolumeAttachmentStuck"
// Indicates that a node has volumes stuck in attaching state and hence it is not fit for scheduling more pods
const nodeWithImpairedVolumes = "NodeWithImpairedVolumes"

const sourceKey = "x-amz-source-arn"
const accountKey = "x-amz-source-account"
kmala marked this conversation as resolved.
Show resolved Hide resolved

const (
// volumeAttachmentConsecutiveErrorLimit is the number of consecutive errors we will ignore when waiting for a volume to attach/detach
volumeAttachmentStatusConsecutiveErrorLimit = 10
Expand Down Expand Up @@ -1289,11 +1292,25 @@ func init() {
var creds *credentials.Credentials
if cfg.Global.RoleARN != "" {
klog.Infof("Using AWS assumed role %v", cfg.Global.RoleARN)
stsClient := sts.New(sess)
nckturner marked this conversation as resolved.
Show resolved Hide resolved
if cfg.Global.KubernetesClusterID != "" {
sourceAcct, sourceArn, err := GetSourceAcctAndArn(cfg.Global.RoleARN, regionName, cfg.Global.KubernetesClusterID)
if err != nil {
return nil, err
}
stsClient.Handlers.Sign.PushFront(func(s *request.Request) {
s.ApplyOptions(request.WithSetRequestHeaders(map[string]string{
sourceKey: sourceArn,
accountKey: sourceAcct,
}))
})
klog.Infof("configuring STS client with extra headers")
}
creds = credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.EnvProvider{},
assumeRoleProvider(&stscreds.AssumeRoleProvider{
Client: sts.New(sess),
Client: stsClient,
RoleARN: cfg.Global.RoleARN,
}),
})
Expand Down
26 changes: 26 additions & 0 deletions pkg/providers/v1/aws_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ limitations under the License.
package aws

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"

"k8s.io/apimachinery/pkg/util/sets"
)
Expand All @@ -43,3 +47,25 @@ func stringSetFromPointers(in []*string) sets.String {
}
return out
}

// GetSourceAcctAndArn constructs source acct and arn and return them for use
func GetSourceAcctAndArn(roleARN, region, clusterName string) (string, string, error) {
// ARN format (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference-arns.html)
// arn:partition:service:region:account-id:resource-type/resource-id
// IAM format, region is always blank
// arn:aws:iam::account:role/role-name-with-path
if !arn.IsARN(roleARN) {
return "", "", fmt.Errorf("incorrect ARN format for role %s", roleARN)
}
if region == "" {
return "", "", errors.New("region can't be empty")
}

parsedArn, err := arn.Parse(roleARN)
if err != nil {
return "", "", err
}

sourceArn := fmt.Sprintf("arn:%s:eks:%s:%s:cluster/%s", parsedArn.Partition, region, parsedArn.AccountID, clusterName)
kmala marked this conversation as resolved.
Show resolved Hide resolved
return parsedArn.AccountID, sourceArn, nil
}
83 changes: 83 additions & 0 deletions pkg/providers/v1/aws_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2014 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package aws

import "testing"

func TestGetSourceAcctAndArn(t *testing.T) {
type args struct {
roleARN string
region string
clusterName string
}
tests := []struct {
name string
args args
want string
want1 string
wantErr bool
}{
{
name: "corect role arn",
args: args{
roleARN: "arn:aws:iam::123456789876:role/test-cluster",
region: "us-west-2",
clusterName: "test-cluster",
},
want: "123456789876",
want1: "arn:aws:eks:us-west-2:123456789876:cluster/test-cluster",
wantErr: false,
},
{
name: "incorect role arn",
args: args{
roleARN: "arn:aws:iam::123456789876",
region: "us-west-2",
clusterName: "test-cluster",
},
want: "",
want1: "",
wantErr: true,
},
{
name: "empty region",
args: args{
roleARN: "arn:aws:iam::123456789876:role/test-cluster",
region: "",
clusterName: "test-cluster",
},
want: "",
want1: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := GetSourceAcctAndArn(tt.args.roleARN, tt.args.region, tt.args.clusterName)
if (err != nil) != tt.wantErr {
t.Errorf("GetSourceAcctAndArn() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("GetSourceAcctAndArn() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("GetSourceAcctAndArn() got1 = %v, want %v", got1, tt.want1)
}
})
}
}