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

Improved Indentation and added Comments in the codebase. #133

Closed
wants to merge 25 commits into from
Closed
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# List of required Python packages and their corresponding versions
pyu2f==0.1.5
google-api-python-client==2.80.0
google-cloud-container==2.17.4
Expand Down
5 changes: 4 additions & 1 deletion scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Import the scanner module from the gcp_scanner package
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant

from src.gcp_scanner import scanner
scanner.main()

# Call the main function of the scanner module to start the scanning process
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant

scanner.main()
1 change: 1 addition & 0 deletions src/gcp_scanner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

3 changes: 3 additions & 0 deletions src/gcp_scanner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

"""

# Importing the scanner module
Copy link
Collaborator

Choose a reason for hiding this comment

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

redundant

from . import scanner

# Checking if the code is running as the main module
if __name__ == '__main__':
# Calling the main function of the scanner module
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant

scanner.main()
165 changes: 91 additions & 74 deletions src/gcp_scanner/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import argparse
import logging


# Define a function to create an argument parser using the argparse module
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant

def arg_parser():
"""Creates an argument parser using the `argparse` module and defines
several command-line arguments.
Expand All @@ -31,102 +33,117 @@ def arg_parser():
argparse.Namespace: A namespace object containing the parsed command-line
arguments.
"""
# Create a new parser object
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is redundant. I am stopping on this one. You do not need to comment obvious sections of the code.

parser = argparse.ArgumentParser(
prog='scanner.py',
description='GCP Scanner',
usage='python3 %(prog)s -o folder_to_save_results -g -')
prog='scanner.py', # program name
description='GCP Scanner', # description
usage='python3 %(prog)s -o folder_to_save_results -g -'
)

# Define a required argument group
required_named = parser.add_argument_group('Required parameters')
# Add a required argument to the group
required_named.add_argument(
'-o',
'--output-dir',
required=True,
dest='output',
default='scan_db',
help='Path to output directory')
'-o', # short option name
'--output-dir', # long option name
required=True,
dest='output',
default='scan_db',
help='Path to output directory'
)

# Add command line arguments to the parser object
parser.add_argument(
'-k',
'--sa-key-path',
default=None,
dest='key_path',
help='Path to directory with SA keys in json format')
'-k',
'--sa-key-path',
default=None, # Default value if option is not specified
dest='key_path',
help='Path to directory with SA keys in json format' # Help message
)
parser.add_argument(
'-g',
'--gcloud-profile-path',
default=None,
dest='gcloud_profile_path',
help='Path to directory with gcloud profile. Specify -\
to search for credentials in default gcloud config path'
'-g',
'--gcloud-profile-path',
default=None,
dest='gcloud_profile_path',
help='Path to directory with gcloud profile. Specify - to search for\
credentials in default gcloud config path'
)
parser.add_argument(
'-m',
'--use-metadata',
default=False,
dest='use_metadata',
action='store_true',
help='Extract credentials from GCE instance metadata')
'-m',
'--use-metadata',
default=False,
dest='use_metadata',
action='store_true',
help='Extract credentials from GCE instance metadata'
)
parser.add_argument(
'-at',
'--access-token-files',
default=None,
dest='access_token_files',
help='A list of comma separated files with access token and OAuth scopes.\
TTL limited. A token and scopes should be stored in JSON format.')
'-at',
'--access-token-files',
default=None,
dest='access_token_files',
help='A list of comma separated files with access token and OAuth scopes\
TTL limited. A token and scopes should be stored in JSON format.'
)
parser.add_argument(
'-rt',
'--refresh-token-files',
default=None,
dest='refresh_token_files',
help='A list of comma separated files with refresh_token, client_id,\
token_uri and client_secret stored in JSON format.'
'-rt',
'--refresh-token-files',
default=None,
dest='refresh_token_files',
help='A list of comma separated files with refresh_token, client_id,\
token_uri and client_secret stored in JSON format.'
)

parser.add_argument(
'-s',
'--service-account',
default=None,
dest='key_name',
help='Name of individual SA to scan')
'-s',
'--service-account',
default=None,
dest='key_name',
help='Name of individual SA to scan')
parser.add_argument(
'-p',
'--project',
default=None,
dest='target_project',
help='Name of individual project to scan')
'-p',
'--project',
default=None,
dest='target_project',
help='Name of individual project to scan')
parser.add_argument(
'-f',
'--force-projects',
default=None,
dest='force_projects',
help='Comma separated list of project names to include in the scan')
'-f',
'--force-projects',
default=None,
dest='force_projects',
help='Comma separated list of project names to include in the scan')
parser.add_argument(
'-c',
'--config',
default=None,
dest='config_path',
help='A path to config file with a set of specific resources to scan.')
'-c',
'--config',
default=None,
dest='config_path',
help='A path to config file with a set of specific resources to scan.')
parser.add_argument(
'-l',
'--logging',
default='WARNING',
dest='log_level',
choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'),
help='Set logging level (INFO, WARNING, ERROR)')
'-l',
'--logging',
default='WARNING',
dest='log_level',
choices=('DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'),
help='Set logging level (INFO, WARNING, ERROR)')
parser.add_argument(
'-lf',
'--log-file',
default=None,
dest='log_file',
help='Save logs to the path specified rather than displaying in\
console')
'-lf',
'--log-file',
default=None,
dest='log_file',
help='Save logs to the path specified rather than displaying in\
console')

# Parse the command line arguments
args: argparse.Namespace = parser.parse_args()

# Check if none of the necessary options are selected
if not args.key_path and not args.gcloud_profile_path \
and not args.use_metadata and not args.access_token_files\
and not args.refresh_token_files:
and not args.use_metadata and not args.access_token_files\
and not args.refresh_token_files:

# If none of the options are selected, log an error message
logging.error(
'Please select at least one option to begin scan\
-k/--sa-key-path,-g/--gcloud-profile-path, -m, -rt, -at'
)
-k/--sa-key-path,-g/--gcloud-profile-path, -m, -rt, -at')

# Return the parsed command line arguments
return args
Loading