Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 8483d21

Browse files
committed
Add unit test for TrustEnv
1 parent 1373030 commit 8483d21

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

coder-sdk/workspace.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package coder
22

33
import (
44
"context"
5+
"crypto/x509"
56
"encoding/pem"
67
"fmt"
78
"io"
@@ -517,8 +518,9 @@ func (c *DefaultClient) SetPolicyTemplate(ctx context.Context, templateID string
517518

518519
// TrustChallengeResponse returns the tls certs used in the response.
519520
type TrustChallengeResponse struct {
520-
// Certificates are the returned response certificates
521-
Certificates [][]byte `json:"-"`
521+
// PemCertificates are the returned response certificates
522+
PemCertificates [][]byte `json:"-"`
523+
Certificates []*x509.Certificate `json:"-"`
522524
}
523525

524526
// TrustEnvironment is used to make a secure handshake with a coderd. This is intended to run from within a workspace.
@@ -534,12 +536,14 @@ func (c *DefaultClient) TrustEnvironment(ctx context.Context, id string) (*Trust
534536
}
535537

536538
if len(resp.TLS.PeerCertificates) > 0 {
537-
for _, c := range resp.TLS.PeerCertificates {
539+
for i := range resp.TLS.PeerCertificates {
540+
c := resp.TLS.PeerCertificates[i]
538541
data := pem.EncodeToMemory(&pem.Block{
539542
Type: "CERTIFICATE",
540543
Bytes: c.Raw,
541544
})
542-
challenge.Certificates = append(challenge.Certificates, data)
545+
challenge.PemCertificates = append(challenge.PemCertificates, data)
546+
challenge.Certificates = append(challenge.Certificates, c)
543547
}
544548
}
545549

coder-sdk/workspace_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"crypto/x509"
7+
"net/http"
8+
"net/http/httptest"
9+
"net/url"
10+
"testing"
11+
"time"
12+
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func Test_TrustEnvironment(t *testing.T) {
17+
ctx := context.Background()
18+
19+
const version = "test"
20+
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
21+
w.Header().Set("coder-version", version)
22+
_, _ = w.Write([]byte("{}"))
23+
w.WriteHeader(http.StatusNoContent)
24+
}))
25+
26+
u, err := url.Parse(srv.URL)
27+
require.NoError(t, err)
28+
29+
c, err := NewClient(ClientOptions{
30+
BaseURL: u,
31+
Token: "random",
32+
})
33+
require.NoError(t, err)
34+
35+
_, err = c.APIVersion(ctx)
36+
require.Error(t, err)
37+
require.Regexp(t, "x509: certificate signed by unknown authority", err.Error())
38+
39+
// TODO: @emyrk check proper handshake
40+
c.httpClient = insecureHTTPClient()
41+
challenge, err := c.TrustEnvironment(ctx, "random")
42+
require.NoError(t, err)
43+
require.Len(t, challenge.PemCertificates, 1)
44+
45+
// Add the cert to the trusted pool, try the api call again
46+
pool := x509.NewCertPool()
47+
for i := range challenge.Certificates {
48+
pool.AddCert(challenge.Certificates[i])
49+
}
50+
conf := &tls.Config{RootCAs: pool}
51+
c.httpClient = &http.Client{
52+
Timeout: time.Second * 3,
53+
Transport: &http.Transport{
54+
TLSClientConfig: conf,
55+
},
56+
}
57+
58+
v, err := c.APIVersion(ctx)
59+
require.NoError(t, err)
60+
require.Equal(t, version, v)
61+
}
62+
63+
func insecureHTTPClient() *http.Client {
64+
conf := &tls.Config{InsecureSkipVerify: true}
65+
return &http.Client{
66+
Timeout: time.Second * 3,
67+
Transport: &http.Transport{
68+
TLSClientConfig: conf,
69+
},
70+
}
71+
}

internal/cmd/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,5 +214,5 @@ func trustCertificate(ctx context.Context, sessionToken string) ([][]byte, error
214214
return nil, xerrors.Errorf("challenge failed: %w", err)
215215
}
216216

217-
return challenge.Certificates, nil
217+
return challenge.PemCertificates, nil
218218
}

0 commit comments

Comments
 (0)