How to access Gravitee AM api - api-gateway

How to curl the basic gravitee am api? i tried the simple one also cant?
curl -X POST http://localhost:8093/admin/token -H 'authorization: Basic base64(admin:adminadmin)'
curl -X POST http://localhost:8093/admin/token -H 'Authorization: Basic YWRtaW46YWRtaW5hZG1pbg=='
It stated 404 Not found. Result are below:
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8093 (#0)
> POST /admin/token HTTP/1.1
> Host: localhost:8093
> User-Agent: curl/7.58.0
> Accept: */*
> Authorization: Basic YWRtaW46YWRtaW5hZG1pbg==
>
< HTTP/1.1 404 Not Found
< Cache-Control: must-revalidate,no-cache,no-store
< Content-Type: text/html;charset=iso-8859-1
< Content-Length: 0
<
* Connection #0 to host localhost left intact

First you have to request an access token:
curl -X POST http://localhost:8093/management/auth/token -u admin:adminadmin
You should receive this payload:
{"access_token":"eyJraWQiOiJkZWZ......TyJ37E","expires_at":"Mon Nov 29 15:06:12 CET 2021","token_type":"bearer"}
Then you can use this access_token to request the access management API.
curl -X GET http://localhost:8093/management/organizations/DEFAULT/environments/DEFAULT/domains/ -H'Authorization: Bearer eyJraWQiOiJkZWZ......TyJ37E'

Related

can't use curl to query neo4j

I am trying to use curl to query neo4j
curl -X POST -H Accept:application/json -H Content-Type:application/json -u neo4j:password -v http://localhost:7474/db/neo4j/tx/commit -d '{"statements":[{"statement":"MATCH (n) RETURN n"}]}'
gives me this response
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1:7474...
* Connected to localhost (127.0.0.1) port 7474 (#0)
* Server auth using Basic with user 'neo4j'
> POST /db/neo4j/tx/commit HTTP/1.1
> Host: localhost:7474
> Authorization: Basic bmVvNGo6cGFzc3dvcmQ=
> User-Agent: curl/7.79.1
> Accept:application/json
> Content-Type:application/json
> Content-Length: 47
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Date: Wed, 27 Jul 2022 09:13:35 GMT
< Access-Control-Allow-Origin: *
< Content-Type: application/json
< Content-Length: 120
<
{"results":[],"errors":[{"code":"Neo.ClientError.Request.InvalidFormat","message":"Could not parse the incoming JSON"}]}* Connection #0 to host localhost left intact
If anyone could help please
Should have mentioned I'm on windows. Apparently you have to escape those double quotes in the json
This works for me now:
curl -X POST -H Accept:application/json -H Content-Type:application/json -u neo4j:password -v http://localhost:7474/db/neo4j/tx/commit -d "{\"statements\":[{\"statement\":\"MATCH (n) RETURN n\"}]}"

Keycloak, adding realm role to user via admin api on curl

Using keycloak 6.0.1
I have created the realm role and the user. I have the user uuid and the role uuid.
If I run
curl -v -X POST -w "\n" http://localhost:8080/auth/admin/realms/SpringBootKeycloak/users/$USER_ID/role-mappings/realm -H "Content-Type: application/json" -H "Authorization: bearer $TOKEN" --data '[{"id":$ROLE_ID,"name":"user"}]'
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /auth/admin/realms/SpringBootKeycloak/users/fe38bcb5-258b-44e4-a056-cf8c1a29b99f/role-mappings/realm HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.58.0
> Accept: */*
> Content-Type: application/json
> Authorization: bearer very long string
> Content-Length: 31
>
* upload completely sent off: 31 out of 31 bytes
< HTTP/1.1 500 Internal Server Error
< Connection: keep-alive
< Content-Length: 0
< Date: Fri, 13 Sep 2019 08:53:14 GMT
<
* Connection #0 to host localhost left intact
I get a server error and a log entry of
10:00:55,903 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-10) Uncaught server error: com.fasterxml.jackson.databind.JsonMappingException: Unrecognized token '$ROLE_ID': was expecting ('true', 'false' or 'null')
at [Source: (io.undertow.servlet.spec.ServletInputStreamImpl); line: 1, column: 3] (through reference chain: java.util.ArrayList[0])
Alternatively if I run curl with data of
--data '[{"id":"$ROLE_ID","name":"user"}]'
then get return of 404 not found.
If I set data to be hardcoded then it works
--data '[{"id":"9b6371f2-646d-4927-b3b6-6e208935517e","name":"user"}]'
but get return of 204 No Content which is success. And the role is added.
Further investigation -
--data '[{"id":"'$ROLE_ID'","name":"user"}]'
gives a 204 status and the role is added to the user.
So get there eventually.
That is a problem with variable substitution:
$ export ROLE_ID=9b6371f2-646d-4927-b3b6-6e208935517e
$ echo '[{"id":"'$ROLE_ID'","name":"user"}]'
[{"id":"9b6371f2-646d-4927-b3b6-6e208935517e","name":"user"}]
$ echo '[{"id":$ROLE_ID,"name":"user"}]'
[{"id":$ROLE_ID,"name":"user"}]
$ echo '[{"id":"$ROLE_ID","name":"user"}]'
[{"id":"$ROLE_ID","name":"user"}]

Elastic Search Bulk API - Not indexing

