-
Notifications
You must be signed in to change notification settings - Fork 0
fix: docs, add python script I used in debugging the MP setup #12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,85 @@ | ||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||
# We at Masterpoint found this python script to be useful when debugging import | ||||||||||||||||||||||||||||||
# issues with existing users and roles into the terraform module to confirm the | ||||||||||||||||||||||||||||||
# expected data values. For example, confirm data and formatting of | ||||||||||||||||||||||||||||||
# exsting users' custom schema keys, values, and json encoded strings. | ||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||
# This is intended only for ad-hoc debugging purposes and has not been thorughly | ||||||||||||||||||||||||||||||
# reviewed or tested. Use at your own risk. | ||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
from google.oauth2 import service_account | ||||||||||||||||||||||||||||||
from googleapiclient.discovery import build | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Path to your service account JSON key | ||||||||||||||||||||||||||||||
SERVICE_ACCOUNT_FILE = 'my-google-admin-api-key.json' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Replace with your impersonated Google Workspace admin email | ||||||||||||||||||||||||||||||
DELEGATED_ADMIN = 'first.last@your-company.io' | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
SCOPES = [ | ||||||||||||||||||||||||||||||
"https://www.googleapis.com/auth/admin.directory.group", | ||||||||||||||||||||||||||||||
"https://www.googleapis.com/auth/admin.directory.user", | ||||||||||||||||||||||||||||||
"https://www.googleapis.com/auth/admin.directory.userschema", | ||||||||||||||||||||||||||||||
"https://www.googleapis.com/auth/apps.groups.settings", | ||||||||||||||||||||||||||||||
"https://www.googleapis.com/auth/iam", | ||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Load credentials and delegate to admin | ||||||||||||||||||||||||||||||
credentials = service_account.Credentials.from_service_account_file( | ||||||||||||||||||||||||||||||
SERVICE_ACCOUNT_FILE, | ||||||||||||||||||||||||||||||
scopes=SCOPES | ||||||||||||||||||||||||||||||
).with_subject(DELEGATED_ADMIN) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Build the service | ||||||||||||||||||||||||||||||
service = build('admin', 'directory_v1', credentials=credentials) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
# Call the Directory API to list all user schemas | ||||||||||||||||||||||||||||||
def list_user_schemas(customer_id='my_customer'): | ||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||
schemas = service.schemas().list(customerId=customer_id).execute() | ||||||||||||||||||||||||||||||
for schema in schemas.get('schemas', []): | ||||||||||||||||||||||||||||||
print(f"Schema ID: {schema['schemaId']}") | ||||||||||||||||||||||||||||||
print(f"Schema Name: {schema['schemaName']}") | ||||||||||||||||||||||||||||||
print(f"Fields:") | ||||||||||||||||||||||||||||||
for field in schema.get('fields', []): | ||||||||||||||||||||||||||||||
print(field) | ||||||||||||||||||||||||||||||
# print(f" - '{field['fieldName']}' ({field['fieldType']})") | ||||||||||||||||||||||||||||||
print(f" - '{field['fieldName']}': '{field['fieldValues']}'") | ||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||
print(f"Failed to retrieve schemas: {e}") | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def get_user_custom_schemas(user_email): | ||||||||||||||||||||||||||||||
try: | ||||||||||||||||||||||||||||||
# Use projection='full' to include custom schemas in the response | ||||||||||||||||||||||||||||||
user = service.users().get(userKey=user_email, projection='full').execute() | ||||||||||||||||||||||||||||||
print(user) | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Remove or conditionally log sensitive user data. Printing the entire user object may expose sensitive PII data. Consider removing this debug print or making it conditional. - print(user)
+ # Uncomment below for detailed debugging (contains sensitive data)
+ # print(user) 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
custom_schemas = user.get('customSchemas', {}) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
print(f"Custom schemas for {user_email}:") | ||||||||||||||||||||||||||||||
for schema_name, schema_data in custom_schemas.items(): | ||||||||||||||||||||||||||||||
print(f" Schema: {schema_name}") | ||||||||||||||||||||||||||||||
for field_name, field_value in schema_data.items(): | ||||||||||||||||||||||||||||||
print(f" {field_name}: {field_value}") | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
return custom_schemas | ||||||||||||||||||||||||||||||
except Exception as e: | ||||||||||||||||||||||||||||||
print(f"Failed to retrieve user custom schemas: {e}") | ||||||||||||||||||||||||||||||
return None | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
def list_group_members(group_email): | ||||||||||||||||||||||||||||||
results = service.members().list(groupKey=group_email).execute() | ||||||||||||||||||||||||||||||
members = results.get('members', []) | ||||||||||||||||||||||||||||||
for member in members: | ||||||||||||||||||||||||||||||
# print(member['email']) | ||||||||||||||||||||||||||||||
print(member) | ||||||||||||||||||||||||||||||
Comment on lines
+74
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add exception handling and clean up commented code. This function lacks exception handling unlike the other functions. Add consistent error handling and remove commented debug code. def list_group_members(group_email):
- results = service.members().list(groupKey=group_email).execute()
- members = results.get('members', [])
- for member in members:
- # print(member['email'])
- print(member)
+ try:
+ results = service.members().list(groupKey=group_email).execute()
+ members = results.get('members', [])
+ for member in members:
+ print(member)
+ except Exception as e:
+ print(f"Failed to retrieve group members: {e}") 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
if __name__ == '__main__': | ||||||||||||||||||||||||||||||
# list_group_members('team@your-company.io') | ||||||||||||||||||||||||||||||
# list_user_schemas() | ||||||||||||||||||||||||||||||
get_user_custom_schemas('first.last@your-company.io') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify the Google OAuth2 import is available.
The static analysis tool flagged this import, but it appears to be the correct import for the google-auth library. Please ensure the
google-auth
package is installed.🏁 Script executed:
Length of output: 212
🏁 Script executed:
Length of output: 230
Ensure
google-auth
is declared as a dependencygoogle-auth
package isn’t installed.requirements.txt
,pyproject.toml
,setup.cfg
, etc.) was found in the repo.google-auth
to your project’s dependencies so tooling and runtime environments can resolve the module.For example, you can create or update a requirements file:
Or in
pyproject.toml
:[tool.poetry.dependencies] + google-auth = "^2.20.0"
🧰 Tools
🪛 Pylint (3.3.7)
[error] 11-11: No name 'oauth2' in module 'google'
(E0611)
🤖 Prompt for AI Agents