@@ -3,8 +3,10 @@ package envbuilder_test
3
3
import (
4
4
"context"
5
5
"crypto/ed25519"
6
+ "encoding/json"
6
7
"fmt"
7
8
"io"
9
+ "net/http"
8
10
"net/http/httptest"
9
11
"net/url"
10
12
"os"
@@ -13,13 +15,16 @@ import (
13
15
"testing"
14
16
15
17
"github.com/coder/coder/v2/codersdk"
18
+ "github.com/coder/coder/v2/codersdk/agentsdk"
16
19
"github.com/coder/envbuilder"
17
20
"github.com/coder/envbuilder/testutil/gittest"
18
21
"github.com/go-git/go-billy/v5"
19
22
"github.com/go-git/go-billy/v5/memfs"
20
23
"github.com/go-git/go-billy/v5/osfs"
21
24
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
22
25
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
26
+ "github.com/google/uuid"
27
+ "github.com/stretchr/testify/assert"
23
28
"github.com/stretchr/testify/require"
24
29
gossh "golang.org/x/crypto/ssh"
25
30
)
@@ -382,6 +387,60 @@ func TestSetupRepoAuth(t *testing.T) {
382
387
auth := envbuilder .SetupRepoAuth (opts )
383
388
require .Nil (t , auth ) // TODO: actually test SSH_AUTH_SOCK
384
389
})
390
+
391
+ t .Run ("SSH/Coder" , func (t * testing.T ) {
392
+ token := uuid .NewString ()
393
+ actualSigner , err := gossh .ParsePrivateKey ([]byte (testKey ))
394
+ require .NoError (t , err )
395
+ handler := func (w http.ResponseWriter , r * http.Request ) {
396
+ hdr := r .Header .Get ("Coder-Session-Token" )
397
+ if ! assert .Equal (t , hdr , token ) {
398
+ w .WriteHeader (http .StatusForbidden )
399
+ return
400
+ }
401
+ switch r .URL .Path {
402
+ case "/api/v2/workspaceagents/me/gitsshkey" :
403
+ _ = json .NewEncoder (w ).Encode (& agentsdk.GitSSHKey {
404
+ PublicKey : string (actualSigner .PublicKey ().Marshal ()),
405
+ PrivateKey : string (testKey ),
406
+ })
407
+ default :
408
+ assert .Fail (t , "unknown path: %q" , r .URL .Path )
409
+ }
410
+ }
411
+ srv := httptest .NewServer (http .HandlerFunc (handler ))
412
+ u , err := url .Parse (srv .URL )
413
+ require .NoError (t , err )
414
+ opts := & envbuilder.Options {
415
+ CoderAgentURL : u ,
416
+ CoderAgentToken : token ,
417
+ GitURL :
"ssh://[email protected] :repo/path" ,
418
+ Logger : testLog (t ),
419
+ }
420
+ auth := envbuilder .SetupRepoAuth (opts )
421
+ pk , ok := auth .(* gitssh.PublicKeys )
422
+ require .True (t , ok )
423
+ require .NotNil (t , pk .Signer )
424
+ require .Equal (t , actualSigner , pk .Signer )
425
+ })
426
+
427
+ t .Run ("SSH/CoderForbidden" , func (t * testing.T ) {
428
+ token := uuid .NewString ()
429
+ handler := func (w http.ResponseWriter , r * http.Request ) {
430
+ w .WriteHeader (http .StatusForbidden )
431
+ }
432
+ srv := httptest .NewServer (http .HandlerFunc (handler ))
433
+ u , err := url .Parse (srv .URL )
434
+ require .NoError (t , err )
435
+ opts := & envbuilder.Options {
436
+ CoderAgentURL : u ,
437
+ CoderAgentToken : token ,
438
+ GitURL :
"ssh://[email protected] :repo/path" ,
439
+ Logger : testLog (t ),
440
+ }
441
+ auth := envbuilder .SetupRepoAuth (opts )
442
+ require .Nil (t , auth )
443
+ })
385
444
}
386
445
387
446
func mustRead (t * testing.T , fs billy.Filesystem , path string ) string {
@@ -405,6 +464,7 @@ func randKeygen(t *testing.T) gossh.Signer {
405
464
406
465
func testLog (t * testing.T ) envbuilder.LoggerFunc {
407
466
return func (_ codersdk.LogLevel , format string , args ... interface {}) {
467
+ t .Helper ()
408
468
t .Logf (format , args ... )
409
469
}
410
470
}
0 commit comments