-
Notifications
You must be signed in to change notification settings - Fork 3
Python - MTLS Changes #23
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
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
a3433a1
App Descriptor changes
surendarraju 516b92f
MTLS implementation changes
surendarraju a64cc6a
client-secret modified as legacy-client-secret
surendarraju cb5ed45
review comments addressed
surendarraju a378fe8
README Tutorial Changes
surendarraju da5c5de
Modified client-secret to legacy-client-secret
surendarraju 1829b63
Minor changes in readme
surendarraju 9b5e0fa
Corrections - proof read
surendarraju 42f8222
Added unit tests and reformatted files
kara-kiss 4ddbd58
Merge branch 'main' into asd
kara-kiss b93b786
Fixed markdownlint issues
kara-kiss 7c41a6b
Review Comments
surendarraju 0336301
PO Review comments addressed
surendarraju 7c841b7
Changes in Read
surendarraju 11a443b
Readme changes
surendarraju 27eba09
ReadMe
surendarraju 0072326
README
surendarraju 8735507
Improvements
surendarraju 0385956
Test cases fix
surendarraju a752e88
Revert
surendarraju fe6817a
:
surendarraju 88a9fa2
Improvements
surendarraju 317e497
backward compatilibility
surendarraju 52a0288
minor changes
surendarraju 11c511c
SDK Review comments addressed
surendarraju d820314
Some more
surendarraju e880c07
Change
surendarraju 946d7f2
REadME
surendarraju 2f21b42
Changes
surendarraju fd322f1
quotes
surendarraju e54ccf3
Spell check
surendarraju d9f0e6b
Review Comments
surendarraju ecb5b0f
Test cases
surendarraju ebc89f5
Review Changes
surendarraju 5ebd88f
Updated version
kara-kiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
3 changes: 3 additions & 0 deletions
3
csar/OtherDefinitions/SecurityManagement/security-metadata.json
This file contains hidden or 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,3 @@ | ||
| { | ||
| "authenticatorType": "client-x509" | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,40 @@ | ||
| '''This module handles environment variables''' | ||
| """This module handles environment variables""" | ||
|
|
||
| import os | ||
|
|
||
|
|
||
| def get_config(): | ||
| ''' | ||
| get env and return config with all env vals required | ||
| ''' | ||
| iam_client_id = get_os_env_string("IAM_CLIENT_ID", "") | ||
| iam_client_secret = get_os_env_string("IAM_CLIENT_SECRET", "") | ||
| iam_base_url = get_os_env_string("IAM_BASE_URL", "") | ||
| ca_cert_file_name = get_os_env_string("CA_CERT_FILE_NAME", "") | ||
| ca_cert_file_path = get_os_env_string("CA_CERT_FILE_PATH", "") | ||
| log_ctrl_file = get_os_env_string("LOG_CTRL_FILE", "") | ||
| log_endpoint = get_os_env_string("LOG_ENDPOINT", "") | ||
| app_key = get_os_env_string("APP_KEY", "") | ||
| app_cert = get_os_env_string("APP_CERT", "") | ||
| app_cert_file_path = get_os_env_string("APP_CERT_FILE_PATH", "") | ||
| """get env and return config with all env vals required""" | ||
| iam_client_id = get_os_env_string("IAM_CLIENT_ID", "") | ||
surendarraju marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| iam_client_secret = get_os_env_string("IAM_CLIENT_SECRET", "") | ||
| iam_base_url = get_os_env_string("IAM_BASE_URL", "") | ||
| ca_cert_file_name = get_os_env_string("CA_CERT_FILE_NAME", "") | ||
| ca_cert_file_path = get_os_env_string("CA_CERT_FILE_PATH", "") | ||
| log_ctrl_file = get_os_env_string("LOG_CTRL_FILE", "") | ||
| log_endpoint = get_os_env_string("LOG_ENDPOINT", "") | ||
| app_key = get_os_env_string("APP_KEY", "") | ||
| app_cert = get_os_env_string("APP_CERT", "") | ||
| app_cert_file_path = get_os_env_string("APP_CERT_FILE_PATH", "") | ||
| client_creds_file_path = get_os_env_string("CLIENT_CREDS_FILE_PATH", "") | ||
| client_id_file_name = get_os_env_string("CLIENT_ID_FILE_NAME", "") | ||
|
|
||
| config = { | ||
| "iam_client_id": iam_client_id, | ||
| "iam_client_secret": iam_client_secret, | ||
| "iam_base_url": iam_base_url, | ||
| "ca_cert_file_name": ca_cert_file_name, | ||
| "ca_cert_file_path": ca_cert_file_path, | ||
| "log_ctrl_file": log_ctrl_file, | ||
| "log_endpoint": log_endpoint, | ||
| "app_key": app_key, | ||
| "app_cert": app_cert, | ||
| "app_cert_file_path": app_cert_file_path | ||
| "iam_client_id": iam_client_id, | ||
| "iam_client_secret": iam_client_secret, | ||
| "iam_base_url": iam_base_url, | ||
| "ca_cert_file_name": ca_cert_file_name, | ||
| "ca_cert_file_path": ca_cert_file_path, | ||
| "log_ctrl_file": log_ctrl_file, | ||
| "log_endpoint": log_endpoint, | ||
| "app_key": app_key, | ||
| "app_cert": app_cert, | ||
| "app_cert_file_path": app_cert_file_path, | ||
| "client_creds_file_path": client_creds_file_path, | ||
| "client_id_file_name": client_id_file_name, | ||
| } | ||
| return config | ||
|
|
||
|
|
||
| def get_os_env_string(env_name, default_value): | ||
| ''' | ||
| get env | ||
| ''' | ||
| """get env""" | ||
| return os.getenv(env_name, default_value).strip() | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,82 +1,96 @@ | ||
| #!/usr/bin/env python3 | ||
| ''' | ||
| """ | ||
| Flask Application for Hello World Service | ||
|
|
||
| This Python script defines a Flask application that implements a simple "Hello World" service | ||
| along with a health check and metrics endpoints. | ||
| ''' | ||
| """ | ||
| import time | ||
| from flask import abort | ||
| from flask import Flask | ||
| from login import login | ||
| from mtls_logging import MtlsLogging, Severity | ||
| from werkzeug.middleware.dispatcher import DispatcherMiddleware | ||
| from prometheus_client import disable_created_metrics, make_wsgi_app, CollectorRegistry, Counter | ||
| from prometheus_client import ( | ||
| disable_created_metrics, | ||
| make_wsgi_app, | ||
| CollectorRegistry, | ||
| Counter, | ||
| ) | ||
|
|
||
| SERVICE_PREFIX = "python_hello_world" | ||
|
|
||
| class Application(Flask): | ||
| '''The Flask application itself. Subclassed for testing.''' | ||
| """The Flask application itself. Subclassed for testing.""" | ||
| def __init__(self): | ||
| super().__init__(__name__) | ||
| disable_created_metrics() | ||
| self.counters = {"total_failed": 0, "total_requests": 0} | ||
| self.session = {"token": None, "expiry_time": 0} | ||
| self.create_metrics() | ||
| self.wsgi_app = DispatcherMiddleware(self.wsgi_app, { | ||
| '/sample-app/python/metrics': make_wsgi_app(registry=self.registry) | ||
| }) | ||
| self.wsgi_app = DispatcherMiddleware( | ||
| self.wsgi_app, | ||
| {"/sample-app/python/metrics": make_wsgi_app(registry=self.registry)}, | ||
| ) | ||
| self.logger = MtlsLogging() | ||
|
|
||
| @self.route("/sample-app/python/") | ||
| def root(): | ||
| '''This route returns a 400 Bad Request HTTP response.''' | ||
| self.logger.log("400 Bad request: User tried accessing '/sample-app/python/'", Severity.INFO) | ||
| """This route returns a 400 Bad Request HTTP response.""" | ||
| self.logger.log( | ||
| "400 Bad request: User tried accessing '/sample-app/python/'", | ||
| Severity.INFO, | ||
| ) | ||
| abort(400) | ||
|
|
||
| @self.route("/sample-app/python/hello") | ||
| def hello(): | ||
| ''' | ||
| """ | ||
| This route performs a login operation and returns | ||
| a simple "Hello World!" greeting and increments the | ||
| total request counter. | ||
| ''' | ||
| """ | ||
| self.update_session() | ||
| self.requests_total.inc() | ||
| self.logger.log("200 OK: Hello World!", Severity.INFO) | ||
| return "Hello World!\n" | ||
|
|
||
| @self.route("/sample-app/python/health") | ||
| def health(): | ||
| ''' | ||
| """ | ||
| This route provides a simple health check endpoint, returning "Ok" to | ||
| indicate that the application is healthy. | ||
| ''' | ||
| """ | ||
| self.update_session() | ||
| self.logger.log("200 OK: Health check", Severity.INFO) | ||
| return "Ok\n" | ||
|
|
||
|
|
||
| def update_session(self): | ||
| '''Refresh session if it expires.''' | ||
| """Refresh session if it expires.""" | ||
| if int(time.time()) >= self.session["expiry_time"]: | ||
| self.session["token"], self.session["expiry_time"] = login() | ||
| if not self.session["token"]: | ||
| # since the token isn't used for anything, | ||
| # this is just a WARNING level log instead of ERROR | ||
| self.logger.log("Login failed", Severity.WARNING) | ||
| try: | ||
| self.session["token"], self.session["expiry_time"] = login() | ||
| except Exception as e: | ||
| # since the token isn't used for anything, | ||
| # this is just a WARNING level log instead of ERROR | ||
| self.logger.log(f"Login failed: {e}", Severity.WARNING) | ||
|
|
||
| def create_metrics(self): | ||
| self.registry = CollectorRegistry() | ||
| self.requests_total = Counter(namespace=SERVICE_PREFIX, | ||
| name="requests_total", | ||
| documentation="Total number of API requests") | ||
| self.requests_failed = Counter(namespace=SERVICE_PREFIX, | ||
| name="requests_failed_total", | ||
| documentation="Total number of API request failures") | ||
| self.requests_total = Counter( | ||
| namespace=SERVICE_PREFIX, | ||
| name="requests_total", | ||
| documentation="Total number of API requests", | ||
| ) | ||
| self.requests_failed = Counter( | ||
| namespace=SERVICE_PREFIX, | ||
| name="requests_failed_total", | ||
| documentation="Total number of API request failures", | ||
| ) | ||
| self.registry.register(self.requests_total) | ||
| self.registry.register(self.requests_failed) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| if __name__ == "__main__": | ||
| instance = Application() | ||
| instance.run(host = '0.0.0.0', port = '8050') | ||
| instance.run(host="0.0.0.0", port="8050") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.