Skip to content

Instantly share code, notes, and snippets.

@tuxfight3r
Last active February 16, 2023 12:44
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save tuxfight3r/0dcdab36e5ef46f82718bedc0cb62455 to your computer and use it in GitHub Desktop.
Save tuxfight3r/0dcdab36e5ef46f82718bedc0cb62455 to your computer and use it in GitHub Desktop.
openshift rest api login / json patch via curl
#Login to openshift and retrieve token
curl -u admin -kv -H "X-CSRF-Token: xxx" \
'https://master.cluster.local:8443/oauth/authorize?client_id=openshift-challenging-client&response_type=token'
#It should give you a bearer token like this
https://master.cluster.local:8443/oauth/token/implicit#access_token=aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4&expires_in=86400&scope=user%3Afull&token_type=Bearer
TOKEN="aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4"
#Get a configmap named configmaptest
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest
## USES JSON PATCH ##
#Test if a value exists inside configmap named configmaptest
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/json-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '[{ "op": "test", "path": "/data/testpath1", "value": "CN=p-ocp-user,OU=openshift,OU=DEVOPS" }]'
#Add aditional value inside configmap named configmaptest
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/json-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '[{ "op": "add", "path": "/data/testpath1", "value": "CN=p-ocp-user,OU=openshift,OU=DEVOPS" }]'
#Delete a value inside configmap named configmaptest
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/json-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '[{ "op": "remove", "path": "/data/testpath4" }]'
#Replace a value inside configmap named configmaptest
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/json-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '[{ "op": "replace", "path": "/data/testpath", "value": "CN=p-ocp-user,OU=openshift,OU=DEVOPS" }]'
## USES MERGE PATCH ##
#Add a basic merge json via curl merges/adds the given data to a configmap
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/merge-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '{"data":{"testpath2":"oliverconfigmaptest2"}}'
#Remove a basic merge json via curl merges/adds the given data to a configmap
curl -H 'Authorization: Bearer aRVmsEHbUEhd4WeP2bctj0n57ogHMBvOIcrtjc7tCw4' \
-H "Content-Type: application/merge-patch+json" \
-H "Accept: application/json" \
-X PATCH \
https://master.cluster.local:8443//api/v1/namespaces/ocp_test/configmaps/configmaptest \
--data '{"data":{"testpath4": null}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment