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

Cypress astra local #1022

Merged
merged 52 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
16ecb6d
Merge pull request #581 from bcgov/dev
ikethecoder Sep 30, 2022
9759c11
Merge pull request #605 from bcgov/dev
ikethecoder Oct 19, 2022
d4be57f
Merge pull request #611 from bcgov/dev
ikethecoder Oct 19, 2022
7e4d1a9
Merge pull request #616 from bcgov/dev
ikethecoder Oct 25, 2022
671b218
Merge pull request #623 from bcgov/dev
ikethecoder Oct 29, 2022
ff834c2
Merge pull request #670 from bcgov/dev
ikethecoder Dec 30, 2022
cbcac10
Merge pull request #677 from bcgov/dev
ikethecoder Jan 3, 2023
67b941a
Merge pull request #681 from bcgov/dev
ikethecoder Jan 4, 2023
110e90f
Merge pull request #688 from bcgov/dev
ikethecoder Jan 6, 2023
c1b20be
Merge pull request #691 from bcgov/dev
ikethecoder Jan 6, 2023
d89cd92
Merge pull request #704 from bcgov/dev
ikethecoder Jan 10, 2023
5303314
Merge pull request #706 from bcgov/dev
ikethecoder Jan 11, 2023
2cbdd3e
Merge pull request #707 from bcgov/dev
ikethecoder Jan 11, 2023
d0d8ab2
Merge pull request #723 from bcgov/dev
ikethecoder Jan 20, 2023
89e4bf4
Merge pull request #759 from bcgov/dev
ikethecoder Feb 22, 2023
bed9a70
Merge pull request #763 from bcgov/dev
ikethecoder Feb 22, 2023
e3bf9bb
Merge pull request #774 from bcgov/dev
ikethecoder Mar 9, 2023
c227da7
Merge pull request #778 from bcgov/dev
ikethecoder Mar 12, 2023
fe74bcd
Merge pull request #782 from bcgov/dev
ikethecoder Mar 14, 2023
f4a6ff0
Merge pull request #807 from bcgov/dev
ikethecoder May 17, 2023
13035f4
Merge pull request #820 from bcgov/dev
ikethecoder May 18, 2023
b236ea7
Merge pull request #826 from bcgov/dev
ikethecoder Jun 8, 2023
42cfcc7
Merge pull request #835 from bcgov/dev
ikethecoder Jun 12, 2023
9301fd1
Merge pull request #849 from bcgov/dev
ikethecoder Jul 12, 2023
a09f38c
Merge pull request #897 from bcgov/dev
ikethecoder Sep 8, 2023
359f97c
Merge pull request #907 from bcgov/dev
ikethecoder Sep 15, 2023
3077112
Merge pull request #914 from bcgov/dev
ikethecoder Sep 18, 2023
d6f212a
Merge pull request #955 from bcgov/dev
Elson9 Nov 7, 2023
336178f
Merge pull request #959 from bcgov/dev
ikethecoder Nov 8, 2023
29fbea3
Merge pull request #963 from bcgov/dev
ikethecoder Dec 5, 2023
a070d5f
Merge pull request #971 from bcgov/dev
ikethecoder Dec 6, 2023
c3d417a
Merge branch 'dev' into test
ikethecoder Dec 18, 2023
bd8e74f
Merge branch 'dev' into test
ikethecoder Dec 28, 2023
859778b
Merge pull request #999 from bcgov/dev
rustyjux Mar 1, 2024
e67727f
Merge pull request #1004 from bcgov/dev
ikethecoder Mar 6, 2024
2e40387
Merge pull request #1008 from bcgov/dev
ikethecoder Mar 7, 2024
b53799e
Update make API request call as per new design
nirajCITZ Mar 26, 2024
11d7ee2
Update APS-API test to call Astra and update keycloak docker image in…
nirajCITZ Mar 27, 2024
c6885fe
Update aps-cypress-e2e.yaml
nirajCITZ Mar 27, 2024
1e87b67
Dataset formatting fix (#1016)
ikethecoder Mar 26, 2024
a2df507
delete cypress/downloads and clear scanResult
rustyjux Apr 2, 2024
cb82251
exclude astra artifacts in gitignore
rustyjux Apr 2, 2024
f07b898
rename addToAstraScanIdList function for clarity
rustyjux Apr 2, 2024
b030312
rename response data
rustyjux Apr 3, 2024
0d7c628
delete commented out code for downloading gwa cli
rustyjux Apr 10, 2024
3ee84e8
create Jira issues w/ Astra scan results
rustyjux Apr 10, 2024
25b53b5
quick test of astra GHA
rustyjux Apr 10, 2024
9bf6ec3
remove test file
rustyjux Apr 10, 2024
e7faa27
uncomment other GHA steps
rustyjux Apr 10, 2024
4347b07
add exit 1 if failures on Cypress tests
rustyjux Apr 10, 2024
e0396e0
added if: so Astra upload runs if Cypress fails
rustyjux Apr 10, 2024
820c7b5
if: failure to only check Astra if Cypress fails
rustyjux Apr 10, 2024
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
127 changes: 127 additions & 0 deletions .github/astra-jira.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import os
from datetime import datetime
import json
from pathlib import Path
import requests
from requests.auth import HTTPBasicAuth
import sys

ASTRA_SCAN_RESULTS = os.environ.get('ASTRA_SCAN_RESULTS')

JIRA_EMAIL = os.environ.get('JIRA_EMAIL')
JIRA_API_KEY = os.environ.get('JIRA_API_KEY')
JIRA_API_URL = "https://dpdd.atlassian.net/rest/api/2"
JIRA_AUTH = HTTPBasicAuth(JIRA_EMAIL, JIRA_API_KEY)
HEADERS = {
"Accept": "application/json",
"Content-Type": "application/json"
}

IMPACT_LEVELS = ["Medium", "High"]

date = datetime.now()
date_str = date.strftime("%d %b %Y")
scan_name = f'{date_str} - Astra Scan Results'

def check_results(scan_result):
"""
Check if there are any significant vulnerabilities
"""
vulnerabilities = [vulnerability for sublist in scan_result for vulnerability in sublist]
for vulnerability in vulnerabilities:
if vulnerability["impact"] in IMPACT_LEVELS:
print('Issues found!')
return True

return False

def format_ticket(scan_results):
"""
Converts vulnerabilities into format that can be posted to Jira.
"""
description = 'See attached scan results for more details'
for sublist in scan_results:
for vulnerability in sublist:
if vulnerability["impact"] in IMPACT_LEVELS:
description += f'\n\n*Name: {vulnerability["name"]}*\n'
description += f'Impact: {vulnerability["impact"]}\n'
description += f'Description: {vulnerability["Description"]}\n'
description += f'Remediation: {vulnerability["remediation"]}\n'
description += f'URL: {vulnerability["url"]}\n'

return {'summary': scan_name, 'description': description}

def filter_vulnerabilities(scan_results):
"""
Filter vulnerabilities with medium and high severity to attach.
"""
filtered_vulnerabilities = []

for sublist in scan_results:
for vulnerability in sublist:
if vulnerability["impact"] in IMPACT_LEVELS:
filtered_vulnerabilities.append(vulnerability)

filtered_vulnerabilities_json = json.dumps(filtered_vulnerabilities, indent=4)
filtered_vulnerabilities_bytes = filtered_vulnerabilities_json.encode()

return filtered_vulnerabilities_bytes

def post_request(ticket, scan_results_data):
"""
Post issue request to Jira.
"""
payload = json.dumps({
"fields": {
"project": {
"key": "APS"
},
"summary": ticket['summary'],
"description": ticket['description'],
"issuetype": {
"name": "Story"
},
"customfield_10014": "APS-908",
"priority": {
"id": "10000"
}
}
})

post_url = JIRA_API_URL + '/issue'

response = requests.post(url=post_url, data=payload,
headers=HEADERS, auth=JIRA_AUTH)

print(response.text)

if response.status_code != 201:
print("Error occurred while creating Jira issue:", response.text)
return

# Attach scan results to the Jira issue
issue_key = response.json().get('key')
attach_url = f"{JIRA_API_URL}/issue/{issue_key}/attachments"
headers = {"X-Atlassian-Token": "nocheck"}
filename = scan_name + '.json'
attach_response = requests.post(url=attach_url, files={'file': (filename, scan_results_data)}, headers=headers, auth=JIRA_AUTH)

if attach_response.status_code == 200:
print("Jira issue created and file attached successfully!")
else:
print("Error occurred while attaching file to Jira issue:", attach_response.text)

def main():
with open(ASTRA_SCAN_RESULTS, "r") as file:
scan_results = json.load(file)
vulnerabilities = check_results(scan_results)

if vulnerabilities:
ticket_data = {}
ticket_data = format_ticket(scan_results)
filtered_vulnerabilities = filter_vulnerabilities(scan_results)
post_request(ticket_data, filtered_vulnerabilities)
sys.exit(1)

if __name__ == '__main__':
main()
53 changes: 28 additions & 25 deletions .github/workflows/aps-cypress-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,31 +28,6 @@ jobs:
- name: Checkout Portal
uses: actions/checkout@v2

# - name: Determine Download file name
# id: set_variable
# run: |
# echo ${{ runner.arch }}
# if [ "${{ runner.arch }}" == "X64" ]; then
# echo "::set-output name=my_variable::gwa_Linux_x86_64.tgz"
# elif [ "${{ runner.arch }}" == "ARM64" ]; then
# echo "::set-output name=my_variable::gwa_Linux_arm64.tgz"
# else
# echo "unsupported architecture"
# fi

# - name: Download Binary
# uses: robinraju/release-downloader@v1.8
# with:
# repository: "bcgov/gwa-cli"
# latest: true
# fileName: ${{ steps.set_variable.outputs.my_variable }}
# out-file-path: "${{ github.workspace }}/e2e"

# - name: Unzip file
# run: |
# cd ${{ github.workspace }}/e2e
# tar xvzf ${{ steps.set_variable.outputs.my_variable }}

- name: Build Docker Images
run: |
docker compose --profile testsuite build
Expand Down Expand Up @@ -89,6 +64,12 @@ jobs:
name: test-results
path: ${{ github.workspace }}/e2e/results/report

- name: Upload E2E Code Coverage Report
uses: actions/upload-artifact@v2
with:
name: code-coverage
path: ${{ github.workspace }}/e2e/coverage

- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
with:
Expand All @@ -113,4 +94,26 @@ jobs:
echo -e "Stats: $STATS\n\nFailed Tests:\n$FAILED_TESTS\n\nRun Link: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" > msg
export MSG=$(cat msg)
gh issue create --title "FAILED: Automated Tests($FAILURE_COUNT)" --body "$MSG" --label "automation" --assignee "${{ env.GIT_COMMIT_AUTHOR }}"
exit 1
fi

- name: Set up Python 3.9
if: failure()
uses: actions/setup-python@v2
with:
python-version: "3.9"
architecture: "x64"

- name: Install Python dependencies
if: failure()
run: |
python -m pip install --upgrade pip
pip install requests

- name: Check Astra results and create Jira issue if necessary
if: failure()
run: python .github/astra-jira.py
env:
JIRA_EMAIL: ${{ secrets.JIRA_EMAIL }}
JIRA_API_KEY: ${{ secrets.JIRA_API_KEY }}
ASTRA_SCAN_RESULTS: ${{ github.workspace }}/e2e/cypress/fixtures/state/scanResult.json
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ kc.js

# vs code settings
.vscode

e2e/cypress/fixtures/*-plugin.yml
e2e/cypress/fixtures/state/*.pub
e2e/cypress/fixtures/state/*.pem
e2e/cypress/fixtures/state/*.pem
e2e/cypress/fixtures/state/scanResult.json
e2e/cypress/fixtures/state/scanID.json
e2e/cypress/downloads/
30 changes: 28 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ x-common-variables: &common-variables

services:
keycloak:
image: jboss/keycloak:15.1.1
image: quay.io/keycloak/keycloak:15.1.1
container_name: keycloak
hostname: keycloak
depends_on:
Expand Down Expand Up @@ -237,5 +237,31 @@ services:
- aps-net
profiles:
- testsuite
astra-mongo:
image: mongo:4.2.2
container_name: astra-mongo
ports:
- '27017:27017'
networks:
aps-net:
aliases:
- mongo.localtest.me
astra-gui:
build:
context: local/astra
container_name: astra-gui
restart: always
environment:
MONGO_PORT_27017_TCP_ADDR: astra-mongo
networks:
aps-net:
aliases:
- astra.localtest.me
depends_on:
- astra-mongo
links:
- astra-mongo:mongo
ports:
- '8094:8094'
networks:
aps-net: {}
aps-net: {}
3 changes: 2 additions & 1 deletion e2e/cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export default defineConfig({
'./cypress/tests/14-*/*.ts',
'./cypress/tests/15-*/*.ts',
'./cypress/tests/16-*/*.ts',
'./cypress/tests/17-*/*.ts'
'./cypress/tests/17-*/*.ts',
'./cypress/tests/18-*/*.ts'
]
return config
},
Expand Down
4 changes: 4 additions & 0 deletions e2e/cypress/fixtures/state/scanID.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"items": [
]
}
Empty file.
44 changes: 39 additions & 5 deletions e2e/cypress/support/auth-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,17 +422,51 @@ Cypress.Commands.add('setAuthorizationToken', (token: string) => {
})

