Как исправить: ошибка 403 с Google Cloud SQL Export - PullRequest
0 голосов
/ 09 мая 2019

Я пытаюсь экспортировать полный дамп SQL одного из наших экземпляров Cloud SQL Postgres в Google Cloud Storage, чтобы мы могли делать более частые резервные копии, используя google-api-go-client (https://github.com/googleapis/google-api-go-client)

)

Независимо от того, что я настраиваю, я получаю эту ошибку: panic: googleapi: Error 403: The client is not authorized to make this request., notAuthorized от sqladminService.Instances.Export.

У меня есть учетная запись службы со следующими разрешениями:

  • Облачный SQL Admin
  • Администратор хранилища
  • Compute Storage Admin (для чего-то другого)

Корзина, в которую я экспортирую, наследует разрешения от роли администратора хранилища.

Код: ./k8s/sql.go

package gcp

import (
    "fmt"
    "time"

    "golang.org/x/net/context"
    "golang.org/x/oauth2/google"
    sqladmin "google.golang.org/api/sqladmin/v1beta4"
)

type SQLService interface {
    Test(project string) error
}

type sqlService struct {
    context         context.Context
    sqladminService *sqladmin.Service
}

func NewSQLService(serviceAccountJSON []byte) (SQLService, error) {
    context := context.Background()

    jwtCfg, err := google.JWTConfigFromJSON(serviceAccountJSON, sqladmin.SqlserviceAdminScope, sqladmin.CloudPlatformScope)
    if err != nil {
        return sqlService{}, err
    }
    httpClient := jwtCfg.Client(context)

    sqladminService, err := sqladmin.New(httpClient)
    if err != nil {
        return sqlService{}, err
    }

    return sqlService{
        context:         context,
        sqladminService: sqladminService,
    }, nil
}

func (s sqlService) Test(project string) error {
    instance := "REGION:INSTANCE_NAME

    storageURI := fmt.Sprintf("gs://BUCKET/FILE-%s.sql.gz", time.Now().Format(time.RFC3339))
    databases := []string{"DATABASE"}

    req := &sqladmin.InstancesExportRequest{
        ExportContext: &sqladmin.ExportContext{
            Uri:       storageURI,
            Databases: databases,
        },
    }

    _resp, err := s.sqladminService.Instances.Export(project, instance, req).Context(s.context).Do()
    if err != nil {
        return err
    }

    return nil
}

Тестовый код:

func Test(cfg config.Config) {
    sql, err := gcp.NewSQLService(cfg.GCPServiceAccountEncodedCreds)
    if err != nil {
        panic(err)
    }

    err = sql.Test(cfg.Project)
    if err != nil {
        panic(err)
    }
}

Любая помощь будет оценена

1 Ответ

0 голосов
/ 09 мая 2019

Документация для InstancesExport показывает, что обязательными параметрами являются "projectId" и "instanceId". Вы объявили instance как "REGION:INSTANCE_NAME" - но вы действительно хотите "INSTANCE_NAME".

Вы не авторизованы для просмотра этого экземпляра (потому что в этом случае он не существует).

...