Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker Nanik Tolaram pdf download
Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker Nanik Tolaram pdf download
https://ebookmeta.com/product/software-development-with-go-cloud-
native-programming-using-golang-with-linux-and-docker-nanik-
tolaram/
https://ebookmeta.com/product/software-development-with-go-cloud-
native-programming-using-golang-with-linux-and-docker-1st-
edition-nanik-tolaram/
https://ebookmeta.com/product/network-programming-with-go-
language-essential-skills-for-programming-using-and-securing-
networks-with-open-source-google-golang-2nd-edition-jan-
newmarch-2/
https://ebookmeta.com/product/network-programming-with-go-
language-essential-skills-for-programming-using-and-securing-
networks-with-open-source-google-golang-2nd-edition-jan-newmarch/
https://ebookmeta.com/product/fit-for-a-duke-dangerous-
dukes-10-1st-edition-wendy-soliman/
Going Global Transnational Perspectives on
Globalization Language and Education 1st Edition Leslie
Seawright
https://ebookmeta.com/product/going-global-transnational-
perspectives-on-globalization-language-and-education-1st-edition-
leslie-seawright/
https://ebookmeta.com/product/php-and-mysql-174-web-development-
luke-welling/
https://ebookmeta.com/product/if-looks-could-kill-80-s-baby-
series-2-1st-edition-tamrin-banks/
https://ebookmeta.com/product/her-special-alpha-hot-seals-x-
ops-3-5-paige-tyler/
https://ebookmeta.com/product/ours-to-share-1st-edition-ar-taboo/
Brand Strategy in Three Steps: A Purpose-Driven
Approach to Branding 1st Edition Jay Mandel
https://ebookmeta.com/product/brand-strategy-in-three-steps-a-
purpose-driven-approach-to-branding-1st-edition-jay-mandel/
Software Development
with Go
Cloud-Native Programming
using Golang with Linux
and Docker
Nanik Tolaram
Software Development with Go: Cloud-Native Programming using Golang
with Linux and Docker
Nanik Tolaram
Sydney, NSW, Australia
Acknowledgments����������������������������������������������������������������������������xvii
Introduction���������������������������������������������������������������������������������������xix
v
Table of Contents
ELF Package�������������������������������������������������������������������������������������������������������22
High-Level ELF Format���������������������������������������������������������������������������������������������� 23
Dump Example���������������������������������������������������������������������������������������������������������� 24
/sys Filesystem���������������������������������������������������������������������������������������������������28
Reading AppArmor����������������������������������������������������������������������������������������������������� 29
Summary������������������������������������������������������������������������������������������������������������31
Summary������������������������������������������������������������������������������������������������������������48
vi
Table of Contents
Docker Proxy�������������������������������������������������������������������������������������������������������98
Container Attack Surface����������������������������������������������������������������������������������105
Summary����������������������������������������������������������������������������������������������������������106
gosec����������������������������������������������������������������������������������������������������������������122
Inside gosec������������������������������������������������������������������������������������������������������������ 123
Rules������������������������������������������������������������������������������������������������������������������������ 128
Summary����������������������������������������������������������������������������������������������������������130
vii
Table of Contents
Chapter 8: Scorecard������������������������������������������������������������������������131
Source Code������������������������������������������������������������������������������������������������������131
What Is Scorecard?�������������������������������������������������������������������������������������������131
Setting Up Scorecard����������������������������������������������������������������������������������������������� 133
Running Scorecard�������������������������������������������������������������������������������������������������� 137
High-Level Flow������������������������������������������������������������������������������������������������������� 139
GitHub���������������������������������������������������������������������������������������������������������������145
GitHub API���������������������������������������������������������������������������������������������������������������� 145
GitHub Explorer�������������������������������������������������������������������������������������������������������� 156
Summary����������������������������������������������������������������������������������������������������������159
UDP Networking������������������������������������������������������������������������������������������������168
UDP Client���������������������������������������������������������������������������������������������������������������� 169
UDP Server�������������������������������������������������������������������������������������������������������������� 172
Concurrent Servers�������������������������������������������������������������������������������������������������� 174
Load Testing������������������������������������������������������������������������������������������������������175
Summary����������������������������������������������������������������������������������������������������������179
viii
Table of Contents
DNS Server��������������������������������������������������������������������������������������������������������188
Running a DNS Server��������������������������������������������������������������������������������������������� 188
DNS Forwarder�������������������������������������������������������������������������������������������������������� 189
Pack and Unpack����������������������������������������������������������������������������������������������������� 193
Summary����������������������������������������������������������������������������������������������������������196
Using gopacket�������������������������������������������������������������������������������������������������205
pcap������������������������������������������������������������������������������������������������������������������������� 205
Networking Sniffer�������������������������������������������������������������������������������������������������� 206
Capturing With BPF�������������������������������������������������������������������������������������������������� 217
Summary����������������������������������������������������������������������������������������������������������222
Epoll Library������������������������������������������������������������������������������������������������������232
Summary����������������������������������������������������������������������������������������������������������235
ix
Table of Contents
Summary����������������������������������������������������������������������������������������������������������263
Summary����������������������������������������������������������������������������������������������������������290
x
Table of Contents
Summary����������������������������������������������������������������������������������������������������������306
Bubbletea����������������������������������������������������������������������������������������������������������313
Init��������������������������������������������������������������������������������������������������������������������������� 315
Update��������������������������������������������������������������������������������������������������������������������� 318
View������������������������������������������������������������������������������������������������������������������������� 319
Summary����������������������������������������������������������������������������������������������������������321
xi
Table of Contents
Index�������������������������������������������������������������������������������������������������377
xii
About the Author
Nanik Tolaram is a big proponent of open source software with over 20
years of industry experience. He has dabbled in different programming
languages like Java, JavaScript, C, and C++. He has developed different
products from the ground up while working in start-up companies. He is
a software engineer at heart, but he loves to write technical articles and
share his knowledge with others. He learned to program with Go during
the COVID-19 pandemic and hasn’t looked back.
xiii
About the Technical Reviewer
Fabio Claudio Ferracchiati is a senior consultant and a senior
analyst/developer using Microsoft technologies. He works for BluArancio
(www.bluarancio.com). He is a Microsoft Certified Solution Developer for
.NET, a Microsoft Certified Application Developer for .NET, a Microsoft
Certified Professional, and a prolific author and technical reviewer.
Over the past ten years, he’s written articles for Italian and international
magazines and coauthored more than ten books on a variety of
computer topics.
xv
Acknowledgments
Thanks to everyone on the Apress team who helped and guided me so
much. Special thanks to James Robinson-Prior who guided me through
the writing process and to Nirmal Selvaraj who made sure everything was
done correctly and things were on track.
Thanks to the technical reviewers for taking time from their busy
schedules to review my book and provide great feedback.
Finally, thanks to you, the reader, for spending time reading this book
and spreading the love of Go.
xvii
Introduction
Go has been out for more than 10 years, and open source projects were
developed using Go. The aim of this book is to show you the way to use Go
to write a variety of applications that are useful in cloud-based systems.
Deploying applications into the cloud is a normal process that
developers do every day. There are many questions that developers ask
themselves about the cloud, like
xix
CHAPTER 1
System Calls
Linux provides a lot of features and provides applications access to
everything that the operating system has access to. When discussing
system calls, most people will turn their attention to using C because
it is the most common language to use when interfacing with the
operating system.
In this chapter, you will explore what system calls are and how you can
program in Go to make system calls. By the end of this chapter, you will
have learned the following:
If you are using Go for the first time, refer to the online documentation
at https://go.dev/doc/install. The online documentation will walk you
through the steps to install Go on your local computer. Go through the Go
tutorial that the Go documentation provides at https://go.dev/doc/.
Source Code
The source code for this chapter is available from the https://github.
com/Apress/Software-Development-Go repository.
4
Chapter 1 System Calls
C System Call
In this section, you will briefly look at how system calls normally work
inside a C program. This will give you an idea of how system calls are done
in C compared to how they are done in Go.
You will see a simple example of using a socket to connect to a server
and read the response. The code can be found inside the chapter1/c
directory. The code creates a socket and uses it to connect to a public
website named httpbin.org and print the response it receives to the
screen. Listing 1-1 shows the sample code.
#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
5
Chapter 1 System Calls
#include<netdb.h>
server.sin_family = AF_INET;
server.sin_port = htons(80);
6
Chapter 1 System Calls
To test the code, make sure you have a C compiler installed in your
machine. Follow the instructions outlined on the GCC website to install
the compiler and tools (https://gcc.gnu.org/). Use the following
command to compile the code:
cc sample.c -o sample
Connected
Data Send
Reply received
HTTP/1.1 200 OK
7
Chapter 1 System Calls
The code sample shows the system call that it uses to resolve the
address of httpbin.org to an IP address by using the gethostbyname
function. It also uses the connect function to use the newly created socket
to connect to the server.
In the next section, you will start exploring Go by using the standard
library to write code using system calls.
sys/unix Package
The sys/unix package is a package provided by the Go language that
provides a system-level interface to interact with the operating system. Go
can run on a variety of operating systems, which means that it provides
different interfaces to applications for different operating systems.
Complete package documentation can be found at https://pkg.go.dev/
golang.org/x/sys/unix. Figure 1-3 shows different system calls in
different operating systems, in this case between Darwin and Linux.
8
Chapter 1 System Calls
Listing 1-2 shows how to use system calls using the sys/unix package.
package main
import (
u "golang.org/x/sys/unix"
"log"
)
func main() {
c := make([]byte, 512)
9
Chapter 1 System Calls
log.Println(string(c))
}
The other system call that the application uses is to get the current
working directory using the Getcwd function.
System Call in Go
In the previous section, you looked at a simple example of using the sys/
unix package. In this section, you will explore more on system calls by
10
Chapter 1 System Calls
File: ./README.md
Size: 476 Blocks: 8
IO Block: 4096 regular file
Device: fd01h/64769d Inode: 2637168
Links: 1
Access: (0644/-rw-r--r--) Uid: (1000/ nanik)
Gid: (1000/ nanik)
Access: 2022-02-19 18:10:29.919351223 +1100 AEDT
Modify: 2022-02-19 18:10:29.919351223 +1100 AEDT
Change: 2022-02-19 18:10:29.919351223 +1100 AEDT
Birth: 2022-02-19 18:10:29.919351223 +1100 AEDT
Attrs: 0000000000000000 (-----....)
How does the application get all this information about the file? It
obtains the information from the operating system by making a system
call. Let's take a look at the code in Listing 1-3.
import (
....
"golang.org/x/sys/unix"
)
11
Chapter 1 System Calls
....
func main() {
log.SetFlags(0)
flag.Parse()
12
Chapter 1 System Calls
13
Chapter 1 System Calls
Summary
In this chapter, you learned what system calls are and how to write a
simple application to interface with the operating system by using the
sys/unix package. You dug deeper into system calls by looking at an open
source project to learn how it uses the system calls to provide statistical
information about a particular file.
In the next chapters, you will explore system calls more and you will
look at various ways to interface with the operating system using Go.
14
CHAPTER 2
Source Code
The source code for this chapter is available from the https://github.
com/Apress/Software-Development-Go repository.
Syscall Package
The syscall package is the standard library provided by Go that provides
function calls that interface with the log-level operating system. The
following are some of the functionalities provided by the package:
• Change directory
syscall Application
Let’s take the existing application from Chapter 1 and convert it to use
the syscall package. The app can be seen inside the chapter2/syscalls
directory. Open terminal and run the sample as follows:
go run main.go
The sample code uses system calls to get information about itself such
as the process id assigned by the operating system for itself, the parent id,
and others. The following shows how it uses the syscall package:
package main
import (
16
Chapter 2 System Calls Using Go
"log"
s "syscall"
)
func main() {
...
...
}
17
Chapter 2 System Calls Using Go
go run main.go
18
Chapter 2 System Calls Using Go
The output shows in gigabytes the total size of the drive, total amount
of disk used, and total amount of disk free. The following code snippet
shows how the disk information is obtained using the syscall package:
func main() {
var statfs = syscall.Statfs_t{}
var total uint64
var used uint64
var free uint64
err := syscall.Statfs("/", &statfs)
if err != nil {
fmt.Printf("[ERROR]: %s\n", err)
} else {
total = statfs.Blocks * uint64(statfs.Bsize)
free = statfs.Bfree * uint64(statfs.Bsize)
used = total - free
}
...
}
As seen in the above code snippet, the application uses the syscall.
Statfs function call to get information about the path. In this case, it’s the
root directory. The result is populated into the statfs variable, which is of
type Statfs_t. The Statfs_t struct declaration looks like the following:
19
Chapter 2 System Calls Using Go
Fsid Fsid
Namelen int64
Frsize int64
Flags int64
Spare [4]int64
}
go run main.go
The web server is now ready to accept connection on port 8888. Open
your browser and type in http://localhost:8888. You will get a response
in your browser: Server with syscall
The following code snippet shows the function that takes care of
starting up the server that listens on port 8888:
20
Chapter 2 System Calls Using Go
21
Chapter 2 System Calls Using Go
for {
cSock, cAddr, err := syscall.Accept(fd)
ELF Package
The standard library provides different packages that can be used to
interact with different parts of the operating system. In the previous
sections, you looked at interacting on a system level by using the different
standard library packages. In this section, you will look at the debug/elf
package.
22
Chapter 2 System Calls Using Go
23
Chapter 2 System Calls Using Go
Dump Example
In this section, you will take a look at an open source project named
GoPlay, which is hosted at https://github.com/n4ss/GoPlay. It can also
be found inside the chapter2/GoPlay directory. This is a simple app that
dumps the contents of a Go ELF executable file. You will look at how the
application uses the Go library to read the ELF file
Compile the GoPlay application to create an executable using the
following command:
go build main.go
24
Chapter 2 System Calls Using Go
....
runtime.(*cpuProfile).addNonGo
....
_cgo_init
runtime.mainPC
go.itab.syscall.Errno,error
runtime.defaultGOROOT.str
runtime.buildVersion.str
type.*
runtime.textsectionmap
....
25
Chapter 2 System Calls Using Go
Let’s start analyzing how the code works and what system calls it is
using to get what information out from the executable file.
func main() {
....
file, err := os.Stat(*filename)
....
f, err := os.Open(*filename)
....
switch *action {
....
case "dump": os.Exit(dump_elf(*filename))
}
} else {
goto Usage
}
....
}
26
Chapter 2 System Calls Using Go
The following is the struct definition of the Symbol struct. As you can
see, it contains useful information.
27
Chapter 2 System Calls Using Go
This function is used to obtain certain parts of the ELF file, which are
passed as parameters when calling the file.DynString function. For
example, when calling
dynstrs, _ = file.DynString(elf.DT_SONAME)
the code will get information about the shared library name of the file.
/sys Filesystem
In this section, you will look at a different way of reading system-level information.
You will not use a function to read system information; rather, you will use system
directories that are made available by the operating system for user applications.
28
Chapter 2 System Calls Using Go
The directory that you want to read is the /sys directory, which is a
virtual filesystem containing device drivers, device information, and other
kernel features. Figure 2-4 shows what the /sys directory contains on a
Linux machine.
Reading AppArmor
Some of the information that is provided by Linux inside the /sys
directory is related to AppArmor (short for Application Armor). What is
AppArmor? It is a kernel security module that gives system administrators
the ability to restrict application capabilities with a profile. This gives
system administrators the power to select which resources a particular
application can have access to. For example, a system administrator can
define Application A to have network access or raw socket access, while
Application B does not have access to network capabilities.
Let’s look at an example application to read AppArmor information
from the /sys filesystem, specifically whether AppArmor is enabled and
whether it is enforced. The following is the sample code that can be found
inside the chapter2/apparmor directory:
import (
"fmt"
29
Chapter 2 System Calls Using Go
...
)
const (
appArmorEnabledPath = "/sys/module/apparmor/parameters/
enabled"
appArmorModePath = "/sys/module/apparmor/parameters/mode"
)
func main() {
fmt.Println("AppArmor mode : ", appArmorMode())
fmt.Println("AppArmor is enabled : ", appArmorEnabled())
}
Since the code is accessing a system filesystem, you must run it using
root. Compile the code and run it as follows:
sudo ./apparmor
The code reads the information from the directory using the standard
library ioUtil.ReadFile, which is just like reading a file, so it’s simpler
than using the function calls that you looked at in the previous sections.
30
Chapter 2 System Calls Using Go
Summary
In this chapter, you looked at using system calls to interface with the
operating system. You looked at using the syscall standard library that
provides a lot of function calls to interface with the operating system
and wrote a sample application to print out disk space information.
You looked at how the debug/elf standard library is used to read Go
ELF file information. Lastly, you looked at the /sys filesystem to extract
information that you want to read to understand whether the operating
system supports AppArmor.
31
Discovering Diverse Content Through
Random Scribd Documents
Catlin, 281.
Cayuse, 44.
Cedar Lake, 121.
Chadwick, Mr., 238, 244.
Chardon, Mr., 304, 306, 315.
Cheyennes, 131, 154, 157, 161, 188, 329, 335.
Chihuahua, 194, 203.
Chihuahua, game of, 203.
Chinooke, 8.
Chinook Point, 9.
Chinooks, 9.
Chipita, 184, 185.
Chouteau County (Mont.), 328.
Chouteau, Jr. & Co., Pierre, 238.
Chouteaus, 130.
Cimarron River, 167.
Clarke, John, 35.
Clark’s Fork, 17.
Cochran, J. V., 358.
Columbia, Forks of, 26.
Columbia River, 3, et seq.
Comanches, attack by, 260.
Comanches, 138, 154, 163, 176, 188, 260.
Come Comly, 37.
Comeloups, 27.
Coolidge, Mr., 264, 266, 267.
Council at Fort Benton, The, 323, 325.
Council Bluffs, 282.
Council Grove, Kan., 241.
Cowlitz River, 72.
Cox, Ross, 5, 24.
Creeks (see River).
Crees, 100.
Crooks, Ramsey, 24, 25, 27.
Crows, 116, 312, 329, 332, 335.
Cuba, 194.
Kamloops, 27.
Kansas City, Mo., 141.
Kearny, General S. W., 142, 161, 162, 204.
Keith, James, 42, 43, 58.
Kiowas, 138, 154, 161, 188.
Kiowa woman, 168.
Kipp, James, 281, 289, 320.
Kipp, Joe, 328.
Kittson, Mr., 77, 78, 81.
Knife River, 304.
Kutenais, 100.
La Bonté, 233.
La Cañada, 148.
Lake Bourbon, 121.
Larocque, Joseph Felix, 121.
Larpenteur’s Post, 304.
Leavenworth, Fort, 132.
Lee, General, 238.
Lewis and Clark, 71, 103.
Lewis Fork, 17, 90.
Life at Bent’s Fort, 170.
Little Blackfoot River, 356.
Little Dog, 330, et seq.
Little Missouri River, 316, 317.
Little Mountain, 138.
Little Rocky Mountains, 348, 349.
Little White Man, 128.
Long Narrows, 24, 26.
“Raccoon,” 37.
Raids by Indians, 198.
Rattling Buttes, 335.
Red Coat Land, 356.
Red River (of North,) 121, 122, 123.
Red River (of Texas), 167.
Red River settlement, 4.
Red Sleeves, 176.
Red Sleeves Creek, 176.
Reed, John, 33.
Reid (See Reed, J.), 88.
Reid’s River (see Payette River, 107).
Reynolds, A. E., 189.
Richardson, Dr., 121.
Rio del Norte, 205.
Rio Grande, 205.
Rivers:
Arkansas, 128, 129, 130, 189;
Arkansor, 278;
Arrow, 333;
Bear, 112;
Beaver, 349;
Box Elder, 336;
Canadian, 138;
Cimarron, 167;
Clark’s Fork, 17;
Columbia, 3, et seq.;
Columbia, Forks of, 26;
Cowlitz, 72;
Deep, 334;
Eagle, 350;
Flathead, 102;
Flat Willow, 336, 338;
Fontaine-qui-bouille, 208;
Fourchette, 348;
Fraser, 23;
Goddin, 104, 105, 116;
Greenhorn, 152, 208;
Huerfano, 208;
Judith, 326, 327, 335;
Knife, 304;
Lewis Fork, 17, 90;
Little Blackfoot, 356;
Little Missouri, 316, 317;
Malades, 105, 112;
Marias, 356;
Missouri, 332;
Musselshell, 336, 337, 339, 348, 357;
North Platte, 183;
Oakinacken, 19;
Okanagan, 19, et seq.;
Oregon, 4;
Pawnee Fork, 176, 260;
Payette, 107;
Pisscow, 19;
Plum, 335;
Porcupine, 340, 343;
Poshett, 348;
Purgatoire, 129, 130, 157, 171;
Red (of North), 121, 122, 123;
Red (of Texas), 167;
Red Sleeves, 176;
Reid’s (See Payette), 107;
Rio del Norte, 205;
Rio Grande, 205;
Rivière aux Malades, 105, 112;
Saint Vrain’s Fork, 138;
Salmon, 104;
Sa-mick-a-meigh, 31;
Skam-naugh, 76;
Smilkameen, 31;
Snake, 25;
Teton, 356;
Walnut, 155;
Willamette, 58;
Willow, 334;
Wolf, 334;
Yellowstone, 281, 306.
Rivière aux Malades, 105, 112.
Rocky Mountain House, 121.
Ross, Alexander, 3, et passim.
Rowan, Mr., 121.
Running down a calf, 314.
Russell, Colonel, 264.
Russian America, 5.
Ruxton, George Frederick, 191, 193.
Missionary Explorers
Among the American
Indians
By MARY GAY HUMPHREYS
The stories of the first and greatest of American
missionaries to the American Indians, told largely in their own
words.