|
| 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 | +const ( |
| 18 | + accessEntryRemediation = "Ensure your EKS cluster has at least one access entry of type HYBRID_LINUX with the hybrid node IAM role as principal." |
| 19 | +) |
| 20 | + |
| 21 | +// ValidateClusterAccess checks if the current IAM role has access to the EKS cluster |
| 22 | +// through an access entry |
| 23 | +func (hnp *HybridNodeProvider) ValidateClusterAccess(ctx context.Context, informer validation.Informer, _ *api.NodeConfig) error { |
| 24 | + var err error |
| 25 | + if hnp.awsConfig == nil { |
| 26 | + err = fmt.Errorf("AWS config not set") |
| 27 | + return err |
| 28 | + } |
| 29 | + |
| 30 | + if hnp.cluster == nil || hnp.cluster.Name == nil { |
| 31 | + informer.Starting(ctx, clusterAccessValidation, "Skipping cluster access validation due to node IAM role missing EKS DescribeCluster permission") |
| 32 | + informer.Done(ctx, clusterAccessValidation, err) |
| 33 | + return nil |
| 34 | + } |
| 35 | + |
| 36 | + informer.Starting(ctx, clusterAccessValidation, "Validating cluster access through EKS access entry") |
| 37 | + defer func() { |
| 38 | + informer.Done(ctx, clusterAccessValidation, err) |
| 39 | + }() |
| 40 | + |
| 41 | + stsClient := sts.NewFromConfig(*hnp.awsConfig) |
| 42 | + eksClient := eks.NewFromConfig(*hnp.awsConfig) |
| 43 | + |
| 44 | + getCallerIdentityOutput, err := stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) |
| 45 | + if err != nil { |
| 46 | + err = validation.WithRemediation(fmt.Errorf("getting caller identity: %w", err), accessEntryRemediation) |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + if getCallerIdentityOutput.Arn == nil { |
| 51 | + err = validation.WithRemediation(fmt.Errorf("caller identity ARN is nil"), accessEntryRemediation) |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + roleArn := *getCallerIdentityOutput.Arn |
| 56 | + parsedARN, err := arn.Parse(roleArn) |
| 57 | + if err != nil { |
| 58 | + err = validation.WithRemediation(fmt.Errorf("parsing role ARN: %w", err), accessEntryRemediation) |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + roleName, ok := extractRoleNameFromARN(parsedARN) |
| 63 | + if !ok || roleName == "" { |
| 64 | + err = validation.WithRemediation(fmt.Errorf("extracting role name from ARN: %s", roleArn), accessEntryRemediation) |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + accessEntries, err := fetchAllAccessEntries(ctx, eksClient, hnp.cluster.Name) |
| 69 | + if err != nil { |
| 70 | + err = validation.WithRemediation(fmt.Errorf("fetching access entries from cluster: %w", err), accessEntryRemediation) |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + foundRole := false |
| 75 | + for _, accessEntry := range accessEntries { |
| 76 | + if strings.Contains(accessEntry, fmt.Sprintf("role/%s", roleName)) || |
| 77 | + strings.Contains(accessEntry, fmt.Sprintf("role/%s/", roleName)) || |
| 78 | + strings.HasSuffix(accessEntry, roleName) { |
| 79 | + foundRole = true |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if !foundRole { |
| 85 | + err = validation.WithRemediation( |
| 86 | + fmt.Errorf("missing access entry of type HYBRID_LINUX with Hybrid Node role principal: %s", roleName), |
| 87 | + accessEntryRemediation, |
| 88 | + ) |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +// extractRoleNameFromARN extracts the role name from an ARN |
| 96 | +// Returns the role name and a boolean indicating if extraction was successful |
| 97 | +func extractRoleNameFromARN(parsedARN arn.ARN) (string, bool) { |
| 98 | + splitArn := strings.Split(parsedARN.Resource, "/") |
| 99 | + |
| 100 | + // Handle assumed role ARN format: arn:aws:sts::123456789012:assumed-role/RoleName/session |
| 101 | + if parsedARN.Service == "sts" && strings.HasPrefix(parsedARN.Resource, "assumed-role") && len(splitArn) >= 2 { |
| 102 | + return splitArn[1], true |
| 103 | + } |
| 104 | + |
| 105 | + // Handle IAM role ARN format: arn:aws:iam::123456789012:role/RoleName |
| 106 | + if parsedARN.Service == "iam" && strings.HasPrefix(parsedARN.Resource, "role") && len(splitArn) >= 2 { |
| 107 | + return splitArn[len(splitArn)-1], true |
| 108 | + } |
| 109 | + |
| 110 | + return "", false |
| 111 | +} |
| 112 | + |
| 113 | +// fetchAllAccessEntries retrieves all access entries for a cluster with pagination handling |
| 114 | +func fetchAllAccessEntries(ctx context.Context, eksClient *eks.Client, clusterName *string) ([]string, error) { |
| 115 | + accessEntries := []string{} |
| 116 | + var nextToken *string |
| 117 | + |
| 118 | + for { |
| 119 | + listAccessEntriesOutput, err := eksClient.ListAccessEntries(ctx, &eks.ListAccessEntriesInput{ |
| 120 | + ClusterName: clusterName, |
| 121 | + NextToken: nextToken, |
| 122 | + }) |
| 123 | + if err != nil { |
| 124 | + return nil, fmt.Errorf("failed to list access entries: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + accessEntries = append(accessEntries, listAccessEntriesOutput.AccessEntries...) |
| 128 | + |
| 129 | + if listAccessEntriesOutput.NextToken == nil || aws.ToString(listAccessEntriesOutput.NextToken) == "" { |
| 130 | + break |
| 131 | + } |
| 132 | + nextToken = listAccessEntriesOutput.NextToken |
| 133 | + } |
| 134 | + |
| 135 | + return accessEntries, nil |
| 136 | +} |
0 commit comments