Go Kit, a package for building microservices

In the enterprise world, people know about Netflix's Eureka and Spring Boot from the Java community. In Go, a package that tries to reach that level of implementation is obviously Go kit. It is a toolkit for building microservices.

It has a Go style of adding services, which makes us feel good. It comes with a procedure for adding the microservices. In the upcoming sections, we will see how to create a microservice with the steps defined by Go Kit. It mainly consists of many layers. There are three layers where request and response flow in Go Kit:

  • Transport layer: This takes care of transferring data from one service to another
  • Endpoint layer: This takes care of building endpoints for the given services
  • Service layer: This is the actual business logic for the API handlers

Install Go Kit using this command:

go get github.com/go-kit/kit

Let us lay down the plan for our first microservice. We all know the encryption of messages. A message string can be encrypted using a key that outputs a gibberish message that can be passed over the wire. The recipient decrypts the message and gets back the original string. This process is called encryption in cryptography. We will try to implement this as part of our microservice illustration:

  • First, develop logic for encryption
  • Then, integrate it with Go Kit

Go comes with packages for encrypting messages. We need to import encrypting algorithms from those packages and use them. As part of the first step, we will write a project that uses an Advanced Encryption Standard (AES).

Create a directory called encryptString in your GOPATH/src/user directory:

mkdir $GOPATH/src/github.com/narenaryan/encryptString
cd $GOPATH/src/github.com/narenaryan/encryptString

Now let us add one more in the new directory, called utils. Add two files, main.go in the project directory and utils.go in the new directory called utils. The directory structure looks like this:

└── encryptString
├── main.go
└── utils
└── utils.go

Now let us add the logic of encryption in our utils.go file. We create two functions, one for encrypting and another for decrypting the messages, as shown in the following code:

package utils
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
)

The AES algorithm takes the initialization vector. Let us define that first:

// Implements AES encryption algorithm(Rijndael Algorithm)
/* Initialization vector for the AES algorithm
More details visit this link https://en.wikipedia.org/wiki/Advanced_Encryption_Standard */
var initVector = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}

Now, let us implement the logic for encryption and decryption:

// EncryptString encrypts the string with given key
func EncryptString(key, text string) string {
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
plaintext := []byte(text)
cfb := cipher.NewCFBEncrypter(block, initVector)
ciphertext := make([]byte, len(plaintext))
cfb.XORKeyStream(ciphertext, plaintext)
return base64.StdEncoding.EncodeToString(ciphertext)
}

In the EncryptString function, we are creating a new cipher block using a key. Then we are passing that block to a cipher block encryptor function. That encryptor takes the block and initialization vector. Then we generate ciphertext (an encrypted message) by doing a XORKeyStream on the cipher block. It fills the ciphertext. Then we need to do a Base64 encoding to generate the protected string:

// DecryptString decrypts the encrypted string to original
func DecryptString(key, text string) string {
block, err := aes.NewCipher([]byte(key))
if err != nil {
panic(err)
}
ciphertext, _ := base64.StdEncoding.DecodeString(text)
cfb := cipher.NewCFBEncrypter(block, initVector)
plaintext := make([]byte, len(ciphertext))
cfb.XORKeyStream(plaintext, ciphertext)
return string(plaintext)
}

In the DecryptString function, decode Base64 encoding and create a cipher block with the key. Pass this cipher block with the initialization vector to NewCFBEncrypter. Next, use XORKeyStream to load content from cipher text to plain text. Basically, it is a process of swapping the encrypted and decrypted messages in XORKeyStream. This finishes the utils.go file.

Now let us edit the main.go file to leverage the preceding utils package:

package main
import (
"log"
"github.com/narenaryan/encryptString/utils"
)
// AES keys should be of length 16, 24, 32
func main() {
key := "111023043350789514532147"
message := "I am A Message"
log.Println("Original message: ", message)
encryptedString := utils.EncryptString(key, message)
log.Println("Encrypted message: ", encryptedString)
decryptedString := utils.DecryptString(key, encryptedString)
log.Println("Decrypted message: ", decryptedString)
}

Here, we are importing the encrypting/decrypting functions from the utils package and using them to show an example.

If we run this program, we see the following output:

go run main.go

Original message: I am A Message
Encrypted message: 8/+JCfTb+ibIjzQtmCo=
Decrypted message: I am A Message

It shows how we can use the AES algorithm to encrypt a message and get it back using the same secret key. This algorithm is also called the Rijndael (pronounced rain-dahl) algorithm. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.79.241