-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from jetstack/VC-36510-compress-requests
VC-36510: Key Pair and Venafi Connection modes now use gzip compression
- Loading branch information
Showing
5 changed files
with
256 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package agent | |
|
||
import ( | ||
"bytes" | ||
"compress/gzip" | ||
"context" | ||
"fmt" | ||
"io" | ||
|
@@ -373,6 +374,19 @@ func Test_ValidateAndCombineConfig(t *testing.T) { | |
assert.IsType(t, &client.OAuthClient{}, cl) | ||
}) | ||
|
||
t.Run("jetstack-secure-oauth-auth: can't use --disable-compression", func(t *testing.T) { | ||
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"}`) | ||
_, _, err := ValidateAndCombineConfig(discardLogs(), | ||
withConfig(testutil.Undent(` | ||
server: https://api.venafi.eu | ||
period: 1h | ||
organization_id: foo | ||
cluster_id: bar | ||
`)), | ||
withCmdLineFlags("--disable-compression", "--credentials-file", path, "--install-namespace", "venafi")) | ||
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") | ||
}) | ||
|
||
t.Run("jetstack-secure-oauth-auth: --credential-file used but file is missing", func(t *testing.T) { | ||
t.Setenv("POD_NAMESPACE", "venafi") | ||
got, _, err := ValidateAndCombineConfig(discardLogs(), | ||
|
@@ -632,6 +646,83 @@ func Test_ValidateAndCombineConfig_VenafiCloudKeyPair(t *testing.T) { | |
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("the request body is compressed", func(t *testing.T) { | ||
srv, cert, setVenafiCloudAssert := testutil.FakeVenafiCloud(t) | ||
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { | ||
if gotReq.URL.Path == "/v1/oauth/token/serviceaccount" { | ||
return | ||
} | ||
assert.Equal(t, "/v1/tlspk/upload/clusterdata/no", gotReq.URL.Path) | ||
|
||
// Let's check that the body is compressed as expected. | ||
assert.Equal(t, "gzip", gotReq.Header.Get("Content-Encoding")) | ||
uncompressR, err := gzip.NewReader(gotReq.Body) | ||
require.NoError(t, err, "body might not be compressed") | ||
defer uncompressR.Close() | ||
uncompressed, err := io.ReadAll(uncompressR) | ||
require.NoError(t, err) | ||
assert.Contains(t, string(uncompressed), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) | ||
}) | ||
privKeyPath := withFile(t, fakePrivKeyPEM) | ||
got, cl, err := ValidateAndCombineConfig(discardLogs(), | ||
withConfig(testutil.Undent(` | ||
server: `+srv.URL+` | ||
period: 1h | ||
cluster_id: "test cluster name" | ||
venafi-cloud: | ||
uploader_id: no | ||
upload_path: /v1/tlspk/upload/clusterdata | ||
`)), | ||
withCmdLineFlags("--client-id", "5bc7d07c-45da-11ef-a878-523f1e1d7de1", "--private-key-path", privKeyPath, "--install-namespace", "venafi"), | ||
) | ||
require.NoError(t, err) | ||
testutil.TrustCA(t, cl, cert) | ||
assert.Equal(t, VenafiCloudKeypair, got.AuthMode) | ||
require.NoError(t, err) | ||
|
||
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("--disable-compression works", func(t *testing.T) { | ||
srv, cert, setVenafiCloudAssert := testutil.FakeVenafiCloud(t) | ||
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { | ||
// Only care about /v1/tlspk/upload/clusterdata/:uploader_id?name= | ||
if gotReq.URL.Path == "/v1/oauth/token/serviceaccount" { | ||
return | ||
} | ||
|
||
assert.Equal(t, "/v1/tlspk/upload/clusterdata/no", gotReq.URL.Path) | ||
|
||
// Let's check that the body isn't compressed. | ||
assert.Equal(t, "", gotReq.Header.Get("Content-Encoding")) | ||
b := new(bytes.Buffer) | ||
_, err := b.ReadFrom(gotReq.Body) | ||
require.NoError(t, err) | ||
assert.Contains(t, b.String(), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) | ||
}) | ||
|
||
privKeyPath := withFile(t, fakePrivKeyPEM) | ||
got, cl, err := ValidateAndCombineConfig(discardLogs(), | ||
withConfig(testutil.Undent(` | ||
server: `+srv.URL+` | ||
period: 1h | ||
cluster_id: "test cluster name" | ||
venafi-cloud: | ||
uploader_id: no | ||
upload_path: /v1/tlspk/upload/clusterdata | ||
`)), | ||
withCmdLineFlags("--disable-compression", "--client-id", "5bc7d07c-45da-11ef-a878-523f1e1d7de1", "--private-key-path", privKeyPath, "--install-namespace", "venafi"), | ||
) | ||
require.NoError(t, err) | ||
testutil.TrustCA(t, cl, cert) | ||
assert.Equal(t, VenafiCloudKeypair, got.AuthMode) | ||
require.NoError(t, err) | ||
|
||
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: "test cluster name"}) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
// 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) { | |
}) | ||
|
||
cfg, cl, err := ValidateAndCombineConfig(discardLogs(), | ||
Config{Server: "http://this-url-should-be-ignored", Period: 1 * time.Hour, ClusterID: "test cluster name"}, | ||
AgentCmdFlags{VenConnName: "venafi-components", InstallNS: "venafi"}) | ||
withConfig(testutil.Undent(` | ||
server: http://this-url-should-be-ignored | ||
period: 1h | ||
cluster_id: test cluster name | ||
`)), | ||
withCmdLineFlags("--venafi-connection", "venafi-components", "--install-namespace", "venafi")) | ||
require.NoError(t, err) | ||
|
||
testutil.VenConnStartWatching(t, cl) | ||
|
@@ -724,6 +819,53 @@ func Test_ValidateAndCombineConfig_VenafiConnection(t *testing.T) { | |
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("the request is compressed by default", func(t *testing.T) { | ||
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { | ||
// Let's check that the body is compressed as expected. | ||
assert.Equal(t, "gzip", gotReq.Header.Get("Content-Encoding")) | ||
uncompressR, err := gzip.NewReader(gotReq.Body) | ||
require.NoError(t, err, "body might not be compressed") | ||
defer uncompressR.Close() | ||
uncompressed, err := io.ReadAll(uncompressR) | ||
require.NoError(t, err) | ||
assert.Contains(t, string(uncompressed), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) | ||
}) | ||
cfg, cl, err := ValidateAndCombineConfig(discardLogs(), | ||
withConfig(testutil.Undent(` | ||
period: 1h | ||
cluster_id: test cluster name | ||
`)), | ||
withCmdLineFlags("--venafi-connection", "venafi-components", "--install-namespace", "venafi")) | ||
require.NoError(t, err) | ||
testutil.VenConnStartWatching(t, cl) | ||
testutil.TrustCA(t, cl, cert) | ||
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("--disable-compression works", func(t *testing.T) { | ||
setVenafiCloudAssert(func(t testing.TB, gotReq *http.Request) { | ||
// Let's check that the body isn't compressed. | ||
assert.Equal(t, "", gotReq.Header.Get("Content-Encoding")) | ||
b := new(bytes.Buffer) | ||
_, err := b.ReadFrom(gotReq.Body) | ||
require.NoError(t, err) | ||
assert.Contains(t, b.String(), `{"agent_metadata":{"version":"development","cluster_id":"test cluster name"}`) | ||
}) | ||
cfg, cl, err := ValidateAndCombineConfig(discardLogs(), | ||
withConfig(testutil.Undent(` | ||
server: `+srv.URL+` | ||
period: 1h | ||
cluster_id: test cluster name | ||
`)), | ||
withCmdLineFlags("--disable-compression", "--venafi-connection", "venafi-components", "--install-namespace", "venafi")) | ||
require.NoError(t, err) | ||
testutil.VenConnStartWatching(t, cl) | ||
testutil.TrustCA(t, cl, cert) | ||
err = cl.PostDataReadingsWithOptions(nil, client.Options{ClusterName: cfg.ClusterID}) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func Test_ParseConfig(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.