Skip to content

Commit b5e3664

Browse files
committed
release 0.4.0
1 parent b2d8c7c commit b5e3664

File tree

4 files changed

+99
-32
lines changed

4 files changed

+99
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to the "kubectl-plugin-ssh-jump" extension will be documented in this file.
44

5+
## 0.4.0
6+
7+
- Support PEM (Privacy Enhanced Mail) scenario where you create key-pair but you only have .pem / private key (downloaded from AWS, for example) and you don't have the public key on your side.
8+
59
## 0.3.2
610

711
- Changed a validation for destination name to support valid characters of hostname for SSH destination node that can start from ASCII letters 'a' through 'z' (in a case-insensitive manner), the digits '0' through '9', or the hyphen ('-'). Ref [RFC952](https://tools.ietf.org/html/rfc952) for valid characters of hostname.
@@ -23,4 +27,5 @@ All notable changes to the "kubectl-plugin-ssh-jump" extension will be documente
2327

2428

2529
## 0.1.0
30+
2631
- Initial release (alpha release)

README.md

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Here is an scenario where you want to connect to Kubernetes node, but you have t
2222
This plugin needs the following programs:
2323
* ssh(1)
2424
* ssh-agent(1)
25+
* ssh-keygen(1)
2526

2627
## Installation
2728

@@ -96,7 +97,7 @@ Options:
9697
ASCII letters 'a' through 'z' or 'A' through 'Z',
9798
the digits '0' through '9', or hyphen ('-'
9899
-u, --user <sshuser> SSH User name
99-
-i, --identity <identity_file> Identity key file
100+
-i, --identity <identity_file> Identity key file, or PEM(Privacy Enhanced Mail)
100101
-p, --pubkey <pub_key_file> Public key file
101102
-P, --port <port> SSH port for target node SSH server (default:22)
102103
-a, --args <args> Args to exec in ssh session
@@ -109,13 +110,22 @@ Options:
109110
--cleanup-jump Clearning up sshjump pod at the end
110111
Default: Skip cleaning up sshjump pod
111112
-h, --help Show this message
113+
114+
Example:
115+
Scenario1 - You have private & public SSH key on your side
116+
$ kubectl ssh-jump -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub hostname
117+
118+
Scenario2 - You have .pem file but you don't have private key on your side
119+
$ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem hostname
112120
```
113121

114122
#### Option parameters Cache
123+
115124
`username`, `identity`, `pubkey`, `port` options are cached, therefore you can omit these options afterward. The options are stored in a file named `$HOME/.kube/kubectlssh/options`
116-
```
125+
126+
```sh
117127
$ cat $HOME/.kube/kubectlssh/options
118-
sshuser=azureuser
128+
sshuser=myuser
119129
identity=/Users/yokawasa/.ssh/id_rsa_k8s
120130
pubkey=/Users/yokawasa/.ssh/id_rsa_k8s.pub
121131
port=22
@@ -132,7 +142,7 @@ In addtion, add `--skip-agent` option if you want to skip automatic starting `ss
132142

133143
Show all node list. Simply executing `kubectl ssh-jump` gives you the list of destination nodes as well as command usage
134144

135-
```sh
145+
```sh
136146
$ kubectl ssh-jump
137147

138148
Usage:
@@ -144,7 +154,7 @@ Options:
144154
ASCII letters 'a' through 'z' or 'A' through 'Z',
145155
the digits '0' through '9', or hyphen ('-')
146156
-u, --user <sshuser> SSH User name
147-
-i, --identity <identity_file> Identity key file
157+
-i, --identity <identity_file> Identity key file, or PEM(Privacy Enhanced Mail)
148158
-p, --pubkey <pub_key_file> Public key file
149159
-P, --port <port> SSH port for target node SSH server (default:22)
150160
-a, --args <args> Args to exec in ssh session
@@ -159,27 +169,35 @@ Options:
159169
-h, --help Show this message
160170

161171
Example:
162-
....
172+
Scenario1 - You have private & public SSH key on your side
173+
$ kubectl ssh-jump -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub hostname
174+
175+
Scenario2 - You have .pem file but you don't have private key on your side
176+
$ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem hostname
163177
164178
List of destination node...
165179
Hostname Internal-IP
166180
aks-nodepool1-18558189-0 10.240.0.4
167-
aks-nodepool1-18558189-1 10.240.0.5
168-
aks-nodepool1-18558189-2 10.240.0.6
181+
...
169182
170183
```
171184
172-
Then, SSH into a node `aks-nodepool1-18558189-0` with options like:
173-
- usernaem: `azureuser`
185+
#### Scenario1 - You have private & public SSH key on your side
186+
187+
Suppose you have private & public SSH key on your side and you want to SSH to a node named `aks-nodepool1-18558189-0`, execute the plugin with options like this:
188+
189+
- usernaem: `myuser`
174190
- identity:`~/.ssh/id_rsa_k8s`
175191
- pubkey:`~/.ssh/id_rsa_k8s.pub`)
192+
176193
```sh
177194
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
178-
-u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub
195+
-u myuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub
179196
```
197+
180198
> [NOTE] you can try SSH into a node using node IP address (`Internal-IP`) instead of `Hostname`
181199
182-
As explained in usage secion, `username`, `identity`, `pubkey` options are cached, therefore you can omit these options afterward.
200+
As explained in usage secion, `username`, `identity`, `pubkey` options will be cached, therefore you can omit these options afterward.
183201
184202
```sh
185203
$ kubectl ssh-jump aks-nodepool1-18558189-0
@@ -202,18 +220,21 @@ Linux aks-nodepool1-18558189-0 4.15.0-1035-azure #36~16.04.1-Ubuntu SMP Fri Nov
202220
```
203221
204222
You can clean up sshjump pod at the end of the command with `--cleanup-jump` option, otherwise, the sshjump pod stay running by default.
223+
205224
```sh
206225
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
207-
-u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
226+
-u myuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
208227
--cleanup-jump
209228
```
210229
211230
You can clean up ssh-agent at the end of the command with `--cleanup-agent` option, otherwise, the ssh-agent process stay running once it's started.
231+
212232
```sh
213233
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
214-
-u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
234+
-u myuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
215235
--cleanup-agent
216236
```
237+
217238
You can skip starting `ssh-agent` by giving `--skip-agent`. This is actually a case where you already have ssh-agent managed. Or you can start new ssh-agent and add an identity key to the ssh-agent like this:
218239

219240
```sh
@@ -223,14 +244,28 @@ $ eval `ssh-agent`
223244
$ ssh-add ~/.ssh/id_rsa_k8s
224245
# Then, run the plugin with --skip-agent
225246
$ kubectl ssh-jump aks-nodepool1-18558189-0 \
226-
-u azureuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
247+
-u myuser -i ~/.ssh/id_rsa_k8s -p ~/.ssh/id_rsa_k8s.pub \
227248
--skip-agent
228249
229250
# At the end, run this if you want to kill the current agent
230251
$ ssh-agent -k
231252
```
232253

254+
#### Scenario2 - You have .pem file but you don't have private key on your side
255+
256+
From v0.4.0, the plugin supports PEM (Privacy Enhanced Mail) scenario where you create key-pair but you only have .pem / private key (downloaded from AWS, for example) and you don't have the public key on your side.
257+
258+
Suppose you've already downloaded a pem file and you want to ssh to your EKS worker node (EC2) named `ip-10-173-62-96.ap-northeast-1.compute.internal` using the pem, execute the plugin with options like this:
259+
260+
- usernaem: `ec2-user`
261+
- identity: `~/.ssh/mykey.pem`
262+
263+
```sh
264+
$ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem ip-10-173-62-96.ap-northeast-1.compute.internal
265+
```
266+
233267
## Useful Links
268+
234269
- [Extend kubectl with plugins](https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/)
235270
- [Write your own kubectl subcommands](https://ahmet.im/blog/kubectl-plugins/)
236271
- [SSH-AGENT - SINGLE SIGN-ON USING SSH](https://www.ssh.com/ssh/agent)

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.3.2
1+
0.4.0

kubectl-ssh-jump

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Options:
2626
ASCII letters 'a' through 'z' or 'A' through 'Z',
2727
the digits '0' through '9', or hyphen ('-')
2828
-u, --user <sshuser> SSH User name
29-
-i, --identity <identity_file> Identity key file
29+
-i, --identity <identity_file> Identity key file, or PEM(Privacy Enhanced Mail)
3030
-p, --pubkey <pub_key_file> Public key file
3131
-P, --port <port> SSH port for target node SSH server (default:22)
3232
-a, --args <args> Args to exec in ssh session
@@ -41,7 +41,12 @@ Options:
4141
-h, --help Show this message
4242
4343
Example:
44-
....
44+
Scenario1 - You have private & public SSH key on your side
45+
$ kubectl ssh-jump -u myuser -i ~/.ssh/id_rsa -p ~/.ssh/id_rsa.pub hostname
46+
47+
Scenario2 - You have .pem file but you don't have private key on your side
48+
$ kubectl ssh-jump -u ec2-user -i ~/.ssh/mykey.pem hostname
49+
4550
EOF
4651
}
4752

@@ -137,15 +142,32 @@ run_ssh_node(){
137142
done
138143
fi
139144

145+
local identity_sshjump=${identity}
146+
local pubkey_sshjump=${pubkey}
147+
if [ ! -f "${pubkey_sshjump}" ]; then
148+
# Generate temp private/public key to ssh to the sshjump if the pubkey isn't given
149+
identity_sshjump=${PLUGIN_DIR}/id_rsa_sshjump
150+
pubkey_sshjump=${PLUGIN_DIR}/id_rsa_sshjump.pub
151+
if [ ! -f "${pubkey_sshjump}" ]; then
152+
echo "Generating nopass SSH pri/pub key to ssh to the sshjump ..."
153+
ssh-keygen -t rsa -f ${identity_sshjump} -N '' > /dev/null
154+
fi
155+
fi
156+
140157
# Setup portforward
141158
kubectl port-forward sshjump 2222:22 2>/dev/null &
142159
pid_port_forward=$!
160+
161+
# Wait a bit for the port forwarding to get ready for connection handling for 2222
162+
sleep 2
163+
143164
# Inject public SSH key to sshjump
144-
cat ${pubkey} | kubectl exec -i sshjump -- /bin/bash -c "cat > /root/.ssh/authorized_keys"
165+
cat ${pubkey_sshjump} | \
166+
kubectl exec -i sshjump -- /bin/bash -c "cat > /root/.ssh/authorized_keys"
145167

146168
# Using the SSH Server as a jumphost (via port-forward proxy), ssh into the desired Node
147169
ssh -i ${identity} -p ${port} ${sshuser}@${destnode} \
148-
-o "ProxyCommand ssh root@127.0.0.1 -p 2222 -i ${identity} -o \"StrictHostKeyChecking=no\" \"nc %h %p\"" \
170+
-o "ProxyCommand ssh root@127.0.0.1 -p 2222 -i ${identity_sshjump} -o \"StrictHostKeyChecking=no\" \"nc %h %p\"" \
149171
-o "StrictHostKeyChecking=no" $sshargs
150172

151173
# Stop port-forward
@@ -223,47 +245,52 @@ plugin_main() {
223245

224246
if [ ! -n "${c_sshuser}" ]; then
225247
if [ ! -n "${sshuser}" ]; then
226-
c_sshuser="$USER" # default: Current executing user
248+
c_sshuser="${USER}" # default: Current executing user
227249
fi
228-
echo "using: sshuser=$sshuser"
250+
echo "using: sshuser=${sshuser}"
229251
c_sshuser="${sshuser}"
230252
fi
231253
if [ ! -f "${c_identity}" ]; then
232254
if [ ! -f "${identity}" ]; then
255+
echo "Error: identity file is required" >&2
233256
help >&2
234257
exit 1
235258
fi
236-
echo "using: identity=$identity"
259+
echo "using: identity=${identity}"
237260
c_identity="${identity}"
238261
fi
239262
if [ ! -f "${c_pubkey}" ]; then
240-
if [ ! -f "${pubkey}" ]; then
241-
help >&2
242-
exit 1
263+
# From v0.4.0 pubkey file is optional to support PEM scenario
264+
# where you don't have public key on your side
265+
#if [ ! -n "${pubkey}" ]; then
266+
# help >&2
267+
# exit 1
268+
#fi
269+
if [ -f "${pubkey}" ]; then
270+
echo "using: pubkey=${pubkey}"
271+
c_pubkey="${pubkey}"
243272
fi
244-
echo "using: pubkey=$pubkey"
245-
c_pubkey="${pubkey}"
246273
fi
247274
if [ ! -n "${c_port}" ]; then
248275
if [ ! -n "${port}" ]; then
249276
port="22" # default: 22
250277
fi
251-
echo "using: port=$port"
278+
echo "using: port=${port}"
252279
c_port="${port}"
253280
fi
254281

255282
if [ "${sshargs}" != "" ]; then
256-
echo "using: args=$sshargs"
283+
echo "using: args=${sshargs}"
257284
fi
258285

259286
# Caching current ssh options
260-
write_options ${c_sshuser} ${c_identity} ${c_pubkey} ${c_port}
287+
write_options "${c_sshuser}" "${c_identity}" "${c_pubkey}" "${c_port}"
261288

262289
if [ "${skip_agent}" = "no" ]; then
263290
check_and_start_agent ${c_identity}
264291
fi
265292
# SSH Logging into desitnation node via Jump host
266-
run_ssh_node ${destnode} ${c_sshuser} ${c_identity} ${c_pubkey} ${c_port} "${sshargs}"
293+
run_ssh_node "${destnode}" "${c_sshuser}" "${c_identity}" "${c_pubkey}" "${c_port}" "${sshargs}"
267294

268295
# Cleaning up resources if needed
269296
if [ "${cleanup_jump}" = "yes" ]; then

0 commit comments

Comments
 (0)