Skip to content

Commit

Permalink
Revert tunnel use of --names flag + finish tunnel param specs refacto…
Browse files Browse the repository at this point in the history
…ring
  • Loading branch information
morestatic committed Jul 12, 2022
1 parent 4ec976c commit b8d3b8e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 128 deletions.
5 changes: 2 additions & 3 deletions cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import (
func init() {
addClientsPaginationFlags(tunnelListCmd)
addClientsSearchFlag(tunnelListCmd)
tunnelListCmd.Flags().StringP(config.ClientNameFlag, "n", "", "[deprecated] Get tunnels of a client by name")
tunnelListCmd.Flags().StringP(config.ClientNamesFlag, "", "", "Get tunnels of a client by name(s)")
tunnelListCmd.Flags().StringP(controllers.ClientID, "c", "", "Get tunnels of a client by client id")
tunnelListCmd.Flags().StringP(config.ClientNameFlag, "n", "", "Get tunnels of a client by name")
tunnelListCmd.Flags().StringP(config.ClientID, "c", "", "Get tunnels of a client by client id")
tunnelsCmd.AddCommand(tunnelListCmd)

config.DefineCommandInputs(tunnelDeleteCmd, getDeleteTunnelRequirements())
Expand Down
9 changes: 2 additions & 7 deletions internal/pkg/config/flags_tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,15 @@ func GetCreateTunnelParamReqs(isRDPUserRequired bool) []ParameterRequirement {
ShortName: "c",
IsRequired: true,
IsEnabled: func(providedParams *options.ParameterBag) bool {
return providedParams.ReadString(ClientNameFlag, "") == "" && providedParams.ReadString(ClientNamesFlag, "") == ""
return providedParams.ReadString(ClientNameFlag, "") == ""
},
Help: "Enter a client ID",
},
{
Field: ClientNameFlag,
Description: `[deprecated] client name, if no client id is provided`,
Description: `client name, if no client id is provided`,
ShortName: "n",
},
{
Field: ClientNamesFlag,
Description: `client name(s), if no client id is provided`,
ShortName: "",
},
{
Field: Local,
Description: CreateTunnelLocalDescr,
Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/controllers/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func TestInvalidInputForCommand(t *testing.T) {
ExecutionHelper: &ExecutionHelper{},
}
params := config.FromValues(map[string]string{
ClientID: "",
config.ClientID: "",
config.ClientNamesFlag: "",
Local: "lohost1:3300",
Remote: "rhost2:3344",
Scheme: utils.SSH,
CheckPort: "1",
config.Local: "lohost1:3300",
config.Remote: "rhost2:3344",
config.Scheme: utils.SSH,
config.CheckPort: "1",
})
err := cc.Start(context.Background(), params)
assert.EqualError(t, err, "no client ids, names or search provided")
Expand Down
92 changes: 35 additions & 57 deletions internal/pkg/controllers/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,6 @@ import (
"github.com/cloudradar-monitoring/rportcli/internal/pkg/api"
)

const (
ClientID = "client"
TunnelID = "tunnel"
Local = "local"
Remote = "remote"
Scheme = "scheme"
ACL = "acl"
CheckPort = "checkp"
IdleTimeoutMinutes = "idle-timeout-minutes"
SkipIdleTimeout = "skip-idle-timeout"
LaunchSSH = "launch-ssh"
LaunchRDP = "launch-rdp"
RDPWidth = "rdp-width"
RDPHeight = "rdp-height"
RDPUser = "rdp-user"
DefaultACL = "<<YOU CURRENT PUBLIC IP>>"
ForceDeletion = "force"
)

type TunnelRenderer interface {
RenderTunnels(tunnels []*models.Tunnel) error
RenderTunnel(t output.KvProvider) error
Expand Down Expand Up @@ -72,8 +53,8 @@ func (tc *TunnelController) Tunnels(ctx context.Context, params *options.Paramet
ctx,
api.NewPaginationFromParams(params),
api.NewFilters(
"id", params.ReadString(ClientID, ""),
"name", config.ReadNames(params),
"id", params.ReadString(config.ClientID, ""),
"name", params.ReadString(config.ClientNameFlag, ""),
"*", params.ReadString(config.ClientSearchFlag, ""),
),
)
Expand All @@ -100,8 +81,8 @@ func (tc *TunnelController) Delete(ctx context.Context, params *options.Paramete
return err
}

tunnelID := params.ReadString(TunnelID, "")
err = tc.Rport.DeleteTunnel(ctx, clientID, tunnelID, params.ReadBool(ForceDeletion, false))
tunnelID := params.ReadString(config.TunnelID, "")
err = tc.Rport.DeleteTunnel(ctx, clientID, tunnelID, params.ReadBool(config.ForceDeletion, false))
if err != nil {
if strings.Contains(err.Error(), "tunnel is still active") {
return fmt.Errorf("%v, use -f to delete it anyway", err)
Expand All @@ -120,35 +101,32 @@ func (tc *TunnelController) Delete(ctx context.Context, params *options.Paramete
func (tc *TunnelController) getClientIDAndClientName(
ctx context.Context,
params *options.ParameterBag,
) (clientID, clientNames string, err error) {
clientID = params.ReadString(ClientID, "")
clientNames = params.ReadString(config.ClientNamesFlag, "")
if clientNames == "" {
clientNames = params.ReadString(config.ClientNameFlag, "")
}
if clientID == "" && clientNames == "" {
err = errors.New("no client id nor name provided")
) (clientID, clientName string, err error) {
clientID = params.ReadString(config.ClientID, "")
clientName = params.ReadString(config.ClientNameFlag, "")
if clientID == "" && clientName == "" {
err = errors.New("no client id or name provided")
return
}
if clientID != "" && clientNames != "" {
err = errors.New("both client id and name provided")
if clientID != "" && clientName != "" {
err = errors.New("both client id and name provided. Please provide one or the other")
return
}

if clientID != "" {
return
}

clients, err := tc.Rport.Clients(ctx, api.NewPaginationWithLimit(2), api.NewFilters("name", clientNames))
clients, err := tc.Rport.Clients(ctx, api.NewPaginationWithLimit(2), api.NewFilters("name", clientName))
if err != nil {
return
}

if len(clients.Data) < 1 {
return "", "", fmt.Errorf("unknown client with name %q", clientNames)
return "", "", fmt.Errorf("unknown client with name %q", clientName)
}
if len(clients.Data) > 1 {
return "", "", fmt.Errorf("client with name %q is ambidguous, use a more precise name or use the client id", clientNames)
return "", "", fmt.Errorf("client with name %q is ambidguous, use a more precise name or use the client id", clientName)
}

client := clients.Data[0]
Expand All @@ -161,8 +139,8 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
return err
}

acl := params.ReadString(ACL, "")
if (acl == "" || acl == DefaultACL) && tc.IPProvider != nil {
acl := params.ReadString(config.ACL, "")
if (acl == "" || acl == config.DefaultACL) && tc.IPProvider != nil {
ip, e := tc.IPProvider.GetIP(ctx)
if e != nil {
logrus.Errorf("failed to fetch IP: %v", e)
Expand All @@ -176,12 +154,12 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
return err
}

local := params.ReadString(Local, "")
checkPort := params.ReadString(CheckPort, "")
skipIdleTimeout := params.ReadBool(SkipIdleTimeout, false)
local := params.ReadString(config.Local, "")
checkPort := params.ReadString(config.CheckPort, "")
skipIdleTimeout := params.ReadBool(config.SkipIdleTimeout, false)
idleTimeoutMinutes := 0
if !skipIdleTimeout {
idleTimeoutMinutes = params.ReadInt(IdleTimeoutMinutes, 0)
idleTimeoutMinutes = params.ReadInt(config.IdleTimeoutMinutes, 0)
}
tunResp, err := tc.Rport.CreateTunnel(
ctx,
Expand Down Expand Up @@ -217,17 +195,17 @@ func (tc *TunnelController) Create(ctx context.Context, params *options.Paramete
return err
}

launchSSHStr := params.ReadString(LaunchSSH, "")
shouldLaunchRDP := params.ReadBool(LaunchRDP, false)
launchSSHStr := params.ReadString(config.LaunchSSH, "")
shouldLaunchRDP := params.ReadBool(config.LaunchRDP, false)

return tc.launchHelperFlowIfNeeded(ctx, launchSSHStr, clientID, clientName, shouldLaunchRDP, tunnelCreated, params)
}

func (tc *TunnelController) resolveRemoteAddrAndScheme(params *options.ParameterBag) (remotePortAndHostStr, scheme string, err error) {
remotePortAndHostStr = params.ReadString(Remote, "")
remotePortAndHostStr = params.ReadString(config.Remote, "")
remotePortInt, _ := utils.ExtractPortAndHost(remotePortAndHostStr)

scheme = params.ReadString(Scheme, "")
scheme = params.ReadString(config.Scheme, "")
if scheme == "" && remotePortInt > 0 {
scheme = utils.GetSchemeByPort(remotePortInt)
}
Expand All @@ -236,27 +214,27 @@ func (tc *TunnelController) resolveRemoteAddrAndScheme(params *options.Parameter
remotePortInt = utils.GetPortByScheme(scheme)
}

launchSSHStr := params.ReadString(LaunchSSH, "")
launchSSHStr := params.ReadString(config.LaunchSSH, "")
if launchSSHStr != "" {
if scheme == "" {
scheme = utils.SSH
}
if scheme != utils.SSH {
err = fmt.Errorf("scheme %s is not compatible with the %s option", scheme, LaunchSSH)
err = fmt.Errorf("scheme %s is not compatible with the %s option", scheme, config.LaunchSSH)
return
}
if remotePortInt == 0 {
remotePortInt = utils.GetPortByScheme(scheme)
}
}

shouldLaunchRDP := params.ReadBool(LaunchRDP, false)
shouldLaunchRDP := params.ReadBool(config.LaunchRDP, false)
if shouldLaunchRDP {
if scheme == "" {
scheme = utils.RDP
}
if scheme != utils.RDP {
err = fmt.Errorf("scheme %s is not compatible with the %s option", scheme, LaunchRDP)
err = fmt.Errorf("scheme %s is not compatible with the %s option", scheme, config.LaunchRDP)
return
}
if remotePortInt == 0 {
Expand Down Expand Up @@ -284,8 +262,8 @@ func (tc *TunnelController) launchHelperFlowIfNeeded(

if launchSSHStr != "" {
deleteTunnelParams := options.New(options.NewMapValuesProvider(map[string]interface{}{
ClientID: clientID,
TunnelID: tunnelCreated.ID,
config.ClientID: clientID,
config.TunnelID: tunnelCreated.ID,
}))
return tc.startSSHFlow(ctx, tunnelCreated, params, deleteTunnelParams)
}
Expand All @@ -312,7 +290,7 @@ func (tc *TunnelController) startSSHFlow(
tunnelCreated *models.TunnelCreated,
params, deleteTunnelParams *options.ParameterBag,
) error {
sshParamsFlat := params.ReadString(LaunchSSH, "")
sshParamsFlat := params.ReadString(config.LaunchSSH, "")
logrus.Debugf("ssh arguments are provided: '%s', will start an ssh session", sshParamsFlat)
port, host, err := tc.extractPortAndHost(tunnelCreated, params)
if err != nil {
Expand All @@ -338,7 +316,7 @@ func (tc *TunnelController) startSSHFlow(
}

func (tc *TunnelController) generateUsage(tunnelCreated *models.TunnelCreated, params *options.ParameterBag) string {
shouldLaunchRDP := params.ReadBool(LaunchRDP, false)
shouldLaunchRDP := params.ReadBool(config.LaunchRDP, false)

if !shouldLaunchRDP {
port, host, err := tc.extractPortAndHost(tunnelCreated, params)
Expand Down Expand Up @@ -401,9 +379,9 @@ func (tc *TunnelController) startRDPFlow(

rdpFileInput := models.FileInput{
Address: fmt.Sprintf("%s:%s", host, port),
ScreenHeight: params.ReadInt(RDPHeight, 0),
ScreenWidth: params.ReadInt(RDPWidth, 0),
UserName: params.ReadString(RDPUser, ""),
ScreenHeight: params.ReadInt(config.RDPHeight, 0),
ScreenWidth: params.ReadInt(config.RDPWidth, 0),
UserName: params.ReadString(config.RDPUser, ""),
FileName: fmt.Sprintf("%s.rdp", clientName),
}

Expand Down
Loading

0 comments on commit b8d3b8e

Please sign in to comment.