pegasus-io / this-is-gravitee

Gravitee On earth
0 stars 0 forks source link

The CORS Configuration problem #5

Open Jean-Baptiste-Lasselle opened 4 years ago

Jean-Baptiste-Lasselle commented 4 years ago

Description of issue

Gravitee APIM version : 1.30.11

Checking what allow-access-origin-* are allowed for a given API in Gravitee

curl -iv https://${GRAVITEE_APIM_GATEWAY}:443/apiverte -H "Access-Control-Request-Method: GET" -H "Origin: https://sub.domain2.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b" |& tee  masortie.out | tail -n 1 | jq .
cat masortie.out | grep 'access-control-allow-'
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ # curl -iv https://${GRAVITEE_APIM_GATEWAY}:443/apiverte -H "Access-Control-Request-Method: GET" -H "Origin: https://sub.domain2.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b" |& tee  masortie.out | tail -n 1 | jq .
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ cat masortie.out | grep 'access-control-allow-'
< access-control-allow-origin: *
access-control-allow-origin: *
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}"jbl@poste-devops-jbl-16gbram:~/atelier-helm$ echo "curl -k -X GET ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}" | jq ."
curl -k -X GET http://127.0.0.1:8484/management/apis/c8e32a32-9e94-41be-a32a-329e9401be26 -H 'Accept: application/json' -H 'Content-Type: application/json' -H Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiobfuscatedZlZjNiYzkiLCJmaXJobfuscatedpc3Npbobfuscated3JpdHkiobfuscatedRNSU4ifV0sImlzcyI6ImdyYXZpdGVobfuscatednQtYXobfuscated2NzobfuscatedPPksQfyH3JawS8g | jq .
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ echo "URL_APPEL_GRAVITEE_APIM_API=[${URL_APPEL_GRAVITEE_APIM_API}]"URL_APPEL_GRAVITEE_APIM_API=[http://127.0.0.1:8484/management/apis/c8e32a32-9e94-41be-a32a-329e9401be26]
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ curl -k -X GET ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}" | jq .
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2204  100  2204    0     0  28684      0 --:--:-- --:--:-- --:--:-- 29000
{
  "id": "c8e32a32-9e94-41be-a32a-329e9401be26",
  "name": "apiVerte",
  "version": "4.1.85",
  "description": "une nouvelle description",
  "visibility": "private",
  "state": "started",
  "tags": [],
  "entrypoints": [
    {
      "target": "https://api.company.com/apiverte"
    }
  ],
  "proxy": {
    "virtual_hosts": [
      {
        "path": "/apiverte"
      }
    ],
    "strip_context_path": false,
    "preserve_host": false,
    "groups": [
      {
        "name": "default-group",
        "endpoints": [
          {
            "name": "default",
            "target": "https://randomuser.me/api",
            "weight": 1,
            "backup": false,
            "type": "HTTP",
            "http": {
              "connectTimeout": 5000,
              "idleTimeout": 60000,
              "keepAlive": true,
              "readTimeout": 10000,
              "pipelining": false,
              "maxConcurrentConnections": 100,
              "useCompression": true,
              "followRedirects": false,
              "encodeURI": false
            }
          }
        ],
        "load_balancing": {
          "type": "ROUND_ROBIN"
        },
        "http": {
          "connectTimeout": 5000,
          "idleTimeout": 60000,
          "keepAlive": true,
          "readTimeout": 10000,
          "pipelining": false,
          "maxConcurrentConnections": 100,
          "useCompression": true,
          "followRedirects": false,
          "encodeURI": false
        }
      }
    ],
    "cors": {
      "enabled": true,
      "allowCredentials": true,
      "allowOrigin": [
        "https://sub.domain1.com",
        "https://sub.domain2.com"
      ],
      "allowHeaders": [],
      "allowMethods": [
        "TRACE",
        "HEAD",
        "DELETE",
        "POST",
        "GET",
        "OPTIONS",
        "PATCH",
        "PUT"
      ],
      "exposeHeaders": [],
      "maxAge": -1
    }
  },
  "paths": {
    "/": []
  },
  "deployed_at": 1594384046615,
  "created_at": 1594373500593,
  "updated_at": 1594390129549,
  "owner": {
    "id": "4f39d236-bb46-4f3b-b9d2-36bb46ef3bc9",
    "email": "",
    "displayName": ""
  },
  "services": {},
  "picture_url": "http://127.0.0.1:8484/management/apis/c8e32a32-9e94-41be-a32a-329e9401be26/picture",
  "resources": [],
  "path_mappings": [],
  "response_templates": {},
  "lifecycle_state": "created"
}

Solution found : but stil...

# allow-cross-origin HTTP metadata here will appear
curl -iv https://${GRAVITEE_APIM_GATEWAY}:443/apiverte -H "Access-Control-Request-Method: GET" -H "Origin: https://sub.domain2.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b" |& tee  masortie.out | tail -n 1 | jq .
cat masortie.out | grep 'access-control-allow-'

