Skip to content

fix(source/cloud-sql-admin): validate source instance for backup-run restore#3555

Open
anxkhn wants to merge 1 commit into
googleapis:mainfrom
anxkhn:loop/mcp-toolbox__003
Open

fix(source/cloud-sql-admin): validate source instance for backup-run restore#3555
anxkhn wants to merge 1 commit into
googleapis:mainfrom
anxkhn:loop/mcp-toolbox__003

Conversation

@anxkhn

@anxkhn anxkhn commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Description

Source.RestoreBackup in internal/sources/cloudsqladmin/cloud_sql_admin.go validates the wrong field on the BackupRun-ID restore path. When backup_id is a numeric BackupRun ID, the code populates RestoreBackupContext from sourceProject and sourceInstance, but the guard checks sourceProject and targetInstance:

if backupRunID, err := strconv.ParseInt(backupID, 10, 64); err == nil {
    if sourceProject == "" || targetInstance == "" { // checks targetInstance
        return nil, fmt.Errorf("source project and instance are required when restoring via backup ID")
    }
    request.RestoreBackupContext = &sqladmin.RestoreBackupContext{
        Project:     sourceProject,
        InstanceId:  sourceInstance, // sourceInstance is what's actually consumed
        BackupRunId: backupRunID,
    }
}

targetInstance is already mandatory for the API call itself (service.Instances.RestoreBackup(targetProject, targetInstance, request)), so the guard never adds anything for it. The only thing it lets through is an empty source_instance, which then sends a malformed RestoreBackupContext{InstanceId: ""} to the API.

The error message already reads "source project and instance are required", and both the tool parameter description (internal/tools/cloudsql/cloudsqlrestorebackup/cloudsqlrestorebackup.go) and the docs (docs/en/integrations/cloud-sql-admin/tools/cloudsqlrestorebackup.md) state that both source_project and source_instance are required when backup_id is a BackupRun ID. So this is a one-character copy/paste bug, not intended behavior.

Fix: guard on sourceInstance == "" instead of targetInstance == "". The error string is unchanged. A regression row is added to the existing tests/cloudsql/cloud_sql_restore_backup_test.go table for the case where source_project is set but source_instance is omitted, which fails before the fix (an empty InstanceId reaches the API) and passes after.

PR Checklist

  • Make sure you reviewed
    CONTRIBUTING.md
  • Make sure to open an issue as a
    bug/issue
    before writing your code! That way we can discuss the change, evaluate
    designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)
  • Make sure to add ! if this involve a breaking change

No tracking issue was opened: this is a small, self-contained correctness fix with a regression test, and the open type: bug issues are already assigned. Happy to file one and link it with Fixes #<n> 🦕 if you would prefer that first. The docs and the tool parameter description already mark both source fields as required, so no docs change is needed; this is not a breaking change (validation is only tightened for input that would already have failed at the API).

@anxkhn
anxkhn requested a review from a team as a code owner June 30, 2026 21:59
@google-cla

google-cla Bot commented Jun 30, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request corrects a validation check in the Cloud SQL Admin source's RestoreBackup function, ensuring that sourceInstance is verified instead of targetInstance when restoring via a backup ID. A new integration test case has been added to cover this scenario. I have no feedback to provide as there are no review comments.

@anxkhn
anxkhn force-pushed the loop/mcp-toolbox__003 branch from 74d8b34 to 56ec7a9 Compare July 1, 2026 07:40
…restore

RestoreBackup's BackupRun-ID path consumes sourceProject + sourceInstance, but the guard required sourceProject + targetInstance. targetInstance is already mandatory for the RestoreBackup API call, so an empty source_instance slipped through and a malformed RestoreBackupContext{InstanceId:""} was sent. Validate sourceInstance, matching the tool parameter and docs that mark both source fields required when backup_id is a BackupRun ID. Adds a regression test case.
@anxkhn
anxkhn force-pushed the loop/mcp-toolbox__003 branch from 56ec7a9 to 8be4de2 Compare July 1, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants