Files
threshold/internal/cmd/commands/connect/mysql.go
T
8b8fb28228 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>
2025-07-08 17:07:27 -05:00

121 lines
3.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package connect
import (
"fmt"
"os"
"strings"
"github.com/hashicorp/boundary/api/proxy"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/posener/complete"
)
const (
mysqlSynopsis = "Authorize a session against a target and invoke a MySQL client to connect"
)
func mysqlOptions(c *Command, set *base.FlagSets) {
f := set.NewFlagSet("MySQL Options")
f.StringVar(&base.StringVar{
Name: "style",
Target: &c.flagMySQLStyle,
EnvVar: "BOUNDARY_CONNECT_MYSQL_STYLE",
Completion: complete.PredictSet("mysql"),
Default: "mysql",
Usage: `Specifies how the CLI will attempt to invoke a MySQL client. This will also set a suitable default for -exec if a value was not specified. Currently-understood values are "mysql".`,
})
f.StringVar(&base.StringVar{
Name: "username",
Target: &c.flagUsername,
EnvVar: "BOUNDARY_CONNECT_USERNAME",
Completion: complete.PredictNothing,
Usage: `Specifies the username to pass through to the client. May be overridden by credentials sourced from a credential store.`,
})
f.StringVar(&base.StringVar{
Name: "dbname",
Target: &c.flagDbname,
EnvVar: "BOUNDARY_CONNECT_DBNAME",
Completion: complete.PredictNothing,
Usage: `Specifies the database name to pass through to the client.`,
})
}
type mysqlFlags struct {
flagMySQLStyle string
}
func (m *mysqlFlags) defaultExec() string {
return strings.ToLower(m.flagMySQLStyle)
}
func (m *mysqlFlags) buildArgs(c *Command, port, ip, _ string, creds proxy.Credentials) (args, envs []string, retCreds proxy.Credentials, retErr error) {
var username, password string
retCreds = creds
if len(retCreds.UsernamePassword) > 0 {
// Mark credential as consumed so it is not printed to user
retCreds.UsernamePassword[0].Consumed = true
// For now just grab the first username password credential brokered
username = retCreds.UsernamePassword[0].Username
password = retCreds.UsernamePassword[0].Password
}
switch m.flagMySQLStyle {
case "mysql":
// Handle password first - defaults-file must be the first argument
if password != "" {
passfile, err := os.CreateTemp("", "*")
if err != nil {
return nil, nil, proxy.Credentials{}, fmt.Errorf("Error saving MySQL password to tmp file: %w", err)
}
c.cleanupFuncs = append(c.cleanupFuncs, func() error {
if err := os.Remove(passfile.Name()); err != nil {
return fmt.Errorf("Error removing temporary password file; consider removing %s manually: %w", passfile.Name(), err)
}
return nil
})
_, err = passfile.Write([]byte("[client]\npassword=" + password))
if err != nil {
_ = passfile.Close()
return nil, nil, proxy.Credentials{}, fmt.Errorf("Error writing password file to %s: %w", passfile.Name(), err)
}
if err := passfile.Close(); err != nil {
return nil, nil, proxy.Credentials{}, fmt.Errorf("Error closing password file after writing to %s: %w", passfile.Name(), err)
}
// --defaults-file must be the first argument
args = append([]string{"--defaults-file=" + passfile.Name()}, args...)
if c.flagDbname == "" {
c.UI.Warn("Credentials are being brokered but no -dbname parameter provided. mysql may misinterpret another parameter as the database name.")
}
} else {
// If no password provided, add -p to prompt for password
args = append(args, "-p")
}
if port != "" {
args = append(args, "-P", port)
}
args = append(args, "-h", ip)
if c.flagDbname != "" {
args = append(args, "-D", c.flagDbname)
}
switch {
case username != "":
args = append(args, "-u", username)
case c.flagUsername != "":
args = append(args, "-u", c.flagUsername)
}
}
return
}