mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-05 12:06:09 +08:00
* Generate access list suggestions on access request creation * Add test Cleanup code * Add comment * Fix typo and refactor access suggestion in services library Fixed a typo in a comment in `access_list.go`. Refactored `accessrequest/suggestion.go` for enhanced code structure. The old model had all methods related to access request in a single interface, `accessRequestAPIGetter`, which had been divided among multiple interfaces for better division of responsibilities. A new function `GetSuggestedAccessLists` was created to narrow down the single duty for each function. These changes ensure more convenient unit testing and better accountability. * Improve access request suggestions logic This commit improves the logic for generating access request suggestions. It includes the requester's roles and traits as well as those from the access list and provides suggestions only for resource-based requests. A new method was also introduced for initializing AccessRequestSuggestions to avoid nil error. * Refactor access list suggestions to promotions The concept of 'suggestions' was updated to 'promotions' in different files (auth.go, client.go, auth_service_test.go, auth_service.proto among others) to make it more intuitive and align with the business context. This implies changing the wording, the functions called and the variable names. The feature itself hasn't changed, just the way it is referred. * This commit changes the method name "GenerateAccessListSuggestions" to "GenerateAccessRequestPromotions" in multiple files. The naming is changed to reflect the actual function of the method, which is generating promotions for given access requests, rather than generating list suggestions. All relevant mentions and usages of this method are updated to reflect the name change. Modification also includes adjustments to the comments and interfaces related to the updated function for better code clarity. * Refactor "GenerateAccessListSuggestions" to "GenerateAccessRequestPromotions" Renamed method "GenerateAccessListSuggestions" to "GenerateAccessRequestPromotions" across the codebase. This change reflects the actual functionality of the method, which is to generate promotions for access requests, not suggestions. This aligns the method name with its purpose, and offers better code readability. Comments related to this function were also updated to maintain consistency. * Update method handling access requests promotions Revised the method of handling failures when generating access requests promotions. Now, even if promotions fail to generate, the request will not fail entirely. Instead, the request can still be approved but without promotions. Also, changed the UpsertAccessRequestAllowedPromotions method to CreateAccessRequestAllowedPromotions to clearly indicate its purpose, while revising related test cases and comments.
280 lines
9.4 KiB
Go
280 lines
9.4 KiB
Go
/*
|
|
Copyright 2017-2021 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 modules allows external packages override certain behavioral
|
|
// aspects of teleport
|
|
package modules
|
|
|
|
import (
|
|
"context"
|
|
"crypto"
|
|
"fmt"
|
|
"runtime"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport"
|
|
"github.com/gravitational/teleport/api/client/proto"
|
|
"github.com/gravitational/teleport/api/constants"
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/api/types/accesslist"
|
|
"github.com/gravitational/teleport/api/utils/keys"
|
|
"github.com/gravitational/teleport/lib/auth/native"
|
|
"github.com/gravitational/teleport/lib/automaticupgrades"
|
|
)
|
|
|
|
// Features provides supported and unsupported features
|
|
type Features struct {
|
|
// Kubernetes enables Kubernetes Access product
|
|
Kubernetes bool
|
|
// App enables Application Access product
|
|
App bool
|
|
// DB enables database access product
|
|
DB bool
|
|
// OIDC enables OIDC connectors
|
|
OIDC bool
|
|
// SAML enables SAML connectors
|
|
SAML bool
|
|
// AccessControls enables FIPS access controls
|
|
AccessControls bool
|
|
// AdvancedAccessWorkflows enables advanced access workflows
|
|
AdvancedAccessWorkflows bool
|
|
// Cloud enables some cloud-related features
|
|
Cloud bool
|
|
// HSM enables PKCS#11 HSM support
|
|
HSM bool
|
|
// Desktop enables desktop access product
|
|
Desktop bool
|
|
// RecoveryCodes enables account recovery codes
|
|
RecoveryCodes bool
|
|
// Plugins enables hosted plugins
|
|
Plugins bool
|
|
// AutomaticUpgrades enables automatic upgrades of agents/services.
|
|
AutomaticUpgrades bool
|
|
// IsUsageBasedBilling enables some usage-based billing features
|
|
IsUsageBasedBilling bool
|
|
// Assist enables Assistant feature
|
|
Assist bool
|
|
// DeviceTrust holds its namesake feature settings.
|
|
DeviceTrust DeviceTrustFeature
|
|
// FeatureHiding enables hiding features from being discoverable for users who don't have the necessary permissions.
|
|
FeatureHiding bool
|
|
// AccessRequests holds its namesake feature settings.
|
|
AccessRequests AccessRequestsFeature
|
|
// CustomTheme holds the name of WebUI custom theme.
|
|
CustomTheme string
|
|
}
|
|
|
|
// DeviceTrustFeature holds the Device Trust feature general and usage-based
|
|
// settings.
|
|
// Requires Teleport Enterprise.
|
|
type DeviceTrustFeature struct {
|
|
// Enabled is true if the Device Trust feature is enabled.
|
|
Enabled bool
|
|
// DevicesUsageLimit is the usage-based limit for the number of
|
|
// registered/enrolled devices, at the implementation's discretion.
|
|
// Meant for usage-based accounts, like Teleport Team. Has no effect if
|
|
// [Features.IsUsageBasedBilling] is `false`.
|
|
DevicesUsageLimit int
|
|
}
|
|
|
|
// AccessRequestsFeature holds the Access Requests feature general and usage-based settings.
|
|
type AccessRequestsFeature struct {
|
|
// MonthlyRequestLimit is the usage-based limit for the number of
|
|
// access requests created in a calendar month.
|
|
// Meant for usage-based accounts, like Teleport Team. Has no effect if
|
|
// [Features.IsUsageBasedBilling] is `false`.
|
|
MonthlyRequestLimit int
|
|
}
|
|
|
|
// ToProto converts Features into proto.Features
|
|
func (f Features) ToProto() *proto.Features {
|
|
return &proto.Features{
|
|
Kubernetes: f.Kubernetes,
|
|
App: f.App,
|
|
DB: f.DB,
|
|
OIDC: f.OIDC,
|
|
SAML: f.SAML,
|
|
AccessControls: f.AccessControls,
|
|
AdvancedAccessWorkflows: f.AdvancedAccessWorkflows,
|
|
Cloud: f.Cloud,
|
|
HSM: f.HSM,
|
|
Desktop: f.Desktop,
|
|
RecoveryCodes: f.RecoveryCodes,
|
|
Plugins: f.Plugins,
|
|
AutomaticUpgrades: f.AutomaticUpgrades,
|
|
IsUsageBased: f.IsUsageBasedBilling,
|
|
Assist: f.Assist,
|
|
FeatureHiding: f.FeatureHiding,
|
|
CustomTheme: f.CustomTheme,
|
|
DeviceTrust: &proto.DeviceTrustFeature{
|
|
Enabled: f.DeviceTrust.Enabled,
|
|
DevicesUsageLimit: int32(f.DeviceTrust.DevicesUsageLimit),
|
|
},
|
|
AccessRequests: &proto.AccessRequestsFeature{
|
|
MonthlyRequestLimit: int32(f.AccessRequests.MonthlyRequestLimit),
|
|
},
|
|
}
|
|
}
|
|
|
|
// AccessResourcesGetter is a minimal interface that is used to get access lists
|
|
// and related resources from the backend.
|
|
type AccessResourcesGetter interface {
|
|
ListAccessLists(context.Context, int, string) ([]*accesslist.AccessList, string, error)
|
|
ListResources(ctx context.Context, req proto.ListResourcesRequest) (*types.ListResourcesResponse, error)
|
|
|
|
GetUser(userName string, withSecrets bool) (types.User, error)
|
|
GetRole(ctx context.Context, name string) (types.Role, error)
|
|
}
|
|
|
|
// Modules defines interface that external libraries can implement customizing
|
|
// default teleport behavior
|
|
type Modules interface {
|
|
// PrintVersion prints teleport version
|
|
PrintVersion()
|
|
// IsBoringBinary checks if the binary was compiled with BoringCrypto.
|
|
IsBoringBinary() bool
|
|
// Features returns supported features
|
|
Features() Features
|
|
// SetFeatures set features queried from Cloud
|
|
SetFeatures(Features)
|
|
// BuildType returns build type (OSS or Enterprise)
|
|
BuildType() string
|
|
// AttestHardwareKey attests a hardware key and returns its associated private key policy.
|
|
AttestHardwareKey(context.Context, interface{}, keys.PrivateKeyPolicy, *keys.AttestationStatement, crypto.PublicKey, time.Duration) (keys.PrivateKeyPolicy, error)
|
|
// GenerateAccessRequestPromotions generates a list of valid promotions for given access request.
|
|
GenerateAccessRequestPromotions(context.Context, AccessResourcesGetter, types.AccessRequest) (*types.AccessRequestAllowedPromotions, error)
|
|
// EnableRecoveryCodes enables the usage of recovery codes for resetting forgotten passwords
|
|
EnableRecoveryCodes()
|
|
// EnablePlugins enables the hosted plugins runtime
|
|
EnablePlugins()
|
|
}
|
|
|
|
const (
|
|
// BuildOSS specifies open source build type
|
|
BuildOSS = "oss"
|
|
// BuildEnterprise specifies enterprise build type
|
|
BuildEnterprise = "ent"
|
|
)
|
|
|
|
// SetModules sets the modules interface
|
|
func SetModules(m Modules) {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
modules = m
|
|
}
|
|
|
|
// GetModules returns the modules interface
|
|
func GetModules() Modules {
|
|
mutex.Lock()
|
|
defer mutex.Unlock()
|
|
return modules
|
|
}
|
|
|
|
// ValidateResource performs additional resource checks.
|
|
func ValidateResource(res types.Resource) error {
|
|
// All checks below are Cloud-specific.
|
|
if !GetModules().Features().Cloud {
|
|
return nil
|
|
}
|
|
|
|
switch r := res.(type) {
|
|
case types.AuthPreference:
|
|
switch r.GetSecondFactor() {
|
|
case constants.SecondFactorOff, constants.SecondFactorOptional:
|
|
return trace.BadParameter("cannot disable two-factor authentication on Cloud")
|
|
}
|
|
case types.SessionRecordingConfig:
|
|
switch r.GetMode() {
|
|
case types.RecordAtProxy, types.RecordAtProxySync:
|
|
return trace.BadParameter("cannot set proxy recording mode on Cloud")
|
|
}
|
|
if !r.GetProxyChecksHostKeys() {
|
|
return trace.BadParameter("cannot disable strict host key checking on Cloud")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type defaultModules struct {
|
|
automaticUpgrades bool
|
|
loadDynamicValues sync.Once
|
|
}
|
|
|
|
// BuildType returns build type (OSS or Enterprise)
|
|
func (p *defaultModules) BuildType() string {
|
|
return BuildOSS
|
|
}
|
|
|
|
// PrintVersion prints the Teleport version.
|
|
func (p *defaultModules) PrintVersion() {
|
|
fmt.Printf("Teleport v%s git:%s %s\n", teleport.Version, teleport.Gitref, runtime.Version())
|
|
}
|
|
|
|
// Features returns supported features
|
|
func (p *defaultModules) Features() Features {
|
|
p.loadDynamicValues.Do(func() {
|
|
p.automaticUpgrades = automaticupgrades.IsEnabled()
|
|
})
|
|
|
|
return Features{
|
|
Kubernetes: true,
|
|
DB: true,
|
|
App: true,
|
|
Desktop: true,
|
|
AutomaticUpgrades: p.automaticUpgrades,
|
|
Assist: true,
|
|
}
|
|
}
|
|
|
|
// SetFeatures sets features queried from Cloud.
|
|
// This is a noop since OSS teleport does not support enterprise features
|
|
func (p *defaultModules) SetFeatures(f Features) {
|
|
}
|
|
|
|
func (p *defaultModules) IsBoringBinary() bool {
|
|
return native.IsBoringBinary()
|
|
}
|
|
|
|
// AttestHardwareKey attests a hardware key.
|
|
func (p *defaultModules) AttestHardwareKey(_ context.Context, _ interface{}, _ keys.PrivateKeyPolicy, _ *keys.AttestationStatement, _ crypto.PublicKey, _ time.Duration) (keys.PrivateKeyPolicy, error) {
|
|
// Default modules do not support attesting hardware keys.
|
|
return keys.PrivateKeyPolicyNone, nil
|
|
}
|
|
|
|
// GenerateAccessRequestPromotions is a noop since OSS teleport does not support generating access list promotions.
|
|
func (p *defaultModules) GenerateAccessRequestPromotions(_ context.Context, _ AccessResourcesGetter, _ types.AccessRequest) (*types.AccessRequestAllowedPromotions, error) {
|
|
// The default module does not support generating access list promotions.
|
|
return types.NewAccessRequestAllowedPromotions(nil), nil
|
|
}
|
|
|
|
// EnableRecoveryCodes enables recovery codes. This is a noop since OSS teleport does not
|
|
// support recovery codes
|
|
func (p *defaultModules) EnableRecoveryCodes() {
|
|
}
|
|
|
|
// EnablePlugins enables hosted plugins runtime.
|
|
// This is a noop since OSS teleport does not support hosted plugins
|
|
func (p *defaultModules) EnablePlugins() {
|
|
}
|
|
|
|
var (
|
|
mutex sync.Mutex
|
|
modules Modules = &defaultModules{}
|
|
)
|