I have output a JSON file in bulk format which I can load in to Kibana with the developer tools. and by inserting a few lines using the -d command
example lines of file:
{"index":{"_index":"els","_type":"logs","_id":1481018400003}}
{"timestamp":1481018400003,"zoneId":29863567,............[]}
{"index":{"_index":"els","_type":"logs","_id":"30cee368073c0c9b"}}
{"timestamp":1481018400005,"zoneId":29863567,............[]}
...
However when I run the bulk api to pot a file it does not do anything. I added verbose to the command and get the following:
* Connected to localhost (::1) port 9200 (#0)
> POST /_bulk HTTP/1.1
> Host: localhost:9200
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Length: 0
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 400 Bad Request
< content-type: application/json; charset=UTF-8
< content-length: 165
* HTTP error before end of send, stop sending
Any help would be great.
Thanks!

Can't insert variable with two headers in curl

I have following sh script
#!/usr/bin/env bash
headers='-H "custom1: ololo1" -H "custom2: ololo2"'
value_for_header="value"
curl -X "PUT" -H "Content-Type: application/json" -H "custom_ololo: $value_for_header" $headers http://localhost:8000/ -d '{"a": true}' -vv
Log when execute it:
* Rebuilt URL to: ololo1"/
* Hostname was NOT found in DNS cache
* Could not resolve host: ololo1"
* Closing connection 0
curl: (6) Could not resolve host: ololo1"
* Rebuilt URL to: ololo2"/
* Hostname was NOT found in DNS cache
* Could not resolve host: ololo2"
* Closing connection 1
curl: (6) Could not resolve host: ololo2"
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#2)
> PUT / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8000
> Accept: */*
> Content-Type: application/json
> custom_ololo: value
> Content-Length: 40
>
* upload completely sent off: 40 out of 40 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 400
< Date: Thu, 21 Jul 2016 12:32:13 GMT
< Server: WSGIServer/0.1 Python/2.7.6
< X-Frame-Options: SAMEORIGIN
< Content-Type: application/json
<
* Closing connection 2
As we can see -H "custom_ololo: $value_for_header" works well > custom_ololo: value
But string $headers is not inserted correctly. I've tried put "$headers" and ${headers} but no result
So, my question is: How is correctly insert strings with several headers into sh script with curl.
You need to use an array, at which point you can put all the headers in the array and simplify your call.
#!/usr/bin/env bash
value_for_header="value"
headers=(
-H "custom1: ololo1"
-H "custom2: ololo2"
-H "Content-Type: application/json"
-H "custom_ololo: $value_for_header"
)
curl -X "PUT" "${headers[#]}" http://localhost:8000/ -d '{"a": true}' -vv
You need to put $headers into ""
curl -X "PUT" -H "Content-Type: application/json" -H "custom_ololo: $value_for_header" "$headers" http://localhost:8000/ -d '{"a": true}' -vv

Django-rest-framework token auth doesn't work

I'm trying to POST json data to url, decorated with login_required, but django returns redirect to login page
DRF setup:
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
),
and rest_framework.authtoken in INSTALLED_APPS
I can obtain auth token via curl
$ curl -X POST -d "{\"username\" : 7, \"password\" : 1}" -H "Content-Type: application/json" http://127.0.0.1:9000/extapi/get-auth-token/
{"token":"bc61497d98bed02bd3a84af2235365d0b2b549ff"}
But when i POST to the view, decorated with login_required, django returns http 302 with Location header pointing to the login page.
$ curl -v -X POST -d '{"event":"14","user":"7","action":"1868","unit":"","value":"-1"}' -H "Content-Type: application/json" -H "Authorization: Token bc61497d98bed02bd3a84af2235365d0b2b549ff" http://127.0.0.1:9000/zk2015/events/actions/api/uservotejournal/7/
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 9000 (#0)
> POST /zk2015/events/actions/api/uservotejournal/7/ HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 127.0.0.1:9000
> Accept: */*
> Content-Type: application/json
> Authorization: Token bc61497d98bed02bd3a84af2235365d0b2b549ff
> Content-Length: 64
>
* upload completely sent off: 64 out of 64 bytes
< HTTP/1.1 302 FOUND
* Server nginx/1.4.6 (Ubuntu) is not blacklisted
< Server: nginx/1.4.6 (Ubuntu)
< Date: Fri, 18 Sep 2015 11:14:31 GMT
< Content-Type: text/html; charset=utf-8
< Location: http://127.0.0.1:9000/accounts/login/?next=/zk2015/events/actions/api/uservotejournal/7/
< Transfer-Encoding: chunked
< Connection: keep-alive
< Vary: Cookie
< X-Frame-Options: SAMEORIGIN
< ETag: "d41d8cd98f00b204e9800998ecf8427e"
< Set-Cookie: csrftoken=G85fWrKKsIA5a2uGPIn9fS4pqKrS51jK; expires=Fri, 16-Sep-2016 11:14:31 GMT; Max-Age=31449600; Path=/
<
* Connection #0 to host 127.0.0.1 left intact
I've tried to set breakpoints in rest_framework.authentication.SessionAuthentication and rest_framework.authentication.TokenAuthentication, but they were never fired
What is wrong in my setup? Help, please.
You are not passing the Authorization in Header in the curl
curl -X POST -d "{\"username\" : 7, \"password\" : 1}" -H "Authorization: Token bc61497d98bed02bd3a84af2235365d0b2b549ff" http://127.0.0.1:9000/extapi/get-auth-token/
The point is that request.user is AnonymousUser in drf.APIView.dispatch(), but is defined as authorized user in drf.APIView.post() and other similar methods.
This differs from django: request.user is defined as authorized user in django.views.View.dispatch()
Also that is the cause, why django.contrib.auth.decorators.login_required is not compatible whith drf views.

Resources