|
1 | 1 | package agent |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "bytes" |
5 | | - "compress/gzip" |
6 | 4 | "context" |
7 | 5 | "fmt" |
8 | | - "io" |
9 | 6 | "net/http" |
10 | 7 | "os" |
11 | 8 | "testing" |
@@ -165,6 +162,26 @@ func Test_ValidateAndCombineConfig(t *testing.T) { |
165 | 162 | assert.Equal(t, true, got.StrictMode) |
166 | 163 | }) |
167 | 164 |
|
| 165 | + t.Run("--disable-compression is deprecated and doesn't do anything", func(t *testing.T) { |
| 166 | + path := withFile( t, `{"user_id":"[email protected]","user_secret":"foo","client_id": "k3TrDbfLhCgnpAbOiiT2kIE1AbovKzjo","client_secret": "f39w_3KT9Vp0VhzcPzvh-uVbudzqCFmHER3Huj0dvHgJwVrjxsoOQPIw_1SDiCfa","auth_server_domain":"auth.jetstack.io"}`) |
| 167 | + log, b := recordLogs(t) |
| 168 | + _, _, err := ValidateAndCombineConfig(log, |
| 169 | + withConfig(testutil.Undent(` |
| 170 | + server: https://api.venafi.eu |
| 171 | + period: 1h |
| 172 | + organization_id: foo |
| 173 | + cluster_id: bar |
| 174 | + `)), |
| 175 | + withCmdLineFlags("--disable-compression", "--credentials-file", path, "--install-namespace", "venafi")) |
| 176 | + require.NoError(t, err) |
| 177 | + |
| 178 | + // The log line printed by pflag is not captured by the log recorder. |
| 179 | + assert.Equal(t, testutil.Undent(` |
| 180 | + INFO Using the Jetstack Secure OAuth auth mode since --credentials-file was specified without --venafi-cloud. |
| 181 | + INFO Using period from config period="1h0m0s" |
| 182 | + `), b.String()) |
| 183 | + }) |
| 184 | + |
168 | 185 | t.Run("error when no auth method specified", func(t *testing.T) { |
169 | 186 | _, cl, err := ValidateAndCombineConfig(discardLogs(), |
170 | 187 | withConfig(testutil.Undent(` |
@@ -375,19 +392,6 @@ func Test_ValidateAndCombineConfig(t *testing.T) { |
375 | 392 | assert.IsType(t, &client.OAuthClient{}, cl) |
376 | 393 | }) |
377 | 394 |
|
378 | | - t.Run("jetstack-secure-oauth-auth: can't use --disable-compression", func(t *testing.T) { |
379 | | - path := withFile( t, `{"user_id":"[email protected]","user_secret":"foo","client_id": "k3TrDbfLhCgnpAbOiiT2kIE1AbovKzjo","client_secret": "f39w_3KT9Vp0VhzcPzvh-uVbudzqCFmHER3Huj0dvHgJwVrjxsoOQPIw_1SDiCfa","auth_server_domain":"auth.jetstack.io"}`) |
380 | | - _, _, err := ValidateAndCombineConfig(discardLogs(), |
381 | | - withConfig(testutil.Undent(` |
382 | | - server: https://api.venafi.eu |
383 | | - period: 1h |
384 | | - organization_id: foo |
385 | | - cluster_id: bar |
386 | | - `)), |
387 | | - withCmdLineFlags("--disable-compression", "--credentials-file", path, "--install-namespace", "venafi")) |
388 | | - 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") |
389 | | - }) |
390 | | - |
391 | 395 | t.Run("jetstack-secure-oauth-auth: --credential-file used but file is missing", func(t *testing.T) { |
392 | 396 | t.Setenv("POD_NAMESPACE", "venafi") |
393 | 397 | got, _, err := ValidateAndCombineConfig(discardLogs(), |
@@ -647,83 +651,6 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) { |
647 | 651 | err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) |
648 | 652 | require.NoError(t, err) |
649 | 653 | }) |
650 | | - |
651 | | - t.Run("the request body is compressed", func(t *testing.T) { |
652 | | - srv, cert, setVenafiCloudAssert := testutil.FakeVenafiCloud(t) |
653 | | - setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { |
654 | | - if gotReq.URL.Path == "/v1/oauth/token/serviceaccount" { |
655 | | - return |
656 | | - } |
657 | | - assert.Equal(t, "/v1/tlspk/upload/clusterdata/no", gotReq.URL.Path) |
658 | | - |
659 | | - // Let's check that the body is compressed as expected. |
660 | | - assert.Equal(t, "gzip", gotReq.Header.Get("Content-Encoding")) |
661 | | - uncompressR, err := gzip.NewReader(gotReq.Body) |
662 | | - require.NoError(t, err, "body might not be compressed") |
663 | | - defer uncompressR.Close() |
664 | | - uncompressed, err := io.ReadAll(uncompressR) |
665 | | - require.NoError(t, err) |
666 | | - assert.Contains(t, string(uncompressed), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) |
667 | | - }) |
668 | | - privKeyPath := withFile(t, fakePrivKeyPEM) |
669 | | - got, cl, err := ValidateAndCombineConfig(discardLogs(), |
670 | | - withConfig(testutil.Undent(` |
671 | | - server: `+srv.URL+` |
672 | | - period: 1h |
673 | | - cluster_id: "test cluster name" |
674 | | - venafi-cloud: |
675 | | - uploader_id: no |
676 | | - upload_path: /v1/tlspk/upload/clusterdata |
677 | | - `)), |
678 | | - withCmdLineFlags("--client-id", "5bc7d07c-45da-11ef-a878-523f1e1d7de1", "--private-key-path", privKeyPath, "--install-namespace", "venafi"), |
679 | | - ) |
680 | | - require.NoError(t, err) |
681 | | - testutil.TrustCA(t, cl, cert) |
682 | | - assert.Equal(t, VenafiCloudKeypair, got.AuthMode) |
683 | | - require.NoError(t, err) |
684 | | - |
685 | | - err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) |
686 | | - require.NoError(t, err) |
687 | | - }) |
688 | | - |
689 | | - t.Run("--disable-compression works", func(t *testing.T) { |
690 | | - srv, cert, setVenafiCloudAssert := testutil.FakeVenafiCloud(t) |
691 | | - setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { |
692 | | - // Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name= |
693 | | - if gotReq.URL.Path == "/v1/oauth/token/serviceaccount" { |
694 | | - return |
695 | | - } |
696 | | - |
697 | | - assert.Equal(t, "/v1/tlspk/upload/clusterdata/no", gotReq.URL.Path) |
698 | | - |
699 | | - // Let's check that the body isn't compressed. |
700 | | - assert.Equal(t, "", gotReq.Header.Get("Content-Encoding")) |
701 | | - b := new(bytes.Buffer) |
702 | | - _, err := b.ReadFrom(gotReq.Body) |
703 | | - require.NoError(t, err) |
704 | | - assert.Contains(t, b.String(), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) |
705 | | - }) |
706 | | - |
707 | | - privKeyPath := withFile(t, fakePrivKeyPEM) |
708 | | - got, cl, err := ValidateAndCombineConfig(discardLogs(), |
709 | | - withConfig(testutil.Undent(` |
710 | | - server: `+srv.URL+` |
711 | | - period: 1h |
712 | | - cluster_id: "test cluster name" |
713 | | - venafi-cloud: |
714 | | - uploader_id: no |
715 | | - upload_path: /v1/tlspk/upload/clusterdata |
716 | | - `)), |
717 | | - withCmdLineFlags("--disable-compression", "--client-id", "5bc7d07c-45da-11ef-a878-523f1e1d7de1", "--private-key-path", privKeyPath, "--install-namespace", "venafi"), |
718 | | - ) |
719 | | - require.NoError(t, err) |
720 | | - testutil.TrustCA(t, cl, cert) |
721 | | - assert.Equal(t, VenafiCloudKeypair, got.AuthMode) |
722 | | - require.NoError(t, err) |
723 | | - |
724 | | - err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) |
725 | | - require.NoError(t, err) |
726 | | - }) |
727 | 654 | } |
728 | 655 |
|
729 | 656 | // Slower test cases due to envtest. That's why they are separated from the |
@@ -820,53 +747,6 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) { |
820 | 747 | err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) |
821 | 748 | require.NoError(t, err) |
822 | 749 | }) |
823 | | - |
824 | | - t.Run("the request is compressed by default", func(t *testing.T) { |
825 | | - setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { |
826 | | - // Let's check that the body is compressed as expected. |
827 | | - assert.Equal(t, "gzip", gotReq.Header.Get("Content-Encoding")) |
828 | | - uncompressR, err := gzip.NewReader(gotReq.Body) |
829 | | - require.NoError(t, err, "body might not be compressed") |
830 | | - defer uncompressR.Close() |
831 | | - uncompressed, err := io.ReadAll(uncompressR) |
832 | | - require.NoError(t, err) |
833 | | - assert.Contains(t, string(uncompressed), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) |
834 | | - }) |
835 | | - cfg, cl, err := ValidateAndCombineConfig(discardLogs(), |
836 | | - withConfig(testutil.Undent(` |
837 | | - period: 1h |
838 | | - cluster_id: test cluster name |
839 | | - `)), |
840 | | - withCmdLineFlags("--venafi-connection", "venafi-components", "--install-namespace", "venafi")) |
841 | | - require.NoError(t, err) |
842 | | - testutil.VenConnStartWatching(t, cl) |
843 | | - testutil.TrustCA(t, cl, cert) |
844 | | - err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) |
845 | | - require.NoError(t, err) |
846 | | - }) |
847 | | - |
848 | | - t.Run("--disable-compression works", func(t *testing.T) { |
849 | | - setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { |
850 | | - // Let's check that the body isn't compressed. |
851 | | - assert.Equal(t, "", gotReq.Header.Get("Content-Encoding")) |
852 | | - b := new(bytes.Buffer) |
853 | | - _, err := b.ReadFrom(gotReq.Body) |
854 | | - require.NoError(t, err) |
855 | | - assert.Contains(t, b.String(), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) |
856 | | - }) |
857 | | - cfg, cl, err := ValidateAndCombineConfig(discardLogs(), |
858 | | - withConfig(testutil.Undent(` |
859 | | - server: `+srv.URL+` |
860 | | - period: 1h |
861 | | - cluster_id: test cluster name |
862 | | - `)), |
863 | | - withCmdLineFlags("--disable-compression", "--venafi-connection", "venafi-components", "--install-namespace", "venafi")) |
864 | | - require.NoError(t, err) |
865 | | - testutil.VenConnStartWatching(t, cl) |
866 | | - testutil.TrustCA(t, cl, cert) |
867 | | - err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) |
868 | | - require.NoError(t, err) |
869 | | - }) |
870 | 750 | } |
871 | 751 |
|
872 | 752 | func Test_ParseConfig(t *testing.T) { |
|
0 commit comments