@@ -2,6 +2,7 @@ package agent
22
33import (
44 "bytes"
5+ "compress/gzip"
56 "context"
67 "fmt"
78 "io"
@@ -341,6 +342,19 @@ func Test_ValidateAndCombineConfig(t *testing.T) {
341342 assert .IsType (t , & client.OAuthClient {}, cl )
342343 })
343344
345+ t .Run ("jetstack-secure-oauth-auth: can't use --disable-compression" , func (t * testing.T ) {
346+ 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"}` )
347+ _ , _ , err := ValidateAndCombineConfig (discardLogs (),
348+ withConfig (testutil .Undent (`
349+ server: https://api.venafi.eu
350+ period: 1h
351+ organization_id: foo
352+ cluster_id: bar
353+ ` )),
354+ withCmdLineFlags ("--disable-compression" , "--credentials-file" , path ))
355+ 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 " )
356+ })
357+
344358 t .Run ("jetstack-secure-oauth-auth: --credential-file used but file is missing" , func (t * testing.T ) {
345359 got , _ , err := ValidateAndCombineConfig (discardLogs (),
346360 withConfig (testutil .Undent (`
@@ -604,6 +618,81 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) {
604618 err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
605619 require .NoError (t , err )
606620 })
621+
622+ t .Run ("the request body is compressed" , func (t * testing.T ) {
623+ srv , cert , setVenafiCloudAssert := testutil .FakeVenafiCloud (t )
624+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
625+ if gotReq .URL .Path == "/v1/oauth/token/serviceaccount" {
626+ return
627+ }
628+ assert .Equal (t , "/v1/tlspk/upload/clusterdata/no" , gotReq .URL .Path )
629+
630+ // Let's check that the body is compressed as expected.
631+ assert .Equal (t , "gzip" , gotReq .Header .Get ("Content-Encoding" ))
632+ uncompressR , err := gzip .NewReader (gotReq .Body )
633+ require .NoError (t , err , "body might not be compressed" )
634+ defer uncompressR .Close ()
635+ uncompressed , err := io .ReadAll (uncompressR )
636+ require .NoError (t , err )
637+ assert .Contains (t , string (uncompressed ), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
638+ })
639+ privKeyPath := withFile (t , fakePrivKeyPEM )
640+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
641+ withConfig (testutil .Undent (`
642+ server: ` + srv .URL + `
643+ period: 1h
644+ cluster_id: "test cluster name"
645+ venafi-cloud:
646+ uploader_id: no
647+ upload_path: /v1/tlspk/upload/clusterdata
648+ ` )),
649+ withCmdLineFlags ("--client-id" , "5bc7d07c-45da-11ef-a878-523f1e1d7de1" , "--private-key-path" , privKeyPath ),
650+ )
651+ testutil .TrustCA (t , cl , cert )
652+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
653+ require .NoError (t , err )
654+
655+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
656+ require .NoError (t , err )
657+ })
658+
659+ t .Run ("--disable-compression works" , func (t * testing.T ) {
660+ srv , cert , setVenafiCloudAssert := testutil .FakeVenafiCloud (t )
661+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
662+ // Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name=
663+ if gotReq .URL .Path == "/v1/oauth/token/serviceaccount" {
664+ return
665+ }
666+
667+ assert .Equal (t , "/v1/tlspk/upload/clusterdata/no" , gotReq .URL .Path )
668+
669+ // Let's check that the body isn't compressed.
670+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
671+ b := new (bytes.Buffer )
672+ _ , err := b .ReadFrom (gotReq .Body )
673+ require .NoError (t , err )
674+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
675+ })
676+
677+ privKeyPath := withFile (t , fakePrivKeyPEM )
678+ got , cl , err := ValidateAndCombineConfig (discardLogs (),
679+ withConfig (testutil .Undent (`
680+ server: ` + srv .URL + `
681+ period: 1h
682+ cluster_id: "test cluster name"
683+ venafi-cloud:
684+ uploader_id: no
685+ upload_path: /v1/tlspk/upload/clusterdata
686+ ` )),
687+ withCmdLineFlags ("--disable-compression" , "--client-id" , "5bc7d07c-45da-11ef-a878-523f1e1d7de1" , "--private-key-path" , privKeyPath ),
688+ )
689+ testutil .TrustCA (t , cl , cert )
690+ assert .Equal (t , VenafiCloudKeypair , got .AuthMode )
691+ require .NoError (t , err )
692+
693+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : "test cluster name" })
694+ require .NoError (t , err )
695+ })
607696}
608697
609698// Slower test cases due to envtest. That's why they are separated from the
@@ -683,8 +772,12 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
683772 })
684773
685774 cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
686- Config {Server : "http://this-url-should-be-ignored" , Period : 1 * time .Hour , ClusterID : "test cluster name" },
687- AgentCmdFlags {VenConnName : "venafi-components" , InstallNS : "venafi" })
775+ withConfig (testutil .Undent (`
776+ server: http://this-url-should-be-ignored
777+ period: 1h
778+ cluster_id: test cluster name
779+ ` )),
780+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
688781 require .NoError (t , err )
689782
690783 testutil .VenConnStartWatching (t , cl )
@@ -696,6 +789,53 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) {
696789 err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
697790 require .NoError (t , err )
698791 })
792+
793+ t .Run ("the request is compressed by default" , func (t * testing.T ) {
794+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
795+ // Let's check that the body is compressed as expected.
796+ assert .Equal (t , "gzip" , gotReq .Header .Get ("Content-Encoding" ))
797+ uncompressR , err := gzip .NewReader (gotReq .Body )
798+ require .NoError (t , err , "body might not be compressed" )
799+ defer uncompressR .Close ()
800+ uncompressed , err := io .ReadAll (uncompressR )
801+ require .NoError (t , err )
802+ assert .Contains (t , string (uncompressed ), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
803+ })
804+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
805+ withConfig (testutil .Undent (`
806+ period: 1h
807+ cluster_id: test cluster name
808+ ` )),
809+ withCmdLineFlags ("--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
810+ require .NoError (t , err )
811+ testutil .VenConnStartWatching (t , cl )
812+ testutil .TrustCA (t , cl , cert )
813+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
814+ require .NoError (t , err )
815+ })
816+
817+ t .Run ("--disable-compression works" , func (t * testing.T ) {
818+ setVenafiCloudAssert (func (t testing.TB , gotReq * http.Request ) {
819+ // Let's check that the body isn't compressed.
820+ assert .Equal (t , "" , gotReq .Header .Get ("Content-Encoding" ))
821+ b := new (bytes.Buffer )
822+ _ , err := b .ReadFrom (gotReq .Body )
823+ require .NoError (t , err )
824+ assert .Contains (t , b .String (), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}` )
825+ })
826+ cfg , cl , err := ValidateAndCombineConfig (discardLogs (),
827+ withConfig (testutil .Undent (`
828+ server: ` + srv .URL + `
829+ period: 1h
830+ cluster_id: test cluster name
831+ ` )),
832+ withCmdLineFlags ("--disable-compression" , "--venafi-connection" , "venafi-components" , "--install-namespace" , "venafi" ))
833+ require .NoError (t , err )
834+ testutil .VenConnStartWatching (t , cl )
835+ testutil .TrustCA (t , cl , cert )
836+ err = cl .PostDataReadingsWithOptions (nil , client.Options {ClusterName : cfg .ClusterID })
837+ require .NoError (t , err )
838+ })
699839}
700840
701841func Test_ParseConfig (t * testing.T ) {
0 commit comments