Skip to content

Commit 8a34fcf

Browse files
committed
venconn: test that venconn's url is used, not server field
1 parent fc82e00 commit 8a34fcf

File tree

3 files changed

+142
-8
lines changed

3 files changed

+142
-8
lines changed

pkg/agent/config_test.go

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,24 @@ package agent
22

33
import (
44
"bytes"
5+
"context"
6+
"crypto/x509"
57
"fmt"
68
"io"
79
"log"
10+
"net/http"
811
"os"
912
"strings"
1013
"testing"
1114
"time"
1215

1316
"github.com/jetstack/preflight/pkg/client"
17+
"github.com/jetstack/preflight/pkg/testutil"
1418
"github.com/kylelemons/godebug/diff"
19+
"github.com/spf13/cobra"
1520
"github.com/stretchr/testify/assert"
21+
"github.com/stretchr/testify/require"
22+
"gopkg.in/yaml.v3"
1623
)
1724

1825
func TestGetConfiguration(t *testing.T) {
@@ -188,6 +195,100 @@ func TestGetConfiguration(t *testing.T) {
188195
})
189196
}
190197

198+
// Slower test cases due to envtest. That's why they are separated from the
199+
// other tests.
200+
func Test_getConfiguration_urlWhenVenafiConnection(t *testing.T) {
201+
t.Run("the server field is ignored when VenafiConnection is used", func(t *testing.T) {
202+
_, restCfg, kcl := testutil.WithEnvtest(t)
203+
os.Setenv("KUBECONFIG", testutil.WithKubeconfig(t, restCfg))
204+
srv, fakeCrt, setVenafiCloudAssert := testutil.FakeVenafiCloud(t)
205+
for _, obj := range testutil.Parse(
206+
testutil.VenConnRBAC + testutil.Undent(fmt.Sprintf(`
207+
---
208+
apiVersion: jetstack.io/v1alpha1
209+
kind: VenafiConnection
210+
metadata:
211+
name: venafi-components
212+
namespace: venafi
213+
spec:
214+
vcp:
215+
url: "%s"
216+
accessToken:
217+
- secret:
218+
name: accesstoken
219+
fields: [accesstoken]
220+
---
221+
apiVersion: v1
222+
kind: Secret
223+
metadata:
224+
name: accesstoken
225+
namespace: venafi
226+
stringData:
227+
accesstoken: VALID_ACCESS_TOKEN
228+
---
229+
apiVersion: rbac.authorization.k8s.io/v1
230+
kind: Role
231+
metadata:
232+
name: venafi-connection-accesstoken-reader
233+
namespace: venafi
234+
rules:
235+
- apiGroups: [""]
236+
resources: ["secrets"]
237+
verbs: ["get"]
238+
resourceNames: ["accesstoken"]
239+
---
240+
apiVersion: rbac.authorization.k8s.io/v1
241+
kind: RoleBinding
242+
metadata:
243+
name: venafi-connection-accesstoken-reader
244+
namespace: venafi
245+
roleRef:
246+
apiGroup: rbac.authorization.k8s.io
247+
kind: Role
248+
name: venafi-connection-accesstoken-reader
249+
subjects:
250+
- kind: ServiceAccount
251+
name: venafi-connection
252+
namespace: venafi`, srv.URL))) {
253+
require.NoError(t, kcl.Create(context.Background(), obj))
254+
}
255+
256+
// The URL received by the fake Venafi Cloud server should be the one
257+
// coming from the VenafiConnection, not the one from the config.
258+
setVenafiCloudAssert(func(t testing.TB, r *http.Request) {
259+
assert.Equal(t, srv.URL, "https://"+r.Host)
260+
})
261+
262+
cfg, err := ParseConfig([]byte(testutil.Undent(`
263+
server: "http://should-be-ignored"
264+
period: 1h
265+
`)), true)
266+
assert.NoError(t, err)
267+
268+
_, cl, err := getConfiguration(discardLogs(t),
269+
cfg,
270+
withCmdLineFlags("--venafi-connection", "venafi-components", "--install-namespace", "venafi"),
271+
)
272+
assert.NoError(t, err)
273+
274+
// `Start(ctx)` needs to be stopped before the apiserver is stopped.
275+
// https://github.com/jetstack/venafi-connection-lib/pull/158#issuecomment-1949002322
276+
ctx, cancel := context.WithCancel(context.Background())
277+
t.Cleanup(cancel)
278+
go func() {
279+
require.NoError(t, cl.(*client.VenConnClient).Start(ctx))
280+
}()
281+
certPool := x509.NewCertPool()
282+
certPool.AddCert(fakeCrt)
283+
tr := http.DefaultTransport.(*http.Transport).Clone()
284+
tr.TLSClientConfig.RootCAs = certPool
285+
cl.(*client.VenConnClient).Client.Transport = tr
286+
287+
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"})
288+
assert.NoError(t, err)
289+
})
290+
}
291+
191292
// Fills in the `server` and `period` as they appear in each and every test
192293
// case.
193294
func fillRequired(c Config) Config {
@@ -457,15 +558,42 @@ func withFile(t testing.TB, content string) string {
457558
return f.Name()
458559
}
459560

460-
func withLogs(t testing.TB) (*log.Logger, *bytes.Buffer) {
561+
func withLogs(_ testing.TB) (*log.Logger, *bytes.Buffer) {
461562
b := bytes.Buffer{}
462563
return log.New(&b, "", 0), &b
463564
}
464565

465-
func discardLogs(t testing.TB) *log.Logger {
566+
func discardLogs(_ testing.TB) *log.Logger {
466567
return log.New(io.Discard, "", 0)
467568
}
468569

570+
// Shortcut for ParseConfig.
571+
func withConfig(s string) Config {
572+
var cfg Config
573+
574+
err := yaml.Unmarshal([]byte(s), &cfg)
575+
if err != nil {
576+
panic(err)
577+
}
578+
return cfg
579+
}
580+
581+
func withCmdLineFlags(flags ...string) AgentCmdFlags {
582+
parsed := withoutCmdLineFlags()
583+
agentCmd := &cobra.Command{}
584+
InitAgentCmdFlags(agentCmd, &parsed)
585+
err := agentCmd.ParseFlags(flags)
586+
if err != nil {
587+
panic(err)
588+
}
589+
590+
return parsed
591+
}
592+
593+
func withoutCmdLineFlags() AgentCmdFlags {
594+
return AgentCmdFlags{}
595+
}
596+
469597
const fakeKubeconfig = `
470598
apiVersion: v1
471599
clusters:

pkg/client/client_venconn.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@ import (
2929
type VenConnClient struct {
3030
agentMetadata *api.AgentMetadata
3131
connHandler venafi_client.ConnectionHandler
32-
installNS string // Namespace in which the agent is running in.
33-
venConnName string // Name of the VenafiConnection resource to use.
34-
venConnNS string // Namespace of the VenafiConnection resource to use.
35-
client *http.Client // Used to make HTTP requests to Venafi Cloud.
32+
installNS string // Namespace in which the agent is running in.
33+
venConnName string // Name of the VenafiConnection resource to use.
34+
venConnNS string // Namespace of the VenafiConnection resource to use.
35+
36+
// Used to make HTTP requests to Venafi Cloud. This field is public for
37+
// testing purposes so that we can configure trusted CAs; there should be a
38+
// way to do that without messing with the client directly (e.g., a flag to
39+
// pass a custom CA?), but it's not there yet.
40+
Client *http.Client
3641
}
3742

3843
// NewVenConnClient lets you make requests to the Venafi Cloud backend using the
@@ -111,7 +116,7 @@ func NewVenConnClient(restcfg *rest.Config, agentMetadata *api.AgentMetadata, in
111116
installNS: installNS,
112117
venConnName: venConnName,
113118
venConnNS: venConnNS,
114-
client: vcpClient,
119+
Client: vcpClient,
115120
}, nil
116121
}
117122

@@ -180,7 +185,7 @@ func (c *VenConnClient) PostDataReadingsWithOptions(readings []*api.DataReading,
180185
}
181186
req.URL.RawQuery = q.Encode()
182187

183-
res, err := c.client.Do(req)
188+
res, err := c.Client.Do(req)
184189
if err != nil {
185190
return err
186191
}

pkg/testutil/testutil.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ func FakeVenafiCloud(t *testing.T) (_ *httptest.Server, _ *x509.Certificate, set
215215
if r.URL.Path == "/v1/tlspk/upload/clusterdata/no" {
216216
if r.URL.Query().Get("name") != "test cluster name" {
217217
w.WriteHeader(http.StatusBadRequest)
218+
_, _ = w.Write([]byte(`{"error":"unexpected name query param in the test server: ` + r.URL.Query().Get("name") + `"}`))
218219
return
219220
}
220221
_, _ = w.Write([]byte(`{"status":"ok","organization":"756db001-280e-11ee-84fb-991f3177e2d0"}`))

0 commit comments

Comments
 (0)