From d314406c6376d55322c903c15cf89362a7a46bdd Mon Sep 17 00:00:00 2001 From: Michael Wilson Date: Fri, 6 Jan 2023 14:36:07 -0500 Subject: [PATCH] Access requests implement ResourceWithLabels. (#19838) Access requests now implement ResourceWithLabels and a few utility functions have been added. These are all for making access requests easier to work with for the new access request watcher introduced in https://github.com/gravitational/teleport/pull/19626. --- api/types/access_request.go | 42 +++++++++++++++++++++++++++++++- api/types/access_request_test.go | 29 ++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 api/types/access_request_test.go diff --git a/api/types/access_request.go b/api/types/access_request.go index 6d21ecb52b3..a08c604c4bc 100644 --- a/api/types/access_request.go +++ b/api/types/access_request.go @@ -30,7 +30,7 @@ import ( // AccessRequest is a request for temporarily granted roles type AccessRequest interface { - Resource + ResourceWithLabels // GetUser gets the name of the requesting user GetUser() string // GetRoles gets the roles being requested by the user @@ -412,6 +412,38 @@ func (r *AccessRequestV3) SetDryRun(dryRun bool) { r.Spec.DryRun = dryRun } +// GetStaticLabels returns the access request static labels. +func (r *AccessRequestV3) GetStaticLabels() map[string]string { + return r.Metadata.Labels +} + +// SetStaticLabels sets the access request static labels. +func (r *AccessRequestV3) SetStaticLabels(sl map[string]string) { + r.Metadata.Labels = sl +} + +// GetAllLabels returns the access request static labels. +func (r *AccessRequestV3) GetAllLabels() map[string]string { + return r.Metadata.Labels +} + +// MatchSearch goes through select field values and tries to +// match against the list of search values. +func (r *AccessRequestV3) MatchSearch(values []string) bool { + fieldVals := append(utils.MapToStrings(r.GetAllLabels()), r.GetName()) + return MatchSearch(fieldVals, values, nil) +} + +// Origin returns the origin value of the resource. +func (r *AccessRequestV3) Origin() string { + return r.Metadata.Origin() +} + +// SetOrigin sets the origin value of the resource. +func (r *AccessRequestV3) SetOrigin(origin string) { + r.Metadata.SetOrigin(origin) +} + // String returns a text representation of this AccessRequest func (r *AccessRequestV3) String() string { return fmt.Sprintf("AccessRequest(user=%v,roles=%+v)", r.Spec.User, r.Spec.Roles) @@ -625,6 +657,14 @@ func (a AccessRequests) ToMap() map[string]AccessRequest { return m } +// AsResources returns these access requests as resources with labels. +func (a AccessRequests) AsResources() (resources ResourcesWithLabels) { + for _, accessRequest := range a { + resources = append(resources, accessRequest) + } + return resources +} + // Len returns the slice length. func (a AccessRequests) Len() int { return len(a) } diff --git a/api/types/access_request_test.go b/api/types/access_request_test.go new file mode 100644 index 00000000000..277a70ac328 --- /dev/null +++ b/api/types/access_request_test.go @@ -0,0 +1,29 @@ +/* +Copyright 2023 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 types + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestAssertAccessRequestImplementsResourceWithLabels(t *testing.T) { + ar, err := NewAccessRequest("test", "test", "test") + require.NoError(t, err) + require.Implements(t, (*ResourceWithLabels)(nil), ar) +}