Skip to content

Commit 84c91f5

Browse files
authored
Merge pull request #40 from nyrahul/main
GAR container img bulk scan
2 parents d7d2818 + c34f2cd commit 84c91f5

File tree

8 files changed

+168
-69
lines changed

8 files changed

+168
-69
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
.venv
55
.ks
66
.kscache
7+
POLDUMP

api-samples/checkNodeEnrollment.sh

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
# To set .accuknox.cfg check https://github.com/accuknox/tools/tree/main/api-samples
44
. ${ACCUKNOX_CFG:-~/.accuknox.cfg}
5+
. util.sh
56

67
# Other params
7-
TMP=/tmp/$(basename $0).$$
8+
TMP=$DIR/$(basename $0)
89
clusterspec=".*" # regex for cluster names
910

1011
get_node_list()
1112
{
12-
curl $CURLOPTS "$CWPP_URL/cm/api/v1/cluster-management/nodes-in-cluster" \
13-
-H "authorization: Bearer $TOKEN" \
14-
-H 'content-type: application/json' \
15-
-H "x-tenant-id: $TENANT_ID" \
16-
--data-raw "{\"workspace_id\":$TENANT_ID,\"cluster_id\":[$cid],\"from_time\":[],\"to_time\":[]}" | jq -r '.result[].NodeName' > $TMP
13+
data_raw="{\"workspace_id\":$TENANT_ID,\"cluster_id\":[$cid],\"from_time\":[],\"to_time\":[]}"
14+
ak_api "$CWPP_URL/cm/api/v1/cluster-management/nodes-in-cluster"
15+
echo $json_string | jq -r '.result[].NodeName' > $TMP
1716
cat <<EOH
1817
{
1918
"Cluster": "$cname",
@@ -31,32 +30,19 @@ EOH
3130

3231
get_cluster_id()
3332
{
33+
ak_api "$CWPP_URL/cluster-onboarding/api/v1/get-onboarded-clusters?wsid=$TENANT_ID"
3434
while read cline; do
3535
cid=${cline/ */}
3636
cname=${cline/* /}
3737
[[ ! $cname =~ $clusterspec ]] && echo "ignoring cluster [$cname] ..." && continue
3838
get_node_list
39-
done < <(curl $CURLOPTS "$CWPP_URL/cluster-onboarding/api/v1/get-onboarded-clusters?wsid=$TENANT_ID" \
40-
-H 'accept: */*' \
41-
-H "authorization: Bearer $TOKEN" \
42-
-H 'content-type: application/json' \
43-
-H "x-tenant-id: $TENANT_ID" | jq -r '.[] | "\(.ID) \(.ClusterName)"')
39+
done < <(echo $json_string | jq -r '.[] | "\(.ID) \(.ClusterName)"')
4440
}
4541

46-
function cleanup {
47-
rm -f $TMP
48-
}
49-
prereq()
50-
{
51-
command -v jq >/dev/null 2>&1 || { echo >&2 "require 'jq' to be installed. Aborting."; exit 1; }
52-
}
53-
54-
trap cleanup EXIT
55-
5642
main()
5743
{
58-
prereq
59-
get_cluster_id | jq .
44+
ak_prereq
45+
get_cluster_id
6046
}
6147

6248
# Processing starts here

api-samples/policyDump.sh

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22

33
# To set .accuknox.cfg check https://github.com/accuknox/tools/tree/main/api-samples
44
. ${ACCUKNOX_CFG:-~/.accuknox.cfg}
5+
. util.sh
56

67
# Other params
78
clusterspec=".*" # regex for cluster name for whom to dump the policies
8-
TMP=/tmp/$(basename $0).$$
9+
TMP=$DIR/$(basename $0).$$
910
OUT="POLDUMP" # output directory where the policies will be dumped
1011

1112
dump_policy_file()
1213
{
13-
policy_id=$1
14-
curl $CURLOPTS "$CWPP_URL/policymanagement/v2/policy/$policy_id" \
15-
-H "Authorization: Bearer $TOKEN" \
16-
-H 'Content-Type: application/json' \
17-
-H "X-Tenant-Id: $TENANT_ID" | jq -r .yaml > $polpath
18-
[[ $? -ne 0 ]] && echo "could not get policy with ID=[$policy_id]" && return
14+
ak_api "$CWPP_URL/policymanagement/v2/policy/$1"
15+
echo $json_string | jq -r .yaml > $polpath
16+
[[ $? -ne 0 ]] && echo "could not get policy with ID=[$1]" && return
1917
}
2018

2119
get_policy_list()
@@ -25,6 +23,8 @@ get_policy_list()
2523
pgnext=$(($pgprev + $polperpage))
2624
echo "fetching policies $pgprev to $pgnext ..."
2725
cnt=0
26+
data_raw="{\"workspace_id\":$TENANT_ID,\"workload\":\"k8s\",\"page_previous\":$pgprev,\"page_next\":$pgnext,\"filter\":{\"cluster_id\":[$1],\"namespace_id\":[],\"workload_id\":[],\"kind\":[],\"node_id\":[],\"pod_id\":[],\"type\":[],\"status\":[],\"tags\":[],\"name\":{\"regex\":[]},\"tldr\":{\"regex\":[]}}}"
27+
ak_api "$CWPP_URL/policymanagement/v2/list-policy"
2828
while read pline; do
2929
((cnt++))
3030
arr=($pline)
@@ -33,17 +33,14 @@ get_policy_list()
3333
polpath=$poldir/${arr[1]}.yaml
3434
echo $polpath
3535
dump_policy_file ${arr[0]}
36-
done < <(curl $CURLOPTS "$CWPP_URL/policymanagement/v2/list-policy" \
37-
-H "Authorization: Bearer $TOKEN" \
38-
-H 'Content-Type: application/json' \
39-
-H "X-Tenant-Id: $TENANT_ID" \
40-
--data-raw "{\"workspace_id\":$TENANT_ID,\"workload\":\"k8s\",\"page_previous\":$pgprev,\"page_next\":$pgnext,\"filter\":{\"cluster_id\":[$1],\"namespace_id\":[],\"workload_id\":[],\"kind\":[],\"node_id\":[],\"pod_id\":[],\"type\":[],\"status\":[],\"tags\":[],\"name\":{\"regex\":[]},\"tldr\":{\"regex\":[]}}}" | jq -r '.list_of_policies[] | "\(.policy_id) \(.name) \(.namespace_name)"')
36+
done < <(echo $json_string | jq -r '.list_of_policies[] | "\(.policy_id) \(.name) \(.namespace_name)"')
4137
[[ $cnt -lt $polperpage ]] && break
4238
done
4339
}
4440

4541
get_cluster_id()
4642
{
43+
ak_api "$CWPP_URL/cluster-onboarding/api/v1/get-onboarded-clusters?wsid=$TENANT_ID"
4744
while read cline; do
4845
cid=${cline/ */}
4946
cname=${cline/* /}
@@ -52,27 +49,12 @@ get_cluster_id()
5249
mkdir $cpath 2>/dev/null
5350
echo "fetching policies for cluster [$cname] ..."
5451
get_policy_list $cid
55-
done < <(curl $CURLOPTS "$CWPP_URL/cluster-onboarding/api/v1/get-onboarded-clusters?wsid=$TENANT_ID" \
56-
-H 'accept: */*' \
57-
-H "authorization: Bearer $TOKEN" \
58-
-H 'content-type: application/json' \
59-
-H "x-tenant-id: $TENANT_ID" | jq -r '.[] | "\(.ID) \(.ClusterName)"')
60-
}
61-
62-
function cleanup {
63-
rm -rf $TMP 2>/dev/null
64-
}
65-
trap cleanup EXIT
66-
67-
init()
68-
{
69-
mkdir -p $TMP 2>/dev/null
70-
mkdir -p $OUT 2>/dev/null
52+
done < <(echo $json_string | jq -r '.[] | "\(.ID) \(.ClusterName)"')
7153
}
7254

7355
main()
7456
{
75-
init
57+
mkdir -p $OUT 2>/dev/null
7658
get_cluster_id
7759
}
7860

api-samples/ticketReport.sh

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,22 @@
22

33
# To set .accuknox.cfg check https://github.com/accuknox/tools/tree/main/api-samples
44
. ${ACCUKNOX_CFG:-~/.accuknox.cfg}
5-
6-
# Other params
7-
TMP=/tmp/$$
5+
. util.sh
86

97
get_ticket_details()
108
{
11-
curl $CURLOPTS "$CSPM_URL/api/v1/tickets/$1" \
12-
-H 'accept: */*' \
13-
-H "authorization: Bearer $TOKEN" \
14-
-H 'content-type: application/json' | jq .
9+
ak_api "$CSPM_URL/api/v1/tickets/$1"
10+
echo $json_string | jq .
1511
}
1612

1713
get_ticket_list()
1814
{
15+
ak_api "$CSPM_URL/api/v1/tickets?page=1&page_size=20&status=Opened&created_before=2024-11-13&created_after=2024-11-05"
1916
while read line; do
2017
echo $line
2118
get_ticket_details $line
22-
done < <(curl $CURLOPTS "$CSPM_URL/api/v1/tickets?page=1&page_size=20&status=Opened&created_before=2024-11-13&created_after=2024-11-05" \
23-
-H 'accept: */*' \
24-
-H "authorization: Bearer $TOKEN" \
25-
-H 'content-type: application/json' | jq -r '.results[].id')
26-
}
27-
28-
29-
function cleanup {
30-
rm -f $TMP 2>/dev/null
19+
done < <(echo $json_string | jq -r '.results[].id')
3120
}
32-
trap cleanup EXIT
3321

3422
main()
3523
{

api-samples/util.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
DIR=/tmp/$$
4+
5+
ak_api()
6+
{
7+
apiverbosity=${API_VERBOSE:-0}
8+
[[ $apiverbosity -gt 0 ]] && echo "API: [$1]"
9+
unset apicmd
10+
unset json_string
11+
read -r -d '' apicmd << EOH
12+
curl $CURLOPTS "$1" \
13+
-H "authorization: Bearer $TOKEN" \
14+
-H 'content-type: application/json' \
15+
-H "x-tenant-id: $TENANT_ID"
16+
EOH
17+
if [ "$data_raw" != "" ]; then
18+
apicmd="$apicmd --data-raw '$data_raw'"
19+
fi
20+
[[ $apiverbosity -gt 1 ]] && echo "$apicmd"
21+
json_string=`eval "$apicmd"`
22+
if ! jq -e . >/dev/null 2>&1 <<<"$json_string"; then
23+
echo "API call failed: [$json_string]"
24+
exit 1
25+
fi
26+
[[ $apiverbosity -gt 1 ]] && echo "$json_string"
27+
unset data_raw
28+
}
29+
30+
ak_prereq()
31+
{
32+
[[ "$DIR" != "" ]] && mkdir -p $DIR
33+
command -v jq >/dev/null 2>&1 || { echo >&2 "require 'jq' to be installed. Aborting."; exit 1; }
34+
}
35+
36+
function ak_cleanup {
37+
[[ "$DIR" != "" ]] && rm -rf $DIR
38+
}
39+
40+
trap ak_cleanup EXIT
41+

imgscan/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM accuknox/accuknox-container-scan:latest
2+
WORKDIR /home/ak
3+
RUN apk add jq curl bash python3
4+
5+
RUN curl https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz > /tmp/google-cloud-sdk.tar.gz
6+
7+
# Installing the package
8+
RUN mkdir -p /usr/local/gcloud \
9+
&& tar -C /usr/local/gcloud -xvf /tmp/google-cloud-sdk.tar.gz \
10+
&& /usr/local/gcloud/google-cloud-sdk/install.sh
11+
12+
COPY imagescan.sh .
13+
RUN chmod +x /home/ak/imagescan.sh
14+
ENV PATH=$PATH:/usr/local/gcloud/google-cloud-sdk/bin
15+
16+
ENTRYPOINT ["/home/ak/imagescan.sh"]

imgscan/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Bulk scan of GAR registry locally
2+
3+
GAR Prerequisites:
4+
1. **REGISTRY**: Full registry path for GAR
5+
2. **SA_EMAIL**: [Service Account Email](../res/gcp-service-account.png)
6+
3. Service Account Json: File containing the creds
7+
4. **IMGSPEC**: Regular expression for images to scan/upload-results. E.g. `.*:latest` => scan all the images having `latest` tag. Sample image name:`us-east1-docker.pkg.dev/kube-airgapped/accuknox-onprem/nginx:foobar`
8+
9+
AccuKnox Prerequisites:
10+
1. **LABEL**: [AccuKnox Label](https://help.accuknox.com/how-to/how-to-create-labels/)
11+
2. **TENANT**: [Tenant ID](https://help.accuknox.com/how-to/how-to-create-tokens/)
12+
3. **TOKEN**: [AccuKnox Token](https://help.accuknox.com/how-to/how-to-create-tokens/)
13+
4. **AKURL**: cspm.demo.accuknox.com
14+
15+
Scan images with tags `foobar`.
16+
```bash
17+
docker run -eIMGSPEC=".*:foobar$" \
18+
-eREGISTRY=us-east1-docker.pkg.dev/kube-airgapped/accuknox-onprem \
19+
-e"SA_EMAIL=<service-account-email>" \
20+
-eLABEL=labeltmp \
21+
-eTENANT=4093 \
22+
-eTOKEN=<get token> \
23+
-eAKURL=cspm.demo.accuknox.com \
24+
-v$PWD/service_account.json:/home/ak/service_account.json \
25+
-it accuknox/accuknox-container-scan:bulk
26+
```
27+
28+
## Jenkins Script
29+
30+
```
31+
pipeline {
32+
agent any
33+
environment {
34+
SA_FILE = credentials('SA_FILE')
35+
TOKEN = credentials('TOKEN')
36+
SA_EMAIL = credentials('SA_EMAIL')
37+
}
38+
stages {
39+
stage('Accuknox') {
40+
steps {
41+
script {
42+
sh 'echo "$SA_FILE" > service_account.json'
43+
44+
sh '''
45+
docker run -e IMGSPEC=".*:foobar$" \
46+
-e REGISTRY=us-east1-docker.pkg.dev/kube-airgapped/accuknox-onprem \
47+
48+
-e LABEL=mylabel \
49+
-e TENANT=4093 \
50+
-e TOKEN=$TOKEN \
51+
-e AKURL=cspm.demo.accuknox.com \
52+
-v "$PWD/service_account.json:/home/ak/service_account.json" \
53+
accuknox/accuknox-container-scan:bulk
54+
'''
55+
}
56+
}
57+
}
58+
}
59+
}
60+
```

imgscan/imagescan.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
SA_JSON="$(pwd)/service_account.json"
4+
[[ "$SA_EMAIL" == "" ]] && echo "SA_EMAIL / ServiceAccount Email not provided" && exit 1
5+
[[ "$AKURL" == "" ]] && echo "AKURL / Accuknox endpoint is not set" && exit 1
6+
[[ "$TENANT" == "" ]] && echo "TENANT / Tenant id is not set" && exit 1
7+
[[ "$LABEL" == "" ]] && echo "LABEL / Labels are not set" && exit 1
8+
[[ "$TOKEN" == "" ]] && echo "TOKEN / Auth token is not set" && exit 1
9+
[[ "$IMGSPEC" == "" ]] && echo "IMGSPEC token is not set" && exit 1
10+
[[ ! -f "$SA_JSON" ]] && echo "$SA_JSON file not found" && exit 1
11+
12+
export GOOGLE_APPLICATION_CREDENTIALS=$SA_JSON
13+
14+
#REGISTRY=${REGISTRY:-us-east1-docker.pkg.dev/kube-airgapped/accuknox-onprem}
15+
16+
gcloud auth activate-service-account $SA_EMAIL --key-file=$SA_JSON
17+
18+
for img in `gcloud artifacts docker images list "$REGISTRY" --include-tags --format=json | jq -r '.[] | "\(.package):\(.tags[])"' 2>/dev/null`; do
19+
[[ ! $img =~ $IMGSPEC ]] && echo -en "\nskipping image [$img] ...\n" && continue
20+
echo -en "\nscanning $img ...\n"
21+
rm -f report.json 2>/dev/null
22+
trivy image $img --format json --timeout 3600s -o report.json > report.log 2>&1
23+
[[ ! -f "report.json" ]] && echo "image scanning failed $img" && continue
24+
curl -L -X POST "https://$AKURL/api/v1/artifact/?tenant_id=$TENANT&data_type=TR&label_id=$LABEL&save_to_s3=false" -H "Tenant-Id: $TENANT" -H "Authorization: Bearer $TOKEN" --form 'file=@"./report.json"'
25+
done

0 commit comments

Comments
 (0)