Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Make Action work without old summary path #18

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
GOPATH: ${{ runner.workspace }}
GO111MODULE: "on"
# Checkout to your repo
- name: Checkout
- name: Checkout to your repo
uses: actions/checkout@v3
# Install k3s cluster(You can setup a k8s cluster here)
- name: Setup k3s cluster
Expand Down Expand Up @@ -95,25 +95,32 @@ jobs:
with:
name: ${{ steps.visualisation.outputs.visualisation-results-artifact }}
path: images
# Checkout to fork repo
- name: Checkout to fork repo
uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
# Store the latest summary report file
- name: Store the latest summary report file
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
deploy_key: ${{ secrets.PERSONAL_TOKEN }}
external_repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
publish_dir: ./summary_reports
keep_files: true
# Store the visualisation results
- name: Store the visualisation results
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
deploy_key: ${{ secrets.PERSONAL_TOKEN }}
external_repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
publish_dir: ./images
keep_files: true
# Comment the visualisation results on the PR
- name: Comment on PR
run: |
gh pr comment ${{ github.event.number }} -b"![system_graph](https://raw.githubusercontent.com/${{ github.repository }}/gh-pages/${{ steps.visualisation.outputs.sys-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![network_graph](https://raw.githubusercontent.com/${{ github.repository }}/gh-pages/${{ steps.visualisation.outputs.network-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![system_graph](https://raw.githubusercontent.com/${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}/gh-pages/${{ steps.visualisation.outputs.sys-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![network_graph](https://raw.githubusercontent.com/${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}/gh-pages/${{ steps.visualisation.outputs.network-visualisation-image }})"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Delete the new app
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
GOPATH: ${{ runner.workspace }}
GO111MODULE: "on"
# Checkout to your repo
- name: Checkout
- name: Checkout to your repo
uses: actions/checkout@v3
# Install k3s cluster(You can setup a k8s cluster here)
- name: Setup k3s cluster
Expand Down Expand Up @@ -157,25 +157,32 @@ jobs:
with:
name: ${{ steps.visualisation.outputs.visualisation-results-artifact }}
path: images
# Checkout to fork repo
- name: Checkout to fork repo
uses: actions/checkout@v3
with:
repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
# Store the latest summary report file
- name: Store the latest summary report file
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
deploy_key: ${{ secrets.PERSONAL_TOKEN }}
external_repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
publish_dir: ./summary_reports
keep_files: true
# Store the visualisation results
- name: Store the visualisation results
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
deploy_key: ${{ secrets.PERSONAL_TOKEN }}
external_repository: ${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}
publish_dir: ./images
keep_files: true
# Comment the visualisation results on the PR
- name: Comment on PR
run: |
gh pr comment ${{ github.event.number }} -b"![system_graph](https://raw.githubusercontent.com/${{ github.repository }}/gh-pages/${{ steps.visualisation.outputs.sys-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![network_graph](https://raw.githubusercontent.com/${{ github.repository }}/gh-pages/${{ steps.visualisation.outputs.network-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![system_graph](https://raw.githubusercontent.com/${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}/gh-pages/${{ steps.visualisation.outputs.sys-visualisation-image }})"
gh pr comment ${{ github.event.number }} -b"![network_graph](https://raw.githubusercontent.com/${{ github.event.pull_request.user.login }}/${{ github.event.repository.name }}/gh-pages/${{ steps.visualisation.outputs.network-visualisation-image }})"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Delete the new app
Expand Down
23 changes: 9 additions & 14 deletions cmd/visual/cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,20 @@ var networkCmd = &cobra.Command{
Short: "network subcommand is a command to visualization network connection behaviors differences.",
Example: "visual network --old [old json file name] --new [new json file name] -app [app name] -o [png file name]",
Run: func(cmd *cobra.Command, args []string) {
a := cmd.Flags().Changed("old")
a := cmd.Flags().Changed("new")
if a == false {
klog.Fatalf("Error: 'old' flag is not set")
}
b := cmd.Flags().Changed("new")
if b == false {
klog.Fatalf("Error: 'new' flag is not set")
}

fmt.Println("old file:", oldFile)
// Check is URL
var err error
if !utils.CheckIsURL(oldFile) {
oldFile, err = filepath.Abs(oldFile)
if err != nil {
klog.Fatalf("Error: getting absolute path of 'oldFile' flag: %v", err)
if oldFile != "" {
fmt.Println("old file:", oldFile)
// Check is URL
if !utils.CheckIsURL(oldFile) {
oldFile, err = filepath.Abs(oldFile)
if err != nil {
klog.Fatalf("Error: getting absolute path of 'oldFile' flag: %v", err)
}
}
}

Expand Down Expand Up @@ -63,9 +61,6 @@ func init() {
flags.StringVarP(&appName, "app", "", "", "filter app name, if you want to visualize specific app")
flags.StringVarP(&netOutput, "output", "o", "net.png", "output image file name")

if err := networkCmd.MarkPersistentFlagRequired("old"); err != nil {
klog.Fatalf("Error: marking 'old' flag as required: %v", err)
}
if err := networkCmd.MarkPersistentFlagRequired("new"); err != nil {
klog.Fatalf("Error: marking 'new' flag as required: %v", err)
}
Expand Down
48 changes: 42 additions & 6 deletions pkg/visualisation/visualisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/kubearmor/kubearmor-action/utils"
exe "github.com/kubearmor/kubearmor-action/utils/exec"
osi "github.com/kubearmor/kubearmor-action/utils/os"
"github.com/sethvargo/go-githubactions"
"k8s.io/klog"
)

Expand Down Expand Up @@ -149,7 +150,7 @@ func ConvertVndToPlantUML(vnd *VisualNetworkData, appName string) error {
}
for _, ip := range ips {
color := "Lightblue"
if strings.Contains(ip, appName) {
if appName != "" && strings.Contains(ip, appName) {
color = "Orange"
}
_, err = file.WriteString(fmt.Sprintf("[%s] #%s\n", ip, color))
Expand Down Expand Up @@ -207,6 +208,38 @@ func ParseNetworkData(sdOlds, sdNews []*SummaryData, appName string) *VisualNetw

vn := &VisualNetworkData{}
vn.NsIps = make(map[string][]string)
// if sdOlds == nil || len(sdOlds) == 0
if sdOlds == nil || len(sdOlds) == 0 {
for _, sdNew := range sdNews {
// Get Namespace Labels
getNsIps(sdNew, nsipsNew)
// Get Different Network Connections
getDiffConnectionData(sdNew, cdsNew, appName)
}

// merge the connections
for k, v := range cdsNew {
// unchanged
if v.kind == 0 {
edge := fmt.Sprintf("[%s] -[#%s]-> [%s] : %s/%s\n", k.src, v.edgeColor, k.dst, k.protocol, k.port)
vn.Connections = append(vn.Connections, edge)
}
// add ips to the ips map, to filter nsips
ips[k.src] = true
ips[k.dst] = true
}

// filter nsips by ips
for ns, ipss := range nsipsNew {
for _, ip := range ipss {
if _, ok := ips[ip]; ok {
vn.NsIps[ns] = append(vn.NsIps[ns], ip)
}
}
}
return vn
}
// if sdOlds != nil && len(sdOlds) != 0
for _, sdOld := range sdOlds {
// Get Namespace Labels
getNsIps(sdOld, nsipsOld)
Expand Down Expand Up @@ -431,12 +464,15 @@ func ConvertNetworkJSONToImage(jsonFileOld string, jsonFileNew string, output st
}

// get old summary data from old json file
klog.Infoln("Parsing Old Summary Data...")
sdOlds := ParseSummaryData(jsonFileOld)
if sdOlds == nil {
return fmt.Errorf("Error: Old SummaryData is nil")
var sdOlds []*SummaryData
if jsonFileOld != "" {
klog.Infoln("Parsing Old Summary Data...")
sdOlds = ParseSummaryData(jsonFileOld)
// handle nil
if sdOlds == nil {
githubactions.Warningf("Warning: Old summary report file path is invalid!")
}
}

// get new summary data from new json file
klog.Infoln("Parsing New Summary Data...")
sdNews := ParseSummaryData(jsonFileNew)
Expand Down
8 changes: 8 additions & 0 deletions utils/urlfile/urlfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/http"
"net/url"
"time"

"k8s.io/klog"
)

// URLVisitor downloads the contents of a URL, and if successful, returns
Expand Down Expand Up @@ -82,6 +84,12 @@ func ReadJSONFromURL(url string) ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
// recover from panic if one occured.
if r := recover(); r != nil {
klog.Warningf("Recovered in ReadJSONFromURL: %v\n", r)
}
}()
defer body.Close()
data, err := io.ReadAll(body)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ func ReadFile(address string) ([]byte, error) {

// CheckIsURL checks if the given address is a url
func CheckIsURL(address string) bool {
// Check if the address is a valid url
_, err := url.ParseRequestURI(address)
if err != nil {
return false
}
// Parse the address as a url
u, err := url.Parse(address)
if err != nil {
Expand Down