diff options
author | Mike Crute <mike@crute.us> | 2023-08-03 09:51:29 -0700 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-08-03 09:51:29 -0700 |
commit | b9d3742fc6312b7495baa0a6c318f2f9d673848b (patch) | |
tree | debd3e51f92a440ea01163e8b7917aa00beebd3f | |
parent | cd294c332a5bde4540f803bb3db85832bf30550c (diff) | |
download | websocket_proxy-b9d3742fc6312b7495baa0a6c318f2f9d673848b.tar.bz2 websocket_proxy-b9d3742fc6312b7495baa0a6c318f2f9d673848b.tar.xz websocket_proxy-b9d3742fc6312b7495baa0a6c318f2f9d673848b.zip |
client: Check agent connection first
-rw-r--r-- | cmd/client/client.go | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/cmd/client/client.go b/cmd/client/client.go index 7799b87..a404c65 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go | |||
@@ -120,16 +120,18 @@ func getCertificateFromCA(ctx context.Context, oauthToken string, certRequest [] | |||
120 | return cert, nil | 120 | return cert, nil |
121 | } | 121 | } |
122 | 122 | ||
123 | func addCertificateToAgent(private any, cert *ssh.Certificate) error { | 123 | func connectToAgent() (agent.ExtendedAgent, error) { |
124 | socket := os.Getenv("SSH_AUTH_SOCK") | 124 | socket := os.Getenv("SSH_AUTH_SOCK") |
125 | conn, err := net.Dial("unix", socket) | 125 | conn, err := net.Dial("unix", socket) |
126 | if err != nil { | 126 | if err != nil { |
127 | return err | 127 | return nil, err |
128 | } | 128 | } |
129 | 129 | ||
130 | agentConn := agent.NewClient(conn) | 130 | return agent.NewClient(conn), nil |
131 | } | ||
131 | 132 | ||
132 | return agentConn.Add(agent.AddedKey{ | 133 | func addCertificateToAgent(conn agent.ExtendedAgent, private any, cert *ssh.Certificate) error { |
134 | return conn.Add(agent.AddedKey{ | ||
133 | PrivateKey: private, | 135 | PrivateKey: private, |
134 | Certificate: cert, | 136 | Certificate: cert, |
135 | LifetimeSecs: 10, | 137 | LifetimeSecs: 10, |
@@ -191,6 +193,11 @@ func clientMain(cfg app.Config, host, port, username string) { | |||
191 | ctx, cancel := context.WithCancel(context.Background()) | 193 | ctx, cancel := context.WithCancel(context.Background()) |
192 | defer cancel() | 194 | defer cancel() |
193 | 195 | ||
196 | agentConn, err := connectToAgent() | ||
197 | if err != nil { | ||
198 | log.Fatalf("Error connecting to agent, is it started?") | ||
199 | } | ||
200 | |||
194 | oauthToken, err := fetchOauthToken(ctx, clientId, cfg.ClientHost) | 201 | oauthToken, err := fetchOauthToken(ctx, clientId, cfg.ClientHost) |
195 | if err != nil { | 202 | if err != nil { |
196 | log.Fatalf("Error fetching oauth token: %s", err) | 203 | log.Fatalf("Error fetching oauth token: %s", err) |
@@ -206,7 +213,7 @@ func clientMain(cfg app.Config, host, port, username string) { | |||
206 | log.Fatalf("Error fetching certificate: %s", err) | 213 | log.Fatalf("Error fetching certificate: %s", err) |
207 | } | 214 | } |
208 | 215 | ||
209 | if err := addCertificateToAgent(privateKey, certificate); err != nil { | 216 | if err := addCertificateToAgent(agentConn, privateKey, certificate); err != nil { |
210 | log.Fatalf("Error adding certificate to agent: %s", err) | 217 | log.Fatalf("Error adding certificate to agent: %s", err) |
211 | } | 218 | } |
212 | 219 | ||