@@ -46,16 +46,18 @@ import (
4646//
4747// Note that this currently only supports requests to CRDB clusters.
4848type RoachtestHTTPClient struct {
49- client * httputil.Client
50- sessionID string
51- cluster cluster.Cluster
52- l * logger.Logger
49+ client * httputil.Client
50+ sessionID string
51+ cluster cluster.Cluster
52+ l * logger.Logger
53+ virtualClusterName string
5354 // Used for safely adding to the cookie jar.
5455 mu syncutil.Mutex
5556}
5657
5758type RoachtestHTTPOptions struct {
58- Timeout time.Duration
59+ Timeout time.Duration
60+ VirtualClusterName string
5961}
6062
6163func HTTPTimeout (timeout time.Duration ) func (options * RoachtestHTTPOptions ) {
@@ -64,6 +66,12 @@ func HTTPTimeout(timeout time.Duration) func(options *RoachtestHTTPOptions) {
6466 }
6567}
6668
69+ func VirtualCluster (name string ) func (* RoachtestHTTPOptions ) {
70+ return func (options * RoachtestHTTPOptions ) {
71+ options .VirtualClusterName = name
72+ }
73+ }
74+
6775// DefaultHTTPClient returns a default RoachtestHTTPClient.
6876// This should be the default HTTP client used if you are
6977// trying to make a request to a secure cluster.
@@ -89,12 +97,13 @@ func DefaultHTTPClient(
8997 if httpOptions .Timeout != 0 {
9098 roachtestHTTP .client .Timeout = httpOptions .Timeout
9199 }
100+ roachtestHTTP .virtualClusterName = httpOptions .VirtualClusterName
92101
93102 return & roachtestHTTP
94103}
95104
96105func (r * RoachtestHTTPClient ) Get (ctx context.Context , url string ) (* http.Response , error ) {
97- if err := r .addCookie (ctx , url ); err != nil {
106+ if err := r .addCookies (ctx , url ); err != nil {
98107 return nil , err
99108 }
100109 return r .client .Get (ctx , url )
@@ -103,7 +112,7 @@ func (r *RoachtestHTTPClient) Get(ctx context.Context, url string) (*http.Respon
103112func (r * RoachtestHTTPClient ) GetJSON (
104113 ctx context.Context , path string , response protoutil.Message , opts ... httputil.JSONOption ,
105114) error {
106- if err := r .addCookie (ctx , path ); err != nil {
115+ if err := r .addCookies (ctx , path ); err != nil {
107116 return err
108117 }
109118 return httputil .GetJSONWithOptions (* r .client .Client , path , response , opts ... )
@@ -112,7 +121,7 @@ func (r *RoachtestHTTPClient) GetJSON(
112121func (r * RoachtestHTTPClient ) PostProtobuf (
113122 ctx context.Context , path string , request , response protoutil.Message ,
114123) error {
115- if err := r .addCookie (ctx , path ); err != nil {
124+ if err := r .addCookies (ctx , path ); err != nil {
116125 return err
117126 }
118127 return httputil .PostProtobuf (ctx , * r .client .Client , path , request , response )
@@ -125,32 +134,38 @@ func (r *RoachtestHTTPClient) ResetSession() {
125134 r .sessionID = ""
126135}
127136
128- func (r * RoachtestHTTPClient ) addCookie (ctx context.Context , cookieUrl string ) error {
137+ func (r * RoachtestHTTPClient ) addCookies (ctx context.Context , cookieUrl string ) error {
129138 // If the cluster is not running in secure mode, don't try to add cookies.
130139 if ! r .cluster .IsSecure () {
131140 return nil
132141 }
133142 // If we haven't extracted the sessionID yet, do so.
134143 if r .sessionID == "" {
135- id , err := getSessionID (ctx , r .cluster , r .l , r .cluster .All ())
144+ id , err := getSessionID (ctx , r .cluster , r .l , r .cluster .All (), r . virtualClusterName )
136145 if err != nil {
137- return errors .Wrapf (err , "roachtestutil.addCookie : unable to extract sessionID" )
146+ return errors .Wrapf (err , "roachtestutil.addCookies : unable to extract sessionID" )
138147 }
139148 r .sessionID = id
140149 }
141150
142151 name , value , found := strings .Cut (r .sessionID , "=" )
143152 if ! found {
144- return errors .New ( "Cookie not formatted correctly" )
153+ return errors .Newf ( "cookie not formatted correctly: %q" , r . sessionID )
145154 }
146155
147156 url , err := url .Parse (cookieUrl )
148157 if err != nil {
149- return errors .Wrapf (err , "roachtestutil.addCookie : unable to parse cookieUrl" )
158+ return errors .Wrapf (err , "roachtestutil.addCookies : unable to parse cookieUrl" )
150159 }
151- err = r .SetCookies (url , []* http.Cookie {{Name : name , Value : value }})
160+
161+ cookies := []* http.Cookie {{Name : name , Value : value }}
162+ if r .virtualClusterName != "" {
163+ cookies = append (cookies , & http.Cookie {Name : "tenant" , Value : r .virtualClusterName })
164+ }
165+
166+ err = r .SetCookies (url , cookies )
152167 if err != nil {
153- return errors .Wrapf (err , "roachtestutil.addCookie : unable to set cookie " )
168+ return errors .Wrapf (err , "roachtestutil.addCookies : unable to set cookies " )
154169 }
155170
156171 return nil
@@ -173,43 +188,46 @@ func (r *RoachtestHTTPClient) SetCookies(u *url.URL, cookies []*http.Cookie) err
173188}
174189
175190func getSessionIDOnSingleNode (
176- ctx context.Context , c cluster.Cluster , l * logger.Logger , node option.NodeListOption ,
191+ ctx context.Context ,
192+ c cluster.Cluster ,
193+ l * logger.Logger ,
194+ node option.NodeListOption ,
195+ virtualClusterName string ,
177196) (string , error ) {
197+ var virtualClusterSelector string
198+ if virtualClusterName != "" {
199+ virtualClusterSelector = fmt .Sprintf (":%s" , virtualClusterName )
200+ }
201+
178202 loginCmd := fmt .Sprintf (
179- "%s auth-session login root --port={pgport%s } --certs-dir ./certs --format raw " ,
180- test .DefaultCockroachPath , node ,
203+ "%s auth-session login root --url={pgurl%s%s } --certs-dir ./certs --only-cookie " ,
204+ test .DefaultCockroachPath , node , virtualClusterSelector ,
181205 )
182206 res , err := c .RunWithDetailsSingleNode (ctx , l , option .WithNodes (node ), loginCmd )
183207 if err != nil {
184208 return "" , errors .Wrap (err , "failed to authenticate" )
185209 }
186210
187- var sessionCookie string
188- for _ , line := range strings .Split (res .Stdout , "\n " ) {
189- if strings .HasPrefix (line , "session=" ) {
190- sessionCookie = line
191- }
192- }
193- if sessionCookie == "" {
194- return "" , fmt .Errorf ("failed to find session cookie in `login` output" )
195- }
196-
197- return sessionCookie , nil
211+ return res .Stdout , nil
198212}
199213
200214func getSessionID (
201- ctx context.Context , c cluster.Cluster , l * logger.Logger , nodes option.NodeListOption ,
215+ ctx context.Context ,
216+ c cluster.Cluster ,
217+ l * logger.Logger ,
218+ nodes option.NodeListOption ,
219+ virtualClusterName string ,
202220) (string , error ) {
203221 var err error
204222 var cookie string
205223 // The session ID should be the same for all nodes so stop after we successfully
206224 // get it from one node.
207225 for _ , node := range nodes {
208- cookie , err = getSessionIDOnSingleNode (ctx , c , l , c .Node (node ))
226+ cookie , err = getSessionIDOnSingleNode (ctx , c , l , c .Node (node ), virtualClusterName )
209227 if err == nil {
210228 break
211229 }
212- l .Printf ("%s auth session login failed on node %d: %v" , test .DefaultCockroachPath , node , err )
230+ l .Printf ("%s auth- session login failed on node %d: %v" , test .DefaultCockroachPath , node , err )
213231 }
214232 if err != nil {
215233 return "" , errors .Wrapf (err , "roachtestutil.GetSessionID" )
0 commit comments