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.
This commit is contained in:
Michael Wilson
2023-01-06 19:36:07 +00:00
committed by GitHub
parent fe1acfc6d3
commit d314406c63
2 changed files with 70 additions and 1 deletions
+41 -1
View File
@@ -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) }
+29
View File
@@ -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)
}