-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
cc89802
commit ac080d7
Showing
30 changed files
with
1,593 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package usertasks | ||
|
||
import ( | ||
"github.com/gravitational/trace" | ||
|
||
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1" | ||
usertasksv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/usertasks/v1" | ||
"github.com/gravitational/teleport/api/types" | ||
) | ||
|
||
// NewUserTask creates a new UserTask object. | ||
// It validates the object before returning it. | ||
func NewUserTask(name string, spec *usertasksv1.UserTaskSpec) (*usertasksv1.UserTask, error) { | ||
cj := &usertasksv1.UserTask{ | ||
Kind: types.KindUserTask, | ||
Version: types.V1, | ||
Metadata: &headerv1.Metadata{ | ||
Name: name, | ||
}, | ||
Spec: spec, | ||
} | ||
|
||
if err := ValidateUserTask(cj); err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
return cj, nil | ||
} | ||
|
||
const ( | ||
// TaskTypeDiscoverEC2 identifies a User Tasks that is created | ||
// when an auto-enrollment of an EC2 instance fails. | ||
// UserTasks that have this Task Type must include the DiscoverEC2 field. | ||
TaskTypeDiscoverEC2 = "discover-ec2" | ||
) | ||
|
||
// ValidateUserTask validates the UserTask object without modifying it. | ||
func ValidateUserTask(uit *usertasksv1.UserTask) error { | ||
switch { | ||
case uit.GetKind() != types.KindUserTask: | ||
return trace.BadParameter("invalid kind") | ||
case uit.GetVersion() != types.V1: | ||
return trace.BadParameter("invalid version") | ||
case uit.GetSubKind() != "": | ||
return trace.BadParameter("invalid sub kind, must be empty") | ||
case uit.GetMetadata() == nil: | ||
return trace.BadParameter("user task metadata is nil") | ||
case uit.Metadata.GetName() == "": | ||
return trace.BadParameter("user task name is empty") | ||
case uit.GetSpec() == nil: | ||
return trace.BadParameter("user task spec is nil") | ||
case uit.GetSpec().Integration == "": | ||
return trace.BadParameter("integration is required") | ||
} | ||
|
||
switch uit.Spec.TaskType { | ||
case TaskTypeDiscoverEC2: | ||
if err := validateDiscoverEC2TaskType(uit); err != nil { | ||
return trace.Wrap(err) | ||
} | ||
default: | ||
return trace.BadParameter("task type %q is not valid", uit.Spec.TaskType) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateDiscoverEC2TaskType(uit *usertasksv1.UserTask) error { | ||
if uit.Spec.DiscoverEc2 == nil { | ||
return trace.BadParameter("%s requires the discover_ec2 field", TaskTypeDiscoverEC2) | ||
} | ||
if uit.Spec.IssueType == "" { | ||
return trace.BadParameter("issue type is required") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package usertasks_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1" | ||
usertasksv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/usertasks/v1" | ||
"github.com/gravitational/teleport/api/types/usertasks" | ||
) | ||
|
||
func TestValidateUserTask(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
task *usertasksv1.UserTask | ||
wantErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "NilUserTask", | ||
task: nil, | ||
wantErr: require.Error, | ||
}, | ||
{ | ||
name: "ValidUserTask", | ||
task: &usertasksv1.UserTask{ | ||
Kind: "user_task", | ||
Version: "v1", | ||
Metadata: &headerv1.Metadata{ | ||
Name: "test", | ||
}, | ||
Spec: &usertasksv1.UserTaskSpec{ | ||
Integration: "my-integration", | ||
TaskType: "discover-ec2", | ||
IssueType: "failed to enroll ec2 instances", | ||
DiscoverEc2: &usertasksv1.DiscoverEC2{}, | ||
}, | ||
}, | ||
wantErr: require.NoError, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := usertasks.ValidateUserTask(tt.task) | ||
tt.wantErr(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.