Skip to content

Commit

Permalink
Validating file system type for EBS-backed task attachment payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mye956 committed Oct 10, 2023
1 parent f70a1a1 commit 9422ad4
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 0 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ecs-agent/acs/session/attach_resource_responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func validateAttachmentAndReturnProperties(message *ecsacs.ConfirmAttachmentMess
if err != nil {
return nil, errors.Wrap(err, "resource attachment validation by attachment type failed")
}
err = resource.ValidateFileSystemType(attachmentProperties[resource.FileSystemKey])
if err != nil {
return nil, errors.Wrap(err, "resource attachment validation by file system property failed")
}
return attachmentProperties, nil
}

Expand Down
28 changes: 28 additions & 0 deletions ecs-agent/acs/session/attach_resource_responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ var (
Name: aws.String(resource.DeviceNameKey),
Value: aws.String("device1"),
},
{
Name: aws.String(resource.FileSystemKey),
Value: aws.String("ext4"),
},
}

testAttachmentProperties = []*ecsacs.AttachmentProperty{
Expand Down Expand Up @@ -244,6 +248,30 @@ func testValidateAttachmentAndReturnPropertiesWithAttachmentType(t *testing.T) {
}
require.True(t, verified, "Missing required property: %s", requiredProperty)
}
validFileSystems := []string{"xfs", "ext2", "ext3", "ext4", "ntfs"}
for _, property := range confirmAttachmentMessageCopy.Attachment.AttachmentProperties {
if aws.StringValue(property.Name) == resource.FileSystemKey {
for _, fs := range validFileSystems {
originalPropertyValue := property.Value
property.Value = aws.String(fs)
_, err = validateAttachmentAndReturnProperties(&confirmAttachmentMessageCopy)
require.NoError(t, err)
property.Value = originalPropertyValue
}
originalPropertyValue := property.Value
property.Value = aws.String("SomeFilesystemType")
_, err = validateAttachmentAndReturnProperties(&confirmAttachmentMessageCopy)
require.Error(t, err)
property.Value = originalPropertyValue

originalPropertyValue = property.Value
property.Value = aws.String("")
_, err = validateAttachmentAndReturnProperties(&confirmAttachmentMessageCopy)
require.Error(t, err)
property.Value = originalPropertyValue
}
}

}

// TestResourceAckHappyPath tests the happy path for a typical ConfirmAttachmentMessage and confirms expected
Expand Down
10 changes: 10 additions & 0 deletions ecs-agent/api/resource/resource_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ const (
FileSystemKey = "fileSystem"
)

var (
AllowedFSTypes = map[string]bool{
"xfs": true,
"ext2": true,
"ext3": true,
"ext4": true,
"ntfs": true,
}
)

// getCommonProperties returns the common properties as used for validating a resource.
func getCommonProperties() (commonProperties []string) {
commonProperties = []string{
Expand Down
9 changes: 9 additions & 0 deletions ecs-agent/api/resource/resource_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,12 @@ func ValidateRequiredProperties(actualProperties map[string]string, requiredProp
}
return nil
}

// For EBS-backed task attachment payload, the file system type is optional. If we do receive a file system type value,
// we want to validate what we receive is one of the following types [xfs, ext2, ext3, ext4, ntfs].
func ValidateFileSystemType(filesystemType string) error {
if filesystemType != "" && !AllowedFSTypes[filesystemType] {
return errors.Errorf("invalid file system type: %s", filesystemType)
}
return nil
}
12 changes: 12 additions & 0 deletions ecs-agent/api/resource/resource_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ func TestValidateVolumeResource(t *testing.T) {
})
}
}

func TestValidateVolumeFileSystem(t *testing.T) {
validFileSystems := []string{"xfs", "ext2", "ext3", "ext4", "ntfs"}

for _, fs := range validFileSystems {
err := ValidateFileSystemType(fs)
require.NoError(t, err)
}

err := ValidateFileSystemType("someFilesystem")
require.Error(t, err)
}

0 comments on commit 9422ad4

Please sign in to comment.