npx skills add ...
npx skills add google/skills --skill iam-recommendations-fetcher
Fetches raw IAM recommendations and associated security insights from Google Cloud for a specified target scope (Organization, Folder, or Project). Use when you need to retrieve security recommendations before analyzing or applying them. Don't use for applying/acting on recommendations (use the recommendation applier skill) or for general allow policy querying (use the allow policy viewer skill).
npx skills add google/skills --skill iam-recommendations-fetcher
This skill provides instructions for fetching IAM recommendations and insights from Google Cloud. It covers validating the input target scope, retrieving recommendations using MCP tools, gcloud commands, or direct API calls, and handling common API errors.
Verify the input target scope.
Check Format: Check if the target matches one of these formats:
organizations/{org_id}folders/{folder_id}projects/{project_id}Handle Ambiguous/Raw Target: If the user provides only a raw ID (without the prefix):
projects/{project_id} and proceed.organizations/, folders/, or projects/) and proceed.Handle Project Numbers: If the target is a project but uses a purely
numeric ID (Project Number) instead of a Project ID (e.g.,
projects/123456789):
gcloud recommender commands require a Project ID. Attempt to resolve
the Project Number to a Project ID using: gcloud projects list --filter="projectNumber={project_number}" --format="value(projectId)"Record Validation: Explicitly state the validated and formatted target
scope (e.g., projects/123456789 or organizations/123456789012) in your
thought/reasoning before proceeding to the fetch step.
Attempt the following retrieval methods in order. Stop at the first success.
CRITICAL: Fail Fast on Errors (To avoid redundant API calls that will fail
for the same authorization/permission reasons): If any attempted method (Option
A or Option B) fails with an API-level error (such as PERMISSION_DENIED,
UNAUTHENTICATED, or resource NOT_FOUND / does not exist) or a CLI validation
error (such as project number not allowed), do NOT attempt any further
options (including Option C or direct API/curl calls). Stop immediately, do
not call any more tools, and return the standardized error JSON as specified in
the "Handle Errors" section.
Use the MCP tool if available. MCP tools are designed for efficient, secure execution within Google's internal environments, often providing streamlined authentication and better integration compared to general-purpose CLI commands.
If an IAM Recommender MCP tool is available in your context:
target parameter.If MCP is unavailable, use run_command to execute the following (replace
variables accordingly):
| Target | Flag |
|---|---|
projects/{project_id} | --project={project_id} |
folders/{folder_id} | --folder={folder_id} |
organizations/{organization_id} | --organization={organization_id} |
Commands to run:
Only attempt this option if gcloud is physically unavailable in the
environment (e.g., gcloud: command not found). API client libraries require
more setup and execution overhead, so they are only used as a last resort if CLI
tools are missing. Do NOT use this option if gcloud is available but failed
with an API error.
Use Google Cloud Recommender API client libraries via a helper script (or direct API calls if libraries are unavailable) to:
list_recommendations (or recommendations.list) for
google.iam.policy.Recommender (filter: stateInfo.state=ACTIVE).list_insights (or insights.list) for google.iam.policy.Insight
(filter: stateInfo.state=ACTIVE).CRITICAL: Only proceed to this step if the retrieval in Step 2 was successful. If the retrieval failed, skip this step and go directly to Handle Errors.
Before presenting the results, determine the desired output format.
CRITICAL: If the user's initial prompt already specifies the output format (e.g., "return the raw results in JSON" or "show it in a table"), bypass asking and proceed directly to that format.
Otherwise, ask the user in a dropdown menu, with the options being:
Based on the choice (either pre-specified or chosen by the user), deliver the output:
iam_recommendations_<target_id>_<timestamp>.json (where <target_id> is
the sanitized resource identifier and <timestamp> is formatted as
YYYYMMDD_HHMMSS) in the current working directory.associatedInsights).iam_recommendations_<target_id>_<timestamp>.md, where <target_id> is the
sanitized resource identifier and <timestamp> is formatted as
YYYYMMDD_HHMMSS) and provide a link for the user to download it.INSIGHT_ID).INSIGHT_STATE).INSIGHT_SUBTYPE).DESCRIPTION).Input Target: projects/my-test-project
Mapped Flag: --project=my-test-project
Action (Option B): Run gcloud recommender recommendations list --recommender=google.iam.policy.Recommender --format=json --location=global --filter=stateInfo.state=ACTIVE --project=my-test-project
Expected Output Structure:
CRITICAL: If you encounter any of the error conditions below (during validation or fetch), stop immediately. Do not attempt to debug, switch accounts, search the codebase, or verify resource existence. Immediately output the specified JSON structure as your final response and call no further tools.
If all methods fail, return:
{"raw_results": null, "error": "The specified target resource is incorrect or does not exist."}{"raw_results": null, "error": "User is unauthenticated. Please authenticate (e.g., run 'gcloud auth login')."}{"raw_results": null, "error": "Insufficient permissions. Please ensure you have 'roles/recommender.iamViewer' on the target scope."}{"raw_results": null, "error": "Failed to fetch: {error_details}"}
(Do not mention MCP tool failures to the user).ACTIVE
recommendations.