Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ func (a *AppV3) SetDynamicLabels(dl map[string]CommandLabel) {
a.Spec.DynamicLabels = LabelsToV2(dl)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (a *AppV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := a.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := a.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the app combined static and dynamic labels.
func (a *AppV3) GetAllLabels() map[string]string {
return CombineLabels(a.Metadata.Labels, a.Spec.DynamicLabels)
Expand Down
13 changes: 13 additions & 0 deletions api/types/appserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ func (s *AppServerV3) SetProxyIDs(proxyIDs []string) {
s.Spec.ProxyIDs = proxyIDs
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *AppServerV3) GetLabel(key string) (value string, ok bool) {
if s.Spec.App != nil {
if v, ok := s.Spec.App.GetLabel(key); ok {
return v, ok
}
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns all resource's labels. Considering:
// * Static labels from `Metadata.Labels` and `Spec.App`.
// * Dynamic labels from `Spec.App.Spec`.
Expand Down
7 changes: 7 additions & 0 deletions api/types/connection_diagnostic.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func (c *ConnectionDiagnosticV1) CheckAndSetDefaults() error {
return nil
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (c *ConnectionDiagnosticV1) GetLabel(key string) (value string, ok bool) {
v, ok := c.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns combined static and dynamic labels.
func (c *ConnectionDiagnosticV1) GetAllLabels() map[string]string {
return CombineLabels(c.Metadata.Labels, nil)
Expand Down
11 changes: 11 additions & 0 deletions api/types/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ func (d *DatabaseV3) SetDynamicLabels(dl map[string]CommandLabel) {
d.Spec.DynamicLabels = LabelsToV2(dl)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (d *DatabaseV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := d.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := d.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the database combined static and dynamic labels.
func (d *DatabaseV3) GetAllLabels() map[string]string {
return CombineLabels(d.Metadata.Labels, d.Spec.DynamicLabels)
Expand Down
13 changes: 13 additions & 0 deletions api/types/databaseserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ func (s *DatabaseServerV3) SetOrigin(origin string) {
s.Metadata.SetOrigin(origin)
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *DatabaseServerV3) GetLabel(key string) (value string, ok bool) {
if s.Spec.Database != nil {
if v, ok := s.Spec.Database.GetLabel(key); ok {
return v, ok
}
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns all resource's labels. Considering:
// * Static labels from `Metadata.Labels` and `Spec.Database`.
// * Dynamic labels from `Spec.DynamicLabels`.
Expand Down
14 changes: 14 additions & 0 deletions api/types/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ func (s *WindowsDesktopServiceV3) SetProxyIDs(proxyIDs []string) {
s.Spec.ProxyIDs = proxyIDs
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *WindowsDesktopServiceV3) GetLabel(key string) (value string, ok bool) {
v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns the resources labels.
func (s *WindowsDesktopServiceV3) GetAllLabels() map[string]string {
return s.Metadata.Labels
Expand Down Expand Up @@ -204,6 +211,13 @@ func (d *WindowsDesktopV3) GetHostID() string {
return d.Spec.HostID
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (d *WindowsDesktopV3) GetLabel(key string) (value string, ok bool) {
v, ok := d.Metadata.Labels[key]
return v, ok
}

// GetAllLabels returns combined static and dynamic labels.
func (d *WindowsDesktopV3) GetAllLabels() map[string]string {
// TODO(zmb3): add dynamic labels when running in agent mode
Expand Down
11 changes: 11 additions & 0 deletions api/types/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ func (k *KubernetesClusterV3) SetName(name string) {
k.Metadata.Name = name
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (k *KubernetesClusterV3) GetLabel(key string) (value string, ok bool) {
if cmd, ok := k.Spec.DynamicLabels[key]; ok {
return cmd.Result, ok
}

v, ok := k.Metadata.Labels[key]
return v, ok
}

// GetStaticLabels returns the static labels.
func (k *KubernetesClusterV3) GetStaticLabels() map[string]string {
return k.Metadata.Labels
Expand Down
2 changes: 2 additions & 0 deletions api/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type ResourceWithOrigin interface {
type ResourceWithLabels interface {
// ResourceWithOrigin is the base resource interface.
ResourceWithOrigin
// GetLabel retrieves the label with the provided key.
GetLabel(key string) (value string, ok bool)
// GetAllLabels returns all resource's labels.
GetAllLabels() map[string]string
// GetStaticLabels returns the resource's static labels.
Expand Down
17 changes: 15 additions & 2 deletions api/types/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,29 @@ func (s *ServerV2) GetHostname() string {
return s.Spec.Hostname
}

// GetLabel retrieves the label with the provided key. If not found
// value will be empty and ok will be false.
func (s *ServerV2) GetLabel(key string) (value string, ok bool) {
if cmd, ok := s.Spec.CmdLabels[key]; ok {
return cmd.Result, ok
}

v, ok := s.Metadata.Labels[key]
return v, ok
}

// GetLabels returns server's static label key pairs.
// GetLabels and GetStaticLabels are the same, and that is intentional. GetLabels
// exists to preserve backwards compatibility, while GetStaticLabels exists to
// implement ResourcesWithLabels.

// GetLabels returns server's static label key pairs
func (s *ServerV2) GetLabels() map[string]string {
return s.Metadata.Labels
}

// GetStaticLabels returns the server static labels.
// GetLabels and GetStaticLabels are the same, and that is intentional. GetLabels
// exists to preserve backwards compatibility, while GetStaticLabels exists to
// implement ResourcesWithLabels.
func (s *ServerV2) GetStaticLabels() map[string]string {
return s.Metadata.Labels
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ require (
github.com/gravitational/trace v1.2.1
github.com/gravitational/ttlmap v0.0.0-20171116003245-91fd36b9004c
github.com/grpc-ecosystem/go-grpc-middleware/providers/openmetrics/v2 v2.0.0-20220308023801-e4a6915ea237
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/jackc/pgconn v1.13.0
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/jackc/pgproto3/v2 v2.3.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,8 @@ github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU=
github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
Expand Down
5 changes: 5 additions & 0 deletions lib/auth/auth_with_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,11 @@ func (a *ServerWithRoles) ListResources(ctx context.Context, req proto.ListResou
req.SearchKeywords = nil
req.PredicateExpression = ""

// Increase the limit to one more than was requested so
// that an additional page load is not needed to determine
// the next key.
req.Limit++

resourceChecker, err := a.newResourceAccessChecker(req.ResourceType)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
102 changes: 102 additions & 0 deletions lib/auth/auth_with_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"crypto/tls"
"crypto/x509/pkix"
"fmt"
"io"
"testing"
"time"

Expand All @@ -29,12 +30,14 @@ import (
"github.com/google/uuid"
"github.com/gravitational/trace"
"github.com/pquerna/otp/totp"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/defaults"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/installers"
Expand Down Expand Up @@ -1502,6 +1505,105 @@ func TestSessionRecordingConfigRBAC(t *testing.T) {
})
}

// time go test ./lib/auth -bench=. -run=^$ -v
// goos: darwin
// goarch: amd64
// pkg: github.com/gravitational/teleport/lib/auth
// cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
// BenchmarkListNodes
// BenchmarkListNodes-16 1 1000469673 ns/op 518721960 B/op 8344858 allocs/op
// PASS
// ok github.com/gravitational/teleport/lib/auth 3.695s
// go test ./lib/auth -bench=. -run=^$ -v 19.02s user 3.87s system 244% cpu 9.376 total
func BenchmarkListNodes(b *testing.B) {
const nodeCount = 50_000
const roleCount = 32

logger := logrus.StandardLogger()
logger.ReplaceHooks(make(logrus.LevelHooks))
logger.SetLevel(logrus.DebugLevel)
logger.SetOutput(io.Discard)

ctx := context.Background()
srv := newTestTLSServer(b)

var values []string
for i := 0; i < roleCount; i++ {
values = append(values, uuid.New().String())
}

values[0] = "hidden"

var hiddenNodes int
// Create test nodes.
for i := 0; i < nodeCount; i++ {
name := uuid.New().String()
val := values[i%len(values)]
if val == "hidden" {
hiddenNodes++
}
node, err := types.NewServerWithLabels(
name,
types.KindNode,
types.ServerSpecV2{},
map[string]string{"key": val},
)
require.NoError(b, err)

_, err = srv.Auth().UpsertNode(ctx, node)
require.NoError(b, err)
}

testNodes, err := srv.Auth().GetNodes(ctx, defaults.Namespace)
require.NoError(b, err)
require.Len(b, testNodes, nodeCount)

var roles []types.Role
for _, val := range values {
role, err := types.NewRole(fmt.Sprintf("role-%s", val), types.RoleSpecV5{})
require.NoError(b, err)

if val == "hidden" {
role.SetNodeLabels(types.Deny, types.Labels{"key": {val}})
} else {
role.SetNodeLabels(types.Allow, types.Labels{"key": {val}})
}
roles = append(roles, role)
}

// create user, role, and client
username := "user"

user, err := CreateUser(srv.Auth(), username, roles...)
require.NoError(b, err)
identity := TestUser(user.GetName())
clt, err := srv.NewClient(identity)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

for n := 0; n < b.N; n++ {
var resources []types.ResourceWithLabels
req := proto.ListResourcesRequest{
ResourceType: types.KindNode,
Namespace: apidefaults.Namespace,
Limit: 1_000,
}
for {
rsp, err := clt.ListResources(ctx, req)
require.NoError(b, err)

resources = append(resources, rsp.Resources...)
req.StartKey = rsp.NextKey
if req.StartKey == "" {
break
}
}
require.Len(b, resources, nodeCount-hiddenNodes)
}
}

// TestGetAndList_Nodes users can retrieve nodes with various filters
// and with the appropriate permissions.
func TestGetAndList_Nodes(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3449,7 +3449,7 @@ func verifyJWT(clock clockwork.Clock, clusterName string, pairs []*types.JWTKeyP
return nil, trace.NewAggregate(errs...)
}

func newTestTLSServer(t *testing.T) *TestTLSServer {
func newTestTLSServer(t testing.TB) *TestTLSServer {
as, err := NewTestAuthServer(TestAuthServerConfig{
Dir: t.TempDir(),
Clock: clockwork.NewFakeClock(),
Expand Down
11 changes: 3 additions & 8 deletions lib/backend/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"time"

"github.com/gravitational/trace"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/jonboulle/clockwork"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -81,7 +81,7 @@ type Reporter struct {
//
// This will keep an upper limit on our memory usage while still always
// reporting the most active keys.
topRequestsCache *lru.Cache
topRequestsCache *lru.Cache[topRequestsCacheKey, struct{}]
}

// NewReporter returns a new Reporter.
Expand All @@ -95,12 +95,7 @@ func NewReporter(cfg ReporterConfig) (*Reporter, error) {
return nil, trace.Wrap(err)
}

cache, err := lru.NewWithEvict(cfg.TopRequestsCount, func(key interface{}, value interface{}) {
labels, ok := key.(topRequestsCacheKey)
if !ok {
log.Errorf("BUG: invalid cache key type: %T", key)
return
}
cache, err := lru.NewWithEvict(cfg.TopRequestsCount, func(labels topRequestsCacheKey, value struct{}) {
// Evict the key from requests metric.
requests.DeleteLabelValues(labels.component, labels.key, labels.isRange)
})
Expand Down
Loading