Cypress.Commands.add('makeAPIRequest', (endPoint: string, methodType: string) => {

let body = {}

let requestData: any = {}
if (methodType.toUpperCase() === 'PUT' || methodType.toUpperCase() === 'POST') {
body = requestBody
}
return cy.request({
url: Cypress.env('BASE_URL') + '/' + endPoint,
method: methodType,
body: body,

requestData['appname'] = "Test1"
requestData['url'] = Cypress.env('BASE_URL') + '/' + endPoint
requestData['headers'] = headers
requestData['body'] = ""
requestData['method'] = methodType

// Scan request with Astra
cy.request({
url: 'http://astra.localtest.me:8094/scan/',
method: 'POST',
body: requestData,
headers: headers,
failOnStatusCode: false
}).then((astraResponse) => {
// Actual API request
cy.request({
url: Cypress.env('BASE_URL') + '/' + endPoint,
method: methodType,
body: body,
headers: headers,
failOnStatusCode: false
}).then((apiResponse) => {
// You can also return data or use it in further tests
const responseData = {
astraRes: astraResponse,
apiRes: apiResponse,
};
// cy.addToAstraScanIdList(response2.body.status)
return responseData;
})
});
})

Cypress.Commands.add('makeAPIRequestForScanResult', (scanID: string) => {
return cy.request({
url: 'http://astra.localtest.me:8094/alerts/' + scanID,
method: 'GET',
failOnStatusCode: false
})
})

Expand Down
6 changes: 6 additions & 0 deletions e2e/cypress/support/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,11 @@ declare namespace Cypress {
updateJsonBoby(json: any, key: string, newValue: string):Chainable<any>

deleteFileInE2EFolder(fileName: string):Chainable<any>

addToAstraScanIdList(item: any):Chainable<any>

checkAstraScanResultForVulnerability():Chainable<any>

makeAPIRequestForScanResult(scanID: string): Chainable<Cypress.Response<any>>
}
}
45 changes: 45 additions & 0 deletions e2e/cypress/support/util-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ Cypress.Commands.add('deleteFileInE2EFolder', (fileName: string) => {
}
});

