|
| 1 | +package hybrid |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws/arn" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/eks" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 12 | + |
| 13 | + "github.com/aws/eks-hybrid/internal/api" |
| 14 | + "github.com/aws/eks-hybrid/internal/validation" |
| 15 | +) |
| 16 | + |
| 17 | +func (hnp *HybridNodeProvider) ValidateClusterAccess(ctx context.Context, informer validation.Informer, _ *api.NodeConfig) error { |
| 18 | + var err error |
| 19 | + informer.Starting(ctx, "cluster-access", "Validating cluster access through EKS access entry or ConfigMap") |
| 20 | + defer func() { |
| 21 | + informer.Done(ctx, "cluster-access", err) |
| 22 | + }() |
| 23 | + |
| 24 | + var roleName string |
| 25 | + stsClient := sts.NewFromConfig(*hnp.awsConfig) |
| 26 | + eksClient := eks.NewFromConfig(*hnp.awsConfig) |
| 27 | + |
| 28 | + getCallerIdentityOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) |
| 29 | + if err != nil { |
| 30 | + return err |
| 31 | + } |
| 32 | + roleArn := *getCallerIdentityOutput.Arn |
| 33 | + parsedARN, err := arn.Parse(roleArn) |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + splitArn := strings.Split(parsedARN.Resource, "/") |
| 39 | + if parsedARN.Service == "sts" && strings.HasPrefix(parsedARN.Resource, "assumed-role") { |
| 40 | + roleName = splitArn[1] |
| 41 | + } else if parsedARN.Service == "iam" && strings.HasPrefix(parsedARN.Resource, "role") { |
| 42 | + roleName = splitArn[len(splitArn)-1] |
| 43 | + } |
| 44 | + |
| 45 | + accessEntries := []string{} |
| 46 | + listAccessEntriesOutput, err := eksClient.ListAccessEntries(ctx, &eks.ListAccessEntriesInput{ |
| 47 | + ClusterName: hnp.cluster.Name, |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + accessEntries = append(accessEntries, listAccessEntriesOutput.AccessEntries...) |
| 54 | + nextToken := listAccessEntriesOutput.NextToken |
| 55 | + |
| 56 | + for nextToken != nil && aws.ToString(nextToken) != "" { |
| 57 | + listAccessEntriesOutput, err = eksClient.ListAccessEntries(ctx, &eks.ListAccessEntriesInput{ |
| 58 | + ClusterName: hnp.cluster.Name, |
| 59 | + NextToken: nextToken, |
| 60 | + }) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + accessEntries = append(accessEntries, listAccessEntriesOutput.AccessEntries...) |
| 66 | + nextToken = listAccessEntriesOutput.NextToken |
| 67 | + } |
| 68 | + |
| 69 | + foundRole := false |
| 70 | + for _, accessEntry := range accessEntries { |
| 71 | + if strings.Contains(accessEntry, roleName) { |
| 72 | + foundRole = true |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + if !foundRole { |
| 77 | + err = validation.WithRemediation(fmt.Errorf("missing access entry with Hybrid Node role principal"), "Ensure your EKS cluster has at least one access entry with the hybrid node IAM role as principal.") |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | +} |
0 commit comments