Skip to content

Commit

Permalink
[v16] User Tasks resource (#46754)
Browse files Browse the repository at this point in the history
* Add User Tasks resource - protos (#46059)

* Add User Integration Tasks resource - protos

* add account id

* move state to task instead of instance

* rename from user integration task to user task

* add instance id

* User Tasks: services and clients implementation (#46131)

This PR adds the implementation for the User Tasks:
- services (backend+cache)
- clients (API + tctl)
- light validation to set up the path for later PRs
  • Loading branch information
marcoandredinis authored Sep 30, 2024
1 parent 10db41f commit 64e32fb
Show file tree
Hide file tree
Showing 37 changed files with 3,618 additions and 289 deletions.
10 changes: 10 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/gravitational/teleport/api/client/secreport"
statichostuserclient "github.com/gravitational/teleport/api/client/statichostuser"
"github.com/gravitational/teleport/api/client/userloginstate"
usertaskapi "github.com/gravitational/teleport/api/client/usertask"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
accesslistv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/accesslist/v1"
Expand Down Expand Up @@ -87,6 +88,7 @@ import (
userloginstatev1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/userloginstate/v1"
userprovisioningpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/userprovisioning/v2"
userspb "github.com/gravitational/teleport/api/gen/proto/go/teleport/users/v1"
usertaskv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/usertasks/v1"
"github.com/gravitational/teleport/api/gen/proto/go/teleport/vnet/v1"
userpreferencespb "github.com/gravitational/teleport/api/gen/proto/go/userpreferences/v1"
"github.com/gravitational/teleport/api/internalutils/stream"
Expand Down Expand Up @@ -4782,6 +4784,14 @@ func (c *Client) UserLoginStateClient() *userloginstate.Client {
return userloginstate.NewClient(userloginstatev1.NewUserLoginStateServiceClient(c.conn))
}

// UserTasksServiceClient returns a UserTask client.
// Clients connecting to older Teleport versions, still get a UserTask client
// when calling this method, but all RPCs will return "not implemented" errors
// (as per the default gRPC behavior).
func (c *Client) UserTasksServiceClient() *usertaskapi.Client {
return usertaskapi.NewClient(usertaskv1.NewUserTaskServiceClient(c.conn))
}

// GetCertAuthority retrieves a CA by type and domain.
func (c *Client) GetCertAuthority(ctx context.Context, id types.CertAuthID, loadKeys bool) (types.CertAuthority, error) {
ca, err := c.TrustClient().GetCertAuthority(ctx, &trustpb.GetCertAuthorityRequest{
Expand Down
8 changes: 8 additions & 0 deletions api/client/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
machineidv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/machineid/v1"
notificationsv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/notifications/v1"
userprovisioningpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/userprovisioning/v2"
usertasksv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/usertasks/v1"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/accesslist"
accesslistv1conv "github.com/gravitational/teleport/api/types/accesslist/convert/v1"
Expand Down Expand Up @@ -99,6 +100,10 @@ func EventToGRPC(in types.Event) (*proto.Event, error) {
out.Resource = &proto.Event_StaticHostUserV2{
StaticHostUserV2: r,
}
case *usertasksv1.UserTask:
out.Resource = &proto.Event_UserTask{
UserTask: r,
}
default:
return nil, trace.BadParameter("resource type %T is not supported", r)
}
Expand Down Expand Up @@ -542,6 +547,9 @@ func EventFromGRPC(in *proto.Event) (*types.Event, error) {
} else if r := in.GetStaticHostUserV2(); r != nil {
out.Resource = types.Resource153ToLegacy(r)
return &out, nil
} else if r := in.GetUserTask(); r != nil {
out.Resource = types.Resource153ToLegacy(r)
return &out, nil
} else {
return nil, trace.BadParameter("received unsupported resource %T", in.Resource)
}
Expand Down
603 changes: 314 additions & 289 deletions api/client/proto/event.pb.go

Large diffs are not rendered by default.

106 changes: 106 additions & 0 deletions api/client/usertask/usertask.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2024 Gravitational, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package usertask

import (
"context"

"github.com/gravitational/trace"

usertaskv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/usertasks/v1"
)

// Client is a client for the User Task API.
type Client struct {
grpcClient usertaskv1.UserTaskServiceClient
}

// NewClient creates a new User Task client.
func NewClient(grpcClient usertaskv1.UserTaskServiceClient) *Client {
return &Client{
grpcClient: grpcClient,
}
}

// ListUserTasks returns a list of User Tasks.
func (c *Client) ListUserTasks(ctx context.Context, pageSize int64, nextToken string) ([]*usertaskv1.UserTask, string, error) {
resp, err := c.grpcClient.ListUserTasks(ctx, &usertaskv1.ListUserTasksRequest{
PageSize: pageSize,
PageToken: nextToken,
})
if err != nil {
return nil, "", trace.Wrap(err)
}

return resp.UserTasks, resp.NextPageToken, nil
}

// CreateUserTask creates a new User Task.
func (c *Client) CreateUserTask(ctx context.Context, req *usertaskv1.UserTask) (*usertaskv1.UserTask, error) {
rsp, err := c.grpcClient.CreateUserTask(ctx, &usertaskv1.CreateUserTaskRequest{
UserTask: req,
})
if err != nil {
return nil, trace.Wrap(err)
}
return rsp, nil
}

// GetUserTask returns a User Task by name.
func (c *Client) GetUserTask(ctx context.Context, name string) (*usertaskv1.UserTask, error) {
rsp, err := c.grpcClient.GetUserTask(ctx, &usertaskv1.GetUserTaskRequest{
Name: name,
})
if err != nil {
return nil, trace.Wrap(err)
}
return rsp, nil
}

// UpdateUserTask updates an existing User Task.
func (c *Client) UpdateUserTask(ctx context.Context, req *usertaskv1.UserTask) (*usertaskv1.UserTask, error) {
rsp, err := c.grpcClient.UpdateUserTask(ctx, &usertaskv1.UpdateUserTaskRequest{
UserTask: req,
})
if err != nil {
return nil, trace.Wrap(err)
}
return rsp, nil
}

// UpsertUserTask upserts a User Task.
func (c *Client) UpsertUserTask(ctx context.Context, req *usertaskv1.UserTask) (*usertaskv1.UserTask, error) {
rsp, err := c.grpcClient.UpsertUserTask(ctx, &usertaskv1.UpsertUserTaskRequest{
UserTask: req,
})
if err != nil {
return nil, trace.Wrap(err)
}
return rsp, nil
}

// DeleteUserTask deletes a User Task.
func (c *Client) DeleteUserTask(ctx context.Context, name string) error {
_, err := c.grpcClient.DeleteUserTask(ctx, &usertaskv1.DeleteUserTaskRequest{
Name: name,
})
return trace.Wrap(err)
}

// DeleteAllUserTasks deletes all User Tasks.
// Not implemented. Added to satisfy the interface.
func (c *Client) DeleteAllUserTasks(_ context.Context) error {
return trace.NotImplemented("DeleteAllUserTasks is not implemented")
}
Loading

0 comments on commit 64e32fb

Please sign in to comment.