Skip to content

Commit

Permalink
add check format providerId
Browse files Browse the repository at this point in the history
Signed-off-by: hanenMizouni <[email protected]>
  • Loading branch information
outscale-hmi committed Jul 12, 2024
1 parent f723a95 commit 723d101
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cloud-controller-manager/osc/instances_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@ func parseInstanceIDFromProviderIDV2(providerID string) (string, error) {
// * /<availability-zone>/<instance-id>
// * <instance-id>
instanceID := ""
if !strings.HasPrefix(providerID, "i-") && !strings.HasPrefix(providerID, "aws://") {
// providerID does not have the expected prefix
return "", errors.New("invalid providerID format: missing 'aws://' prefix or 'i-' prefix")
}

metadata := strings.Split(strings.TrimPrefix(providerID, "aws://"), "/")
if len(metadata) == 1 {
// instance-id
Expand All @@ -265,6 +270,11 @@ func parseInstanceIDFromProviderIDV2(providerID string) (string, error) {
} else if len(metadata) == 3 {
// /az/instance-id
instanceID = metadata[2]
} else {
return "", errors.New("invalid providerID format")
}
if !strings.HasPrefix(instanceID, "i-") {
return "", errors.New("instance ID not found in providerID or it's wrong format")
}

return instanceID, nil
Expand Down
39 changes: 39 additions & 0 deletions cloud-controller-manager/osc/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1805,6 +1805,45 @@ func TestNodeNameToProviderID(t *testing.T) {
assert.NoError(t, err)
}

func TestParseInstanceIDFromProviderIDV2(t *testing.T) {
testCases := []struct {
providerID string
expectedInstanceID string
expectedErrorString string
}{
// Valid cases
{providerID: "aws://eu-west-2a/i-xxxxx", expectedInstanceID: "i-xxxxx", expectedErrorString: ""},
{providerID: "i-xxxx", expectedInstanceID: "i-xxxx", expectedErrorString: ""},
{providerID: "aws://i-xxxxx", expectedInstanceID: "i-xxxxx", expectedErrorString: ""},
{providerID: "aws:///eu-west-2a/i-xxxxx", expectedInstanceID: "i-xxxxx", expectedErrorString: ""},
{providerID: "aws:///i-xxxx", expectedInstanceID: "i-xxxx", expectedErrorString: ""},

// Invalid cases
{providerID: "invalid-format", expectedInstanceID: "", expectedErrorString: "invalid providerID format"},
{providerID: "aws://", expectedInstanceID: "", expectedErrorString: "instance ID not found in providerID or it's wrong format"},
{providerID: "aws://eu-west-2a/", expectedInstanceID: "", expectedErrorString: "instance ID not found in providerID or it's wrong format"},
{providerID: "aws:///eu-west-2a/", expectedInstanceID: "", expectedErrorString: "instance ID not found in providerID or it's wrong format"},

}
for _, tc := range testCases {
t.Run(fmt.Sprintf("ProviderID_%s", tc.providerID), func(t *testing.T) {
instanceID, err := parseInstanceIDFromProviderIDV2(tc.providerID)
if tc.expectedErrorString == "" {
if err != nil {
t.Errorf("expected no error for providerID '%s', but got: %v", tc.providerID, err)
}
if instanceID != tc.expectedInstanceID {
t.Errorf("expected instanceID '%s' for providerID '%s', but got '%s'", tc.expectedInstanceID, tc.providerID, instanceID)
}
} else {
if err == nil || !strings.Contains(err.Error(), tc.expectedErrorString) {
t.Errorf("expected error containing '%s' for providerID '%s', but got no error or different error: %v", tc.expectedErrorString, tc.providerID, err)
}
}
})
}
}

func informerSynced() bool {
return true
}
Expand Down

0 comments on commit 723d101

Please sign in to comment.