Skip to content

Commit 60400e6

Browse files
jbornemannprydie
authored andcommitted
Runtime checks around providerID (#228)
* Runtime checks around providerID * Added additional required authorization for security lists to example
1 parent 2040fd6 commit 60400e6

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

manifests/cloud-provider-example.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ loadBalancer:
3535
subnet2: ocid1.subnet.oc1.phx.aaaaaaaahuxrgvs65iwdz7ekwgg3l5gyah7ww5klkwjcso74u3e4i64hvtvq
3636

3737
# SecurityListManagementMode configures how security lists are managed by the CCM.
38+
# If you choose to have security lists managed by the CCM, ensure you have setup the following additional OCI policy:
39+
# Allow dynamic-group [your dynamic group name] to manage security-lists in compartment [your compartment name]
40+
#
3841
# "All" (default): Manage all required security list rules for load balancer services.
3942
# "Frontend": Manage only security list rules for ingress to the load
4043
# balancer. Requires that the user has setup a rule that

pkg/oci/instances.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
9797
// in this method to obtain nodeaddresses.
9898
func (cp *CloudProvider) NodeAddressesByProviderID(ctx context.Context, providerID string) ([]api.NodeAddress, error) {
9999
cp.logger.With("instanceID", providerID).Debug("Getting node addresses by provider id")
100-
instanceID := util.MapProviderIDToInstanceID(providerID)
100+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
101+
if err != nil {
102+
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
103+
}
101104
vnic, err := cp.client.Compute().GetPrimaryVNICForInstance(ctx, cp.config.CompartmentID, instanceID)
102105
if err != nil {
103106
return nil, errors.Wrap(err, "GetPrimaryVNICForInstance")
@@ -157,7 +160,10 @@ func (cp *CloudProvider) InstanceType(ctx context.Context, name types.NodeName)
157160
func (cp *CloudProvider) InstanceTypeByProviderID(ctx context.Context, providerID string) (string, error) {
158161
cp.logger.With("instanceID", providerID).Debug("Getting instance type by provider id")
159162

160-
instanceID := util.MapProviderIDToInstanceID(providerID)
163+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
164+
if err != nil {
165+
return "", errors.Wrap(err, "MapProviderIDToInstanceID")
166+
}
161167
inst, err := cp.client.Compute().GetInstance(ctx, instanceID)
162168
if err != nil {
163169
return "", errors.Wrap(err, "GetInstance")
@@ -183,7 +189,10 @@ func (cp *CloudProvider) CurrentNodeName(ctx context.Context, hostname string) (
183189
// instance will be immediately deleted by the cloud controller manager.
184190
func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, providerID string) (bool, error) {
185191
cp.logger.With("instanceID", providerID).Debug("Checking instance exists by provider id")
186-
instanceID := util.MapProviderIDToInstanceID(providerID)
192+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
193+
if err != nil {
194+
return false, err
195+
}
187196
instance, err := cp.client.Compute().GetInstance(ctx, instanceID)
188197
if client.IsNotFound(err) {
189198
return false, nil

pkg/oci/load_balancer.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,15 @@ func getSubnetsForNodes(ctx context.Context, nodes []*v1.Node, client client.Int
149149
continue
150150
}
151151

152-
id := util.MapProviderIDToInstanceID(node.Spec.ProviderID)
152+
if node.Spec.ProviderID == "" {
153+
return nil, errors.Errorf(".spec.providerID was not present on node %q", node.Name)
154+
}
155+
156+
id, err := util.MapProviderIDToInstanceID(node.Spec.ProviderID)
157+
if err != nil {
158+
return nil, errors.Wrap(err, "MapProviderIDToInstanceID")
159+
}
160+
153161
vnic, err := client.Compute().GetPrimaryVNICForInstance(ctx, compartmentID, id)
154162
if err != nil {
155163
return nil, err

pkg/oci/util/util.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package util
1717
import (
1818
"strings"
1919

20+
"github.com/pkg/errors"
2021
api "k8s.io/api/core/v1"
2122
)
2223

@@ -28,11 +29,14 @@ const (
2829
)
2930

3031
// MapProviderIDToInstanceID parses the provider id and returns the instance ocid.
31-
func MapProviderIDToInstanceID(providerID string) string {
32+
func MapProviderIDToInstanceID(providerID string) (string, error) {
33+
if providerID == "" {
34+
return providerID, errors.New("provider ID is empty")
35+
}
3236
if strings.HasPrefix(providerID, providerPrefix) {
33-
return strings.TrimPrefix(providerID, providerPrefix)
37+
return strings.TrimPrefix(providerID, providerPrefix), nil
3438
}
35-
return providerID
39+
return providerID, nil
3640
}
3741

3842
// NodeInternalIP returns the nodes internal ip

pkg/oci/util/util_test.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,34 @@ import "testing"
1919
func TestMapProviderIDToInstanceID(t *testing.T) {
2020
testCases := map[string]struct {
2121
providerID string
22-
expected string
22+
instanceID string
23+
error bool
2324
}{
2425
"no cloud prefix": {
2526
providerID: "testid",
26-
expected: "testid",
27+
instanceID: "testid",
28+
error: false,
2729
},
2830
"cloud prefix": {
2931
providerID: providerPrefix + "testid",
30-
expected: "testid",
32+
instanceID: "testid",
33+
error: false,
34+
},
35+
"empty string": {
36+
providerID: "",
37+
instanceID: "",
38+
error: true,
3139
},
3240
}
3341

3442
for name, tc := range testCases {
3543
t.Run(name, func(t *testing.T) {
36-
result := MapProviderIDToInstanceID(tc.providerID)
37-
if result != tc.expected {
38-
t.Errorf("Expected instance id %q, but got %q", tc.expected, result)
44+
result, err := MapProviderIDToInstanceID(tc.providerID)
45+
if result != tc.instanceID {
46+
t.Errorf("Expected instance id %q, but got %q", tc.instanceID, result)
47+
}
48+
if (err == nil && tc.error) || (!tc.error && err != nil) {
49+
t.Errorf("Expected an error condition for input %q, but did no receive one; or received one, when not expecting", tc.providerID)
3950
}
4051
})
4152
}

pkg/oci/zones.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ func (cp *CloudProvider) GetZone(ctx context.Context) (cloudprovider.Zone, error
4747
// particularly used in the context of external cloud providers where node
4848
// initialization must be down outside the kubelets.
4949
func (cp *CloudProvider) GetZoneByProviderID(ctx context.Context, providerID string) (cloudprovider.Zone, error) {
50-
instanceID := util.MapProviderIDToInstanceID(providerID)
50+
instanceID, err := util.MapProviderIDToInstanceID(providerID)
51+
if err != nil {
52+
return cloudprovider.Zone{}, err
53+
}
5154
instance, err := cp.client.Compute().GetInstance(ctx, instanceID)
5255
if err != nil {
5356
return cloudprovider.Zone{}, err

0 commit comments

Comments
 (0)