2022-09-19 23:11:01 -05:00
package coderdenttest
import (
"context"
"crypto/ed25519"
"crypto/rand"
2022-10-17 08:43:30 -05:00
"crypto/tls"
2022-09-19 23:11:01 -05:00
"io"
2022-10-17 08:43:30 -05:00
"net/http"
2024-12-17 21:57:03 +00:00
"os/exec"
2024-07-25 16:07:53 -05:00
"strings"
2022-09-19 23:11:01 -05:00
"testing"
"time"
2024-11-18 14:09:22 +04:00
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
2024-07-25 16:07:53 -05:00
"github.com/moby/moby/pkg/namesgenerator"
2024-11-18 14:09:22 +04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2024-12-17 21:57:03 +00:00
"cdr.dev/slog"
2024-07-25 16:07:53 -05:00
2024-11-18 14:09:22 +04:00
"github.com/coder/coder/v2/coderd/coderdtest"
2023-11-30 19:33:04 -06:00
"github.com/coder/coder/v2/coderd/database"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/database/pubsub"
2024-11-18 14:09:22 +04:00
"github.com/coder/coder/v2/codersdk"
2025-05-12 10:31:38 -05:00
"github.com/coder/coder/v2/codersdk/drpcsdk"
2024-11-18 14:09:22 +04:00
"github.com/coder/coder/v2/enterprise/coderd"
"github.com/coder/coder/v2/enterprise/coderd/license"
"github.com/coder/coder/v2/enterprise/dbcrypt"
2024-07-25 16:07:53 -05:00
"github.com/coder/coder/v2/provisioner/echo"
2024-12-17 21:57:03 +00:00
"github.com/coder/coder/v2/provisioner/terraform"
2024-07-25 16:07:53 -05:00
"github.com/coder/coder/v2/provisionerd"
provisionerdproto "github.com/coder/coder/v2/provisionerd/proto"
"github.com/coder/coder/v2/provisionersdk"
sdkproto "github.com/coder/coder/v2/provisionersdk/proto"
"github.com/coder/coder/v2/testutil"
2022-09-19 23:11:01 -05:00
)
const (
testKeyID = "enterprise-test"
)
var (
testPrivateKey ed25519 . PrivateKey
testPublicKey ed25519 . PublicKey
2022-10-06 19:28:22 -05:00
Keys = map [ string ] ed25519 . PublicKey {}
2022-09-19 23:11:01 -05:00
)
func init () {
var err error
testPublicKey , testPrivateKey , err = ed25519 . GenerateKey ( rand . Reader )
if err != nil {
panic ( err )
}
2022-10-06 19:28:22 -05:00
Keys [ testKeyID ] = testPublicKey
2022-09-19 23:11:01 -05:00
}
type Options struct {
* coderdtest . Options
2025-07-15 14:55:34 +10:00
ConnectionLogging bool
2023-12-15 00:27:56 -08:00
AuditLogging bool
BrowserOnly bool
EntitlementsUpdateInterval time . Duration
SCIMAPIKey [] byte
UserWorkspaceQuota int
ProxyHealthInterval time . Duration
LicenseOptions * LicenseOptions
DontAddLicense bool
DontAddFirstUser bool
ReplicaSyncUpdateInterval time . Duration
2024-04-17 13:30:32 -04:00
ReplicaErrorGracePeriod time . Duration
2023-12-15 00:27:56 -08:00
ExternalTokenEncryption [] dbcrypt . Cipher
ProvisionerDaemonPSK string
2022-09-19 23:11:01 -05:00
}
// New constructs a codersdk client connected to an in-memory Enterprise API instance.
2023-07-12 13:35:29 +04:00
func New ( t * testing . T , options * Options ) ( * codersdk . Client , codersdk . CreateFirstUserResponse ) {
client , _ , _ , user := NewWithAPI ( t , options )
return client , user
2022-09-19 23:11:01 -05:00
}
2023-11-30 19:33:04 -06:00
func NewWithDatabase ( t * testing . T , options * Options ) ( * codersdk . Client , database . Store , codersdk . CreateFirstUserResponse ) {
client , _ , api , user := NewWithAPI ( t , options )
return client , api . Database , user
}
2023-07-12 13:35:29 +04:00
func NewWithAPI ( t * testing . T , options * Options ) (
* codersdk . Client , io . Closer , * coderd . API , codersdk . CreateFirstUserResponse ,
) {
2023-06-13 20:18:31 +03:00
t . Helper ()
2022-09-19 23:11:01 -05:00
if options == nil {
options = & Options {}
}
if options . Options == nil {
options . Options = & coderdtest . Options {}
}
2023-07-12 13:35:29 +04:00
require . False ( t , options . DontAddFirstUser && ! options . DontAddLicense , "DontAddFirstUser requires DontAddLicense" )
2022-12-16 04:43:00 +10:00
setHandler , cancelFunc , serverURL , oop := coderdtest . NewOptions ( t , options . Options )
2022-09-19 23:11:01 -05:00
coderAPI , err := coderd . New ( context . Background (), & coderd . Options {
2022-10-17 08:43:30 -05:00
RBAC : true ,
2025-07-15 14:55:34 +10:00
ConnectionLogging : options . ConnectionLogging ,
2022-10-05 13:45:05 -04:00
AuditLogging : options . AuditLogging ,
2022-09-22 10:14:22 -05:00
BrowserOnly : options . BrowserOnly ,
2022-09-20 15:16:26 -05:00
SCIMAPIKey : options . SCIMAPIKey ,
2022-10-17 08:43:30 -05:00
DERPServerRelayAddress : oop . AccessURL . String (),
2023-07-26 09:21:04 -07:00
DERPServerRegionID : oop . BaseDERPMap . RegionIDs ()[ 0 ],
ReplicaSyncUpdateInterval : options . ReplicaSyncUpdateInterval ,
2024-04-17 13:30:32 -04:00
ReplicaErrorGracePeriod : options . ReplicaErrorGracePeriod ,
2022-09-19 23:11:01 -05:00
Options : oop ,
EntitlementsUpdateInterval : options . EntitlementsUpdateInterval ,
2023-09-07 15:49:49 +01:00
LicenseKeys : Keys ,
2023-05-10 19:23:16 -05:00
ProxyHealthInterval : options . ProxyHealthInterval ,
2023-07-20 06:35:41 -07:00
DefaultQuietHoursSchedule : oop . DeploymentValues . UserQuietHoursSchedule . DefaultSchedule . Value (),
2023-08-04 12:32:28 +04:00
ProvisionerDaemonPSK : options . ProvisionerDaemonPSK ,
2023-09-07 15:49:49 +01:00
ExternalTokenEncryption : options . ExternalTokenEncryption ,
2022-09-19 23:11:01 -05:00
})
2023-06-13 20:18:31 +03:00
require . NoError ( t , err )
2022-10-17 08:43:30 -05:00
setHandler ( coderAPI . AGPL . RootHandler )
2022-09-19 23:11:01 -05:00
var provisionerCloser io . Closer = nopcloser {}
if options . IncludeProvisionerDaemon {
provisionerCloser = coderdtest . NewProvisionerDaemon ( t , coderAPI . AGPL )
}
2022-10-10 15:37:06 -05:00
2022-09-19 23:11:01 -05:00
t . Cleanup ( func () {
cancelFunc ()
_ = provisionerCloser . Close ()
_ = coderAPI . Close ()
})
2022-12-16 04:43:00 +10:00
client := codersdk . New ( serverURL )
2022-10-17 08:43:30 -05:00
client . HTTPClient = & http . Client {
Transport : & http . Transport {
TLSClientConfig : & tls . Config {
//nolint:gosec
InsecureSkipVerify : true ,
},
},
}
2023-07-12 13:35:29 +04:00
var user codersdk . CreateFirstUserResponse
if ! options . DontAddFirstUser {
user = coderdtest . CreateFirstUser ( t , client )
if ! options . DontAddLicense {
lo := LicenseOptions {}
if options . LicenseOptions != nil {
lo = * options . LicenseOptions
// The pgCoord is not supported by the fake DB & in-memory Pubsub. It only works on a real postgres.
if lo . AllFeatures || ( lo . Features != nil && lo . Features [ codersdk . FeatureHighAvailability ] != 0 ) {
// we check for the in-memory test types so that the real types don't have to exported
_ , ok := coderAPI . Pubsub .( * pubsub . MemoryPubsub )
require . False ( t , ok , "FeatureHighAvailability is incompatible with MemoryPubsub" )
}
}
_ = AddLicense ( t , client , lo )
}
}
return client , provisionerCloser , coderAPI , user
2022-09-19 23:11:01 -05:00
}
2024-07-24 07:07:59 -10:00
// LicenseOptions is used to generate a license for testing.
// It supports the builder pattern for easy customization.
2022-09-19 23:11:01 -05:00
type LicenseOptions struct {
2024-04-29 16:50:11 -04:00
AccountType string
AccountID string
DeploymentIDs [] string
Trial bool
2024-07-24 07:07:59 -10:00
FeatureSet codersdk . FeatureSet
2024-04-29 16:50:11 -04:00
AllFeatures bool
2024-07-24 07:07:59 -10:00
// GraceAt is the time at which the license will enter the grace period.
GraceAt time . Time
// ExpiresAt is the time at which the license will hard expire.
// ExpiresAt should always be greater then GraceAt.
ExpiresAt time . Time
2024-09-03 09:09:38 +04:00
// NotBefore is the time at which the license becomes valid. If set to the
// zero value, the `nbf` claim on the license is set to 1 minute in the
// past.
NotBefore time . Time
2025-07-17 20:19:01 +10:00
// IssuedAt is the time at which the license was issued. If set to the
// zero value, the `iat` claim on the license is set to 1 minute in the
// past.
IssuedAt time . Time
Features license . Features
}
func ( opts * LicenseOptions ) WithIssuedAt ( now time . Time ) * LicenseOptions {
opts . IssuedAt = now
return opts
2024-07-24 07:07:59 -10:00
}
func ( opts * LicenseOptions ) Expired ( now time . Time ) * LicenseOptions {
2025-07-17 20:19:01 +10:00
opts . NotBefore = now . Add ( time . Hour * 24 * - 4 ) // needs to be before the grace period
2024-07-24 07:07:59 -10:00
opts . ExpiresAt = now . Add ( time . Hour * 24 * - 2 )
opts . GraceAt = now . Add ( time . Hour * 24 * - 3 )
return opts
}
func ( opts * LicenseOptions ) GracePeriod ( now time . Time ) * LicenseOptions {
2025-07-17 20:19:01 +10:00
opts . NotBefore = now . Add ( time . Hour * 24 * - 2 ) // needs to be before the grace period
2024-07-24 07:07:59 -10:00
opts . ExpiresAt = now . Add ( time . Hour * 24 )
opts . GraceAt = now . Add ( time . Hour * 24 * - 1 )
return opts
}
func ( opts * LicenseOptions ) Valid ( now time . Time ) * LicenseOptions {
opts . ExpiresAt = now . Add ( time . Hour * 24 * 60 )
opts . GraceAt = now . Add ( time . Hour * 24 * 53 )
return opts
}
2024-09-03 09:22:46 +04:00
func ( opts * LicenseOptions ) FutureTerm ( now time . Time ) * LicenseOptions {
opts . NotBefore = now . Add ( time . Hour * 24 )
opts . ExpiresAt = now . Add ( time . Hour * 24 * 60 )
opts . GraceAt = now . Add ( time . Hour * 24 * 53 )
return opts
}
2024-07-24 07:07:59 -10:00
func ( opts * LicenseOptions ) UserLimit ( limit int64 ) * LicenseOptions {
return opts . Feature ( codersdk . FeatureUserLimit , limit )
}
2025-07-17 20:19:01 +10:00
func ( opts * LicenseOptions ) ManagedAgentLimit ( soft int64 , hard int64 ) * LicenseOptions {
// These don't use named or exported feature names, see
// enterprise/coderd/license/license.go.
opts = opts . Feature ( codersdk . FeatureName ( "managed_agent_limit_soft" ), soft )
opts = opts . Feature ( codersdk . FeatureName ( "managed_agent_limit_hard" ), hard )
return opts
}
2024-07-24 07:07:59 -10:00
func ( opts * LicenseOptions ) Feature ( name codersdk . FeatureName , value int64 ) * LicenseOptions {
if opts . Features == nil {
opts . Features = license . Features {}
}
opts . Features [ name ] = value
return opts
}
func ( opts * LicenseOptions ) Generate ( t * testing . T ) string {
return GenerateLicense ( t , * opts )
2022-09-19 23:11:01 -05:00
}
2023-07-02 21:29:52 -05:00
// AddFullLicense generates a license with all features enabled.
func AddFullLicense ( t * testing . T , client * codersdk . Client ) codersdk . License {
return AddLicense ( t , client , LicenseOptions { AllFeatures : true })
}
2022-09-19 23:11:01 -05:00
// AddLicense generates a new license with the options provided and inserts it.
func AddLicense ( t * testing . T , client * codersdk . Client , options LicenseOptions ) codersdk . License {
2022-10-06 19:28:22 -05:00
l , err := client . AddLicense ( context . Background (), codersdk . AddLicenseRequest {
2022-09-19 23:11:01 -05:00
License : GenerateLicense ( t , options ),
})
require . NoError ( t , err )
2022-10-06 19:28:22 -05:00
return l
2022-09-19 23:11:01 -05:00
}
// GenerateLicense returns a signed JWT using the test key.
func GenerateLicense ( t * testing . T , options LicenseOptions ) string {
2025-07-17 20:19:01 +10:00
t . Helper ()
2022-09-19 23:11:01 -05:00
if options . ExpiresAt . IsZero () {
options . ExpiresAt = time . Now (). Add ( time . Hour )
}
if options . GraceAt . IsZero () {
options . GraceAt = time . Now (). Add ( time . Hour )
}
2024-09-03 09:09:38 +04:00
if options . NotBefore . IsZero () {
options . NotBefore = time . Now (). Add ( - time . Minute )
}
2022-12-06 12:38:38 -06:00
2025-07-17 20:19:01 +10:00
issuedAt := options . IssuedAt
if issuedAt . IsZero () {
issuedAt = time . Now (). Add ( - time . Minute )
}
2022-10-06 19:28:22 -05:00
c := & license . Claims {
2022-09-19 23:11:01 -05:00
RegisteredClaims : jwt . RegisteredClaims {
2023-02-13 18:21:58 -06:00
ID : uuid . NewString (),
2022-09-19 23:11:01 -05:00
Issuer : "test@testing.test" ,
ExpiresAt : jwt . NewNumericDate ( options . ExpiresAt ),
2024-09-03 09:09:38 +04:00
NotBefore : jwt . NewNumericDate ( options . NotBefore ),
2025-07-17 20:19:01 +10:00
IssuedAt : jwt . NewNumericDate ( issuedAt ),
2022-09-19 23:11:01 -05:00
},
LicenseExpires : jwt . NewNumericDate ( options . GraceAt ),
AccountType : options . AccountType ,
AccountID : options . AccountID ,
2024-04-29 16:50:11 -04:00
DeploymentIDs : options . DeploymentIDs ,
2022-10-06 19:28:22 -05:00
Trial : options . Trial ,
Version : license . CurrentVersion ,
AllFeatures : options . AllFeatures ,
2024-07-24 07:07:59 -10:00
FeatureSet : options . FeatureSet ,
2023-01-17 17:04:29 -06:00
Features : options . Features ,
2022-09-19 23:11:01 -05:00
}
2025-07-17 20:19:01 +10:00
return GenerateLicenseRaw ( t , c )
}
func GenerateLicenseRaw ( t * testing . T , claims jwt . Claims ) string {
t . Helper ()
tok := jwt . NewWithClaims ( jwt . SigningMethodEdDSA , claims )
2022-10-06 19:28:22 -05:00
tok . Header [ license . HeaderKeyID ] = testKeyID
2022-09-19 23:11:01 -05:00
signedTok , err := tok . SignedString ( testPrivateKey )
require . NoError ( t , err )
return signedTok
}
type nopcloser struct {}
func ( nopcloser ) Close () error { return nil }
2024-07-25 16:07:53 -05:00
type CreateOrganizationOptions struct {
// IncludeProvisionerDaemon will spin up an external provisioner for the organization.
// This requires enterprise and the feature 'codersdk.FeatureExternalProvisionerDaemons'
IncludeProvisionerDaemon bool
}
func CreateOrganization ( t * testing . T , client * codersdk . Client , opts CreateOrganizationOptions , mutators ... func ( * codersdk . CreateOrganizationRequest )) codersdk . Organization {
ctx := testutil . Context ( t , testutil . WaitMedium )
req := codersdk . CreateOrganizationRequest {
Name : strings . ReplaceAll ( strings . ToLower ( namesgenerator . GetRandomName ( 0 )), "_" , "-" ),
DisplayName : namesgenerator . GetRandomName ( 1 ),
Description : namesgenerator . GetRandomName ( 1 ),
Icon : "" ,
}
for _ , mutator := range mutators {
mutator ( & req )
}
org , err := client . CreateOrganization ( ctx , req )
require . NoError ( t , err )
if opts . IncludeProvisionerDaemon {
closer := NewExternalProvisionerDaemon ( t , client , org . ID , map [ string ] string {})
t . Cleanup ( func () {
_ = closer . Close ()
})
}
return org
}
2024-12-17 21:57:03 +00:00
// NewExternalProvisionerDaemon runs an external provisioner daemon in a
// goroutine and returns a closer to stop it. The echo provisioner is used
// here. This is the default provisioner for tests and should be fine for
// most use cases. If you need to test terraform-specific behaviors, use
// NewExternalProvisionerDaemonTerraform instead.
2024-07-25 16:07:53 -05:00
func NewExternalProvisionerDaemon ( t testing . TB , client * codersdk . Client , org uuid . UUID , tags map [ string ] string ) io . Closer {
t . Helper ()
2024-12-17 21:57:03 +00:00
return newExternalProvisionerDaemon ( t , client , org , tags , codersdk . ProvisionerTypeEcho )
}
// NewExternalProvisionerDaemonTerraform runs an external provisioner daemon in
// a goroutine and returns a closer to stop it. The terraform provisioner is
// used here. Avoid using this unless you need to test terraform-specific
// behaviors!
func NewExternalProvisionerDaemonTerraform ( t testing . TB , client * codersdk . Client , org uuid . UUID , tags map [ string ] string ) io . Closer {
t . Helper ()
return newExternalProvisionerDaemon ( t , client , org , tags , codersdk . ProvisionerTypeTerraform )
}
// nolint // This function is a helper for tests and should not be linted.
func newExternalProvisionerDaemon ( t testing . TB , client * codersdk . Client , org uuid . UUID , tags map [ string ] string , provisionerType codersdk . ProvisionerType ) io . Closer {
t . Helper ()
2024-07-25 16:07:53 -05:00
entitlements , err := client . Entitlements ( context . Background ())
if err != nil {
t . Errorf ( "external provisioners requires a license with entitlements. The client failed to fetch the entitlements, is this an enterprise instance of coderd?" )
t . FailNow ()
return nil
}
feature := entitlements . Features [ codersdk . FeatureExternalProvisionerDaemons ]
if ! feature . Enabled || feature . Entitlement != codersdk . EntitlementEntitled {
2024-12-17 21:57:03 +00:00
t . Errorf ( "external provisioner daemons require an entitled license" )
t . FailNow ()
2024-07-25 16:07:53 -05:00
return nil
}
2025-05-12 10:31:38 -05:00
provisionerClient , provisionerSrv := drpcsdk . MemTransportPipe ()
2024-07-25 16:07:53 -05:00
ctx , cancelFunc := context . WithCancel ( context . Background ())
serveDone := make ( chan struct {})
t . Cleanup ( func () {
2024-12-17 21:57:03 +00:00
_ = provisionerClient . Close ()
_ = provisionerSrv . Close ()
2024-07-25 16:07:53 -05:00
cancelFunc ()
<- serveDone
})
2024-12-17 21:57:03 +00:00
switch provisionerType {
case codersdk . ProvisionerTypeTerraform :
// Ensure the Terraform binary is present in the path.
// If not, we fail this test rather than downloading it.
terraformPath , err := exec . LookPath ( "terraform" )
require . NoError ( t , err , "terraform binary not found in PATH" )
t . Logf ( "using Terraform binary at %s" , terraformPath )
go func () {
defer close ( serveDone )
assert . NoError ( t , terraform . Serve ( ctx , & terraform . ServeOptions {
BinaryPath : terraformPath ,
CachePath : t . TempDir (),
ServeOptions : & provisionersdk . ServeOptions {
Listener : provisionerSrv ,
WorkDirectory : t . TempDir (),
},
}))
}()
case codersdk . ProvisionerTypeEcho :
go func () {
defer close ( serveDone )
assert . NoError ( t , echo . Serve ( ctx , & provisionersdk . ServeOptions {
Listener : provisionerSrv ,
WorkDirectory : t . TempDir (),
}))
}()
default :
t . Fatalf ( "unsupported provisioner type: %s" , provisionerType )
return nil
}
2024-07-25 16:07:53 -05:00
daemon := provisionerd . New ( func ( ctx context . Context ) ( provisionerdproto . DRPCProvisionerDaemonClient , error ) {
return client . ServeProvisionerDaemon ( ctx , codersdk . ServeProvisionerDaemonRequest {
2025-01-28 09:11:39 +00:00
Name : testutil . GetRandomName ( t ),
2024-07-25 16:07:53 -05:00
Organization : org ,
2024-12-17 21:57:03 +00:00
Provisioners : [] codersdk . ProvisionerType { provisionerType },
2024-07-25 16:07:53 -05:00
Tags : tags ,
})
}, & provisionerd . Options {
2024-12-17 21:57:03 +00:00
Logger : testutil . Logger ( t ). Named ( "provisionerd" ). Leveled ( slog . LevelDebug ),
2024-07-25 16:07:53 -05:00
UpdateInterval : 250 * time . Millisecond ,
ForceCancelInterval : 5 * time . Second ,
Connector : provisionerd . LocalProvisioners {
2024-12-17 21:57:03 +00:00
string ( provisionerType ): sdkproto . NewDRPCProvisionerClient ( provisionerClient ),
2024-07-25 16:07:53 -05:00
},
})
closer := coderdtest . NewProvisionerDaemonCloser ( daemon )
t . Cleanup ( func () {
_ = closer . Close ()
})
return closer
}