Skip to content

feat(parser): add TransferFamilySchema for AWS Transfer Family events #3575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/utilities/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Parser comes with the following built-in schemas:
| **SesSchema** | Lambda Event Source payload for Amazon Simple Email Service |
| **SnsSchema** | Lambda Event Source payload for Amazon Simple Notification Service |
| **SqsSchema** | Lambda Event Source payload for Amazon SQS |
| **TransferFamilySchema** | Lambda Event Source payload for AWS Transfer Family events |
| **VpcLatticeSchema** | Lambda Event Source payload for Amazon VPC Lattice |
| **VpcLatticeV2Schema** | Lambda Event Source payload for Amazon VPC Lattice v2 payload |

Expand Down
1 change: 1 addition & 0 deletions packages/parser/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ export {
SnsNotificationSchema,
} from './sns.js';
export { SqsSchema, SqsRecordSchema } from './sqs.js';
export { TransferFamilySchema } from './transfer-family.js';
export { VpcLatticeSchema } from './vpc-lattice.js';
export { VpcLatticeV2Schema } from './vpc-latticev2.js';
25 changes: 25 additions & 0 deletions packages/parser/src/schemas/transfer-family.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from 'zod';

/**
* TransferFamilySchema validates events coming from AWS Transfer Family.
*/
export const TransferFamilySchema = z.object({
username: z.string(),
password: z.string(),
protocol: z.string(),
serverId: z.string(),
// Validates that sourceIp is a valid IPv4/IPv6 string. Adjust as needed.
sourceIp: z
.string()
.refine(
(ip) =>
/^(\d{1,3}\.){3}\d{1,3}$/.test(ip) ||
!!ip.match(/^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}$/i),
{ message: 'Invalid IP address' }
),
});

/**
* Type alias for TransferFamilyEvent, inferred from this schema.
*/
export type TransferFamilyEvent = z.infer<typeof TransferFamilySchema>;
4 changes: 4 additions & 0 deletions packages/parser/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {
SnsSqsNotificationSchema,
SqsRecordSchema,
SqsSchema,
TransferFamilySchema,
VpcLatticeSchema,
VpcLatticeV2Schema,
} from '../schemas/index.js';
Expand Down Expand Up @@ -129,6 +130,8 @@ type SqsEvent = z.infer<typeof SqsSchema>;

type SqsRecord = z.infer<typeof SqsRecordSchema>;

type TransferFamilyEvent = z.infer<typeof TransferFamilySchema>;

type VpcLatticeEvent = z.infer<typeof VpcLatticeSchema>;

type VpcLatticeEventV2 = z.infer<typeof VpcLatticeV2Schema>;
Expand Down Expand Up @@ -171,6 +174,7 @@ export type {
SnsRecord,
SqsEvent,
SqsRecord,
TransferFamilyEvent,
VpcLatticeEvent,
VpcLatticeEventV2,
};
8 changes: 8 additions & 0 deletions packages/parser/tests/events/transfer-family/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"username": "value",
"password": "value",
"protocol": "SFTP",
"serverId": "s-abcd123456",
"sourceIp": "192.168.0.100"
}

30 changes: 30 additions & 0 deletions packages/parser/tests/unit/schema/transfer-family.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { describe, expect, it } from 'vitest'; // or jest, depending on the project configuration
import { TransferFamilySchema } from '../../../src/schemas/transfer-family';

describe('TransferFamilySchema', () => {
it('should validate a valid TransferFamily event', () => {
const validEvent = {
username: 'testUser',
password: 'testPass',
protocol: 'SFTP',
serverId: 's-abcd123456',
sourceIp: '192.168.0.100',
};

// This should not throw an error if the event is valid.
expect(() => TransferFamilySchema.parse(validEvent)).not.toThrow();
});

it('should throw an error for an invalid TransferFamily event', () => {
const invalidEvent = {
username: 'testUser',
// missing password
protocol: 'SFTP',
serverId: 's-abcd123456',
sourceIp: 'invalid-ip',
};

// This should throw an error due to missing fields / invalid IP.
expect(() => TransferFamilySchema.parse(invalidEvent)).toThrow();
});
});