Skip to content

Commit 56ec7a9

Browse files
committed
fix(source/cloud-sql-admin): validate source instance for backup-run 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.
1 parent 35c13ad commit 56ec7a9

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

internal/sources/cloudsqladmin/cloud_sql_admin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (s *Source) RestoreBackup(ctx context.Context, targetProject, targetInstanc
412412
// 'projects/{project-id}/backups/{backup-uid}'. In this case, the Backup
413413
// field should be populated.
414414
if backupRunID, err := strconv.ParseInt(backupID, 10, 64); err == nil {
415-
if sourceProject == "" || targetInstance == "" {
415+
if sourceProject == "" || sourceInstance == "" {
416416
return nil, fmt.Errorf("source project and instance are required when restoring via backup ID")
417417
}
418418
request.RestoreBackupContext = &sqladmin.RestoreBackupContext{

tests/cloudsql/cloud_sql_restore_backup_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ func TestRestoreBackupToolEndpoints(t *testing.T) {
186186
body: `{"target_project": "p1", "target_instance": "instance-project-level", "backup_id": "12345"}`,
187187
want: `{"error":"error processing GCP request: source project and instance are required when restoring via backup ID"}`,
188188
},
189+
{
190+
name: "missing source instance with source project for standard backup",
191+
toolName: "restore-backup",
192+
body: `{"target_project": "p1", "target_instance": "instance-project-level", "backup_id": "12345", "source_project": "p1"}`,
193+
want: `{"error":"error processing GCP request: source project and instance are required when restoring via backup ID"}`,
194+
},
189195
{
190196
name: "missing backup identifier",
191197
toolName: "restore-backup",

0 commit comments

Comments
 (0)