云原生安全检测器 Narrows(CNSI)的部署和使用

近日, 云原生安全检测器 Narrows(Cloud Native Security Inspector,简称CNSI)发布了0.2.0版本。
(https://github.com/vmware-tanzu/cloud-native-security-inspector)

此项目旨在对K8s集群中的工作负载进行动态(运行时)的安全检测,并报告安全问题,从而弥补了现有静态安全扫描技术的不足之处。

Harbor等云原生镜像仓库提供了静态扫描能力,例如镜像CVE扫描,覆盖的范围主要是镜像的应用层。当镜像被部署到K8s集群后,在不同的配置条件下可能会出现新的漏洞。Narrows可以探测到这部分漏洞并报告给用户,而且可以采取相应的措施(如网络隔离等)减少漏洞的影响。

本文的目的在于使用一个简单的Demo来指导用户在自己的K8s集群中快速部署Narrows(CNSI)。我们将使用Minikube来部署一个简单的K8s集群,部署一个Prometheus工作荷载,并使用CNSI来检查这个Prometheus负载的风险。

前置条件

1.需要准备一台Linux机器,本文中我们使用了一台“CentOS Linux release 7.9.2009 (Core)”型号的机器。

2.我们的机器拥有32核CPU和64GB内存,此配置不代表实现demo的最小配置。

3.在机器上需要安装好Docker, 参考https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-centos-7

4.需要已经有一个版本为2.5.0以上的Harbor(https://goharbor.io/)实例。并且已经配置好Harbor的安全扫描功能(https://goharbor.io/docs/main/administration/vulnerability-scanning/)。CNSI会利用Harbor的安全扫描功能。

使用Minikube准备K8s集群

部署一个简单的K8s集群有多种方式,例如使用Kind或者使用kubeadm。在本文中我们使用Minikube来部署一个单节点的K8s集群。

执行下面三行命令,即可在Linux机器上部署K8s v1.23.0。

在这里插入图片描述

验证K8s集群部署成功:

在这里插入图片描述

在K8s集群上部署CNSI

安装依赖程序

执行以下四行命令来安装git, wget, gcc和helm:

在这里插入图片描述

在K8s集群上部署CNSI

执行以下三行命令来部署CNSI:

在这里插入图片描述</

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是使用Faster R-CNN获取图像中交通标志位置的代码: ```python # 导入必要的库 import torch import torchvision import cv2 import numpy as np import matplotlib.pyplot as plt # 加载训练好的 Faster R-CNN 模型 model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) # 设置为评估模式 model.eval() # 定义标签和颜色 labels = [ 'speed limit 20', 'speed limit 30', 'speed limit 50', 'speed limit 60', 'speed limit 70', 'speed limit 80', 'end of speed limit 80', 'speed limit 100', 'speed limit 120', 'no passing', 'no passing for vehicles over 3.5 metric tons', 'right-of-way at the next intersection', 'priority road', 'yield', 'stop', 'no vehicles', 'vehicles over 3.5 metric tons prohibited', 'no entry', 'general caution', 'dangerous curve to the left', 'dangerous curve to the right', 'double curve', 'bumpy road', 'slippery road', 'road narrows on the right', 'road work', 'traffic signals', 'pedestrians', 'children crossing', 'bicycles crossing', 'beware of ice/snow', 'wild animals crossing', 'end of all speed and passing limits', 'turn right ahead', 'turn left ahead', 'ahead only', 'go straight or right', 'go straight or left', 'keep right', 'keep left', 'roundabout mandatory', 'end of no passing', 'end of no passing by vehicles over 3.5 metric tons' ] colors = np.random.uniform(0, 255, size=(len(labels), 3)) # 加载测试图像 image = cv2.imread('test.jpg') # 转换为 PyTorch 张量 image = torch.from_numpy(image / 255.0).float().permute(2, 0, 1) # 将图像放入模型中进行预测 with torch.no_grad(): predictions = model([image]) # 获取预测结果 boxes = predictions[0]['boxes'] scores = predictions[0]['scores'] labels = predictions[0]['labels'] # 设置阈值,过滤掉低置信度的预测结果 threshold = 0.5 boxes = boxes[scores >= threshold] labels = labels[scores >= threshold] # 将预测结果绘制在图像上 image = cv2.imread('test.jpg') for box, label in zip(boxes, labels): x1, y1, x2, y2 = box.cpu().numpy().astype(np.int32) color = colors[label] cv2.rectangle(image, (x1, y1), (x2, y2), color, 2) cv2.putText(image, labels[label], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2) # 显示图像 plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.show() ``` 接下来是使用 ResNet 提取交通标志区域特征和使用卷积神经网络实现交通标志分类的代码: ```python # 导入必要的库 import torch import torchvision import cv2 import numpy as np import matplotlib.pyplot as plt # 加载训练好的 Faster R-CNN 模型 model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True) # 设置为评估模式 model.eval() # 加载训练好的交通标志分类模型 classifier = torchvision.models.resnet18(pretrained=True) classifier.fc = torch.nn.Linear(classifier.fc.in_features, 43) classifier.load_state_dict(torch.load('classifier.pth')) classifier.eval() # 定义标签和颜色 labels = [ 'speed limit 20', 'speed limit 30', 'speed limit 50', 'speed limit 60', 'speed limit 70', 'speed limit 80', 'end of speed limit 80', 'speed limit 100', 'speed limit 120', 'no passing', 'no passing for vehicles over 3.5 metric tons', 'right-of-way at the next intersection', 'priority road', 'yield', 'stop', 'no vehicles', 'vehicles over 3.5 metric tons prohibited', 'no entry', 'general caution', 'dangerous curve to the left', 'dangerous curve to the right', 'double curve', 'bumpy road', 'slippery road', 'road narrows on the right', 'road work', 'traffic signals', 'pedestrians', 'children crossing', 'bicycles crossing', 'beware of ice/snow', 'wild animals crossing', 'end of all speed and passing limits', 'turn right ahead', 'turn left ahead', 'ahead only', 'go straight or right', 'go straight or left', 'keep right', 'keep left', 'roundabout mandatory', 'end of no passing', 'end of no passing by vehicles over 3.5 metric tons' ] colors = np.random.uniform(0, 255, size=(len(labels), 3)) # 加载测试图像 image = cv2.imread('test.jpg') # 转换为 PyTorch 张量 image = torch.from_numpy(image / 255.0).float().permute(2, 0, 1) # 将图像放入模型中进行预测 with torch.no_grad(): predictions = model([image]) # 获取预测结果 boxes = predictions[0]['boxes'] scores = predictions[0]['scores'] labels = predictions[0]['labels'] # 设置阈值,过滤掉低置信度的预测结果 threshold = 0.5 boxes = boxes[scores >= threshold] labels = labels[scores >= threshold] # 遍历每个交通标志区域,提取特征并进行分类 image = cv2.imread('test.jpg') for box in boxes: x1, y1, x2, y2 = box.cpu().numpy().astype(np.int32) roi = image[y1:y2, x1:x2] roi = cv2.resize(roi, (32, 32)) roi = torch.from_numpy(roi / 255.0).float().permute(2, 0, 1) with torch.no_grad(): features = classifier(roi.unsqueeze(0)) probs = torch.softmax(features, dim=1)[0] label = torch.argmax(probs) color = colors[label] cv2.rectangle(image, (x1, y1), (x2, y2), color, 2) cv2.putText(image, labels[label], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, color, 2) # 显示图像 plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.show() ``` 注意,上述代码中的 `classifier.pth` 是训练好的交通标志分类模型的权重文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值