-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from RedHatProductSecurity/feature/OSIDB-3742…
…-cwe-api-service ✨ OSIDB-3480: CWE api service implementation
- Loading branch information
Showing
10 changed files
with
142 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/sh | ||
|
||
# Skip creating proxy file if no proxies are configured | ||
if [ -z "$OSIM_NGINX_PROXY_MITRE" ]; then | ||
exit 0; | ||
fi | ||
|
||
# Get proxy certificate | ||
curl --xattr "$OSIM_NGINX_PROXY_CA" -o /tmp/Proxy-CA.crt | ||
|
||
# Ensure no trailing slash in variable, so no duplicate trailing slash is added in proxy_pass | ||
OSIM_NGINX_PROXY_MITRE="${OSIM_NGINX_PROXY_MITRE%/}" | ||
|
||
echo resolver "$(awk -v ORS=' ' '$1=="nameserver" {print $2}' /etc/resolv.conf)" ";" >/etc/nginx/conf.d/resolvers.conf | ||
|
||
# Add MITRE reverse proxy endpoint | ||
cat <<EOF >/tmp/osim-nginx-proxy.conf | ||
location /proxy/mitre/ { | ||
# Trailing slash in proxy_pass strips the location directive prefix from the downstream URL | ||
proxy_pass ${OSIM_NGINX_PROXY_MITRE}/; | ||
proxy_http_version 1.1; | ||
} | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { osimRuntime } from '@/stores/osimRuntime'; | ||
import type { CWEMemberType } from '@/types/mitreCwe'; | ||
|
||
const DATA_KEY = 'CWE:API-DATA'; | ||
const VERSION_KEY = 'CWE:API-VERSION'; | ||
|
||
interface CweViews { | ||
Views: { | ||
Members: { | ||
CweID: string; | ||
}[]; | ||
}[]; | ||
} | ||
|
||
interface CweCategories { | ||
Categories: { | ||
ID: string; | ||
Name: string; | ||
}[]; | ||
} | ||
|
||
export async function updateCWEData() { | ||
const baseUrl = osimRuntime.value.backends.mitre; | ||
try { | ||
if (!baseUrl) { | ||
console.debug('No Mitre backed configured, skipping CWE API cache update.'); | ||
return; | ||
} | ||
|
||
const [version, isNew] = await checkNewVersion(baseUrl); | ||
if (!isNew) { | ||
console.debug('✅ CWE API cache is up-to-date.'); | ||
return; | ||
} | ||
|
||
fetchAndCache(baseUrl); | ||
|
||
localStorage.setItem(VERSION_KEY, version); | ||
} catch (error) { | ||
console.error('CweService::fetchAndCacheAPI() Error getting CWE data', error); | ||
} | ||
} | ||
|
||
async function checkNewVersion(baseUrl: string): Promise<[string, boolean]> { | ||
const version = await fetch(`${baseUrl}/cwe/version`); | ||
|
||
if (!version.ok) { | ||
throw new Error('Failed to fetch CWE version data'); | ||
} | ||
|
||
const versionData = await version.json(); | ||
const storedVersion = localStorage.getItem(VERSION_KEY); | ||
|
||
const isNew = storedVersion !== versionData.ContentVersion; | ||
return [versionData.ContentVersion, isNew]; | ||
} | ||
|
||
async function fetchAndCache(baseUrl: string) { | ||
const cweIds = await fetchCweIds(baseUrl); | ||
const cweData = await fetchCweNames(baseUrl, cweIds); | ||
localStorage.setItem(DATA_KEY, JSON.stringify(cweData)); | ||
console.debug('✅ CWE API cache updated.'); | ||
} | ||
|
||
async function fetchCweIds(baseUrl: string) { | ||
// 699 is the id for the "Software Development" CWE view | ||
const view = await fetch(`${baseUrl}/cwe/view/699`); | ||
if (!view.ok) { | ||
throw new Error('Failed to fetch CWE OpenAPI data'); | ||
} | ||
const cweData: CweViews = await view.json(); | ||
const cweIds = cweData.Views[0].Members.map(member => member.CweID); | ||
return cweIds; | ||
} | ||
|
||
async function fetchCweNames(baseUrl: string, cweIds: string[]) { | ||
const detailedResponse = await fetch(`${baseUrl}/cwe/category/${cweIds.join(',')}`); | ||
if (!detailedResponse.ok) { | ||
throw new Error('Failed to fetch detailed CWE data'); | ||
} | ||
|
||
const detailedData: CweCategories = await detailedResponse.json(); | ||
const customStructure: CWEMemberType[] = detailedData.Categories.map(category => ({ | ||
id: category.ID, | ||
name: category.Name, | ||
})); | ||
return customStructure; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface CWEMemberType { | ||
id: string; | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters