Files
teleport/lib/resourceusage/usage.go
T
Justinas Stankevičius 96f3df96b1 Enable limited Access Requests feature for the Team plan (#29866)
* 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
2023-08-16 17:20:34 +00:00

69 lines
2.0 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"
"time"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/types"
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/lib/events"
)
// GetAccessRequestMonthlyUsage returns the number of access requests that have been created this month.
func GetAccessRequestMonthlyUsage(ctx context.Context, alog events.AuditLogger, now time.Time) (int, error) {
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.UTC)
created := make(map[string]struct{})
var results []apievents.AuditEvent
var startKey string
var err error
for {
results, startKey, err = alog.SearchEvents(ctx, events.SearchEventsRequest{
From: monthStart,
To: now,
Order: types.EventOrderAscending,
EventTypes: []string{events.AccessRequestCreateEvent},
StartKey: startKey,
})
if err != nil {
return 0, trace.Wrap(err)
}
for _, ev := range results {
ev, ok := ev.(*apievents.AccessRequestCreate)
if !ok {
return 0, trace.BadParameter("expected *AccessRequestCreate, but got %T", ev)
}
id := ev.RequestID
switch ev.GetType() {
case events.AccessRequestCreateEvent:
created[id] = struct{}{}
default:
log.Warnf("Expected event type %q, got %q", events.AccessRequestCreateEvent, ev.GetType())
}
}
if startKey == "" {
break
}
}
return len(created), nil
}