ebook img

Software Development with Go. Cloud-Native Programming using Golang with Linux and Docker PDF

381 Pages·2023·4.376 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Software Development with Go. Cloud-Native Programming using Golang with Linux and Docker

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 ISBN-13 (pbk): 978-1-4842-8730-9 ISBN-13 (electronic): 978-1-4842-8731-6 https://doi.org/10.1007/978-1-4842-8731-6 Copyright © 2023 by Nanik Tolaram This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director, Apress Media LLC: Welmoed Spahr Acquisitions Editor: James Robinson-Prior Development Editor: James Markham Coordinating Editor: Gryffin Winkler Copy Editor: Mary Behr Cover designed by eStudioCalamar Cover image designed by Freepik (www.freepik.com) Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights, please e-mail [email protected]. Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales web page at www.apress.com/bulk-sales. Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub (https://github.com/Apress). For more detailed information, please visit www.apress.com/source-code. Printed on acid-free paper I would like to dedicate this book to my late Dad who stood by me and encouraged me to write my very first book when I was 17 years old. To my dearest Mum who always supported me in pursuing my dreams and encouraged me to keep on going no matter what life brings. To my beautiful wife and best friend for allowing me the time to write the book and supporting me in every step of our life. To both my sons, Rahul and Manav, for allowing me to spend time in front of the computer on weekends to chase my dream and passion. Last but not least, to God for giving me this life and opportunity to be where I am in this world. Table of Contents About the Author �������������������������������������������������������������������������������xiii About the Technical Reviewer ������������������������������������������������������������xv Acknowledgments ����������������������������������������������������������������������������xvii Introduction ���������������������������������������������������������������������������������������xix Part I: System Programming ������������������������������������������������1 Chapter 1: System Calls �����������������������������������������������������������������������3 Source Code ����������������������������������������������������������������������������������������������������������3 What Is a System Call? �����������������������������������������������������������������������������������������4 C System Call ��������������������������������������������������������������������������������������������������������5 sys/unix Package ��������������������������������������������������������������������������������������������������8 System Call in Go �������������������������������������������������������������������������������������������������������10 Summary�������������������������������������������������������������������������������������������������������������14 Chapter 2: System Calls Using Go�������������������������������������������������������15 Source Code ��������������������������������������������������������������������������������������������������������15 Syscall Package ��������������������������������������������������������������������������������������������������16 syscall Application �����������������������������������������������������������������������������������������������������16 Checking Disk Space �������������������������������������������������������������������������������������������������18 Webserver with syscall ����������������������������������������������������������������������������������������������20 v Table of ConTenTs ELF Package��������������������������������������������������������������������������������������������������������22 High-Level ELF Format ����������������������������������������������������������������������������������������������23 Dump Example ����������������������������������������������������������������������������������������������������������24 /sys Filesystem ���������������������������������������������������������������������������������������������������28 Reading AppArmor �����������������������������������������������������������������������������������������������������29 Summary�������������������������������������������������������������������������������������������������������������31 Chapter 3: Accessing proc File System ����������������������������������������������33 Source Code ��������������������������������������������������������������������������������������������������������33 Peeking Inside procfs ������������������������������������������������������������������������������������������34 Reading Memory Information �����������������������������������������������������������������������������38 Peeking Network Information ������������������������������������������������������������������������������������41 Using the procfs Library ��������������������������������������������������������������������������������������44 Code Sample �������������������������������������������������������������������������������������������������������������44 Inside the procfs Library ��������������������������������������������������������������������������������������������46 Summary�������������������������������������������������������������������������������������������������������������48 Part II: Containers ���������������������������������������������������������������49 Chapter 4: Simple Containers �������������������������������������������������������������51 Linux Namespace ������������������������������������������������������������������������������������������������51 cgroups ���������������������������������������������������������������������������������������������������������������55 rootfs �������������������������������������������������������������������������������������������������������������������58 Gontainer Project ������������������������������������������������������������������������������������������������63 Summary�������������������������������������������������������������������������������������������������������������70 vi Table of ConTenTs Chapter 5: Containers with Networking ���������������������������������������������71 Source Code ��������������������������������������������������������������������������������������������������������71 Network Namespace �������������������������������������������������������������������������������������������������72 Setting Up with the ip Tool �����������������������������������������������������������������������������������������74 Containers with Networks �����������������������������������������������������������������������������������������79 Summary �������������������������������������������������������������������������������������������������������������������88 Chapter 6: Docker Security �����������������������������������������������������������������89 Source Code ��������������������������������������������������������������������������������������������������������89 seccomp Profiles ������������������������������������������������������������������������������������������������89 libseccomp ����������������������������������������������������������������������������������������������������������������91 Docker seccomp ��������������������������������������������������������������������������������������������������������95 Docker Proxy �������������������������������������������������������������������������������������������������������98 Container Attack Surface ����������������������������������������������������������������������������������105 Summary�����������������������������������������������������������������������������������������������������������106 Part III: Application Security ��������������������������������������������109 Chapter 7: Gosec and AST ����������������������������������������������������������������111 Source Code ������������������������������������������������������������������������������������������������������111 Abstract Syntax Tree �����������������������������������������������������������������������������������������111 Modules �������������������������������������������������������������������������������������������������������������������115 Sample Code �����������������������������������������������������������������������������������������������������������116 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 Part IV: Networking ����������������������������������������������������������161 Chapter 9: Simple Networking ����������������������������������������������������������163 Source Code ������������������������������������������������������������������������������������������������������163 TCP Networking ������������������������������������������������������������������������������������������������163 TCP Client ����������������������������������������������������������������������������������������������������������������164 TCP Server ���������������������������������������������������������������������������������������������������������������166 UDP Networking ������������������������������������������������������������������������������������������������168 UDP Client ����������������������������������������������������������������������������������������������������������������169 UDP Server ��������������������������������������������������������������������������������������������������������������172 Concurrent Servers ��������������������������������������������������������������������������������������������������174 Load Testing ������������������������������������������������������������������������������������������������������175 Summary�����������������������������������������������������������������������������������������������������������179 Chapter 10: System Networking �������������������������������������������������������181 Source Code ������������������������������������������������������������������������������������������������������181 Ping Utility ���������������������������������������������������������������������������������������������������������181 Code Walkthrough ���������������������������������������������������������������������������������������������������182 viii Table of ConTenTs DNS Server ��������������������������������������������������������������������������������������������������������188 Running a DNS Server ���������������������������������������������������������������������������������������������188 DNS Forwarder ��������������������������������������������������������������������������������������������������������189 Pack and Unpack �����������������������������������������������������������������������������������������������������193 Summary�����������������������������������������������������������������������������������������������������������196 Chapter 11: Google gopacket ������������������������������������������������������������199 Source Code ������������������������������������������������������������������������������������������������������199 gopacket �����������������������������������������������������������������������������������������������������������200 Layer ������������������������������������������������������������������������������������������������������������������������200 Packet ����������������������������������������������������������������������������������������������������������������������204 Using gopacket �������������������������������������������������������������������������������������������������205 pcap �������������������������������������������������������������������������������������������������������������������������205 Networking Sniffer ��������������������������������������������������������������������������������������������������206 Capturing With BPF ��������������������������������������������������������������������������������������������������217 Summary�����������������������������������������������������������������������������������������������������������222 Chapter 12: Epoll Library ������������������������������������������������������������������223 Source Code ������������������������������������������������������������������������������������������������������224 Understanding epoll ������������������������������������������������������������������������������������������224 epoll in Golang ��������������������������������������������������������������������������������������������������226 Epoll Registration ����������������������������������������������������������������������������������������������������227 Epoll Wait �����������������������������������������������������������������������������������������������������������������229 Epoll Library ������������������������������������������������������������������������������������������������������232 Summary�����������������������������������������������������������������������������������������������������������235 ix Table of ConTenTs Part V: Securing Linux ������������������������������������������������������239 Chapter 13: Vulnerability Scanner ����������������������������������������������������239 Source Code ������������������������������������������������������������������������������������������������������239 Vulnerability Scanners ��������������������������������������������������������������������������������������240 Using Vuls����������������������������������������������������������������������������������������������������������241 Checking Out the Code ��������������������������������������������������������������������������������������������241 Running Scan ����������������������������������������������������������������������������������������������������������243 Learning From Vuls �������������������������������������������������������������������������������������������248 Port Scan �����������������������������������������������������������������������������������������������������������������248 Exec �������������������������������������������������������������������������������������������������������������������������255 SQLite ����������������������������������������������������������������������������������������������������������������������257 Summary�����������������������������������������������������������������������������������������������������������263 Chapter 14: CrowdSec ����������������������������������������������������������������������265 Source Code ������������������������������������������������������������������������������������������������������265 CrowdSec Project ����������������������������������������������������������������������������������������������266 Using CrowdSec ������������������������������������������������������������������������������������������������������266 crowdsec�db ������������������������������������������������������������������������������������������������������������270 Learning From CrowdSec����������������������������������������������������������������������������������273 System Signal Handling �������������������������������������������������������������������������������������������274 Handling Service Dependencies ������������������������������������������������������������������������������280 GeoIP Database �������������������������������������������������������������������������������������������������������285 Summary�����������������������������������������������������������������������������������������������������������290 x

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.