Skip to content

Commit cebcab0

Browse files
authored
update handling for ports in proxy commands (#789)
1 parent a64c6b7 commit cebcab0

File tree

6 files changed

+31
-29
lines changed

6 files changed

+31
-29
lines changed

pkg/granted/eks/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func OpenKubeConfig() (*api.Config, string, error) {
3838
return config, kubeConfigPath, nil
3939
}
4040

41-
func AddContextToConfig(ensureAccessOutput *proxy.EnsureAccessOutput[*accessv1alpha1.AWSEKSProxyOutput], port string) error {
41+
func AddContextToConfig(ensureAccessOutput *proxy.EnsureAccessOutput[*accessv1alpha1.AWSEKSProxyOutput], port int) error {
4242

4343
kc, kubeConfigPath, err := OpenKubeConfig()
4444
if err != nil {
@@ -59,7 +59,7 @@ func AddContextToConfig(ensureAccessOutput *proxy.EnsureAccessOutput[*accessv1al
5959
delete(kc.AuthInfos, username)
6060

6161
newCluster := api.NewCluster()
62-
newCluster.Server = fmt.Sprintf("http://localhost:%s", port)
62+
newCluster.Server = fmt.Sprintf("http://localhost:%d", port)
6363
newCluster.InsecureSkipTLSVerify = true
6464
//add the new cluster and context back in
6565
kc.Clusters[clusterName] = newCluster

pkg/granted/proxy/initiateconnection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
type InitiateSessionConnectionInput struct {
1515
GrantID string
1616
RequestURL string
17-
LocalPort string
17+
LocalPort int
1818
}
1919

2020
// InitiateSessionConnection starts a new tcp connection to through the SSM port forward and completes a handshake with the proxy server
@@ -24,8 +24,8 @@ func InitiateSessionConnection(cfg *config.Context, input InitiateSessionConnect
2424
// First dial the local SSM portforward, which will be running on a randomly chosen port
2525
// or the local proxy server instance if it's local dev mode
2626
// this establishes the initial connection to the Proxy server
27-
clio.Debugw("dialing proxy server", "host", "localhost:"+input.LocalPort)
28-
rawServerConn, err := net.Dial("tcp", "localhost:"+input.LocalPort)
27+
clio.Debugw("dialing proxy server", "host", fmt.Sprintf("localhost:%d", input.LocalPort))
28+
rawServerConn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", input.LocalPort))
2929
if err != nil {
3030
return nil, nil, clierr.New("failed to establish a connection to the remote proxy server", clierr.Error(err), clierr.Infof("Your grant may have expired, you can check the status here: %s and retry connecting", input.RequestURL))
3131
}

pkg/granted/proxy/listenandproxy.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414

1515
// ListenAndProxy will listen for new client connections and start a stream over the established proxy server session.
1616
// if the proxy server terminates the session, like when a grant expires, this listener will detect it and terminate the CLI commmand with an error explaining what happened
17-
func ListenAndProxy(ctx context.Context, yamuxStreamConnection *yamux.Session, clientConnectionPort string, requestURL string) error {
18-
ln, err := net.Listen("tcp", "localhost:"+clientConnectionPort)
17+
func ListenAndProxy(ctx context.Context, yamuxStreamConnection *yamux.Session, clientConnectionPort int, requestURL string) error {
18+
ln, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", clientConnectionPort))
1919
if err != nil {
20-
return fmt.Errorf("failed to start listening for connections on port: %s. %w", clientConnectionPort, err)
20+
return fmt.Errorf("failed to start listening for connections on port: %d. %w", clientConnectionPort, err)
2121
}
2222
defer ln.Close()
2323

pkg/granted/proxy/ports.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,36 @@ package proxy
22

33
import (
44
"net"
5-
"strconv"
65
)
76

87
// Returns the proxy port to connect to and a local port to send client connections to
98
// in production, an SSM portforward process is running which is used to connect to the proxy server
109
// and over the top of this connection, a handshake process takes place and connection multiplexing is used to handle multiple database clients
11-
func Ports(isLocalMode bool) (serverPort, localPort string, err error) {
10+
func Ports(isLocalMode bool) (serverPort, localPort int, err error) {
1211
// in local mode the SSM port forward is not used can skip using ssm and just use a local port forward instead
1312
if isLocalMode {
14-
return "7070", "7070", nil
13+
return 7070, 7070, nil
1514
}
1615
// find an unused local port to use for the ssm server
1716
// the user doesn't directly connect to this, they connect through our local proxy
1817
// which adds authentication
1918
ssmPortforwardLocalPort, err := GrabUnusedPort()
2019
if err != nil {
21-
return "", "", err
20+
return 0, 0, err
2221
}
23-
return "8080", ssmPortforwardLocalPort, nil
22+
return 8080, ssmPortforwardLocalPort, nil
2423
}
2524

26-
func GrabUnusedPort() (string, error) {
25+
func GrabUnusedPort() (int, error) {
2726
listener, err := net.Listen("tcp", ":0")
2827
if err != nil {
29-
return "", err
28+
return 0, err
3029
}
3130

3231
port := listener.Addr().(*net.TCPAddr).Port
3332
err = listener.Close()
3433
if err != nil {
35-
return "", err
34+
return 0, err
3635
}
37-
return strconv.Itoa(port), nil
36+
return port, nil
3837
}

pkg/granted/proxy/proxy.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"strconv"
89
"time"
910

1011
awsConfig "github.com/aws/aws-sdk-go-v2/config"
@@ -39,8 +40,8 @@ type AWSConfig struct {
3940
NoCache bool
4041
}
4142
type ConnectionOpts struct {
42-
ServerPort string
43-
LocalPort string
43+
ServerPort int
44+
LocalPort int
4445
}
4546
type WaitForSSMConnectionToProxyServerOpts struct {
4647
AWSConfig AWSConfig
@@ -89,8 +90,8 @@ func WaitForSSMConnectionToProxyServer(ctx context.Context, opts WaitForSSMConne
8990
Target: &opts.AWSConfig.SSMSessionTarget,
9091
DocumentName: &documentName,
9192
Parameters: map[string][]string{
92-
"portNumber": {opts.ConnectionOpts.ServerPort},
93-
"localPortNumber": {opts.ConnectionOpts.LocalPort},
93+
"portNumber": {strconv.Itoa(opts.ConnectionOpts.ServerPort)},
94+
"localPortNumber": {strconv.Itoa(opts.ConnectionOpts.LocalPort)},
9495
},
9596
Reason: grab.Ptr(fmt.Sprintf("Session started for Granted %s connection with Common Fate. GrantID: %s, AccessRequestID: %s", opts.DisplayOpts.SessionType, opts.GrantID, opts.RequestID)),
9697
}
@@ -109,7 +110,7 @@ func WaitForSSMConnectionToProxyServer(ctx context.Context, opts WaitForSSMConne
109110
SessionId: *sessionOutput.SessionId,
110111
TokenValue: *sessionOutput.TokenValue,
111112
IsAwsCliUpgradeNeeded: false,
112-
Endpoint: "localhost:" + opts.ConnectionOpts.LocalPort,
113+
Endpoint: fmt.Sprintf("localhost:%d", opts.ConnectionOpts.LocalPort),
113114
DataChannel: &datachannel.DataChannel{},
114115
ClientId: clientId,
115116
}

pkg/granted/rds/rds.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ var proxyCommand = cli.Command{
134134
return err
135135
}
136136

137-
printConnectionParameters(connectionString, cliString, clientConnectionPort, ensuredAccess.GrantOutput.RdsDatabase.Engine)
137+
printConnectionParameters(connectionString, cliString, ensuredAccess.GrantOutput.RdsDatabase.Engine, clientConnectionPort)
138138

139139
return proxy.ListenAndProxy(ctx, yamuxStreamConnection, clientConnectionPort, requestURL)
140140
},
@@ -210,36 +210,38 @@ func promptForDatabaseAndUser(ctx context.Context, cfg *config.Context) (*access
210210
return selectorVal.(*accessv1alpha1.Entitlement), nil
211211
}
212212

213-
func clientConnectionParameters(c *cli.Context, ensuredAccess *proxy.EnsureAccessOutput[*accessv1alpha1.AWSRDSOutput]) (connectionString, cliString, port string, err error) {
213+
func clientConnectionParameters(c *cli.Context, ensuredAccess *proxy.EnsureAccessOutput[*accessv1alpha1.AWSRDSOutput]) (connectionString, cliString string, port int, err error) {
214214
// Print the connection information to the user based on the database they are connecting to
215215
// the passwords are always 'password' while the username and database will match that of the target being connected to
216216
yellow := color.New(color.FgYellow)
217217
switch ensuredAccess.GrantOutput.RdsDatabase.Engine {
218218
case "postgres", "aurora-postgresql":
219-
port := getLocalPort(getLocalPortInput{
219+
port = getLocalPort(getLocalPortInput{
220220
OverrideFlag: c.Int("port"),
221221
DefaultFromServer: int(ensuredAccess.GrantOutput.DefaultLocalPort),
222222
Fallback: 5432,
223223
})
224+
224225
connectionString = yellow.Sprintf("postgresql://%s:[email protected]:%d/%s?sslmode=disable", ensuredAccess.GrantOutput.User.Username, port, ensuredAccess.GrantOutput.RdsDatabase.Database)
225226
cliString = yellow.Sprintf(`psql "postgresql://%s:[email protected]:%d/%s?sslmode=disable"`, ensuredAccess.GrantOutput.User.Username, port, ensuredAccess.GrantOutput.RdsDatabase.Database)
226227
case "mysql", "aurora-mysql":
227-
port := getLocalPort(getLocalPortInput{
228+
port = getLocalPort(getLocalPortInput{
228229
OverrideFlag: c.Int("port"),
229230
DefaultFromServer: int(ensuredAccess.GrantOutput.DefaultLocalPort),
230231
Fallback: 3306,
231232
})
233+
232234
connectionString = yellow.Sprintf("%s:password@tcp(127.0.0.1:%d)/%s", ensuredAccess.GrantOutput.User.Username, port, ensuredAccess.GrantOutput.RdsDatabase.Database)
233235
cliString = yellow.Sprintf(`mysql -u %s -p'password' -h 127.0.0.1 -P %d %s`, ensuredAccess.GrantOutput.User.Username, port, ensuredAccess.GrantOutput.RdsDatabase.Database)
234236
default:
235-
return "", "", "", fmt.Errorf("unsupported database engine: %s, maybe you need to update your `cf` cli", ensuredAccess.GrantOutput.RdsDatabase.Engine)
237+
return "", "", 0, fmt.Errorf("unsupported database engine: %s, maybe you need to update your `cf` cli", ensuredAccess.GrantOutput.RdsDatabase.Engine)
236238
}
237239
return
238240
}
239241

240-
func printConnectionParameters(connectionString, cliString, port, engine string) {
242+
func printConnectionParameters(connectionString, cliString, engine string, port int) {
241243
clio.NewLine()
242-
clio.Infof("Database proxy ready for connections on 127.0.0.1:%s", port)
244+
clio.Infof("Database proxy ready for connections on 127.0.0.1:%d", port)
243245
clio.NewLine()
244246

245247
clio.Infof("You can connect now using this connection string: %s", connectionString)

0 commit comments

Comments
 (0)