@@ -2,6 +2,7 @@ package agent
22
33import (
44 "bytes"
5+ "compress/gzip"
56 "context"
67 "fmt"
78 "io"
@@ -373,6 +374,19 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
373374 assert .IsType (t , & client.OAuthClient {}, cl )
374375 })
375376
377+ t .Run ("jetstack-secure-oauth-auth: can't use --disable-compression" , func (t * testing.T ) {
378+ path := withFile (t , `{"user_id":"fpp2624799349@affectionate-hertz6.platform.jetstack.io","user_secret":"foo","client_id": "k3TrDbfLhCgnpAbOiiT2kIE1AbovKzjo","client_secret": "f39w_3KT9Vp0VhzcPzvh-uVbudzqCFmHER3Huj0dvHgJwVrjxsoOQPIw_1SDiCfa","auth_server_domain":"auth.jetstack.io"}` )
379+ _ , _ , err := ValidateAndCombineConfig (discardLogs (),
380+ withConfig (testutil .Undent (`
381+ server: https://api.venafi.eu
382+ period: 1h
383+ organization_id: foo
384+ cluster_id: bar
385+ ` )),
386+ withCmdLineFlags ("--disable-compression" , "--credentials-file" , path ))
387+ require .EqualError (t , err , "1 error occurred:\n \t * --disable-compression can only be used with the Venafi Cloud Key Pair Service Account and Venafi Cloud VenafiConnection modes\n \n " )
388+ })
389+
376390 t .Run ("jetstack-secure-oauth-auth: --credential-file used but file is missing" , func (t * testing.T ) {
377391 t .Setenv ("POD_NAMESPACE" , "venafi" )
378392 got , _ , err := ValidateAndCombineConfig (discardLogs (),
@@ -632,6 +646,81 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
632646 err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
633647 require .NoError (t , err )
634648 })
649+
650+ t .Run ("the request body is compressed" , func (t * testing.T ) {
651+ srv , cert , setVenafiCloudAssert := testutil .FakeVenafiCloud (t )
652+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
653+ if gotReq .URL .Path == "/v1/oauth/token/serviceaccount" {
654+ return
655+ }
656+ assert .Equal (t , "/v1/tlspk/upload/clusterdata/no" , gotReq .URL .Path )
657+
658+ // Let's check that the body is compressed as expected.
659+ assert .Equal (t , "gzip" , gotReq .Header .Get ("Content-Encoding" ))
660+ uncompressR , err := gzip .NewReader (gotReq .Body )
661+ require .NoError (t , err , "body might not be compressed" )
662+ defer uncompressR .Close ()
663+ uncompressed , err := io .ReadAll (uncompressR )
664+ require .NoError (t , err )
665+ assert .Contains (t , string (uncompressed ), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
666+ })
667+ privKeyPath := withFile (t , fakePrivKeyPEM )
668+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
669+ withConfig (testutil .Undent (`
670+ server: ` + srv .URL + `
671+ period: 1h
672+ cluster_id: "test cluster name"
673+ venafi-cloud:
674+ uploader_id: no
675+ upload_path: /v1/tlspk/upload/clusterdata
676+ ` )),
677+ withCmdLineFlags ("--client-id" , "5bc7d07c-45da-11ef-a878-523f1e1d7de1" , "--private-key-path" , privKeyPath ),
678+ )
679+ testutil .TrustCA (t , cl , cert )
680+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
681+ require .NoError (t , err )
682+
683+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
684+ require .NoError (t , err )
685+ })
686+
687+ t .Run ("--disable-compression works" , func (t * testing.T ) {
688+ srv , cert , setVenafiCloudAssert := testutil .FakeVenafiCloud (t )
689+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
690+ // Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name=
691+ if gotReq .URL .Path == "/v1/oauth/token/serviceaccount" {
692+ return
693+ }
694+
695+ assert .Equal (t , "/v1/tlspk/upload/clusterdata/no" , gotReq .URL .Path )
696+
697+ // Let's check that the body isn't compressed.
698+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
699+ b := new (bytes.Buffer )
700+ _ , err := b .ReadFrom (gotReq .Body )
701+ require .NoError (t , err )
702+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
703+ })
704+
705+ privKeyPath := withFile (t , fakePrivKeyPEM )
706+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
707+ withConfig (testutil .Undent (`
708+ server: ` + srv .URL + `
709+ period: 1h
710+ cluster_id: "test cluster name"
711+ venafi-cloud:
712+ uploader_id: no
713+ upload_path: /v1/tlspk/upload/clusterdata
714+ ` )),
715+ withCmdLineFlags ("--disable-compression" , "--client-id" , "5bc7d07c-45da-11ef-a878-523f1e1d7de1" , "--private-key-path" , privKeyPath ),
716+ )
717+ testutil .TrustCA (t , cl , cert )
718+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
719+ require .NoError (t , err )
720+
721+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
722+ require .NoError (t , err )
723+ })
635724}
636725
637726// Slower test cases due to envtest. That's why they are separated from the
@@ -711,8 +800,12 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
711800 })
712801
713802 cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
714- Config {Server : "http://this-url-should-be-ignored" , Period : 1 * time .Hour , ClusterID : "test cluster name" },
715- AgentCmdFlags {VenConnName : "venafi-components" , InstallNS : "venafi" })
803+ withConfig (testutil .Undent (`
804+ server: http://this-url-should-be-ignored
805+ period: 1h
806+ cluster_id: test cluster name
807+ ` )),
808+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
716809 require .NoError (t , err )
717810
718811 testutil .VenConnStartWatching (t , cl )
@@ -724,6 +817,53 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
724817 err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
725818 require .NoError (t , err )
726819 })
820+
821+ t .Run ("the request is compressed by default" , func (t * testing.T ) {
822+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
823+ // Let's check that the body is compressed as expected.
824+ assert .Equal (t , "gzip" , gotReq .Header .Get ("Content-Encoding" ))
825+ uncompressR , err := gzip .NewReader (gotReq .Body )
826+ require .NoError (t , err , "body might not be compressed" )
827+ defer uncompressR .Close ()
828+ uncompressed , err := io .ReadAll (uncompressR )
829+ require .NoError (t , err )
830+ assert .Contains (t , string (uncompressed ), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
831+ })
832+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
833+ withConfig (testutil .Undent (`
834+ period: 1h
835+ cluster_id: test cluster name
836+ ` )),
837+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
838+ require .NoError (t , err )
839+ testutil .VenConnStartWatching (t , cl )
840+ testutil .TrustCA (t , cl , cert )
841+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
842+ require .NoError (t , err )
843+ })
844+
845+ t .Run ("--disable-compression works" , func (t * testing.T ) {
846+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
847+ // Let's check that the body isn't compressed.
848+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
849+ b := new (bytes.Buffer )
850+ _ , err := b .ReadFrom (gotReq .Body )
851+ require .NoError (t , err )
852+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
853+ })
854+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
855+ withConfig (testutil .Undent (`
856+ server: ` + srv .URL + `
857+ period: 1h
858+ cluster_id: test cluster name
859+ ` )),
860+ withCmdLineFlags ("--disable-compression" , "--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
861+ require .NoError (t , err )
862+ testutil .VenConnStartWatching (t , cl )
863+ testutil .TrustCA (t , cl , cert )
864+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
865+ require .NoError (t , err )
866+ })
727867}
728868
729869func Test_ParseConfig (t * testing.T ) {
0 commit comments