-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Static host users - Add gRPC service #45292
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,94 @@ | ||
| // 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 statichostuser | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/gravitational/trace" | ||
|
|
||
| userprovisioningpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/userprovisioning/v1" | ||
| ) | ||
|
|
||
| // Client is a StaticHostUser client. | ||
| type Client struct { | ||
| grpcClient userprovisioningpb.StaticHostUsersServiceClient | ||
| } | ||
|
|
||
| // NewClient creates a new StaticHostUser client. | ||
| func NewClient(grpcClient userprovisioningpb.StaticHostUsersServiceClient) *Client { | ||
| return &Client{ | ||
| grpcClient: grpcClient, | ||
| } | ||
| } | ||
|
|
||
| // ListStaticHostUsers lists static host users. | ||
| func (c *Client) ListStaticHostUsers(ctx context.Context, pageSize int, pageToken string) ([]*userprovisioningpb.StaticHostUser, string, error) { | ||
| resp, err := c.grpcClient.ListStaticHostUsers(ctx, &userprovisioningpb.ListStaticHostUsersRequest{ | ||
| PageSize: int32(pageSize), | ||
| PageToken: pageToken, | ||
| }) | ||
| if err != nil { | ||
| return nil, "", trace.Wrap(err) | ||
| } | ||
| return resp.Users, resp.NextPageToken, nil | ||
| } | ||
|
|
||
| // GetStaticHostUser returns a static host user by name. | ||
| func (c *Client) GetStaticHostUser(ctx context.Context, name string) (*userprovisioningpb.StaticHostUser, error) { | ||
| if name == "" { | ||
| return nil, trace.BadParameter("missing name") | ||
| } | ||
| out, err := c.grpcClient.GetStaticHostUser(ctx, &userprovisioningpb.GetStaticHostUserRequest{ | ||
| Name: name, | ||
| }) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // CreateStaticHostUser creates a static host user. | ||
| func (c *Client) CreateStaticHostUser(ctx context.Context, in *userprovisioningpb.StaticHostUser) (*userprovisioningpb.StaticHostUser, error) { | ||
| out, err := c.grpcClient.CreateStaticHostUser(ctx, &userprovisioningpb.CreateStaticHostUserRequest{ | ||
| User: in, | ||
| }) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // UpdateStaticHostUser updates a static host user. | ||
| func (c *Client) UpdateStaticHostUser(ctx context.Context, in *userprovisioningpb.StaticHostUser) (*userprovisioningpb.StaticHostUser, error) { | ||
| out, err := c.grpcClient.UpdateStaticHostUser(ctx, &userprovisioningpb.UpdateStaticHostUserRequest{ | ||
| User: in, | ||
| }) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // UpsertStaticHostUser upserts a static host user. | ||
| func (c *Client) UpsertStaticHostUser(ctx context.Context, in *userprovisioningpb.StaticHostUser) (*userprovisioningpb.StaticHostUser, error) { | ||
| out, err := c.grpcClient.UpsertStaticHostUser(ctx, &userprovisioningpb.UpsertStaticHostUserRequest{ | ||
| User: in, | ||
| }) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // DeleteStaticHostUser deletes a static host user. Note that this does not | ||
| // remove any host users created on nodes from the resource. | ||
| func (c *Client) DeleteStaticHostUser(ctx context.Context, name string) error { | ||
| if name == "" { | ||
| return trace.BadParameter("missing name") | ||
| } | ||
| _, err := c.grpcClient.DeleteStaticHostUser(ctx, &userprovisioningpb.DeleteStaticHostUserRequest{ | ||
| Name: name, | ||
| }) | ||
| return trace.Wrap(err) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,176 @@ | ||
| // 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 statichostuser | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "google.golang.org/protobuf/types/known/emptypb" | ||
|
|
||
| userprovisioningpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/userprovisioning/v1" | ||
| "github.com/gravitational/teleport/api/types" | ||
| "github.com/gravitational/teleport/lib/authz" | ||
| "github.com/gravitational/teleport/lib/services" | ||
| ) | ||
|
|
||
| // ServiceConfig holds configuration options for the static host user gRPC service. | ||
| type ServiceConfig struct { | ||
| // Authorizer is the authorizer used to check access to resources. | ||
| Authorizer authz.Authorizer | ||
| // Backend is the backend used to store static host users. | ||
| Backend services.StaticHostUser | ||
| // TODO(atburke): add cache | ||
|
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. Let's also add a TODO in the places that are currently consuming from the backend that should be migrated to use the cache. |
||
| } | ||
|
|
||
| // Service implements the static host user RPC service. | ||
| type Service struct { | ||
| userprovisioningpb.UnimplementedStaticHostUsersServiceServer | ||
|
|
||
| authorizer authz.Authorizer | ||
| backend services.StaticHostUser | ||
|
rosstimothy marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // NewService creates a new static host user gRPC service. | ||
| func NewService(cfg ServiceConfig) (*Service, error) { | ||
| switch { | ||
| case cfg.Backend == nil: | ||
| return nil, trace.BadParameter("backend is required") | ||
| case cfg.Authorizer == nil: | ||
| return nil, trace.BadParameter("authorizer is required") | ||
| } | ||
|
|
||
| return &Service{ | ||
| authorizer: cfg.Authorizer, | ||
| backend: cfg.Backend, | ||
| }, nil | ||
| } | ||
|
|
||
| // ListStaticHostUsers lists static host users. | ||
| func (s *Service) ListStaticHostUsers(ctx context.Context, req *userprovisioningpb.ListStaticHostUsersRequest) (*userprovisioningpb.ListStaticHostUsersResponse, error) { | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbList, types.VerbRead); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| // TODO(atburke): Switch to using the cache after static host users have been added to the cache. | ||
| users, nextToken, err := s.backend.ListStaticHostUsers(ctx, int(req.PageSize), req.PageToken) | ||
|
rosstimothy marked this conversation as resolved.
|
||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return &userprovisioningpb.ListStaticHostUsersResponse{ | ||
| Users: users, | ||
| NextPageToken: nextToken, | ||
| }, nil | ||
| } | ||
|
|
||
| // GetStaticHostUser returns a static host user by name. | ||
| func (s *Service) GetStaticHostUser(ctx context.Context, req *userprovisioningpb.GetStaticHostUserRequest) (*userprovisioningpb.StaticHostUser, error) { | ||
| if req.Name == "" { | ||
| return nil, trace.BadParameter("missing name") | ||
| } | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbRead); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| // TODO(atburke): Switch to using the cache after static host users have been added to the cache. | ||
| out, err := s.backend.GetStaticHostUser(ctx, req.Name) | ||
|
rosstimothy marked this conversation as resolved.
|
||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // CreateStaticHostUser creates a static host user. | ||
| func (s *Service) CreateStaticHostUser(ctx context.Context, req *userprovisioningpb.CreateStaticHostUserRequest) (*userprovisioningpb.StaticHostUser, error) { | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbCreate); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| out, err := s.backend.CreateStaticHostUser(ctx, req.User) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // UpdateStaticHostUser updates a static host user. | ||
| func (s *Service) UpdateStaticHostUser(ctx context.Context, req *userprovisioningpb.UpdateStaticHostUserRequest) (*userprovisioningpb.StaticHostUser, error) { | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbUpdate); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| out, err := s.backend.UpdateStaticHostUser(ctx, req.User) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // UpsertStaticHostUser upserts a static host user. | ||
| func (s *Service) UpsertStaticHostUser(ctx context.Context, req *userprovisioningpb.UpsertStaticHostUserRequest) (*userprovisioningpb.StaticHostUser, error) { | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbCreate, types.VerbUpdate); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| out, err := s.backend.UpsertStaticHostUser(ctx, req.User) | ||
| return out, trace.Wrap(err) | ||
| } | ||
|
|
||
| // DeleteStaticHostUser deletes a static host user. Note that this does not | ||
| // remove any host users created on nodes from the resource. | ||
| func (s *Service) DeleteStaticHostUser(ctx context.Context, req *userprovisioningpb.DeleteStaticHostUserRequest) (*emptypb.Empty, error) { | ||
| if req.Name == "" { | ||
| return nil, trace.BadParameter("missing name") | ||
| } | ||
| authCtx, err := s.authorizer.Authorize(ctx) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.CheckAccessToKind(types.KindStaticHostUser, types.VerbDelete); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if err := authCtx.AuthorizeAdminAction(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return &emptypb.Empty{}, trace.Wrap(s.backend.DeleteStaticHostUser(ctx, req.Name)) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.