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

platform(general): Implement SSO Relay State Parameter in Checkov Output Links #5217

Merged
Merged
Show file tree
Hide file tree
Changes from 11 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
37 changes: 37 additions & 0 deletions checkov/common/bridgecrew/platform_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import uuid
import webbrowser
from collections import namedtuple
from urllib.parse import urlparse
from concurrent import futures
from io import StringIO
from json import JSONDecodeError
Expand Down Expand Up @@ -1041,5 +1042,41 @@ def get_default_headers(self, request_type: str) -> dict[str, Any]:
logging.info(f"Unsupported request {request_type}")
return {}

# Define the function that will get the relay state from the Prisma Cloud Platform.
def get_sso_prismacloud_url(self, report_url: str) -> str:
if not bc_integration.prisma_api_url:
return report_url
url_saml_config = f"{bc_integration.prisma_api_url}/saml/config"
token = self.get_auth_token()
headers = merge_dicts(get_auth_header(token),
get_default_get_headers(self.bc_source, self.bc_source_version))

request = self.http.request("GET", url_saml_config, headers=headers, timeout=10)
SimOnPanw marked this conversation as resolved.
Show resolved Hide resolved
if request.status == 401:
logging.error(f'Received 401 response from Prisma /login endpoint: {request.data.decode("utf8")}')
raise BridgecrewAuthError()
elif request.status == 403:
logging.error('Received 403 (Forbidden) response from Prisma /login endpoint')
raise BridgecrewAuthError()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeurbanski1 any thoughts about raising an exception here or should we just log it and return the normal URL?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like returning the normal URL. Then at least they can go to it after signing in separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeurbanski1 @gruebel instead of raising an exception, I could return the normal URL as follow:

request = self.http.request("GET", url_saml_config, headers=headers, timeout=10)  # type:ignore[no-untyped-call]
if request.status >= 300:
  return report_url

With the problem that we don't trigger an exception if the BC_API_KEY is not provided. I am not sure this is better. What do you think ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't get to this code if there is no BC_API_KEY though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeurbanski1, of course, you are correct. I committed the change.


data = json.loads(request.data.decode("utf8"))

relay_state_param_name = data.get("relayStateParamName")
access_saml_url = data.get("redLockAccessSamlUrl")

if relay_state_param_name and access_saml_url:
parsed_url = urlparse(report_url)
uri = parsed_url.path
# If there are any query parameters, append them to the URI
if parsed_url.query:
uri = f"{uri}?{parsed_url.query}"
# Check if the URL already contains GET parameters.
if "?" in access_saml_url:
report_url = f"{access_saml_url}&{relay_state_param_name}={uri}"
else:
report_url = f"{access_saml_url}?{relay_state_param_name}={uri}"

return report_url


bc_integration = BcPlatformIntegration()
3 changes: 2 additions & 1 deletion checkov/common/runners/runner_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,14 +579,15 @@ def print_reports(

def _print_to_console(self, output_formats: dict[str, str], output_format: str, output: str, url: str | None = None) -> None:
"""Prints the output to console, if needed"""

output_dest = output_formats[output_format]
if output_dest == CONSOLE_OUTPUT:
del output_formats[output_format]

print(output)
if url:
url = bc_integration.get_sso_prismacloud_url(report_url=url)
gruebel marked this conversation as resolved.
Show resolved Hide resolved
print(f"More details: {url}")

if CONSOLE_OUTPUT in output_formats.values():
print(OUTPUT_DELIMITER)

Expand Down