Control your Linux servers by using a Go Api

Emre Karadeniz
hepsiburadatech
Published in
3 min readOct 25, 2019

As a software developer you can do many tasks other than writing codes like create a docker image, publish a new version of your app to Kubernetes cluster etc. While doing this kind of tasks we are running some commands over ssh servers. Go has an ability to run ssh commands for you. In this article i will talk about this feature of Go by showing examples of how to execute ssh commands on our Kubernetes clusters.

First Part: Create an SSH Connection

In this part, we will create an ssh connection by using the “golang.org/x/crypto/ssh” library. We will create an interface called ISshClient. By defining this interface, our ssh provider will be more readable, testable and injectable.

Interface for ssh client

Then we need an implemantation for this interface. “golang.org/x/crypto/ssh” provides multiple authentication methods. We will use Password authentication. You can learn more about AuthMethod from here.

Implemantation of ssh client

Our config contains a property which is called HostKeyCallback. This property is used for verification of server keys. In this example we use InsecureIgnoreHostKey. That means the server accept any host key. Beware of not using this property in production. Learn more about HostKeyCallback.

Second Part: Run SSH Commands

I created an interface called ISshCommandExecuter contains RunSshCommand method to run ssh commands.

Ssh Command Executer Interface

To implement this interface we need to inject our ISshClient because we’ll get the connection from our ssh client then create a new session. When we create a new session, a connection establishes between the given remote machine and our go app. After that, you can run any command on the remote machine just like you’re using PuTTY.

Ssh Command Executer Implemantation

What’s next?

Now we are able to connect any server via ssh. I developed a Go api to control our Kubernetes cluster. The Go api serves deployments, pods, namespaces etc.

With this api, you can restrict the users on the ssh servers and give them permissions only execute specified commands. This makes your servers more secure and you may avoid unexpected situations like deleting a service from kubernetes. This way creates a more secure and controllable interface between users and servers.

Robo Kube Dashboard

I created a react app calling Robo Kube that you can manage your Kubernetes cluster like in the screenshot above. By using Robo Kube I’m able to edit the selected pod’s image, restart pod, access to a desired port on a pod with port-forwarding etc. To achieve this, we need to send kubectl commands to our cluster.
The given code below gets the deployment information for the given namespace and deployment. With jsonpath option in the command, you can filter the command result and obtain only required fields.

Get deployment info with ssh

You can see my github repository. It is not completed but you can use and change it as you want.

See you in the next article.

--

--