-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
site-specific security example mostly working except for keycloak one
- Loading branch information
1 parent
520c9f6
commit 3804309
Showing
36 changed files
with
1,490 additions
and
561 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
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
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
53 changes: 53 additions & 0 deletions
53
...pute_system/06.3_site_security/custom_client_side_auth_system_integration/code/fl_jobs.py
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,53 @@ | ||
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import os | ||
|
||
from src.fedavg import FedAvg | ||
from src.network import SimpleNetwork | ||
|
||
from nvflare.job_config.api import FedJob | ||
from nvflare.job_config.script_runner import ScriptRunner | ||
|
||
if __name__ == "__main__": | ||
num_clients = 2 | ||
num_rounds = 2 | ||
job_name = "fedavg" | ||
train_script = "src/client.py" | ||
config_dir = "/tmp/nvflare/jobs/workdir" | ||
|
||
|
||
job = FedJob(name = job_name, min_clients = num_clients) | ||
controller = FedAvg( | ||
stop_cond = "accuracy > 25", | ||
save_filename = "global_model.pt", | ||
initial_model = SimpleNetwork(), | ||
num_clients = num_clients, | ||
num_rounds = num_rounds, | ||
) | ||
|
||
job.to_server(controller) | ||
|
||
# Add clients | ||
for i in range(num_clients): | ||
executor = ScriptRunner(script=train_script, script_args="") | ||
job.to(executor, f"site-{i+1}") | ||
|
||
job_config_dir = os.path.join(config_dir, job_name) | ||
print(f"job-config for {job_name} is at ",job_config_dir) | ||
job.export_job(config_dir) | ||
# job.simulator_run(config_dir) | ||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions
65
...6.3_site_security/custom_client_side_auth_system_integration/edit_site_local_resources.py
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,65 @@ | ||
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import json | ||
import sys | ||
import os | ||
|
||
|
||
def get_security_handler() -> dict: | ||
return json.loads( | ||
""" | ||
{ | ||
"id": "security_handler", | ||
"path": "keycloak_security_handler.CustomSecurityHandler" | ||
} | ||
""" | ||
) | ||
|
||
|
||
def add_components_to_json( | ||
input_file_path, output_file_path, site: str, receiving: bool = False, streaming_to_server: bool = False | ||
): | ||
try: | ||
with open(input_file_path, "r") as file: | ||
data = json.load(file) | ||
except (FileNotFoundError, json.JSONDecodeError): | ||
print(f"Error: Unable to read or parse JSON file: {input_file_path}") | ||
return | ||
|
||
new_components = [get_security_handler()] | ||
|
||
# Append new components to the list | ||
data["components"].extend(new_components) | ||
|
||
# Write the updated JSON back to the file | ||
with open(output_file_path, "w") as file: | ||
json.dump(data, file, indent=4) | ||
|
||
print(f"Successfully generate file: '{output_file_path}'.") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
site_name = sys.argv[1] | ||
project_root_dir = sys.argv[2] | ||
|
||
|
||
print(site_name, project_root_dir) | ||
|
||
input_file_path = os.path.join(project_root_dir, site_name, "local", "resources.json.default") | ||
output_file_path = os.path.join(project_root_dir, site_name, "local", "resources.json") | ||
|
||
add_components_to_json(input_file_path, output_file_path, site_name) |
60 changes: 60 additions & 0 deletions
60
...6.3_site_security/custom_client_side_auth_system_integration/get_keycloak_access_token.py
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,60 @@ | ||
import requests | ||
import os | ||
import sys | ||
|
||
def save_access_token(access_token:str, destination_path): | ||
|
||
# Ensure the destination directory exists | ||
os.makedirs(os.path.dirname(destination_path), exist_ok=True) | ||
|
||
with open(destination_path, "w") as f: | ||
f.write(access_token) | ||
print(f"Access token saved to {destination_path}") | ||
|
||
|
||
def get_keycloak_acces_token(username, password, client_id,keycloak_url) -> str: | ||
|
||
|
||
# Request payload | ||
data = { | ||
"username": username, | ||
"password": password, | ||
"grant_type": "password", | ||
"client_id": client_id | ||
} | ||
|
||
try: | ||
# Make a POST request to get the access token | ||
response = requests.post(keycloak_url, data=data, headers={"Content-Type": "application/x-www-form-urlencoded"}) | ||
response_data = response.json() | ||
|
||
# Extract the access token | ||
access_token = response_data.get("access_token") | ||
|
||
if not access_token: | ||
print("Failed to retrieve access token.") | ||
else: | ||
return access_token | ||
|
||
except Exception as e: | ||
print(f"Error fetching access token: {e}") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
||
# Define variables | ||
keycloak_url = "http://localhost:8080/realms/master/protocol/openid-connect/token" | ||
username = "admin" | ||
password = "admin123" | ||
client_id = "admin-cli" | ||
destination_path = sys.argv[1] | ||
|
||
token = get_keycloak_acces_token(username=username, password=password, client_id= client_id, keycloak_url=keycloak_url) | ||
|
||
print("token=", token) | ||
|
||
save_access_token(token, destination_path=destination_path) | ||
|
||
|
||
|
||
|
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
18 changes: 18 additions & 0 deletions
18
...m/06.3_site_security/custom_client_side_auth_system_integration/keycloak-setup/dockerfile
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,18 @@ | ||
FROM bitnami/keycloak:24 | ||
|
||
USER root | ||
|
||
# Install jq (and any other necessary dependencies) | ||
RUN apt-get update && apt-get install -y jq | ||
|
||
# Set working directory | ||
WORKDIR /opt/keycloak-setup | ||
|
||
# Copy the setup scripts to the container | ||
COPY ./init.sh /opt/keycloak-setup/init.sh | ||
|
||
# Set permissions to ensure init.sh is executable | ||
RUN chmod +x /opt/keycloak-setup/init.sh | ||
|
||
# Set the entrypoint | ||
ENTRYPOINT ["/bin/sh", "-c", "/opt/bitnami/scripts/keycloak/run.sh & sleep 10 && /opt/keycloak-setup/init.sh && tail -f /dev/null"] |
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
Oops, something went wrong.