google-cloud-endpointsopenapigoogle-cloud-endpoints-v2

Cloud endpoints is it possible to protect all API accesses using only a base URL?


As in let's say my api is located at domain/_ah/api. We have domain/_ah/api/getUser, domain/_ah/api/stuff/getStuff, domain/_ah/api/stuff/moreStuff/postMoreStuff.

Is it possible to do that by only defining something like this?´

  swagger: '2.0'
  info:
    title: "Cloud Endpoints + Cloud Run"
    description: "Sample API on Cloud Endpoints with a Cloud Run backend"
    version: "1.0.0"
  host: "domain"
  schemes:
    - "https"
  produces:
    - "application/json"
  x-google-backend:
    jwt_audience: "audience"
    address: "domain_backend"
    protocol: "h2"
  paths:
    /_ah/api/*:
      get, post, put, etc:
        description: "Protects Base URL"
        operationId: "authInfoFirebase"
        security: 
          - firebase: []

  securityDefinitions:
    firebase:
      authorizationUrl: ""
      flow: "implicit"
      type: "oauth2"
      x-google-issuer: "https://securetoken.google.com/<project_id>"
      x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/metadata/x509/securetoken@system.gserviceaccount.com"
      x-google-audiences: "<project_id>"

Solution

  • I am afraid Cloud Endpoints does not recognize wildcards as you specified.

    Quoting the documentation:

    “Endpoints only supports URL path template parameters that correspond to entire path segments (delimited by slashes /). URL path template parameters that correspond to partial path segments aren't supported.”[1]

    A workaround to wildcards would be to use path templates. You can use curly braces {} to mark parts of an URL as path parameters, using your example:

    domain/_ah/api/{value1}

    domain/_ah/api/{value1}/{value2}

    domain/_ah/api/{value1}/{value2}/{value3}

    Just be careful not to overlap the path templates, like in this example:

    /items/{itemid} ---> This is valid

    /items/{itemId}/subitem ----> This is valid

    /items/cat ----> This is NOT valid

    [1] https://cloud.google.com/endpoints/docs/openapi/openapi-limitations#url_path_templating