mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-04 01:23:29 +08:00
* Add access request monthly limit to Features * Add a method to retrieve usage of Access Requests * Verify access request limit when creating * Remove AccessRequestsFeature.Enabled Feature is always enabled now, the flag serves no purpose * Expose usage of access requests in auth server * Reimplement access request usage using audit log * Disallow unauthenticated access to resource usage * Tidy proto structures * Start enforcing access request limit in auth * Run GCI * Add a license header to usage_test.go * Add missing godoc * Restructure usage proto per PR comments * Construct proto.AccessRequestUsage in getARMU * Reduce log level when encountering an unexpected event * Simplify return * Add godoc to GetResourcesUsage in client * Fix merge mess-ups * Count all requests, no matter their review state * Factor out GetAccessRequestMonthlyUsage * Move out resource usage to its own service * Validate the limit after dry run is done Unbreak UI when the limit is up * Fix imports * Remove Limit SearchEvents will default to defaults.EventsIterationLimit * Rename the service proto * Add godocs to the gRPC service * Move resource usage to a dedicated package * Define single GetUsage rpc for all resource types * Additions for limited access requests UI rework * Add a test for GetAccessRequestMonthlyUsage * Address lint * Run GCI * Add licenses where missing * Expect CTA_UNSPEICIFED rather than `undefined` * Expect CTA_UNSPECIFIED instead of undefined
64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
// 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 resourceusage
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/gravitational/trace"
|
|
"github.com/jonboulle/clockwork"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/exp/slices"
|
|
|
|
apievents "github.com/gravitational/teleport/api/types/events"
|
|
"github.com/gravitational/teleport/lib/events"
|
|
eventstest "github.com/gravitational/teleport/lib/events/test"
|
|
)
|
|
|
|
func TestGetAccessRequestMonthlyUsage(t *testing.T) {
|
|
makeEvent := func(eventType string, id string, timestamp time.Time) apievents.AuditEvent {
|
|
return &apievents.AccessRequestCreate{
|
|
Metadata: apievents.Metadata{
|
|
Type: eventType,
|
|
Time: timestamp,
|
|
},
|
|
RequestID: id,
|
|
}
|
|
}
|
|
|
|
// Mock audit log
|
|
clock := clockwork.NewFakeClockAt(time.Date(2023, 07, 15, 1, 2, 3, 0, time.UTC))
|
|
now := clock.Now()
|
|
mockEvents := []apievents.AuditEvent{
|
|
makeEvent(events.AccessRequestCreateEvent, "aaa", now.AddDate(0, 0, -4)),
|
|
makeEvent(events.AccessRequestCreateEvent, "bbb", now.AddDate(0, 0, -3)),
|
|
makeEvent(events.AccessRequestCreateEvent, "ccc", now.AddDate(0, 0, -2)),
|
|
makeEvent(events.AccessRequestCreateEvent, "ddd", now.AddDate(0, 0, -1)),
|
|
}
|
|
|
|
al := eventstest.NewMockAuditLogSessionStreamer(mockEvents, func(req events.SearchEventsRequest) error {
|
|
if !slices.Equal([]string{events.AccessRequestCreateEvent}, req.EventTypes) {
|
|
return trace.BadParameter("expected AccessRequestCreateEvent only, got %v", req.EventTypes)
|
|
}
|
|
return nil
|
|
})
|
|
|
|
result, err := GetAccessRequestMonthlyUsage(context.Background(), al, now)
|
|
require.NoError(t, err)
|
|
require.Equal(t, len(mockEvents), result)
|
|
}
|