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 9, 2024
1 parent f650a39 commit dc77f67
Show file tree
Hide file tree
Showing 2 changed files with 24 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 @@ -245,6 +245,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 @@ -255,6 +260,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 instanceID == "" {
return "", fmt.Errorf("instance ID not found in providerID: %s", providerID)
}

return instanceID, nil
Expand Down
14 changes: 14 additions & 0 deletions cloud-controller-manager/osc/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1803,6 +1803,20 @@ func TestNodeNameToProviderID(t *testing.T) {
assert.NoError(t, err)
_, err = c.nodeNameToProviderID(testNodeName)
assert.NoError(t, err)

// Test case for invalid providerID format
testInvalidProviderID := "invalid-format"
err = c.nodeInformer.Informer().GetStore().Add(&v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: string(testNodeName) + "-invalid",
},
Spec: v1.NodeSpec{
ProviderID: testInvalidProviderID,
},
})
assert.NoError(t, err)
_, err = c.nodeNameToProviderID(types.NodeName(string(testNodeName) + "-invalid"))
assert.Error(t, err)
}

func informerSynced() bool {
Expand Down

0 comments on commit dc77f67

Please sign in to comment.