@@ -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 , "--install-namespace" , "venafi" ))
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,83 @@ 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 , "--install-namespace" , "venafi" ),
678+ )
679+ require .NoError (t , err )
680+ testutil .TrustCA (t , cl , cert )
681+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
682+ require .NoError (t , err )
683+
684+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
685+ require .NoError (t , err )
686+ })
687+
688+ t .Run ("--disable-compression works" , func (t * testing.T ) {
689+ srv , cert , setVenafiCloudAssert := testutil .FakeVenafiCloud (t )
690+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
691+ // Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name=
692+ if gotReq .URL .Path == "/v1/oauth/token/serviceaccount" {
693+ return
694+ }
695+
696+ assert .Equal (t , "/v1/tlspk/upload/clusterdata/no" , gotReq .URL .Path )
697+
698+ // Let's check that the body isn't compressed.
699+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
700+ b := new (bytes.Buffer )
701+ _ , err := b .ReadFrom (gotReq .Body )
702+ require .NoError (t , err )
703+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
704+ })
705+
706+ privKeyPath := withFile (t , fakePrivKeyPEM )
707+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
708+ withConfig (testutil .Undent (`
709+ server: ` + srv .URL + `
710+ period: 1h
711+ cluster_id: "test cluster name"
712+ venafi-cloud:
713+ uploader_id: no
714+ upload_path: /v1/tlspk/upload/clusterdata
715+ ` )),
716+ withCmdLineFlags ("--disable-compression" , "--client-id" , "5bc7d07c-45da-11ef-a878-523f1e1d7de1" , "--private-key-path" , privKeyPath , "--install-namespace" , "venafi" ),
717+ )
718+ require .NoError (t , err )
719+ testutil .TrustCA (t , cl , cert )
720+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
721+ require .NoError (t , err )
722+
723+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
724+ require .NoError (t , err )
725+ })
635726}
636727
637728// Slower test cases due to envtest. That's why they are separated from the
@@ -711,8 +802,12 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
711802 })
712803
713804 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" })
805+ withConfig (testutil .Undent (`
806+ server: http://this-url-should-be-ignored
807+ period: 1h
808+ cluster_id: test cluster name
809+ ` )),
810+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
716811 require .NoError (t , err )
717812
718813 testutil .VenConnStartWatching (t , cl )
@@ -724,6 +819,53 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
724819 err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
725820 require .NoError (t , err )
726821 })
822+
823+ t .Run ("the request is compressed by default" , func (t * testing.T ) {
824+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
825+ // Let's check that the body is compressed as expected.
826+ assert .Equal (t , "gzip" , gotReq .Header .Get ("Content-Encoding" ))
827+ uncompressR , err := gzip .NewReader (gotReq .Body )
828+ require .NoError (t , err , "body might not be compressed" )
829+ defer uncompressR .Close ()
830+ uncompressed , err := io .ReadAll (uncompressR )
831+ require .NoError (t , err )
832+ assert .Contains (t , string (uncompressed ), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
833+ })
834+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
835+ withConfig (testutil .Undent (`
836+ period: 1h
837+ cluster_id: test cluster name
838+ ` )),
839+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
840+ require .NoError (t , err )
841+ testutil .VenConnStartWatching (t , cl )
842+ testutil .TrustCA (t , cl , cert )
843+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
844+ require .NoError (t , err )
845+ })
846+
847+ t .Run ("--disable-compression works" , func (t * testing.T ) {
848+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
849+ // Let's check that the body isn't compressed.
850+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
851+ b := new (bytes.Buffer )
852+ _ , err := b .ReadFrom (gotReq .Body )
853+ require .NoError (t , err )
854+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
855+ })
856+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
857+ withConfig (testutil .Undent (`
858+ server: ` + srv .URL + `
859+ period: 1h
860+ cluster_id: test cluster name
861+ ` )),
862+ withCmdLineFlags ("--disable-compression" , "--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
863+ require .NoError (t , err )
864+ testutil .VenConnStartWatching (t , cl )
865+ testutil .TrustCA (t , cl , cert )
866+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
867+ require .NoError (t , err )
868+ })
727869}
728870
729871func Test_ParseConfig (t * testing.T ) {
0 commit comments