Files
teleport/lib/modules/test.go
T
Lisa Kim ef92182ef4 [reopen] Handle private key policy errors for the web UI (#17928)
- When user registers or resets, when running into
policy errors, we don't send back an error, but instead
a 200 (to indicate user has successfully registered/resetted)
and a flag to determine if policy was enabled
- Send back policy configurations for cluster config,
this will allow the web UI login page to just display
redirection messages without login attempts
- For role configured policy, login attempts will be
required and a specific error will be returned so that
the UI can then better redirect the user
2022-10-31 23:18:54 +00:00

95 lines
2.9 KiB
Go

/*
Copyright 2022 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
import (
"context"
"crypto"
"testing"
"time"
"github.com/gravitational/teleport/api/utils/keys"
)
// TestModules implements the Modules interface for testing.
//
// Setting Test* fields will return those values from interface
// methods. IsBoringBinary and PrintVersion functions return the
// same values from default modules.
//
// See SetTestModules for an example.
type TestModules struct {
// TestBuildType is returned from the BuiltType function.
TestBuildType string
// TestFeatures is returned from the Features function.
TestFeatures Features
defaultModules
MockAttestHardwareKey func(_ context.Context, _ interface{}, policy keys.PrivateKeyPolicy, _ *keys.AttestationStatement, _ crypto.PublicKey, _ time.Duration) (keys.PrivateKeyPolicy, error)
}
// SetTestModules sets the value returned from GetModules to testModules
// and reverts the change in the test cleanup function.
// It must not be used in parallel tests.
//
// func TestWithFakeModules(t *testing.T) {
// modules.SetTestModules(t, &modules.TestModules{
// TestBuildType: modules.BuildEnterprise,
// TestFeatures: modules.Features{
// Cloud: true,
// },
// })
//
// // test implementation
//
// // cleanup will revert module changes after test completes
// }
func SetTestModules(t *testing.T, testModules *TestModules) {
defaultModules := GetModules()
t.Cleanup(func() { SetModules(defaultModules) })
SetModules(testModules)
}
// PrintVersion prints teleport version
func (m *TestModules) PrintVersion() {
m.defaultModules.PrintVersion()
}
// IsBoringBinary checks if the binary was compiled with BoringCrypto.
func (m *TestModules) IsBoringBinary() bool {
return m.defaultModules.IsBoringBinary()
}
// Features returns supported features.
func (m *TestModules) Features() Features {
return m.TestFeatures
}
// BuildType returns build type (OSS or Enterprise).
func (m *TestModules) BuildType() string {
return m.TestBuildType
}
// AttestHardwareKey attests a hardware key.
func (m *TestModules) AttestHardwareKey(ctx context.Context, obj interface{}, policy keys.PrivateKeyPolicy, as *keys.AttestationStatement, pk crypto.PublicKey, d time.Duration) (keys.PrivateKeyPolicy, error) {
if m.MockAttestHardwareKey != nil {
return m.MockAttestHardwareKey(ctx, obj, policy, as, pk, d)
}
return policy, nil
}