Skip to content
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

feat: accept traits on create central #1689

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 12 additions & 12 deletions .secrets.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@
"filename": "internal/dinosaur/pkg/api/public/api/openapi.yaml",
"hashed_secret": "5b455797b93de5b6a19633ba22127c8a610f5c1b",
"is_verified": false,
"line_number": 1535
"line_number": 1543
}
],
"internal/dinosaur/pkg/services/dinosaurservice_moq.go": [
Expand Down Expand Up @@ -493,70 +493,70 @@
"filename": "templates/service-template.yml",
"hashed_secret": "13032f402fed753c2248419ea4f69f99931f6dbc",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "30025f80f6e22cdafb85db387d50f90ea884576a",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "355f24fd038bcaf85617abdcaa64af51ed19bbcf",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "3d8a1dcd2c3c765ce35c9a9552d23273cc4ddace",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "4ac7b0522761eba972467942cd5cd7499dd2c361",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "7639ab2a6bcf2ea30a055a99468c9cd844d4c22a",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "b56360daf4793d2a74991a972b34d95bc00fb2da",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "c9a73ef9ee8ce9f38437227801c70bcc6740d1a1",
"is_verified": false,
"line_number": 524
"line_number": 540
},
{
"type": "Base64 High Entropy String",
"filename": "templates/service-template.yml",
"hashed_secret": "14736999d9940728c5294277831a702f7882dece",
"is_verified": false,
"line_number": 561
"line_number": 577
},
{
"type": "Secret Keyword",
"filename": "templates/service-template.yml",
"hashed_secret": "4e199b4a1c40b497a95fcd1cd896351733849949",
"is_verified": false,
"line_number": 708,
"line_number": 724,
"is_secret": false
}
],
Expand Down Expand Up @@ -586,5 +586,5 @@
}
]
},
"generated_at": "2024-02-05T19:02:34Z"
"generated_at": "2024-03-05T08:39:44Z"
}
8 changes: 8 additions & 0 deletions internal/dinosaur/pkg/api/admin/private/api/openapi.yaml

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.

8 changes: 8 additions & 0 deletions internal/dinosaur/pkg/api/public/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,9 @@ components:
description: Schema for the request body sent to /centrals POST
example:
cloud_account_id: cloud_account_id
traits:
- traits
- traits
multi_az: true
name: name
cloud_provider: cloud_provider
Expand All @@ -1310,6 +1313,11 @@ components:
description: The region where the Central component cluster will be created
in
type: string
traits:
description: Set of central traits
items:
type: string
type: array
required:
- name
type: object
Expand Down

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

4 changes: 2 additions & 2 deletions internal/dinosaur/pkg/generated/bindata.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/dinosaur/pkg/handlers/admin_dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (h adminCentralHandler) Create(w http.ResponseWriter, r *http.Request) {
ValidateDinosaurClaims(ctx, &centralRequest, &convCentral),
ValidateCloudProvider(&h.service, &convCentral, h.providerConfig, "creating central requests"),
handlers.ValidateMultiAZEnabled(&centralRequest.MultiAz, "creating central requests"),
ValidateDinosaurTraits(&centralRequest, &convCentral),
},
Action: func() (interface{}, *errors.ServiceError) {
svcErr := h.service.RegisterDinosaurJob(ctx, &convCentral)
Expand Down
1 change: 1 addition & 0 deletions internal/dinosaur/pkg/handlers/dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (h dinosaurHandler) Create(w http.ResponseWriter, r *http.Request) {
ValidateDinosaurClaims(ctx, &centralRequest, convCentral),
ValidateCloudProvider(&h.service, convCentral, h.providerConfig, "creating central requests"),
handlers.ValidateMultiAZEnabled(&centralRequest.MultiAz, "creating central requests"),
ValidateDinosaurTraits(&centralRequest, convCentral),
},
Action: func() (interface{}, *errors.ServiceError) {
// Set the central request as internal, **iff** the user agent used within the creation request is contained
Expand Down
15 changes: 15 additions & 0 deletions internal/dinosaur/pkg/handlers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stackrox/acs-fleet-manager/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/handlers"
coreServices "github.com/stackrox/acs-fleet-manager/pkg/services"
"github.com/stackrox/acs-fleet-manager/pkg/shared/utils/arrays"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -126,3 +127,17 @@ func validateResourceName(name corev1.ResourceName) (corev1.ResourceName, bool)
}
return "", false
}

func ValidateDinosaurTraits(dinosaurRequestPayload *public.CentralRequestPayload, dinosaurRequest *dbapi.CentralRequest) handlers.Validate {
return func() *errors.ServiceError {
for _, trait := range dinosaurRequestPayload.Traits {
if !validTraitRegexp.MatchString(trait) {
return errors.FailedToParseQueryParms("bad trait %q", trait)
}
if !arrays.Contains(dinosaurRequest.Traits, trait) {
dinosaurRequest.Traits = append(dinosaurRequest.Traits, trait)
}
}
return nil
}
}
37 changes: 37 additions & 0 deletions internal/dinosaur/pkg/handlers/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"testing"

"github.com/lib/pq"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/dinosaurs/types"

"github.com/onsi/gomega"
Expand Down Expand Up @@ -335,3 +336,39 @@ func Test_Validation_validateCloudProvider(t *testing.T) {
})
}
}

func Test_Validations_validateDinosaurTraits(t *testing.T) {
tests := map[string]struct {
traits []string
expectedTraits pq.StringArray
expectedError *errors.ServiceError
}{
"no traits": {},
"some traits": {
[]string{"test"},
pq.StringArray{"test"},
nil,
},
"bad trait": {
[]string{"good_trait", "bad/trait", "another_trait"},
pq.StringArray{"good_trait"},
errors.FailedToParseQueryParms(`bad trait "bad/trait"`),
},
"more traits": {
[]string{"test1", "test2", "test1"},
pq.StringArray{"test1", "test2"},
nil,
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
gomega.RegisterTestingT(t)

output := dbapi.CentralRequest{}
validateFn := ValidateDinosaurTraits(&public.CentralRequestPayload{Traits: test.traits}, &output)
svcErr := validateFn()
gomega.Expect(svcErr).To(gomega.BeEquivalentTo(test.expectedError))
gomega.Expect(output.Traits).To(gomega.Equal(test.expectedTraits))
})
}
}
5 changes: 5 additions & 0 deletions openapi/fleet-manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,11 @@ components:
region:
description: The region where the Central component cluster will be created in
type: string
traits:
description: Set of central traits
type: array
items:
type: string
CloudProviderList:
allOf:
- $ref: "#/components/schemas/List"
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func ValidateRegex(r *http.Request, field string, regex *regexp.Regexp) Validate
return func() *errors.ServiceError {
value := mux.Vars(r)[field]
if !regex.MatchString(value) {
return errors.MalformedServiceAccountName("%s %q does not match %s", field, value, regex.String())
return errors.FailedToParseQueryParms("%s %q does not match %s", field, value, regex.String())
}
return nil
}
Expand Down
Loading