Skip to content

Commit

Permalink
MGMT-14726: Ensure that manifest filename does not contain spaces.
Browse files Browse the repository at this point in the history
Ensure that in any cases where we are writing or updating a manifest
file that the filename does not contain any spaces.
  • Loading branch information
paul-maidment committed Jul 6, 2023
1 parent fcc7514 commit 4a0c17d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
10 changes: 9 additions & 1 deletion internal/manifests/manifests.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,19 @@ func (m *Manifests) fetchManifestContent(ctx context.Context, clusterID strfmt.U

func (m *Manifests) validateManifestFileNames(ctx context.Context, clusterID strfmt.UUID, fileNames []string) error {
for _, fileName := range fileNames {
if strings.Contains(fileName, " ") {
return m.prepareAndLogError(
ctx,
http.StatusBadRequest,
errors.Errorf("Cluster manifest %s for cluster %s should not include a space in it's name.",
fileName,
clusterID))
}
if strings.ContainsRune(fileName, os.PathSeparator) {
return m.prepareAndLogError(
ctx,
http.StatusBadRequest,
errors.Errorf("Cluster manifest %s for cluster %s should not include a directory in its name.",
errors.Errorf("Cluster manifest %s for cluster %s should not include a directory in it's name.",
fileName,
clusterID))
}
Expand Down
38 changes: 37 additions & 1 deletion internal/manifests/manifests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,23 @@ var _ = Describe("ClusterManifestTests", func() {
return fmt.Sprintf("{\"data\":\"%s\"}", largeElementBuilder.String())
}

It("Does not accept a filename that contains spaces", func(){
clusterID := registerCluster().ID
content := "{}"
fileName := "dest FileName"
response := manifestsAPI.V2CreateClusterManifest(ctx, operations.V2CreateClusterManifestParams{
ClusterID: *clusterID,
CreateManifestParams: &models.CreateManifestParams{
Content: &content,
FileName: &fileName,
},
})
err := response.(*common.ApiErrorResponse)
expectedErrorMessage := fmt.Sprintf("Cluster manifest %s for cluster %s should not include a space in it's name.", fileName, clusterID)
Expect(err.StatusCode()).To(Equal(int32(http.StatusBadRequest)))
Expect(err.Error()).To(Equal(expectedErrorMessage))
})

It("accepts manifest in json format and .json extension", func() {
clusterID := registerCluster().ID
jsonContent := encodeToBase64(contentAsJSON)
Expand Down Expand Up @@ -417,7 +434,7 @@ spec:
Expect(response).Should(BeAssignableToTypeOf(common.NewApiError(http.StatusBadRequest, errors.New(""))))
err := response.(*common.ApiErrorResponse)
Expect(err.StatusCode()).To(Equal(int32(http.StatusBadRequest)))
Expect(err.Error()).To(ContainSubstring("Cluster manifest openshift/99-test.yaml for cluster " + clusterID.String() + " should not include a directory in its name."))
Expect(err.Error()).To(ContainSubstring("Cluster manifest openshift/99-test.yaml for cluster " + clusterID.String() + " should not include a directory in it's name."))
})

It("Creation fails for a manifest file that exceeds the maximum upload size", func() {
Expand Down Expand Up @@ -637,6 +654,25 @@ spec:
})

Context("UpdateClusterManifest", func() {
It("Does not accept a filename that contains spaces", func(){
clusterID := registerCluster().ID
destFolder := "manifests"
destFileName := "dest FileName"
response := manifestsAPI.V2UpdateClusterManifest(ctx, operations.V2UpdateClusterManifestParams{
ClusterID: *clusterID,
UpdateManifestParams: &models.UpdateManifestParams{
FileName: fileNameYaml,
Folder: defaultFolder,
UpdatedFolder: &destFolder,
UpdatedFileName: &destFileName,
},
})
err := response.(*common.ApiErrorResponse)
expectedErrorMessage := fmt.Sprintf("Cluster manifest %s for cluster %s should not include a space in it's name.", destFileName, clusterID)
Expect(err.StatusCode()).To(Equal(int32(http.StatusBadRequest)))
Expect(err.Error()).To(Equal(expectedErrorMessage))
})

It("moves existing file from one path to another", func() {
clusterID := registerCluster().ID
destFolder := "manifests"
Expand Down

0 comments on commit 4a0c17d

Please sign in to comment.