Cypress.Commands.add('addToAstraScanIdList', (item) => {
cy.readFile('cypress/fixtures/state/scanID.json').then((fileContent) => {
// Initialize the list if it doesn't exist
const items = fileContent.items || [];

// Append the new item to the list
items.push(item);

// Create an object with the updated list
const updatedData = { items };

// Write the updated object back to the file
cy.writeFile('cypress/fixtures/state/scanID.json', updatedData);
});
});

Cypress.Commands.add('replaceWord', (originalString: string, wordToReplace: string, replacementWord: string)=> {
// Create a regular expression with the 'g' flag for global search
Expand All @@ -180,4 +195,34 @@ Cypress.Commands.add('replaceWord', (originalString: string, wordToReplace: stri
replacedString = originalString.replace(regex, replacementWord);

return replacedString;
})

Cypress.Commands.add('checkAstraScanResultForVulnerability', () => {
let aggregatedData = {};
let existingData: any = [];
let flag = false
cy.readFile('cypress/fixtures/state/scanID.json').then((fileContent) => {
fileContent.items.forEach((item: string) => {
// Perform an action based on each item in the array
cy.makeAPIRequestForScanResult(item).then((response) => {
const newResponse = JSON.parse(JSON.stringify(response.body));
existingData.push(newResponse);
});
});
}).then(() => {
cy.writeFile('cypress/fixtures/state/scanResult.json', JSON.stringify(existingData))
for (var i = 0; i < existingData.length; i++) {
var jsonObject = existingData[i];
for (var j = 0; i < jsonObject.length; i++) {
if (jsonObject[j].hasOwnProperty("impact") &&
["High", "Medium"].includes(jsonObject[j]["impact"])) {
flag = true;
}
}
}
}).then(()=>{
if (flag){
assert.fail("Some of the results have high or medium severity security vulnerabilities. Please check the result file for more details.");
}
})
})
Loading
Loading