Skip to content

Commit

Permalink
feat(#227): add keys validation and silently drop forbidden keys
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanHCB committed Nov 8, 2023
1 parent a83b62b commit 5b2a0b5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
46 changes: 46 additions & 0 deletions internal/service/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,21 @@ func (s *Impl) GetRepository(ctx context.Context, repoKey string) (openapi.Repos
}
}

if err == nil && repositoryDto.Filecategory != nil {
// filter by allowed keys
allowedKeys := s.CustomConfiguration.AllowedFileCategories()
for key, _ := range *repositoryDto.Filecategory {
if !sliceContains(allowedKeys, key) {
delete(*repositoryDto.Filecategory, key)
}
}

if len(*repositoryDto.Filecategory) == 0 {
// drop empty map completely
repositoryDto.Filecategory = nil
}
}

return repositoryDto, err
}

Expand Down Expand Up @@ -240,6 +255,9 @@ func (s *Impl) validateRepositoryCreateDto(ctx context.Context, key string, dto
if dto.JiraIssue == "" {
messages = append(messages, "field jiraIssue is mandatory")
}
if dto.Filecategory != nil {
messages = s.validateFilecategory(messages, *dto.Filecategory)
}

if len(messages) > 0 {
details := strings.Join(messages, ", ")
Expand Down Expand Up @@ -306,6 +324,9 @@ func (s *Impl) validateExistingRepositoryDto(ctx context.Context, key string, dt
if dto.JiraIssue == "" {
messages = append(messages, "field jiraIssue is mandatory for updates")
}
if dto.Filecategory != nil {
messages = s.validateFilecategory(messages, *dto.Filecategory)
}

if len(messages) > 0 {
details := strings.Join(messages, ", ")
Expand Down Expand Up @@ -370,6 +391,9 @@ func (s *Impl) validateRepositoryPatchDto(ctx context.Context, key string, patch
messages = validateOwner(messages, dto.Owner)
messages = validateUrl(messages, dto.Url)
messages = validateMainline(messages, dto.Mainline)
if dto.Filecategory != nil {
messages = s.validateFilecategory(messages, *dto.Filecategory)
}

Check warning on line 396 in internal/service/repositories/repositories.go

View check run for this annotation

Codecov / codecov/patch

internal/service/repositories/repositories.go#L395-L396

Added lines #L395 - L396 were not covered by tests

if patchDto.CommitHash == "" {
messages = append(messages, "field commitHash is mandatory for patching")
Expand All @@ -380,6 +404,7 @@ func (s *Impl) validateRepositoryPatchDto(ctx context.Context, key string, patch
if patchDto.JiraIssue == "" {
messages = append(messages, "field jiraIssue is mandatory for patching")
}

if len(messages) > 0 {
details := strings.Join(messages, ", ")
s.Logging.Logger().Ctx(ctx).Info().Printf("repository values invalid: %s", details)
Expand Down Expand Up @@ -616,3 +641,24 @@ func validateMainline(messages []string, mainline string) []string {
}
return messages
}

func (s *Impl) validateFilecategory(messages []string, filecategories map[string][]string) []string {
allowedCategories := s.CustomConfiguration.AllowedFileCategories()

for category, _ := range filecategories {
if !sliceContains(allowedCategories, category) {
messages = append(messages, fmt.Sprintf("filecategory keys must be one of %s", strings.Join(allowedCategories, ",")))
}

Check warning on line 651 in internal/service/repositories/repositories.go

View check run for this annotation

Codecov / codecov/patch

internal/service/repositories/repositories.go#L650-L651

Added lines #L650 - L651 were not covered by tests
}

return messages
}

func sliceContains[T comparable](haystack []T, needle T) bool {
for _, e := range haystack {
if e == needle {
return true
}
}
return false
}
3 changes: 3 additions & 0 deletions test/mock/metadatamock/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ configuration:
const deployment2 = `mainline: main
url: ssh://[email protected]:7999/PROJECT/whatever-deployment.git
generator: third-party-software
filecategory:
forbidden-key:
- some/interesting/file.txt
`

const implementation = `mainline: master
Expand Down
2 changes: 1 addition & 1 deletion test/resources/valid-config-unique.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ REPOSITORY_TYPES: 'some-type,some-other-type'

NOTIFICATION_CONSUMER_CONFIGS: "{}"

ALLOWED_FILE_CATEGORIES: '["yaml-template"]'
ALLOWED_FILE_CATEGORIES: ''
2 changes: 1 addition & 1 deletion test/resources/valid-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ NOTIFICATION_CONSUMER_CONFIGS: >-
}
}
ALLOWED_FILE_CATEGORIES: '["a","b"]'
ALLOWED_FILE_CATEGORIES: '["cached-template"]'

0 comments on commit 5b2a0b5

Please sign in to comment.