How to generate an IBM Cloud token from an API Key - ibm-cloud

I have generated an API key for IBM Cloud, how do I programmatically generate a token from the API key?

Here is a curl request to do that.
curl --location --request POST 'https://iam.cloud.ibm.com/identity/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Authorization: Basic Yng6Yng=' \
--data-urlencode 'apikey=xxx' \
--data-urlencode 'response_type=cloud_iam' \
--data-urlencode 'grant_type=urn:ibm:params:oauth:grant-type:apikey'
Replace xxx with your API key.
The basic authorization is base64 encoded with a username of bx and a password of bx.
The response looks like the following:
{
"access_token": "bearer token",
"refresh_token": "refresh token",
"token_type": "Bearer",
"expires_in": 3600,
"expiration": 1581031424,
"scope": "ibm openid"
}

Related

Activate flow using Tooling API

I am trying to activate a flow using Tooling REST API. Documentation indicates that this is possible.
But when I try to do it I get this error in http response body:
"message": "You must provide a valid Metadata field for InteractionDefinitionVersion",
"errorCode": "REQUIRED_FIELD_MISSING",
"fields": []
Here is my curl request:
curl --location --request PATCH 'https://XXX.my.salesforce.com/services/data/v55.0/tooling/sobjects/Flow/3015Y000000YJ7pQAG' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer XXX' \
--data-raw '{
"Status": "Active"
}'

how to regenerate pagerduty integration key programmatically

I have integrated Jenkins CI with pagerduty. Once I do that, I can see intergration key generated.
That will be used in jenkins to send the events to pagerduty.
The requirement is to rotate the keys after some time. I want to automate this.
Is there any api to regenerate the intergration key and return the key in response to be stored in jenkins?
I think the simplest solution here is to use the REST API -- it isn't possible to regenerate the integration key directly, but you can delete the integration and create a new one programmatically.
First fetch the service details:
curl --location --request GET 'https://api.pagerduty.com/services/<service_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'
This will include all of the integrations on the service -- make note of the integration_id and the vendor_id.
The delete endpoint isn't documented but it does seem to exist:
curl --location --request DELETE 'https://api.pagerduty.com/services/<service_id>/integrations/<integration_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'
And finally you can create the new integration, using the vendor_id from the GET request:
curl --request POST \
--url https://api.pagerduty.com/services/id/integrations \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>' \
--header 'Content-Type: application/json' \
--data '{
"integration": {
"type": "generic_email_inbound_integration",
"name": "Email",
"service": {
"id": "<service_id>",
"type": "service_reference"
},
"integration_email": "my-email-based-integration#subdomain.pagerduty.com",
"vendor": {
"type": "vendor_reference",
"id": "<vendor_id>"
}
}
On doing inspect element to UI button
Its executing POST API:
https://xxxxxxx.pagerduty.com/api/v1/services/XXXXXXX/integrations/XXXXXXX/regenerate_key

Unable to access github API getting bad credentials error

I am trying to add a custom code check for a PR. After doing some research I found out that it can be done using the API mentioned below.
POST /repos/{owner}/{repo}/check-runs
Initially, it was giving me this error:
{
"message": "You must authenticate via a GitHub App.",
"documentation_url": "https://docs.github.com/rest/reference/checks#create-a-check-run"
}
I followed the guideline provided in this link.
I created a GitHub app.
Gave it required permission.
Generated a private key.
Generated a JWT token using the private key.
Installed the Github app in the repo too
I created a curl request:
curl --location --request POST 'https://api.github.com/repos/X/X-app/check-runs' \
--header 'Accept: application/vnd.github.v3+json' \
--header 'Authorization: Bearer eyJhbGciOiJSUzI1NiJ9.X.X-X-SAFvDnSkaJDjMI2T_BAC2iLlRZ7uNyFSe-X-UgFBFjoFrwsbcYFKfDM8f3FNPYpA6afhr18DLZ6rzu35klA' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "loremipsum"
}'
But, now I am getting this error
{
"message": "Bad credentials",
"documentation_url": "https://docs.github.com/rest"
}
I am not sure what I am missing here.
I figured this out. The GH documentation is a bit unclear/misleading. Here are the steps to make this work:
with the JWT bearer token, list your installations and note the installation id for your app
$ curl -i \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations
then get an installation access token for the above id
$ curl -i -X POST \
-H "Authorization: Bearer YOUR_JWT" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/app/installations/:installation_id/access_tokens
then with that token create the check run but use "Authorization: token" header
curl -i -H "Authorization: token YOUR_INSTALLATION_ACCESS_TOKEN"

Unable to update couchbase document using POST REST API

I want to update a couchbase document with REST API
ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}
When I hit below CURL command in postman, I receive 200 OK response Code with response as blank json Array: []
CURL:
curl --location --request POST 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff' \
--data-raw '{"name": "Nisarg", "age": 50}'
When I retrieve this document by below CURL:
curl --location --request GET 'ilceabcd1233.corp.abcd.com:8091/pools/default/buckets/{bucketName}/docs/{documentId}' \
--header 'Accept: application/json' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpBZG1pbmlzdHJhdG9yff'
it responds:
{
"meta": {
"id": "112176152456",
"rev": "4-1637ac65ed7900000000000002000006",
"att_reason": "invalid_json",
"expiration": 0,
"flags": 33554438
},
"base64": "",
"xattrs": {}
}
On Couchbase web console I see message:
"Binary document. Base64 not available"
Can any one please help, what I am doing wrong ?
The trick here is that this API doesn't actually accept JSON. It's looking for application/x-www-form-urlencoded. Otherwise it will assume you are storing a binary document. You actually need a form value, which itself contains JSON. For example:
curl --location --request POST 'http://localhost:8091/pools/default/buckets/demo/docs/doc1' \
--header 'Accept: application/json, text/plain, */*' \
--header 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
--header 'Authorization: Basic QWRtaW5pc3RyYXRvcjpwYXNzd29yZA==' \
--data-urlencode 'value={"foo": "bar"}'
And just to echo my comment, this is an undocumented, unsupported endpoint that's really meant for internal use only. It's recommended instead to use an SDK (like the Couchbase Java SDK, for instance) to read/write documents. The REST API is intended for Cluster management, not for CRUD.

How to delete Keycloak realm via REST API

I'm not able to delete realm via REST API on Keycloak 8.0.2.
My Keycloak runs on localhost:38080. I followed this documentation https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_realms_admin_resource, see "Delete the realm" paragraph. First, I got token for admin user at http://localhost:38080/auth/realms/master/protocol/openid-connect/token
curl --location --request POST 'localhost:38080/auth/realms/master/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=admin' \
--data-urlencode 'password=admin' \
--data-urlencode 'client_id=vga_tests' \
--data-urlencode 'grant_type=password'
Then I used the token and tried to delete the example realm by this call
curl --location --request DELETE 'localhost:38080/auth/Example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/json' \
--data-raw ''
(Note: curl commands are exported Postman calls)
I got this 404 response
{
"error": "RESTEASY003210: Could not find resource for full path: http://localhost:38080/auth/Example"
}
What am I doing wrong?
The documentation is incorrect, the address I needed to send the DELETE request to is localhost:38080/auth/admin/realms/example
curl command:
curl --location --request DELETE 'localhost:38080/auth/admin/realms/example' \
--header 'Authorization: Bearer <TOKEN HERE>' \
--header 'Content-Type: application/x-www-form-urlencoded'
Alternatively, using the CLI interface (search for "deleting a realm"),
you can do:
$ kcadm.sh delete realms/example