2022-01-20 07:46:51 -06:00
package coderdtest
import (
2022-03-25 13:48:08 -06:00
"bytes"
2022-01-20 07:46:51 -06:00
"context"
2022-03-28 13:31:03 -06:00
"crypto"
2022-03-25 13:48:08 -06:00
"crypto/rand"
"crypto/rsa"
2022-03-28 13:31:03 -06:00
"crypto/sha256"
"crypto/x509"
2022-04-19 08:48:13 -05:00
"crypto/x509/pkix"
2022-03-25 13:48:08 -06:00
"encoding/base64"
"encoding/json"
2022-03-28 13:31:03 -06:00
"encoding/pem"
2022-09-06 12:07:00 -05:00
"errors"
2022-05-12 15:56:23 -05:00
"fmt"
2022-02-03 14:34:50 -06:00
"io"
2022-03-25 13:48:08 -06:00
"math/big"
2022-02-18 22:06:56 -06:00
"net"
2022-03-25 13:48:08 -06:00
"net/http"
2022-01-20 07:46:51 -06:00
"net/http/httptest"
"net/url"
2022-08-31 20:09:44 -05:00
"strconv"
2022-02-05 18:24:51 -06:00
"strings"
2022-01-20 07:46:51 -06:00
"testing"
2022-02-03 14:34:50 -06:00
"time"
2022-01-20 07:46:51 -06:00
2022-03-25 13:48:08 -06:00
"cloud.google.com/go/compute/metadata"
2022-04-19 08:48:13 -05:00
"github.com/fullsailor/pkcs7"
2022-03-25 13:48:08 -06:00
"github.com/golang-jwt/jwt"
2022-02-05 18:24:51 -06:00
"github.com/google/uuid"
"github.com/moby/moby/pkg/namesgenerator"
2022-06-24 10:25:01 -05:00
"github.com/spf13/afero"
2022-05-24 08:58:39 +01:00
"github.com/stretchr/testify/assert"
2022-01-20 07:46:51 -06:00
"github.com/stretchr/testify/require"
2022-06-29 14:17:32 -05:00
"golang.org/x/xerrors"
2022-02-21 14:36:29 -06:00
"google.golang.org/api/idtoken"
"google.golang.org/api/option"
2022-09-14 20:21:53 -05:00
"tailscale.com/net/stun/stuntest"
2022-08-31 20:09:44 -05:00
"tailscale.com/tailcfg"
2022-09-14 20:21:53 -05:00
"tailscale.com/types/nettype"
2022-01-20 07:46:51 -06:00
2022-02-03 14:34:50 -06:00
"cdr.dev/slog"
2022-01-20 07:46:51 -06:00
"cdr.dev/slog/sloggers/slogtest"
"github.com/coder/coder/coderd"
2022-09-08 21:16:16 -05:00
"github.com/coder/coder/coderd/audit"
2022-05-11 23:03:02 +01:00
"github.com/coder/coder/coderd/autobuild/executor"
2022-03-28 13:31:03 -06:00
"github.com/coder/coder/coderd/awsidentity"
2022-03-25 16:07:45 -05:00
"github.com/coder/coder/coderd/database"
2022-10-10 15:37:06 -05:00
"github.com/coder/coder/coderd/database/dbtestutil"
2022-04-05 19:18:26 -05:00
"github.com/coder/coder/coderd/gitsshkey"
2022-06-24 10:25:01 -05:00
"github.com/coder/coder/coderd/rbac"
"github.com/coder/coder/coderd/telemetry"
"github.com/coder/coder/coderd/util/ptr"
2022-01-20 07:46:51 -06:00
"github.com/coder/coder/codersdk"
2022-03-25 13:48:08 -06:00
"github.com/coder/coder/cryptorand"
2022-02-04 16:51:54 -06:00
"github.com/coder/coder/provisioner/echo"
2022-02-03 14:34:50 -06:00
"github.com/coder/coder/provisionerd"
"github.com/coder/coder/provisionersdk"
"github.com/coder/coder/provisionersdk/proto"
2022-08-01 15:45:05 +03:00
"github.com/coder/coder/testutil"
2022-01-20 07:46:51 -06:00
)
2022-02-21 14:36:29 -06:00
type Options struct {
2022-09-23 08:30:32 +10:00
AppHostname string
2022-04-19 08:48:13 -05:00
AWSCertificates awsidentity . Certificates
2022-05-17 13:43:19 -05:00
Authorizer rbac . Authorizer
2022-04-19 08:48:13 -05:00
AzureCertificates x509 . VerifyOptions
2022-04-23 17:58:57 -05:00
GithubOAuth2Config * coderd . GithubOAuth2Config
2022-07-31 23:05:35 -05:00
OIDCConfig * coderd . OIDCConfig
2022-04-19 08:48:13 -05:00
GoogleTokenValidator * idtoken . Validator
SSHKeygenAlgorithm gitsshkey . Algorithm
2022-04-22 15:27:55 -05:00
APIRateLimit int
2022-08-26 05:32:35 +10:00
AutoImportTemplates [] coderd . AutoImportTemplate
2022-05-13 17:14:24 +01:00
AutobuildTicker <- chan time . Time
2022-05-30 20:23:36 +01:00
AutobuildStats chan <- executor . Stats
2022-09-08 21:16:16 -05:00
Auditor audit . Auditor
2022-05-19 15:47:45 -07:00
2022-09-04 11:28:09 -05:00
// IncludeProvisionerDaemon when true means to start an in-memory provisionerD
2022-09-06 12:07:00 -05:00
IncludeProvisionerDaemon bool
MetricsCacheRefreshInterval time . Duration
AgentStatsRefreshInterval time . Duration
2022-10-10 15:04:15 -04:00
DeploymentFlags * codersdk . DeploymentFlags
2022-02-21 14:36:29 -06:00
}
2022-05-25 22:14:08 -05:00
// New constructs a codersdk client connected to an in-memory API instance.
2022-05-18 15:49:46 -05:00
func New ( t * testing . T , options * Options ) * codersdk . Client {
2022-06-27 13:50:52 -05:00
client , _ := newWithCloser ( t , options )
2022-05-25 22:14:08 -05:00
return client
2022-05-18 15:49:46 -05:00
}
2022-06-27 13:50:52 -05:00
// NewWithProvisionerCloser returns a client as well as a handle to close
// the provisioner. This is a temporary function while work is done to
// standardize how provisioners are registered with coderd. The option
// to include a provisioner is set to true for convenience.
func NewWithProvisionerCloser ( t * testing . T , options * Options ) ( * codersdk . Client , io . Closer ) {
if options == nil {
options = & Options {}
}
2022-09-04 11:28:09 -05:00
options . IncludeProvisionerDaemon = true
2022-06-27 13:50:52 -05:00
client , closer := newWithCloser ( t , options )
return client , closer
}
// newWithCloser constructs a codersdk client connected to an in-memory API instance.
// The returned closer closes a provisioner if it was provided
// The API is intentionally not returned here because coderd tests should not
// require a handle to the API. Do not expose the API or wrath shall descend
// upon thee. Even the io.Closer that is exposed here shouldn't be exposed
// and is a temporary measure while the API to register provisioners is ironed
// out.
func newWithCloser ( t * testing . T , options * Options ) ( * codersdk . Client , io . Closer ) {
2022-09-19 23:11:01 -05:00
client , closer , _ := NewWithAPI ( t , options )
2022-08-24 12:05:46 -07:00
return client , closer
}
2022-09-19 23:11:01 -05:00
func NewOptions ( t * testing . T , options * Options ) ( * httptest . Server , context . CancelFunc , * coderd . Options ) {
2022-02-21 14:36:29 -06:00
if options == nil {
options = & Options {}
}
2022-04-19 08:48:13 -05:00
if options . GoogleTokenValidator == nil {
2022-02-21 14:36:29 -06:00
ctx , cancelFunc := context . WithCancel ( context . Background ())
t . Cleanup ( cancelFunc )
var err error
2022-04-19 08:48:13 -05:00
options . GoogleTokenValidator , err = idtoken . NewValidator ( ctx , option . WithoutAuthentication ())
2022-02-21 14:36:29 -06:00
require . NoError ( t , err )
}
2022-05-13 17:14:24 +01:00
if options . AutobuildTicker == nil {
2022-05-11 23:03:02 +01:00
ticker := make ( chan time . Time )
2022-05-13 17:14:24 +01:00
options . AutobuildTicker = ticker
2022-05-11 23:03:02 +01:00
t . Cleanup ( func () { close ( ticker ) })
}
2022-05-30 20:23:36 +01:00
if options . AutobuildStats != nil {
t . Cleanup ( func () {
close ( options . AutobuildStats )
})
}
2022-02-21 14:36:29 -06:00
2022-10-10 15:37:06 -05:00
db , pubsub := dbtestutil . NewDB ( t )
2022-01-22 15:58:26 -06:00
2022-04-18 17:40:25 -05:00
ctx , cancelFunc := context . WithCancel ( context . Background ())
2022-05-11 23:03:02 +01:00
lifecycleExecutor := executor . New (
ctx ,
db ,
slogtest . Make ( t , nil ). Named ( "autobuild.executor" ). Leveled ( slog . LevelDebug ),
2022-05-13 17:14:24 +01:00
options . AutobuildTicker ,
2022-05-30 20:23:36 +01:00
). WithStatsChannel ( options . AutobuildStats )
2022-05-11 23:03:02 +01:00
lifecycleExecutor . Run ()
srv := httptest . NewUnstartedServer ( nil )
2022-02-18 22:06:56 -06:00
srv . Config . BaseContext = func ( _ net . Listener ) context . Context {
return ctx
}
srv . Start ()
2022-07-25 19:22:02 +03:00
t . Cleanup ( srv . Close )
2022-09-13 13:31:33 -04:00
tcpAddr , ok := srv . Listener . Addr ().( * net . TCPAddr )
require . True ( t , ok )
2022-01-20 10:00:13 -06:00
serverURL , err := url . Parse ( srv . URL )
2022-01-20 07:46:51 -06:00
require . NoError ( t , err )
2022-09-13 13:31:33 -04:00
serverURL . Host = fmt . Sprintf ( "localhost:%d" , tcpAddr . Port )
2022-04-05 19:18:26 -05:00
2022-08-31 20:09:44 -05:00
derpPort , err := strconv . Atoi ( serverURL . Port ())
require . NoError ( t , err )
2022-09-14 20:21:53 -05:00
stunAddr , stunCleanup := stuntest . ServeWithPacketListener ( t , nettype . Std {})
t . Cleanup ( stunCleanup )
2022-04-05 19:18:26 -05:00
// match default with cli default
if options . SSHKeygenAlgorithm == "" {
options . SSHKeygenAlgorithm = gitsshkey . AlgorithmEd25519
}
2022-09-19 23:11:01 -05:00
return srv , cancelFunc , & coderd . Options {
2022-04-11 18:54:30 -05:00
AgentConnectionUpdateFrequency : 150 * time . Millisecond ,
2022-06-27 10:06:51 -05:00
// Force a long disconnection timeout to ensure
// agents are not marked as disconnected during slow tests.
2022-08-01 15:45:05 +03:00
AgentInactiveDisconnectTimeout : testutil . WaitShort ,
2022-03-22 13:17:50 -06:00
AccessURL : serverURL ,
2022-09-23 08:30:32 +10:00
AppHostname : options . AppHostname ,
2022-03-22 13:17:50 -06:00
Logger : slogtest . Make ( t , nil ). Leveled ( slog . LevelDebug ),
2022-07-25 19:24:32 +03:00
CacheDir : t . TempDir (),
2022-03-22 13:17:50 -06:00
Database : db ,
Pubsub : pubsub ,
2022-02-28 12:26:01 -06:00
2022-09-19 23:11:01 -05:00
Auditor : options . Auditor ,
2022-04-19 08:48:13 -05:00
AWSCertificates : options . AWSCertificates ,
AzureCertificates : options . AzureCertificates ,
2022-04-23 17:58:57 -05:00
GithubOAuth2Config : options . GithubOAuth2Config ,
2022-07-31 23:05:35 -05:00
OIDCConfig : options . OIDCConfig ,
2022-04-19 08:48:13 -05:00
GoogleTokenValidator : options . GoogleTokenValidator ,
2022-04-05 19:18:26 -05:00
SSHKeygenAlgorithm : options . SSHKeygenAlgorithm ,
2022-04-22 15:27:55 -05:00
APIRateLimit : options . APIRateLimit ,
2022-05-17 13:43:19 -05:00
Authorizer : options . Authorizer ,
2022-06-17 00:26:40 -05:00
Telemetry : telemetry . NewNoop (),
2022-08-31 20:09:44 -05:00
DERPMap : & tailcfg . DERPMap {
Regions : map [ int ] * tailcfg . DERPRegion {
1 : {
2022-09-26 12:56:04 -05:00
EmbeddedRelay : true ,
RegionID : 1 ,
RegionCode : "coder" ,
RegionName : "Coder" ,
2022-08-31 20:09:44 -05:00
Nodes : [] * tailcfg . DERPNode {{
Name : "1a" ,
RegionID : 1 ,
IPv4 : "127.0.0.1" ,
DERPPort : derpPort ,
2022-09-14 20:21:53 -05:00
STUNPort : stunAddr . Port ,
2022-08-31 20:09:44 -05:00
InsecureForTests : true ,
ForceHTTP : true ,
}},
},
},
},
2022-09-01 14:58:23 -05:00
AutoImportTemplates : options . AutoImportTemplates ,
2022-09-06 12:07:00 -05:00
MetricsCacheRefreshInterval : options . MetricsCacheRefreshInterval ,
AgentStatsRefreshInterval : options . AgentStatsRefreshInterval ,
2022-10-10 15:04:15 -04:00
DeploymentFlags : options . DeploymentFlags ,
2022-09-19 23:11:01 -05:00
}
}
2022-06-27 13:50:52 -05:00
2022-09-19 23:11:01 -05:00
// NewWithAPI constructs an in-memory API instance and returns a client to talk to it.
// Most tests never need a reference to the API, but AuthorizationTest in this module uses it.
// Do not expose the API or wrath shall descend upon thee.
func NewWithAPI ( t * testing . T , options * Options ) ( * codersdk . Client , io . Closer , * coderd . API ) {
if options == nil {
options = & Options {}
}
srv , cancelFunc , newOptions := NewOptions ( t , options )
// We set the handler after server creation for the access URL.
coderAPI := coderd . New ( newOptions )
srv . Config . Handler = coderAPI . RootHandler
2022-06-27 13:50:52 -05:00
var provisionerCloser io . Closer = nopcloser {}
2022-09-04 11:28:09 -05:00
if options . IncludeProvisionerDaemon {
2022-06-27 13:50:52 -05:00
provisionerCloser = NewProvisionerDaemon ( t , coderAPI )
2022-05-19 15:47:45 -07:00
}
2022-02-20 16:29:16 -06:00
t . Cleanup ( func () {
2022-09-19 23:11:01 -05:00
cancelFunc ()
2022-06-27 13:50:52 -05:00
_ = provisionerCloser . Close ()
2022-09-19 23:11:01 -05:00
_ = coderAPI . Close ()
2022-02-20 16:29:16 -06:00
})
2022-09-19 23:11:01 -05:00
return codersdk . New ( coderAPI . AccessURL ), provisionerCloser , coderAPI
2022-05-17 13:43:19 -05:00
}
2022-02-05 18:24:51 -06:00
// NewProvisionerDaemon launches a provisionerd instance configured to work
// well with coderd testing. It registers the "echo" provisioner for
// quick testing.
2022-05-25 22:14:08 -05:00
func NewProvisionerDaemon ( t * testing . T , coderAPI * coderd . API ) io . Closer {
2022-02-05 18:24:51 -06:00
echoClient , echoServer := provisionersdk . TransportPipe ()
ctx , cancelFunc := context . WithCancel ( context . Background ())
t . Cleanup ( func () {
_ = echoClient . Close ()
_ = echoServer . Close ()
cancelFunc ()
})
2022-06-16 10:09:22 -05:00
fs := afero . NewMemMapFs ()
2022-02-05 18:24:51 -06:00
go func () {
2022-06-16 10:09:22 -05:00
err := echo . Serve ( ctx , fs , & provisionersdk . ServeOptions {
2022-02-05 18:24:51 -06:00
Listener : echoServer ,
})
2022-05-24 08:58:39 +01:00
assert . NoError ( t , err )
2022-02-05 18:24:51 -06:00
}()
2022-05-25 22:14:08 -05:00
closer := provisionerd . New ( coderAPI . ListenProvisionerDaemon , & provisionerd . Options {
2022-06-16 10:09:22 -05:00
Filesystem : fs ,
2022-03-22 13:17:50 -06:00
Logger : slogtest . Make ( t , nil ). Named ( "provisionerd" ). Leveled ( slog . LevelDebug ),
2022-09-04 11:28:09 -05:00
PollInterval : 50 * time . Millisecond ,
UpdateInterval : 250 * time . Millisecond ,
2022-07-21 20:29:45 +01:00
ForceCancelInterval : time . Second ,
2022-02-05 18:24:51 -06:00
Provisioners : provisionerd . Provisioners {
string ( database . ProvisionerTypeEcho ): proto . NewDRPCProvisionerClient ( provisionersdk . Conn ( echoClient )),
},
WorkDirectory : t . TempDir (),
})
t . Cleanup ( func () {
_ = closer . Close ()
})
return closer
}
2022-05-06 09:20:08 -05:00
var FirstUserParams = codersdk . CreateFirstUserRequest {
Email : "testuser@coder.com" ,
Username : "testuser" ,
Password : "testpass" ,
OrganizationName : "testorg" ,
}
2022-03-07 11:40:54 -06:00
// CreateFirstUser creates a user with preset credentials and authenticates
2022-02-05 18:24:51 -06:00
// with the passed in codersdk client.
2022-03-22 13:17:50 -06:00
func CreateFirstUser ( t * testing . T , client * codersdk . Client ) codersdk . CreateFirstUserResponse {
2022-05-06 09:20:08 -05:00
resp , err := client . CreateFirstUser ( context . Background (), FirstUserParams )
2022-02-05 18:24:51 -06:00
require . NoError ( t , err )
2022-03-22 13:17:50 -06:00
login , err := client . LoginWithPassword ( context . Background (), codersdk . LoginWithPasswordRequest {
2022-05-06 09:20:08 -05:00
Email : FirstUserParams . Email ,
Password : FirstUserParams . Password ,
2022-02-05 18:24:51 -06:00
})
require . NoError ( t , err )
2022-02-12 13:34:04 -06:00
client . SessionToken = login . SessionToken
2022-03-07 11:40:54 -06:00
return resp
2022-02-05 18:24:51 -06:00
}
2022-03-07 11:40:54 -06:00
// CreateAnotherUser creates and authenticates a new user.
2022-05-12 15:56:23 -05:00
func CreateAnotherUser ( t * testing . T , client * codersdk . Client , organizationID uuid . UUID , roles ... string ) * codersdk . Client {
2022-08-09 13:16:53 -05:00
userClient , _ := createAnotherUserRetry ( t , client , organizationID , 5 , roles ... )
return userClient
}
func CreateAnotherUserWithUser ( t * testing . T , client * codersdk . Client , organizationID uuid . UUID , roles ... string ) ( * codersdk . Client , codersdk . User ) {
2022-06-29 14:17:32 -05:00
return createAnotherUserRetry ( t , client , organizationID , 5 , roles ... )
}
2022-08-09 13:16:53 -05:00
func createAnotherUserRetry ( t * testing . T , client * codersdk . Client , organizationID uuid . UUID , retries int , roles ... string ) ( * codersdk . Client , codersdk . User ) {
2022-03-22 13:17:50 -06:00
req := codersdk . CreateUserRequest {
2022-07-20 18:03:04 +03:00
Email : namesgenerator . GetRandomName ( 10 ) + "@coder.com" ,
2022-03-07 11:40:54 -06:00
Username : randomUsername (),
Password : "testpass" ,
2022-04-01 14:42:36 -05:00
OrganizationID : organizationID ,
2022-03-07 11:40:54 -06:00
}
2022-06-29 14:17:32 -05:00
2022-05-12 15:56:23 -05:00
user , err := client . CreateUser ( context . Background (), req )
2022-06-29 14:17:32 -05:00
var apiError * codersdk . Error
// If the user already exists by username or email conflict, try again up to "retries" times.
if err != nil && retries >= 0 && xerrors . As ( err , & apiError ) {
if apiError . StatusCode () == http . StatusConflict {
retries --
return createAnotherUserRetry ( t , client , organizationID , retries , roles ... )
}
}
2022-03-07 11:40:54 -06:00
require . NoError ( t , err )
2022-03-22 13:17:50 -06:00
login , err := client . LoginWithPassword ( context . Background (), codersdk . LoginWithPasswordRequest {
2022-03-07 11:40:54 -06:00
Email : req . Email ,
Password : req . Password ,
})
require . NoError ( t , err )
other := codersdk . New ( client . URL )
other . SessionToken = login . SessionToken
2022-05-12 15:56:23 -05:00
if len ( roles ) > 0 {
// Find the roles for the org vs the site wide roles
orgRoles := make ( map [ string ][] string )
var siteRoles [] string
for _ , roleName := range roles {
roleName := roleName
orgID , ok := rbac . IsOrgRole ( roleName )
if ok {
orgRoles [ orgID ] = append ( orgRoles [ orgID ], roleName )
} else {
siteRoles = append ( siteRoles , roleName )
}
}
// Update the roles
for _ , r := range user . Roles {
siteRoles = append ( siteRoles , r . Name )
}
2022-05-17 13:43:19 -05:00
_ , err := client . UpdateUserRoles ( context . Background (), user . ID . String (), codersdk . UpdateRoles { Roles : siteRoles })
2022-05-12 15:56:23 -05:00
require . NoError ( t , err , "update site roles" )
// Update org roles
for orgID , roles := range orgRoles {
organizationID , err := uuid . Parse ( orgID )
require . NoError ( t , err , fmt . Sprintf ( "parse org id %q" , orgID ))
2022-05-25 11:00:59 -05:00
_ , err = client . UpdateOrganizationMemberRoles ( context . Background (), organizationID , user . ID . String (),
2022-06-01 09:07:50 -05:00
codersdk . UpdateRoles { Roles : roles })
2022-05-12 15:56:23 -05:00
require . NoError ( t , err , "update org membership roles" )
}
}
2022-08-09 13:16:53 -05:00
return other , user
2022-03-07 11:40:54 -06:00
}
2022-04-06 12:42:40 -05:00
// CreateTemplateVersion creates a template import provisioner job
2022-02-08 12:00:44 -06:00
// with the responses provided. It uses the "echo" provisioner for compatibility
// with testing.
2022-04-06 12:42:40 -05:00
func CreateTemplateVersion ( t * testing . T , client * codersdk . Client , organizationID uuid . UUID , res * echo . Responses ) codersdk . TemplateVersion {
2022-10-10 15:37:06 -05:00
t . Helper ()
2022-02-08 12:00:44 -06:00
data , err := echo . Tar ( res )
require . NoError ( t , err )
2022-03-07 11:40:54 -06:00
file , err := client . Upload ( context . Background (), codersdk . ContentTypeTar , data )
2022-02-08 12:00:44 -06:00
require . NoError ( t , err )
2022-04-06 12:42:40 -05:00
templateVersion , err := client . CreateTemplateVersion ( context . Background (), organizationID , codersdk . CreateTemplateVersionRequest {
2022-10-13 18:02:52 -05:00
FileID : file . ID ,
2022-05-19 13:04:44 -05:00
StorageMethod : codersdk . ProvisionerStorageMethodFile ,
Provisioner : codersdk . ProvisionerTypeEcho ,
2022-02-08 12:00:44 -06:00
})
require . NoError ( t , err )
2022-04-06 12:42:40 -05:00
return templateVersion
2022-02-08 12:00:44 -06:00
}
2022-05-18 09:33:33 -07:00
// CreateWorkspaceBuild creates a workspace build for the given workspace and transition.
func CreateWorkspaceBuild (
t * testing . T ,
client * codersdk . Client ,
workspace codersdk . Workspace ,
2022-07-22 20:02:49 +03:00
transition database . WorkspaceTransition ,
) codersdk . WorkspaceBuild {
2022-05-18 09:33:33 -07:00
req := codersdk . CreateWorkspaceBuildRequest {
2022-05-19 13:04:44 -05:00
Transition : codersdk . WorkspaceTransition ( transition ),
2022-05-18 09:33:33 -07:00
}
build , err := client . CreateWorkspaceBuild ( context . Background (), workspace . ID , req )
require . NoError ( t , err )
return build
}
2022-04-06 12:42:40 -05:00
// CreateTemplate creates a template with the "echo" provisioner for
2022-02-05 18:24:51 -06:00
// compatibility with testing. The name assigned is randomly generated.
2022-06-07 13:37:45 +01:00
func CreateTemplate ( t * testing . T , client * codersdk . Client , organization uuid . UUID , version uuid . UUID , mutators ... func ( * codersdk . CreateTemplateRequest )) codersdk . Template {
req := codersdk . CreateTemplateRequest {
2022-05-16 15:56:11 -05:00
Name : randomUsername (),
Description : randomUsername (),
VersionID : version ,
2022-06-07 13:37:45 +01:00
}
for _ , mut := range mutators {
mut ( & req )
}
template , err := client . CreateTemplate ( context . Background (), organization , req )
2022-02-05 18:24:51 -06:00
require . NoError ( t , err )
2022-04-06 12:42:40 -05:00
return template
2022-02-05 18:24:51 -06:00
}
2022-05-11 23:03:02 +01:00
// UpdateTemplateVersion creates a new template version with the "echo" provisioner
// and associates it with the given templateID.
func UpdateTemplateVersion ( t * testing . T , client * codersdk . Client , organizationID uuid . UUID , res * echo . Responses , templateID uuid . UUID ) codersdk . TemplateVersion {
data , err := echo . Tar ( res )
require . NoError ( t , err )
file , err := client . Upload ( context . Background (), codersdk . ContentTypeTar , data )
require . NoError ( t , err )
templateVersion , err := client . CreateTemplateVersion ( context . Background (), organizationID , codersdk . CreateTemplateVersionRequest {
TemplateID : templateID ,
2022-10-13 18:02:52 -05:00
FileID : file . ID ,
2022-05-19 13:04:44 -05:00
StorageMethod : codersdk . ProvisionerStorageMethodFile ,
Provisioner : codersdk . ProvisionerTypeEcho ,
2022-05-11 23:03:02 +01:00
})
require . NoError ( t , err )
return templateVersion
}
2022-04-06 12:42:40 -05:00
// AwaitTemplateImportJob awaits for an import job to reach completed status.
func AwaitTemplateVersionJob ( t * testing . T , client * codersdk . Client , version uuid . UUID ) codersdk . TemplateVersion {
2022-07-22 20:02:49 +03:00
t . Helper ()
2022-07-01 09:55:46 -07:00
t . Logf ( "waiting for template version job %s" , version )
2022-04-06 12:42:40 -05:00
var templateVersion codersdk . TemplateVersion
2022-09-04 11:28:09 -05:00
require . Eventually ( t , func () bool {
2022-02-05 18:24:51 -06:00
var err error
2022-09-04 11:28:09 -05:00
templateVersion , err = client . TemplateVersion ( context . Background (), version )
2022-07-22 20:02:49 +03:00
return assert . NoError ( t , err ) && templateVersion . Job . CompletedAt != nil
2022-09-04 11:28:09 -05:00
}, testutil . WaitShort , testutil . IntervalFast )
2022-04-06 12:42:40 -05:00
return templateVersion
2022-02-12 13:34:04 -06:00
}
2022-03-07 11:40:54 -06:00
// AwaitWorkspaceBuildJob waits for a workspace provision job to reach completed status.
2022-03-22 13:17:50 -06:00
func AwaitWorkspaceBuildJob ( t * testing . T , client * codersdk . Client , build uuid . UUID ) codersdk . WorkspaceBuild {
2022-07-22 20:02:49 +03:00
t . Helper ()
t . Logf ( "waiting for workspace build job %s" , build )
2022-03-22 13:17:50 -06:00
var workspaceBuild codersdk . WorkspaceBuild
2022-09-04 11:28:09 -05:00
require . Eventually ( t , func () bool {
workspaceBuild , err := client . WorkspaceBuild ( context . Background (), build )
2022-07-22 20:02:49 +03:00
return assert . NoError ( t , err ) && workspaceBuild . Job . CompletedAt != nil
2022-09-04 11:28:09 -05:00
}, testutil . WaitShort , testutil . IntervalFast )
2022-03-07 11:40:54 -06:00
return workspaceBuild
}
// AwaitWorkspaceAgents waits for all resources with agents to be connected.
2022-10-03 17:01:13 -04:00
func AwaitWorkspaceAgents ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID ) [] codersdk . WorkspaceResource {
2022-07-22 20:02:49 +03:00
t . Helper ()
2022-10-03 17:01:13 -04:00
t . Logf ( "waiting for workspace agents (workspace %s)" , workspaceID )
2022-03-22 13:17:50 -06:00
var resources [] codersdk . WorkspaceResource
2022-09-04 11:28:09 -05:00
require . Eventually ( t , func () bool {
2022-03-07 11:40:54 -06:00
var err error
2022-10-03 17:01:13 -04:00
workspace , err := client . Workspace ( context . Background (), workspaceID )
2022-07-22 20:02:49 +03:00
if ! assert . NoError ( t , err ) {
return false
}
2022-10-03 17:01:13 -04:00
if workspace . LatestBuild . Job . CompletedAt . IsZero () {
return false
}
for _ , resource := range workspace . LatestBuild . Resources {
2022-04-11 16:06:15 -05:00
for _ , agent := range resource . Agents {
2022-04-11 18:54:30 -05:00
if agent . Status != codersdk . WorkspaceAgentConnected {
2022-08-05 16:34:44 +01:00
t . Logf ( "agent %s not connected yet" , agent . Name )
2022-04-11 16:06:15 -05:00
return false
}
2022-03-07 11:40:54 -06:00
}
}
2022-10-03 17:01:13 -04:00
resources = workspace . LatestBuild . Resources
2022-03-07 11:40:54 -06:00
return true
2022-09-04 11:28:09 -05:00
}, testutil . WaitLong , testutil . IntervalFast )
2022-03-07 11:40:54 -06:00
return resources
2022-02-05 18:24:51 -06:00
}
2022-04-06 12:42:40 -05:00
// CreateWorkspace creates a workspace for the user and template provided.
2022-02-05 18:24:51 -06:00
// A random name is generated for it.
2022-05-23 23:31:41 +01:00
// To customize the defaults, pass a mutator func.
func CreateWorkspace ( t * testing . T , client * codersdk . Client , organization uuid . UUID , templateID uuid . UUID , mutators ... func ( * codersdk . CreateWorkspaceRequest )) codersdk . Workspace {
t . Helper ()
req := codersdk . CreateWorkspaceRequest {
TemplateID : templateID ,
Name : randomUsername (),
2022-06-07 13:37:45 +01:00
AutostartSchedule : ptr . Ref ( "CRON_TZ=US/Central 30 9 * * 1-5" ),
2022-06-02 11:23:34 +01:00
TTLMillis : ptr . Ref (( 8 * time . Hour ). Milliseconds ()),
2022-05-23 23:31:41 +01:00
}
for _ , mutator := range mutators {
mutator ( & req )
}
2022-09-23 20:17:10 -05:00
workspace , err := client . CreateWorkspace ( context . Background (), organization , codersdk . Me , req )
2022-02-05 18:24:51 -06:00
require . NoError ( t , err )
return workspace
}
2022-06-07 13:37:45 +01:00
// TransitionWorkspace is a convenience method for transitioning a workspace from one state to another.
func MustTransitionWorkspace ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID , from , to database . WorkspaceTransition ) codersdk . Workspace {
t . Helper ()
ctx := context . Background ()
workspace , err := client . Workspace ( ctx , workspaceID )
require . NoError ( t , err , "unexpected error fetching workspace" )
require . Equal ( t , workspace . LatestBuild . Transition , codersdk . WorkspaceTransition ( from ), "expected workspace state: %s got: %s" , from , workspace . LatestBuild . Transition )
template , err := client . Template ( ctx , workspace . TemplateID )
require . NoError ( t , err , "fetch workspace template" )
build , err := client . CreateWorkspaceBuild ( ctx , workspace . ID , codersdk . CreateWorkspaceBuildRequest {
TemplateVersionID : template . ActiveVersionID ,
Transition : codersdk . WorkspaceTransition ( to ),
})
require . NoError ( t , err , "unexpected error transitioning workspace to %s" , to )
_ = AwaitWorkspaceBuildJob ( t , client , build . ID )
updated := MustWorkspace ( t , client , workspace . ID )
require . Equal ( t , codersdk . WorkspaceTransition ( to ), updated . LatestBuild . Transition , "expected workspace to be in state %s but got %s" , to , updated . LatestBuild . Transition )
return updated
}
// MustWorkspace is a convenience method for fetching a workspace that should exist.
func MustWorkspace ( t * testing . T , client * codersdk . Client , workspaceID uuid . UUID ) codersdk . Workspace {
t . Helper ()
ctx := context . Background ()
ws , err := client . Workspace ( ctx , workspaceID )
if err != nil && strings . Contains ( err . Error (), "status code 410" ) {
ws , err = client . DeletedWorkspace ( ctx , workspaceID )
}
require . NoError ( t , err , "no workspace found with id %s" , workspaceID )
return ws
}
2022-03-25 13:48:08 -06:00
// NewGoogleInstanceIdentity returns a metadata client and ID token validator for faking
// instance authentication for Google Cloud.
// nolint:revive
func NewGoogleInstanceIdentity ( t * testing . T , instanceID string , expired bool ) ( * idtoken . Validator , * metadata . Client ) {
keyID , err := cryptorand . String ( 12 )
require . NoError ( t , err )
claims := jwt . MapClaims {
"google" : map [ string ] interface {}{
"compute_engine" : map [ string ] string {
"instance_id" : instanceID ,
},
},
}
if ! expired {
claims [ "exp" ] = time . Now (). AddDate ( 1 , 0 , 0 ). Unix ()
}
token := jwt . NewWithClaims ( jwt . SigningMethodRS256 , claims )
token . Header [ "kid" ] = keyID
privateKey , err := rsa . GenerateKey ( rand . Reader , 2048 )
require . NoError ( t , err )
signedKey , err := token . SignedString ( privateKey )
require . NoError ( t , err )
// Taken from: https://github.com/googleapis/google-api-go-client/blob/4bb729045d611fa77bdbeb971f6a1204ba23161d/idtoken/validate.go#L57-L75
type jwk struct {
Kid string `json:"kid"`
N string `json:"n"`
E string `json:"e"`
}
type certResponse struct {
Keys [] jwk `json:"keys"`
}
validator , err := idtoken . NewValidator ( context . Background (), option . WithHTTPClient ( & http . Client {
Transport : roundTripper ( func ( r * http . Request ) ( * http . Response , error ) {
data , err := json . Marshal ( certResponse {
Keys : [] jwk {{
Kid : keyID ,
N : base64 . RawURLEncoding . EncodeToString ( privateKey . N . Bytes ()),
E : base64 . RawURLEncoding . EncodeToString ( new ( big . Int ). SetInt64 ( int64 ( privateKey . E )). Bytes ()),
}},
})
require . NoError ( t , err )
return & http . Response {
StatusCode : http . StatusOK ,
2022-03-29 14:59:32 -05:00
Body : io . NopCloser ( bytes . NewReader ( data )),
2022-03-25 13:48:08 -06:00
Header : make ( http . Header ),
}, nil
}),
}))
require . NoError ( t , err )
return validator , metadata . NewClient ( & http . Client {
Transport : roundTripper ( func ( r * http . Request ) ( * http . Response , error ) {
return & http . Response {
StatusCode : http . StatusOK ,
2022-03-29 14:59:32 -05:00
Body : io . NopCloser ( bytes . NewReader ([] byte ( signedKey ))),
2022-03-25 13:48:08 -06:00
Header : make ( http . Header ),
}, nil
}),
})
}
2022-03-28 13:31:03 -06:00
// NewAWSInstanceIdentity returns a metadata client and ID token validator for faking
// instance authentication for AWS.
func NewAWSInstanceIdentity ( t * testing . T , instanceID string ) ( awsidentity . Certificates , * http . Client ) {
privateKey , err := rsa . GenerateKey ( rand . Reader , 2048 )
require . NoError ( t , err )
document := [] byte ( `{"instanceId":"` + instanceID + `"}` )
hashedDocument := sha256 . Sum256 ( document )
signatureRaw , err := rsa . SignPKCS1v15 ( rand . Reader , privateKey , crypto . SHA256 , hashedDocument [:])
require . NoError ( t , err )
signature := make ([] byte , base64 . StdEncoding . EncodedLen ( len ( signatureRaw )))
base64 . StdEncoding . Encode ( signature , signatureRaw )
certificate , err := x509 . CreateCertificate ( rand . Reader , & x509 . Certificate {
SerialNumber : big . NewInt ( 2022 ),
}, & x509 . Certificate {}, & privateKey . PublicKey , privateKey )
require . NoError ( t , err )
certificatePEM := bytes . Buffer {}
err = pem . Encode ( & certificatePEM , & pem . Block {
Type : "CERTIFICATE" ,
Bytes : certificate ,
})
require . NoError ( t , err )
return awsidentity . Certificates {
awsidentity . Other : certificatePEM . String (),
}, & http . Client {
Transport : roundTripper ( func ( r * http . Request ) ( * http . Response , error ) {
// Only handle metadata server requests.
if r . URL . Host != "169.254.169.254" {
return http . DefaultTransport . RoundTrip ( r )
}
switch r . URL . Path {
case "/latest/api/token" :
return & http . Response {
StatusCode : http . StatusOK ,
2022-03-29 14:59:32 -05:00
Body : io . NopCloser ( bytes . NewReader ([] byte ( "faketoken" ))),
2022-03-28 13:31:03 -06:00
Header : make ( http . Header ),
}, nil
case "/latest/dynamic/instance-identity/signature" :
return & http . Response {
StatusCode : http . StatusOK ,
2022-03-29 14:59:32 -05:00
Body : io . NopCloser ( bytes . NewReader ( signature )),
2022-03-28 13:31:03 -06:00
Header : make ( http . Header ),
}, nil
case "/latest/dynamic/instance-identity/document" :
return & http . Response {
StatusCode : http . StatusOK ,
2022-03-29 14:59:32 -05:00
Body : io . NopCloser ( bytes . NewReader ( document )),
2022-03-28 13:31:03 -06:00
Header : make ( http . Header ),
}, nil
default :
panic ( "unhandled route: " + r . URL . Path )
}
}),
}
}
2022-04-19 08:48:13 -05:00
// NewAzureInstanceIdentity returns a metadata client and ID token validator for faking
// instance authentication for Azure.
func NewAzureInstanceIdentity ( t * testing . T , instanceID string ) ( x509 . VerifyOptions , * http . Client ) {
privateKey , err := rsa . GenerateKey ( rand . Reader , 2048 )
require . NoError ( t , err )
rawCertificate , err := x509 . CreateCertificate ( rand . Reader , & x509 . Certificate {
SerialNumber : big . NewInt ( 2022 ),
NotAfter : time . Now (). AddDate ( 1 , 0 , 0 ),
Subject : pkix . Name {
CommonName : "metadata.azure.com" ,
},
}, & x509 . Certificate {}, & privateKey . PublicKey , privateKey )
require . NoError ( t , err )
certificate , err := x509 . ParseCertificate ( rawCertificate )
require . NoError ( t , err )
signed , err := pkcs7 . NewSignedData ([] byte ( `{"vmId":"` + instanceID + `"}` ))
require . NoError ( t , err )
err = signed . AddSigner ( certificate , privateKey , pkcs7 . SignerInfoConfig {})
require . NoError ( t , err )
signatureRaw , err := signed . Finish ()
require . NoError ( t , err )
signature := make ([] byte , base64 . StdEncoding . EncodedLen ( len ( signatureRaw )))
base64 . StdEncoding . Encode ( signature , signatureRaw )
payload , err := json . Marshal ( codersdk . AzureInstanceIdentityToken {
Signature : string ( signature ),
Encoding : "pkcs7" ,
})
require . NoError ( t , err )
certPool := x509 . NewCertPool ()
certPool . AddCert ( certificate )
return x509 . VerifyOptions {
Intermediates : certPool ,
Roots : certPool ,
}, & http . Client {
Transport : roundTripper ( func ( r * http . Request ) ( * http . Response , error ) {
// Only handle metadata server requests.
if r . URL . Host != "169.254.169.254" {
return http . DefaultTransport . RoundTrip ( r )
}
switch r . URL . Path {
case "/metadata/attested/document" :
return & http . Response {
StatusCode : http . StatusOK ,
Body : io . NopCloser ( bytes . NewReader ( payload )),
Header : make ( http . Header ),
}, nil
default :
panic ( "unhandled route: " + r . URL . Path )
}
}),
}
}
2022-02-05 18:24:51 -06:00
func randomUsername () string {
2022-07-20 18:03:04 +03:00
return strings . ReplaceAll ( namesgenerator . GetRandomName ( 10 ), "_" , "-" )
2022-01-20 07:46:51 -06:00
}
2022-03-25 13:48:08 -06:00
// Used to easily create an HTTP transport!
type roundTripper func ( req * http . Request ) ( * http . Response , error )
func ( r roundTripper ) RoundTrip ( req * http . Request ) ( * http . Response , error ) {
return r ( req )
}
2022-06-27 13:50:52 -05:00
type nopcloser struct {}
func ( nopcloser ) Close () error { return nil }
2022-09-06 12:07:00 -05:00
// SDKError coerces err into an SDK error.
func SDKError ( t * testing . T , err error ) * codersdk . Error {
var cerr * codersdk . Error
require . True ( t , errors . As ( err , & cerr ))
return cerr
}