feat: add MySQL command support PORT (#5891)

* feat: add MySQL command support

* Fix MySQL connection according to review feedback

* Update internal/cmd/commands/connect/mysql.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* Update internal/cmd/commands/connect/mysql.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* Update internal/cmd/commands/connect/mysql.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* feat: add MySQL support to boundary connect

* docs: add MySQL to navigation menu

   Fixes content check error by adding mysql.mdx to docs navigation.
   MySQL page now appears in Commands > connect section alongside postgres.

* Update testing/internal/e2e/tests/base_plus/target_tcp_connect_mysql_test.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* test: add containerized MySQL connection test for target functionality

* Remove MySQL environment variables from e2e test config

* test: move MySQL e2e test from base_plus to base folder

* Fix MySQL test to support both MySQL and MariaDB clients

* Fix MySQL test to support both MySQL and MariaDB clients

* lint cleanup

* adding error checks

* Make gen

* Test case testing

* Reverting bad comment

* cleaning up extra test file

* Add Mysql to runner (#5893)

* Adding back correct test file

* Removing base plus test file

* Removing base plus test file

* Revert "Removing base plus test file"

This reverts commit ace9cb3e90a028adc1f7c8ef55436ca843abfc15.

* forcing enos docker network name

* Revert "forcing enos docker network name"

This reverts commit 631bdb4a0ef87f5435e06456e8ec0a4573ab3e49.

* changing hosts to looks for sql container

* fix container name

* Adjusting for e2e tests and a bit of cleanup

* missed a commented string

* Bgajjala mysql e2etest v2 (#5900)

* Using Env Vars rather than parsing uri

* Comments fix

* spacing

* Update testing/internal/e2e/tests/base/target_tcp_connect_mysql_test.go

* Spacing

* Update internal/cmd/commands/connect/mysql.go

* adding error checks

* Make gen

* Update testing/internal/e2e/tests/base/target_tcp_connect_mysql_test.go

* Update internal/cmd/commands/connect/mysql.go

* Make gen

* derving target port from uri

* nit

* updating test to use url strings

* nit: spacing

* nit: code organization

* nit: code organization
Co-authored-by: Enbiya Göral

---------

Co-authored-by: Enbiya <menbiyagoral@gmail.com>
Co-authored-by: Enbiya Göral <100806254+enbiyagoral@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
Co-authored-by: Ryan Derr <54389874+RyanDerr@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-07-08 17:07:27 -05:00
1 parent ca899f4fff
commit 8b8fb28228
10 files changed
+400 -1

No files matched your search

+41
View File
@@ -303,3 +303,44 @@ func StartOpenSshServer(t testing.TB, pool *dockertest.Pool, network *dockertest
UriNetwork: networkAlias,
}
}
// StartMysql starts a MySQL database in a docker container.
// Returns information about the container
func StartMysql(t testing.TB, pool *dockertest.Pool, network *dockertest.Network, repository, tag string) *Container {
t.Log("Starting MySQL database...")
c, err := LoadConfig()
require.NoError(t, err)
err = pool.Client.PullImage(docker.PullImageOptions{
Repository: fmt.Sprintf("%s/%s", c.DockerMirror, repository),
Tag: tag,
}, docker.AuthConfiguration{})
require.NoError(t, err)
networkAlias := "e2emysql"
mysqlDb := "e2eboundarydb"
mysqlUser := "e2eboundary"
mysqlPassword := "e2eboundary"
mysqlRootPassword := "rootpassword"
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: fmt.Sprintf("%s/%s", c.DockerMirror, repository),
Tag: tag,
Env: []string{
"MYSQL_DATABASE=" + mysqlDb,
"MYSQL_USER=" + mysqlUser,
"MYSQL_PASSWORD=" + mysqlPassword,
"MYSQL_ROOT_PASSWORD=" + mysqlRootPassword,
},
ExposedPorts: []string{"3306/tcp"},
Name: networkAlias,
Networks: []*dockertest.Network{network},
})
require.NoError(t, err)
return &Container{
Resource: resource,
UriLocalhost: fmt.Sprintf("mysql://%s:%s@localhost:3306/%s", mysqlUser, mysqlPassword, mysqlDb),
UriNetwork: fmt.Sprintf("mysql://%s:%s@%s:3306/%s", mysqlUser, mysqlPassword, networkAlias, mysqlDb),
}
}
@@ -0,0 +1,127 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package base_test
import (
"bytes"
"context"
"io"
"net/url"
"os/exec"
"testing"
"github.com/creack/pty"
"github.com/hashicorp/boundary/internal/target"
"github.com/hashicorp/boundary/testing/internal/e2e"
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
"github.com/hashicorp/boundary/testing/internal/e2e/infra"
"github.com/ory/dockertest/v3"
"github.com/stretchr/testify/require"
)
// TestCliTcpTargetConnectMysql uses the boundary cli to connect to a
// target using `connect mysql`
func TestCliTcpTargetConnectMysql(t *testing.T) {
e2e.MaybeSkipTest(t)
ctx := context.Background()
pool, err := dockertest.NewPool("")
require.NoError(t, err)
// e2e_cluster network is created by the e2e infra setup
network, err := pool.NetworksByName("e2e_cluster")
require.NoError(t, err, "Failed to get e2e_cluster network")
c := infra.StartMysql(t, pool, &network[0], "mysql", "8.0")
require.NotNil(t, c, "MySQL container should not be nil")
t.Cleanup(func() {
if err := pool.Purge(c.Resource); err != nil {
t.Logf("Failed to purge MySQL container: %v", err)
}
})
u, err := url.Parse(c.UriNetwork)
require.NoError(t, err, "Failed to parse MySQL URL")
user, hostname, port, db := u.User.Username(), u.Hostname(), u.Port(), u.Path[1:]
pw, pwSet := u.User.Password()
t.Logf("MySQL info: user=%s, db=%s, host=%s, port=%s, password-set:%t",
user, db, hostname, port, pwSet)
// Wait for MySQL to be ready
err = pool.Retry(func() error {
return exec.CommandContext(ctx, "docker", "exec", hostname,
"mysql", "-u"+user, "-p"+pw, "-e", "SELECT 1").Run()
})
require.NoError(t, err, "MySQL container failed to start")
boundary.AuthenticateAdminCli(t, ctx)
orgId, err := boundary.CreateOrgCli(t, ctx)
require.NoError(t, err)
t.Cleanup(func() {
ctx := context.Background()
boundary.AuthenticateAdminCli(t, ctx)
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", orgId))
require.NoError(t, output.Err, string(output.Stderr))
})
projectId, err := boundary.CreateProjectCli(t, ctx, orgId)
require.NoError(t, err)
targetId, err := boundary.CreateTargetCli(
t,
ctx,
projectId,
port,
target.WithAddress(hostname),
)
require.NoError(t, err)
storeId, err := boundary.CreateCredentialStoreStaticCli(t, ctx, projectId)
require.NoError(t, err)
credentialId, err := boundary.CreateStaticCredentialPasswordCli(
t,
ctx,
storeId,
user,
pw,
)
require.NoError(t, err)
err = boundary.AddBrokeredCredentialSourceToTargetCli(t, ctx, targetId, credentialId)
require.NoError(t, err)
cmd := exec.CommandContext(ctx,
"boundary",
"connect", "mysql",
"-target-id", targetId,
"-dbname", db,
)
f, err := pty.Start(cmd)
require.NoError(t, err)
t.Cleanup(func() {
err := f.Close()
require.NoError(t, err)
})
_, err = f.Write([]byte("SHOW TABLES;\n"))
require.NoError(t, err)
_, err = f.Write([]byte("SELECT DATABASE();\n"))
require.NoError(t, err)
_, err = f.Write([]byte("EXIT;\n"))
require.NoError(t, err)
_, err = f.Write([]byte{4})
require.NoError(t, err)
var buf bytes.Buffer
_, _ = io.Copy(&buf, f)
output := buf.String()
t.Logf("MySQL session output: %s", output)
require.Contains(t, output, "| "+db+" |", "Session did not return expected database query result")
t.Log("Successfully connected to MySQL target")
}