@@ -2,17 +2,24 @@ package agent
22
33import (
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
1825func 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.
193294func 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+
469597const fakeKubeconfig = `
470598apiVersion: v1
471599clusters:
0 commit comments