Repository: serverless/serverless (JavaScript)

Issue:
Wrong ALB event ip condition validation
### Are you certain it's a bug?

- [X] Yes, it looks like a bug

### Is the issue caused by a plugin?

- [X] It is not a plugin issue

### Are you using the latest v3 release?

- [X] Yes, I'm using the latest v3 release

### Is there an existing issue for this?

- [X] I have searched existing issues, it hasn't been reported yet

### Issue description

CIDR blocs are required inside alb event's ip IPv4 and IPv6 declaration.

<img width="494" alt="Screenshot 2023-04-01 at 23 57 11" src="https://user-images.githubusercontent.com/69360379/229315845-95cd2a02-618d-4828-9eb8-0b40d03b3e08.png">


Currently Serverless schema validator prints a warning message when we include CIDR into ip declaration.

### Service configuration (serverless.yml) content

```yaml
service: sls-project

frameworkVersion: "3"

package:
  individually: true

provider:
  name: aws
  runtime: nodejs18.x
  versionFunctions: false
  region: eu-west-3
  vpc:
    subnetIds: [subnet-0e2db7f9558f501d7, subnet-0cde9f3600cab039a]
    securityGroupIds: [sg-029d2fbba38db2dc7]

functions:
  lambda:
    handler: src/handlers/lambda.default
    events:
      - alb:
          listenerArn: arn:aws:elasticloadbalancing:xxxx
          priority: 936
          conditions:
            path: "/users"
            method: GET
            ip:
              - fe80:0000:0000:0000:0204:61ff:fe9d:f156/6
              - 192.168.0.1/0
```


### Command name and used flags

serverless package

### Command output

```shell
Warning: Invalid configuration encountered
  at 'functions.lambda.events.0.alb.conditions.ip.0': unsupported string format
  at 'functions.lambda.events.0.alb.conditions.ip.1': unsupported string format

Learn more about configuration validation here: http://slss.io/configuration-validation
```


### Environment information

```shell
Framework Core: 3.29.0 (local)
Plugin: 6.2.3
SDK: 4.3.2
```



Discussion:
@Inqnuam I'm not sure if I understand this report right.

Is the issue that the Framework schema does not allow the CIDR format, which is supported on the ALB side?

If that's the case, and fix is not trivial, let's relax schema to allow any string there (and let to the point validation be done on AWS side)
> Is the issue that the Framework schema does not allow the CIDR format, which is supported on the ALB? 

Yes Sir!
It is not only supported but **required**!

As the Regex for IPv6 + CIDR is too heavy I made a new PR which allows any string for ip, and checks uniqueness of values as required by AWS



Patch:
diff --git a/lib/plugins/aws/package/compile/events/alb/index.js b/lib/plugins/aws/package/compile/events/alb/index.js
index 933dcff28e6..694f6f2919c 100644
--- a/lib/plugins/aws/package/compile/events/alb/index.js
+++ b/lib/plugins/aws/package/compile/events/alb/index.js
@@ -7,8 +7,8 @@ const compileTargetGroups = require('./lib/target-groups');
 const compileListenerRules = require('./lib/listener-rules');
 const compilePermissions = require('./lib/permissions');
 
-function defineArray(schema) {
-  return { type: 'array', items: schema };
+function defineArray(schema, options = {}) {
+  return { type: 'array', items: schema, ...options };
 }
 
 const ALB_HTTP_HEADER_SCHEMA = {
@@ -55,12 +55,7 @@ class AwsCompileAlbEvents {
               pattern: '^[A-Za-z0-9*?.-]+$',
               maxLength: 128,
             }),
-            ip: defineArray({
-              anyOf: [
-                { type: 'string', format: 'ipv4' },
-                { type: 'string', format: 'ipv6' },
-              ],
-            }),
+            ip: defineArray({ type: 'string' }, { uniqueItems: true }),
             method: defineArray({ type: 'string', pattern: '^[A-Z_-]+$', maxLength: 40 }),
             path: defineArray({
               type: 'string',


Test patch:
diff --git a/test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js b/test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js
index 9679d706e01..8e27c8f269b 100644
--- a/test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js
+++ b/test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js
@@ -123,6 +123,20 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
               },
             ],
           },
+          fnIpCondition: {
+            handler: 'index.handler',
+            events: [
+              {
+                alb: {
+                  ...validBaseEventConfig,
+                  priority: 10,
+                  conditions: {
+                    ip: ['fe80:0000:0000:0000:0204:61ff:fe9d:f156/6', '192.168.0.1/0'],
+                  },
+                },
+              },
+            ],
+          },
         },
       },
     });
@@ -347,4 +361,18 @@ describe('test/unit/lib/plugins/aws/package/compile/events/alb/index.test.js', (
       expect(config3.Values[1]).to.equal('dummyMultiValue2');
     });
   });
+
+  describe('should set alb ip conditions', () => {
+    it('should allow IP CIDR format', () => {
+      const albListenerRuleLogicalId = naming.getAlbListenerRuleLogicalId('fnIpCondition', 10);
+      const conditions = cfResources[albListenerRuleLogicalId].Properties.Conditions;
+
+      expect(conditions).to.have.length(1);
+      const config = conditions[0].SourceIpConfig;
+      expect(config.Values).to.deep.equal([
+        'fe80:0000:0000:0000:0204:61ff:fe9d:f156/6',
+        '192.168.0.1/0',
+      ]);
+    });
+  });
 });