-
Notifications
You must be signed in to change notification settings - Fork 174
[ROX-12622] : Add create collection service #3600
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,19 +2,23 @@ package service | |
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
|
|
||
| "github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
| "github.com/pkg/errors" | ||
| "github.com/stackrox/rox/central/resourcecollection/datastore" | ||
| "github.com/stackrox/rox/central/role/resources" | ||
| "github.com/stackrox/rox/central/vulnerabilityrequest/utils" | ||
| v1 "github.com/stackrox/rox/generated/api/v1" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/auth/permissions" | ||
| "github.com/stackrox/rox/pkg/errox" | ||
| "github.com/stackrox/rox/pkg/features" | ||
| "github.com/stackrox/rox/pkg/grpc/authz" | ||
| "github.com/stackrox/rox/pkg/grpc/authz/or" | ||
| "github.com/stackrox/rox/pkg/grpc/authz/perrpc" | ||
| "github.com/stackrox/rox/pkg/grpc/authz/user" | ||
| "github.com/stackrox/rox/pkg/protoconv" | ||
| "google.golang.org/grpc" | ||
| ) | ||
|
|
||
|
|
@@ -28,14 +32,21 @@ var ( | |
| }, | ||
| user.With(permissions.Modify(resources.WorkflowAdministration)): { | ||
| // "/v1.CollectionService/AutoCompleteCollection", TODO ROX-12616 | ||
| // "/v1.CollectionService/CreateCollection", TODO ROX-12622 | ||
| "/v1.CollectionService/CreateCollection", | ||
| // "/v1.CollectionService/DeleteCollection", TODO ROX-13030 | ||
| // "/v1.CollectionService/DryRunCollection", TODO ROX-13031 | ||
| // "/v1.CollectionService/UpdateCollection", TODO ROX-13032 | ||
| }, | ||
| })) | ||
| ) | ||
|
|
||
| type collectionRequest interface { | ||
| GetName() string | ||
| GetDescription() string | ||
| GetResourceSelectors() []*storage.ResourceSelector | ||
| GetEmbeddedCollectionIds() []string | ||
| } | ||
|
|
||
| // serviceImpl is the struct that manages the collection API | ||
| type serviceImpl struct { | ||
| v1.UnimplementedCollectionServiceServer | ||
|
|
@@ -83,3 +94,64 @@ func (s *serviceImpl) getCollection(ctx context.Context, id string) (*v1.GetColl | |
| Deployments: nil, | ||
| }, nil | ||
| } | ||
|
|
||
| // CreateCollection creates a new collection from the given request | ||
| func (s *serviceImpl) CreateCollection(ctx context.Context, request *v1.CreateCollectionRequest) (*v1.CreateCollectionResponse, error) { | ||
| if !features.ObjectCollections.Enabled() { | ||
| return nil, errors.New("Resource collections is not enabled") | ||
| } | ||
|
|
||
| collection, err := collectionRequestToCollection(ctx, request, true, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = s.datastore.AddCollection(ctx, collection) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &v1.CreateCollectionResponse{Collection: collection}, nil | ||
| } | ||
|
|
||
| func collectionRequestToCollection(ctx context.Context, request collectionRequest, isCreate bool, getID func() string) (*storage.ResourceCollection, error) { | ||
| if request.GetName() == "" { | ||
| return nil, errors.Wrap(errox.InvalidArgs, "Collection name should not be empty") | ||
| } | ||
|
|
||
| slimUser := utils.UserFromContext(ctx) | ||
| if slimUser == nil { | ||
| return nil, errors.New("Could not determine user identity from provided context") | ||
| } | ||
|
|
||
| if len(request.GetResourceSelectors()) == 0 { | ||
| return nil, errors.Wrap(errox.InvalidArgs, "No resource selectors were provided") | ||
| } | ||
|
|
||
| timeNow := protoconv.ConvertTimeToTimestamp(time.Now()) | ||
|
|
||
| collection := &storage.ResourceCollection{ | ||
| Name: request.GetName(), | ||
| Description: request.GetDescription(), | ||
| LastUpdated: timeNow, | ||
| UpdatedBy: slimUser, | ||
| ResourceSelectors: request.GetResourceSelectors(), | ||
| } | ||
|
|
||
| if isCreate { | ||
| collection.CreatedBy = slimUser | ||
| collection.CreatedAt = timeNow | ||
| } else if getID != nil { | ||
| collection.Id = getID() | ||
| } | ||
|
Comment on lines
+144
to
+146
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. implying
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I merged this PR before noticing the comment. I will add it in my Delete collections service PR. Should I change to something like below? |
||
|
|
||
| if len(request.GetEmbeddedCollectionIds()) > 0 { | ||
| embeddedCollections := make([]*storage.ResourceCollection_EmbeddedResourceCollection, 0, len(request.GetEmbeddedCollectionIds())) | ||
| for _, id := range request.GetEmbeddedCollectionIds() { | ||
| embeddedCollections = append(embeddedCollections, &storage.ResourceCollection_EmbeddedResourceCollection{Id: id}) | ||
| } | ||
| collection.EmbeddedCollections = embeddedCollections | ||
| } | ||
|
|
||
| return collection, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
valid resource selectors are non-empty
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added an error on empty resource selectors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@keyallis