curl -X OPTIONS -iv https://${GRAVITEE_APIM_GATEWAY}:443/apiverte -H "Access-Control-Request-Method: OPTIONS" -H "Origin: https://sub.domain2.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b" |& tee  masortie.options.out | tail -n 1 | jq .
cat masortie.options.out | grep 'access-control-allow-'
* So indeed, now My Configuration on Gravitee Applies for sure, we no longer have : 

```bashjbl@poste-devops-jbl-16gbram:~/atelier-helm$ cat masortie.out | grep 'access-control-allow-'
< access-control-allow-origin: *
access-control-allow-origin: *
curl -ivk https://${GRAVITEE_APIM_GATEWAY_HOST}:443/apiverte -H "Access-Control-Request-Method: GET" -H "Origin: https://unalloawed.domain3.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b"

< {"results":[{"gender":"male","name":{"title":"Mr","first":"Per","last":"Grøstad"},"location":{"street":{"number":5071,"name":"Eiriks gate"},"city":"Fannrem","state":"Akershus","country":"Norway","postcode":"0240","coordinates":{"latitude":"56.9742","longitude":"-102.9261"},"timezone":{"offset":"-9:00","description":"Alaska"}},"email":"per.grostad@example.com","login":{"uuid":"c4182206-4729-4c8c-8bc3-f1b033f90adc","username":"silverduck992","password":"rebels","salt":"tBlKUGWW","md5":"5d65c6e03be544bc643c5f0ad75809b7","sha1":"2397e927eb28eb8f48144b2ac806b7c6520ed477","sha256":"f85a40e6f5fa2d9c0b07622ef83a4c0c423b1341ad9cb4f8e9d761c023ad29b3"},"dob":{"date":"1949-05-04T17:48:47.690Z","age":71},"registered":{"date":"2014-11-13T18:59:50.434Z","age":6},"phone":"31950687","cell":"45957016","id":{"name":"FN","value":"04054909581"},"picture":{"large":"https://randomuser.me/api/portraits/men/4.jpg","medium":"https://randomuser.me/api/portraits/med/men/4.jpg","thumbnail":"https://randomuser.me/api/portraits/thumb/men/* Curl_http_done: called premature == 0

Re-deploy an Out-Of-Sync API in Gravitee

Here below is the shell script that I tested (works) :

# -------------------------------------------------------------------------
# ENV 'Gravitee APIM API'
# Gravitee APIM version : `1.30.11`
# -------------------------------------------------------------------------
#
export GRAVITEE_APIM_USER_NAME=admin
export GRAVITEE_APIM_USER_PWD=admin
export GRAVITEE_APIM_API_HOST=apim.gravitee.io
export GRAVITEE_APIM_API_HOST=127.0.0.1
export GRAVITEE_APIM_API_PORT=8484
export GRAVITEE_APIM_API_PROTOCOL=https
export GRAVITEE_APIM_API_PROTOCOL=http

# --
export GRAVITEE_APIM_API_TOKEN=$(cat ./my.gravitee-apim.api.token.json | jq -r '.token')
echo "GRAVITEE_APIM_API_TOKEN=[${GRAVITEE_APIM_API_TOKEN}]"

# re-deploy out of sync api like DEMO WEB UI : is it just running the deploy operation again ?

export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}/deploy"
curl -k -X POST ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}" | jq .

# re-deploy out of sync api like DEMO WEB UI : is it just running the deploy operation again ? No, did not work fro me.

# --- STOP the API
# So (n+1=0) to return to initial state : first : stop the started API
export MY_API_GRAVITEE_UID=$(cat my.gravitee-apim.apiVerte.json | jq .id | awk -F '"' '{print $2}')
echo "MY_API_GRAVITEE_UID=[${MY_API_GRAVITEE_UID}]"
# could be either START or STOP
export GRAVITEE_API_LICYCLE_ACTION=STOP
export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}?api=${MY_API_GRAVITEE_UID}&action=${GRAVITEE_API_LICYCLE_ACTION}"

curl -k -X POST ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}"

# --- DEPLOY API
export MY_API_GRAVITEE_UID=$(cat my.gravitee-apim.apiVerte.json | jq .id | awk -F '"' '{print $2}')
echo "MY_API_GRAVITEE_UID=[${MY_API_GRAVITEE_UID}]"
export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}/deploy?api=${MY_API_GRAVITEE_UID}"

curl -k -X POST ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}" | jq .

# ---
# --- REVIEW API before STARTING IT
# --- https://docs.gravitee.io/apim/1.x/management-api/1.30/#operation/doReviewAction
#
export MY_API_GRAVITEE_UID=$(cat my.gravitee-apim.apiVerte.json | jq .id | awk -F '"' '{print $2}')
echo "MY_API_GRAVITEE_UID=[${MY_API_GRAVITEE_UID}]"
# could be  either "ASK" or "ACCEPT"
export GRAVITEE_API_REVIEWS_ACTION=ASK
export GRAVITEE_API_REVIEWS_MSG="I am $(whoami) -bot and silently reviewed the Gravitee API of UID [${MY_API_GRAVITEE_UID}]"
export GRAVITEE_API_REVIEWS_MSG="simpleotherreviewmessageforurlencoding"

export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}/reviews?api=${MY_API_GRAVITEE_UID}&action=${GRAVITEE_API_REVIEWS_ACTION}"

# That'"s going to be the reviewer's (me) comment"
export PAYLOAD="{
  \"message\": \"${GRAVITEE_API_REVIEWS_MSG}\"
}"

curl -ivk -X POST ${URL_APPEL_GRAVITEE_APIM_API} --data "${PAYLOAD}" -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}"

# The HTTP 204 No Content success status response code indicates that the request has succeeded, but that the client doesn't need to go away from its current page. A 204 response is cacheable by default. An ETag header is included in such a response.
# The common use case is to return 204 as a result of a PUT request, updating a resource, without changing the current content of the page displayed to the user. If the resource is created, 201 Created is returned instead. If the page should be changed to the newly updated page, the 200 should be used instead.

# --- ALSO NEED TO ACCEPT REVIEW 
# --- (Note VALIDATION COULD BE SET TO AUTO ACCEPT DEPLOYMENTS)

export GRAVITEE_API_REVIEWS_ACTION=ACCEPT
export GRAVITEE_API_REVIEWS_MSG="I am $(whoami) -bot and silently reviewed the Gravitee API of UID [${MY_API_GRAVITEE_UID}]"
export GRAVITEE_API_REVIEWS_MSG="acceptingapiotherreviewfrombot"

export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}/reviews?api=${MY_API_GRAVITEE_UID}&action=${GRAVITEE_API_REVIEWS_ACTION}"

# That'"s going to be the reviewer's (me) comment"
export PAYLOAD="{
  \"message\": \"${GRAVITEE_API_REVIEWS_MSG}\"
}"

curl -k -X POST ${URL_APPEL_GRAVITEE_APIM_API} --data "${PAYLOAD}" -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}"

# expected : HTTP 204

# --- (RE-)START the API
export MY_API_GRAVITEE_UID=$(cat my.gravitee-apim.apiVerte.json | jq .id | awk -F '"' '{print $2}')
echo "MY_API_GRAVITEE_UID=[${MY_API_GRAVITEE_UID}]"
# could be either START or STOP
export GRAVITEE_API_LICYCLE_ACTION=START
export URL_APPEL_GRAVITEE_APIM_API="${GRAVITEE_APIM_API_PROTOCOL}://${GRAVITEE_APIM_API_HOST}:${GRAVITEE_APIM_API_PORT}/management/apis/${MY_API_GRAVITEE_UID}?api=${MY_API_GRAVITEE_UID}&action=${GRAVITEE_API_LICYCLE_ACTION}"

curl -k -X POST ${URL_APPEL_GRAVITEE_APIM_API} -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer ${GRAVITEE_APIM_API_TOKEN}"

Comparing to swagger.io 's Petstore CORS Configuration

jbl@poste-devops-jbl-16gbram:~/atelier-helm/config-api-verte/configs/jullet/semaine2/integration$ curl -I https://${GRAVITEE_APIM_GATEWAY_HOST}:443/apiverte -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b"
HTTP/2 200 
cache-control: no-cache
cf-cache-status: DYNAMIC
cf-ray: 5b1442b07bf6e678-LHR
cf-request-id: 03e088024c0000e67826a0c200000001
content-type: application/json; charset=utf-8
date: Sat, 11 Jul 2020 17:31:51 GMT
etag: W/"484-VyZcJbM8iMj8PygzruibXfPhXY8"
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
server: cloudflare
set-cookie: __cfduid=d32339f88cd6871adee789c0e446b07b71594488711; expires=Mon, 10-Aug-20 17:31:51 GMT; path=/; domain=.randomuser.me; HttpOnly; SameSite=Lax
vary: Accept-Encoding
vary: Accept-Encoding
x-gravitee-transaction-id: 6214f98a-ebc2-4af3-94f9-8aebc2caf3e7
x-powered-by: Express

jbl@poste-devops-jbl-16gbram:~/atelier-helm/config-api-verte/configs/jullet/semaine2/integration$ curl -I "https://petstore.swagger.io/v2/swagger.json"HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type, api_key, Authorization
Access-Control-Allow-Methods: GET, POST, DELETE, PUT
Access-Control-Allow-Origin: *
Content-Length: 0
Content-Type: application/json
Date: Sat, 11 Jul 2020 17:32:22 GMT
Server: Jetty(9.2.9.v20150224)
Connection: keep-alive

jbl@poste-devops-jbl-16gbram:~/atelier-helm/config-api-verte/configs/jullet/semaine2/integration$ 
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ # curl -iv https://${GRAVITEE_APIM_GATEWAY_HOST}:443/apiverte -H "Access-Control-Request-Method: GET" -H "Origin: https://sub.domain2.com" -H "X-Gravitee-Api-Key: 6f90f6ec-4c4e-4029-aa0c-b79826dec06b" |& tee  masortie.out | tail -n 1 | jq .
jbl@poste-devops-jbl-16gbram:~/atelier-helm$ cat masortie.out | grep 'access-control-allow-'
< access-control-allow-origin: *
access-control-allow-origin: *
Jean-Baptiste-Lasselle commented 4 years ago

Ok, so to properly test CORS, I am implementing this :