一、简介
libveinmind是由长亭科技研发开源的可扩展容器安全 SDK。
问脉 (TM) SDK 提供了容器安全领域所关心的容器、镜像和运行时等对象的信息获取、镜像内容器访问等相关操作。并进行合理的抽象以提高容器安全工具对 Docker、Containerd 和 Kubernetes 等不同容器产品的兼容性,简化容器安全工具的开发和维护。
二、软件包依赖安装
Ubuntu 和 Debian 平台:
echo 'deb [trusted=yes] https://download.veinmind.tech/libveinmind/apt/ ./' | sudo tee /etc/apt/sources.list.d/libveinmind.list
sudo apt-get update
sudo apt-get install libveinmind-dev
RedHat 和 CentOS 平台:
sudo cat > /etc/yum.repos.d/libveinmind.repo << ==EOF==
[libveinmind]
name=libVeinMind SDK yum repository
baseurl=https://download.veinmind.tech/libveinmind/yum/
enabled=1
gpgcheck=0
==EOF==
sudo yum install libveinmind-devel
三、示例代码
笔者以GO版本SDK为例,展示适用该SDK获取容器镜像、运行时的相关容器信息。
首先,引入SDK依赖包:
import (
api "github.com/chaitin/libveinmind/go"
"github.com/chaitin/libveinmind/go/cmd"
"github.com/chaitin/libveinmind/go/plugin"
"github.com/chaitin/libveinmind/go/plugin/log"
)
接下来,添加命令函数:
func init() {
rootCmd.AddCommand(cmd.MapImageCommand(scanCmd, scan)) //容器镜像相关SDK函数
rootCmd.AddCommand(cmd.MapRuntimeCommand(runCmd, runtime)) //运行时相关SDK函数
rootCmd.AddCommand(cmd.NewInfoCommand(plugin.Manifest{
Name: "veinmind-example",
Author: "veinmind-team",
}))
}
我们添加了镜像以及运行时2个SDK的阶段函数。
以打印所有docker images id为例,scan函数应如下声明:
func scan(c *cmd.Command, image api.Image) error {
log.Info(image.ID())
return nil
}
以获取运行中的第一个CONTAINER的文件信息为例,runtime函数如下声明:
func runtime(c *cmd.Command, runtime api.Runtime) error {
ids,_ := runtime.ListImageIDs()
Aimage,_ := runtime.OpenImageByID(ids[0])
fileHandle ,_:= Aimage.Stat("/etc/passwd")
fmt.Println(fileHandle.Size(),fileHandle.Name())
return nil
}
可以看到,程序打印出了所有images ID ,以及运行中的第一个CONTAINER里`/etc/passwd`的文件信息。