mirror of https://github.com/hashicorp/boundary
Cassandra Connect Support (#5952)
* Init cassandra * Init cassandra * Initial Cassandra Command Support For cqlsh * Add Make Gen * adding cassandra test * Cleanup cassandra docker setup * Adding container wait * Container restart * Minor Fix * Cleanup docker and test * Adding final tests * Removing comment * Fixing cleanup (uncomment) * Removing bad yaml * Cleanup * Cleanup * Adressing PR review comments * Adding test support * fix spacing * Adding changelog * docs * docs * adding cassandra page * nav * Versbose commenting * cleanup naming of file * updating helper func to return errors * doc change * Removed an incorrect comment and now passing the config by reference. * Comment cleanup * Fix option warning in cassandra connect command (#5951) * Add healthcheck to caller function as it's resposible for container health * Resolve Comments * Update Log Typo --------- Co-authored-by: Bharath Gajjala <120367134+bgajjala8@users.noreply.github.com>pull/5966/head
parent
029aa361bd
commit
4d3cb76c28
@ -0,0 +1,117 @@
|
||||
// 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 (
|
||||
cassandraSynopsis = "Authorize a session against a target and invoke a Cassandra client to connect"
|
||||
)
|
||||
|
||||
func cassandraOptions(c *Command, set *base.FlagSets) {
|
||||
f := set.NewFlagSet("Cassandra Options")
|
||||
|
||||
f.StringVar(&base.StringVar{
|
||||
Name: "style",
|
||||
Target: &c.flagCassandraStyle,
|
||||
EnvVar: "BOUNDARY_CONNECT_CASSANDRA_STYLE",
|
||||
Completion: complete.PredictSet("cqlsh"),
|
||||
Default: "cqlsh",
|
||||
Usage: `Specifies how the CLI will attempt to invoke a Cassandra client. This will also set a suitable default for -exec if a value was not specified. Currently-understood values are "cqlsh".`,
|
||||
})
|
||||
|
||||
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: "keyspace",
|
||||
Target: &c.flagDbname,
|
||||
EnvVar: "BOUNDARY_CONNECT_KEYSPACE",
|
||||
Completion: complete.PredictNothing,
|
||||
Usage: `Specifies the keyspace name to pass through to the client.`,
|
||||
})
|
||||
}
|
||||
|
||||
type cassandraFlags struct {
|
||||
flagCassandraStyle string
|
||||
}
|
||||
|
||||
func (m *cassandraFlags) defaultExec() string {
|
||||
return strings.ToLower(m.flagCassandraStyle)
|
||||
}
|
||||
|
||||
func (m *cassandraFlags) 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, such that it is not printed to the user
|
||||
retCreds.UsernamePassword[0].Consumed = true
|
||||
|
||||
// Grab the first available username/password credential brokered
|
||||
username = retCreds.UsernamePassword[0].Username
|
||||
password = retCreds.UsernamePassword[0].Password
|
||||
}
|
||||
|
||||
switch m.flagCassandraStyle {
|
||||
case "cqlsh":
|
||||
switch {
|
||||
case username != "":
|
||||
args = append(args, "-u", username)
|
||||
case c.flagUsername != "":
|
||||
args = append(args, "-u", c.flagUsername)
|
||||
}
|
||||
|
||||
if c.flagDbname != "" {
|
||||
args = append(args, "-k", c.flagDbname)
|
||||
} else {
|
||||
c.UI.Warn("Credentials are being brokered but no -keyspace parameter provided. cqlsh may misinterpret another parameter as the keyspace name.")
|
||||
}
|
||||
|
||||
if password != "" {
|
||||
passfile, err := os.CreateTemp("", "*")
|
||||
if err != nil {
|
||||
return nil, nil, proxy.Credentials{}, fmt.Errorf("Error saving cassandra 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.WriteString("[PlainTextAuthProvider]\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)
|
||||
}
|
||||
|
||||
args = append(args, "--credentials", passfile.Name())
|
||||
}
|
||||
|
||||
args = append(args, ip)
|
||||
if port != "" {
|
||||
args = append(args, port)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -0,0 +1,127 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package base_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// TestCliTcpTargetConnectCassandra uses the boundary cli to connect to a
|
||||
// target using `connect cassandra`
|
||||
func TestCliTcpTargetConnectCassandra(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
|
||||
pool, err := dockertest.NewPool("")
|
||||
require.NoError(t, err)
|
||||
|
||||
// Increase timeout to accommodate Cassandra's longer startup duration due to gossip needing to settle
|
||||
pool.MaxWait = 90 * time.Second
|
||||
ctx := context.Background()
|
||||
|
||||
// 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.StartCassandra(t, pool, &network[0], "cassandra", "latest")
|
||||
require.NotNil(t, c, "Cassandra container should not be nil")
|
||||
t.Cleanup(func() {
|
||||
if err := pool.Purge(c.Resource); err != nil {
|
||||
t.Logf("Failed to purge Cassandra container: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
u, err := url.Parse(c.UriNetwork)
|
||||
t.Log(u)
|
||||
require.NoError(t, err, "Failed to parse Cassandra URL")
|
||||
|
||||
user, hostname, port, keyspace := u.User.Username(), u.Hostname(), u.Port(), u.Path[1:]
|
||||
pw, pwSet := u.User.Password()
|
||||
|
||||
t.Logf("Cassandra info: user=%s, keyspace=%s, host=%s, port=%s, password-set:%t",
|
||||
user, keyspace, hostname, port, pwSet)
|
||||
|
||||
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", "cassandra",
|
||||
"-target-id", targetId,
|
||||
"-keyspace", keyspace,
|
||||
)
|
||||
f, err := pty.Start(cmd)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
err := f.Close()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
_, err = f.Write([]byte("DESCRIBE KEYSPACES;\n"))
|
||||
require.NoError(t, err)
|
||||
_, err = f.Write([]byte(fmt.Sprintf("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = '%s';\n", keyspace)))
|
||||
require.NoError(t, err)
|
||||
_, err = f.Write([]byte("exit\n"))
|
||||
require.NoError(t, err)
|
||||
|
||||
var buf bytes.Buffer
|
||||
_, _ = io.Copy(&buf, f)
|
||||
|
||||
output := buf.String()
|
||||
t.Logf("Cassandra session output: %s", output)
|
||||
|
||||
require.Contains(t, output, "keyspace_name")
|
||||
require.Contains(t, output, keyspace)
|
||||
|
||||
t.Log("Successfully connected to Cassandra target")
|
||||
}
|
||||
@ -0,0 +1,69 @@
|
||||
---
|
||||
layout: docs
|
||||
page_title: connect cassandra - Command
|
||||
description: >-
|
||||
The "connect cassandra" command performs a target authorization or consumes an existing authorization token, and then launches a proxied Cassandra (CQL) connection.
|
||||
---
|
||||
|
||||
# connect cassandra
|
||||
|
||||
Command: `boundary connect cassandra`
|
||||
The `connect cassandra` command authorizes a session against a target and invokes a Cassandra client for the connection.
|
||||
The command fills in the local address and port.
|
||||
|
||||
> **Note:** Currently, only configurations using Cassandra's `PasswordAuthenticator` are supported.
|
||||
|
||||
@include 'cmd-connect-env-vars.mdx'
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
The following example shows how to connect to a target with the ID `ttcp_eTcZMueUYv` using a Cassandra helper:
|
||||
|
||||
```shell-session
|
||||
$ boundary connect cassandra -target-id=ttcp_eTcZMueUYv \
|
||||
-keyspace=mykeyspace \
|
||||
-username=superuser
|
||||
```
|
||||
|
||||
When prompted, you must enter the password for the user, "superuser":
|
||||
|
||||
<CodeBlockConfig hideClipboard>
|
||||
|
||||
```plaintext
|
||||
Password:
|
||||
Connected to example-cluster at localhost:9042
|
||||
[cqlsh 6.2.0 | Cassandra 5.0.4 | CQL spec 3.4.7 | Native protocol v5]
|
||||
Use HELP for help.
|
||||
superuser@cqlsh:mykeyspace>
|
||||
```
|
||||
|
||||
</CodeBlockConfig>
|
||||
|
||||
## Usage
|
||||
|
||||
<CodeBlockConfig hideClipboard>
|
||||
|
||||
```shell-session
|
||||
$ boundary connect cassandra [options] [args]
|
||||
```
|
||||
|
||||
</CodeBlockConfig>
|
||||
|
||||
@include 'cmd-connect-command-options.mdx'
|
||||
|
||||
### Cassandra options:
|
||||
|
||||
- `-keyspace` `(string: "")` - The keyspace name you want to pass through to the client.
|
||||
You can also specify the keyspace name using the **BOUNDARY_CONNECT_KEYSPACE** environment variable.
|
||||
|
||||
- `-style` `(string: "")` - How the CLI attempts to invoke a Cassandra client.
|
||||
This value also sets a suitable default for `-exec`, if you did not specify a value.
|
||||
The default and currently-understood value is `cqlsh`.
|
||||
You can also specify how the CLI attempts to invoke a Cassandra client using the **BOUNDARY_CONNECT_CASSANDRA_STYLE** environment variable.
|
||||
|
||||
- `-username` `(string: "")` - The username you want to pass through to the client.
|
||||
This value may be overridden by credentials sourced from a credential store.
|
||||
You can also specify a username using the **BOUNDARY_CONNECT_USERNAME** environment variable.
|
||||
|
||||
@include 'cmd-option-note.mdx'
|
||||
Loading…
Reference in new issue