Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests using a proxy with mTLS for control plane with Elastic Defend installed #5889

Merged
merged 14 commits into from
Nov 5, 2024

Conversation

AndersonQ
Copy link
Member

@AndersonQ AndersonQ commented Oct 30, 2024

What does this PR do?

Add integration tests for the Elastic Agent running Elastic Defend with a mTLS proxy
See #5716 for tje covered test cases

Why is it important?

To have automated tests covering mTLS and Elastic Defend

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • [ ] I have made corresponding changes to the documentation
  • [ ] I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • [ ] I have added an entry in ./changelog/fragments using the [changelog tool](https://github.com/elastic/elastic-agent#changelog)
  • I have added an integration test or an E2E test

Disruptive User Impact

  • N/A

How to test this PR locally

In short, manually reproduce the tests described on #5716.

The long version:

  • run 3 proxies:

    • mTLScli -> a proxy with mTLS to be passed as cli arguments
    • mTLSpolicy -> a proxy with mTLS configured through the policy
    • oneWayTLSpolicy -> a proxy with simple/one way TLS which defines its CA in the policy
    • all the certificates must be RSA because of an Elastic Defend restriction
  • set up the proxies:

    • add a test to testing/integration/endpoint_security_test.go to configure and run the needed proxies:
func TestDebugTestProxies(t *testing.T) {
	prepareProxies(t, &url.URL{Host: "a.wrong.fleet.host"}, "the.real.fleet.host")

	t.Logf("CTRL + C to exit")
	<-make(chan struct{})
}
  • run the test with:
go test -v -timeout 0 -tags integration -run TestDebugTestProxies ./testing/integration/

=== RUN   TestDebugTestProxies
    endpoint_security_test.go:1333: [mtlsCLI] certificates saved on: /tmp/TestDebugTestProxies-mtlsCLI2475114678
    endpoint_security_test.go:1333: [mtlsPolicy] certificates saved on: /tmp/TestDebugTestProxies-mtlsPolicy1317235685
    endpoint_security_test.go:1333: [oneWayTLSPolicy] certificates saved on: /tmp/TestDebugTestProxies-oneWayTLSPolicy327431283

Related issues

Questions to ask yourself

  • How are we going to support this in production?
  • How are we going to measure its adoption?
  • How are we going to debug this?
  • What are the metrics I should take care of?
  • ...

@AndersonQ AndersonQ added skip-changelog backport-8.x Automated backport to the 8.x branch with mergify backport-8.16 Automated backport with mergify labels Oct 30, 2024
@AndersonQ AndersonQ self-assigned this Oct 30, 2024
@AndersonQ AndersonQ force-pushed the 5716-5491-mtls-integration-test branch 2 times, most recently from 2ffe8aa to a16d046 Compare October 30, 2024 15:10
Copy link
Contributor

mergify bot commented Oct 30, 2024

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b 5716-5491-mtls-integration-test upstream/5716-5491-mtls-integration-test
git merge upstream/main
git push upstream 5716-5491-mtls-integration-test

@AndersonQ AndersonQ force-pushed the 5716-5491-mtls-integration-test branch 2 times, most recently from 33d59b8 to 403e023 Compare October 31, 2024 10:21
@AndersonQ AndersonQ changed the title [WIP] Add integration tests using a proxy with mTLS for control plane with Elastic Defend installed Add integration tests using a proxy with mTLS for control plane with Elastic Defend installed Oct 31, 2024
@AndersonQ AndersonQ marked this pull request as ready for review October 31, 2024 14:12
@AndersonQ AndersonQ requested a review from a team as a code owner October 31, 2024 14:12
@AndersonQ AndersonQ force-pushed the 5716-5491-mtls-integration-test branch from 8c96584 to c7df66a Compare October 31, 2024 14:21
@AndersonQ AndersonQ force-pushed the 5716-5491-mtls-integration-test branch from 6ad71d0 to 563b607 Compare October 31, 2024 15:39
@pierrehilbert pierrehilbert added the Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team label Oct 31, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

@AndersonQ AndersonQ force-pushed the 5716-5491-mtls-integration-test branch from c33f868 to d673f76 Compare November 4, 2024 09:24
@pierrehilbert pierrehilbert requested a review from pchila November 4, 2024 12:20
@AndersonQ
Copy link
Member Author

folks (@blakerouse, @pchila, @kaanyalti), all the tests are passing now. In case you were waiting the tests to pass to review it

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this looks good.

I do question the change from returning errors to using require.NoError directly. Why was that change made?

@AndersonQ
Copy link
Member Author

I do question the change from returning errors to using require.NoError directly. Why was that change made?

to follow the convention we have on our tests. The majority of the tests use this approach

Copy link
Member

@pchila pchila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments here and there, mostly about readability

pkg/testing/tools/tools.go Outdated Show resolved Hide resolved
@@ -894,3 +901,536 @@ func TestForceInstallOverProtectedPolicy(t *testing.T) {
out, err := fixture.Exec(ctx, args)
require.Errorf(t, err, "No error detected, command output: %s", out)
}

func TestInstallDefendWithMTLSandEncCertKey(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: To help my brain tokenize the string maybe we can write it this way?

Suggested change
func TestInstallDefendWithMTLSandEncCertKey(t *testing.T) {
func TestInstallDefendWithmTlsAndEncCertKey(t *testing.T) {

(I know that TLS is an acronym but I think that in this case the go linter will let it slide 😄 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about mTLS -> TestInstallDefendWithmTLSAndEncCertKey?

testing/integration/endpoint_security_test.go Outdated Show resolved Hide resolved
testing/integration/endpoint_security_test.go Outdated Show resolved Hide resolved
Comment on lines +1139 to +1150
buff := &strings.Builder{}
assert.Eventuallyf(t, func() bool {
buff.Reset()

got, err := f.ExecInspect(ctx)
if err != nil {
buff.WriteString(fmt.Sprintf("error running inspect cmd: %v", err))
return false
}

return proxyPolicymTLS.URL == got.Fleet.ProxyURL
}, time.Minute, time.Second, "inspect never showed proxy from policy: %s", buff)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buff is really the last error encountered in the assert.Eventually, maybe we can define and treat it as such:

Suggested change
buff := &strings.Builder{}
assert.Eventuallyf(t, func() bool {
buff.Reset()
got, err := f.ExecInspect(ctx)
if err != nil {
buff.WriteString(fmt.Sprintf("error running inspect cmd: %v", err))
return false
}
return proxyPolicymTLS.URL == got.Fleet.ProxyURL
}, time.Minute, time.Second, "inspect never showed proxy from policy: %s", buff)
// feel free to change the variable name
var buff error
assert.Eventuallyf(t, func() bool {
got, err := f.ExecInspect(ctx)
if err != nil {
buff = fmt.Errorf("error running inspect cmd: %w", err)
return false
}
// (optional) reset the error
buff = nil
return proxyPolicymTLS.URL == got.Fleet.ProxyURL
}, time.Minute, time.Second, "inspect never showed proxy from policy: %s", buff)

testing/integration/endpoint_security_test.go Show resolved Hide resolved
testing/integration/endpoint_test_tools.go Show resolved Hide resolved
@AndersonQ AndersonQ requested a review from pchila November 5, 2024 14:33
Copy link

Copy link
Member

@pchila pchila left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AndersonQ AndersonQ merged commit a338543 into elastic:main Nov 5, 2024
14 checks passed
@AndersonQ AndersonQ deleted the 5716-5491-mtls-integration-test branch November 5, 2024 16:34
mergify bot pushed a commit that referenced this pull request Nov 5, 2024
…Elastic Defend installed (#5889)

(cherry picked from commit a338543)

# Conflicts:
#	testing/proxytest/https.go
#	testing/proxytest/proxytest.go
mergify bot pushed a commit that referenced this pull request Nov 5, 2024
…Elastic Defend installed (#5889)

(cherry picked from commit a338543)
oakrizan pushed a commit that referenced this pull request Nov 7, 2024
AndersonQ added a commit that referenced this pull request Nov 7, 2024
…Elastic Defend installed (#5889)

(cherry picked from commit a338543)
AndersonQ added a commit that referenced this pull request Nov 8, 2024
…or control plane with Elastic Defend installed (#5943)

* add integration tests using a proxy with mTLS for control plane with Elastic Defend installed (#5889)

(cherry picked from commit a338543)

* update elastic-agent-libs so it works with 8.x stacks

---------

Co-authored-by: Anderson Queiroz <anderson.queiroz@elastic.co>
AndersonQ added a commit that referenced this pull request Nov 14, 2024
…Elastic Defend installed (#5889)

(cherry picked from commit a338543)

# Conflicts:
#	testing/proxytest/https.go
#	testing/proxytest/proxytest.go
AndersonQ added a commit that referenced this pull request Nov 14, 2024
…for control plane with Elastic Defend installed (#5942)

* add integration tests using a proxy with mTLS for control plane with Elastic Defend installed (#5889)

(cherry picked from commit a338543)

# Conflicts:
#	testing/proxytest/https.go
#	testing/proxytest/proxytest.go

* fix conflicts: use proxytest.go from main

* update elastic-agent-libs to v0.17.3

---------

Co-authored-by: Anderson Queiroz <anderson.queiroz@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify backport-8.16 Automated backport with mergify skip-changelog Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team
Projects
None yet
5 participants