puppetlabs / puppetdb

Centralized Puppet Storage
http://docs.puppetlabs.com/puppetdb
Apache License 2.0
301 stars 226 forks source link

Error: java.lang.String cannot be cast to class clojure.lang.Associative #3934

Closed elfranne closed 7 months ago

elfranne commented 9 months ago

I have been trying to query the PuppetDB with a Golang binary (Sensu monitoring) but the PuppetDB is not very happy about it and gives a error 500 back:

Puppetdb version: 7.15.0 Puppet version 7.27.0 Ubuntu 20.04 kernel 5.4.0-169

Sources of the Golang binary: https://github.com/elfranne/http-checks/tree/master/cmd/http-json https://github.com/elfranne/http-checks/releases

Command used:

http-json --url https://puppet.example.com:8081/pdb/query/v4/reports --mtls-cert-file /etc/puppetlabs/puppet/ssl/certs/puppet01.example.com.pem --mtls-key-file /etc/puppetlabs/puppet/ssl/private_keys/puppet01.example.com.pem --trusted-ca-file  /etc/puppetlabs/puppet/ssl/certs/ca.pem --method POST --post-data '{"query":["and",["=","latest_report?","true"],["=","certname","puppet01.example.com"]]}' --query '.[].status == "success" or .[].status == "unchanged" or .[].status == "changed"' --expression 'true' --header 'Content-Type:application/json' --debug

output:

DEBUG-REQUEST:
POST /pdb/query/v4/reports HTTP/1.1
Host: puppet.example.com:8081
Content-Type: application/json

"{\"query\":[\"and\",[\"=\",\"latest_report?\",\"true\"],[\"=\",\"certname\",\"puppet01.example.com\"]]}"
DEBUG-RESPONSE:
HTTP/1.1 500 Server Error
Content-Length: 198
Content-Type: text/plain;charset=utf-8
Date: Mon, 15 Jan 2024 14:30:37 GMT

class java.lang.String cannot be cast to class clojure.lang.Associative (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Associative is in unnamed module of loader 'app')Could not unmarshal response body into JSON: invalid character 'c' looking for beginning of value

PuppetDB log:

ERROR [p.p.middleware] #error {
 :cause class java.lang.String cannot be cast to class clojure.lang.Associative (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Associative is in unnamed module of loader 'app')
 :via
 [{:type java.lang.ClassCastException
   :message class java.lang.String cannot be cast to class clojure.lang.Associative (java.lang.String is in module java.base of loader 'bootstrap'; clojure.lang.Associative is in unnamed module of loader 'app')
   :at [clojure.lang.RT assoc RT.java 827]}]
 :trace
 [[clojure.lang.RT assoc RT.java 827]
  [clojure.core$assoc__5481 invokeStatic core.clj 193]
  [clojure.core$update invokeStatic core.clj 6231]
  [clojure.core$update invoke core.clj 6223]
  [puppetlabs.puppetdb.http.query$post_req__GT_query invokeStatic query.clj 353]
  [puppetlabs.puppetdb.http.query$post_req__GT_query invoke query.clj 344]
  [puppetlabs.puppetdb.http.query$fn__30571$create_query_map__30576$fn__30577 invoke query.clj 366]
  [puppetlabs.puppetdb.http.query$fn__30571$create_query_map__30576 invoke query.clj 358]
  [puppetlabs.puppetdb.http.query$extract_query$fn__30591 invoke query.clj 382]
  [bidi.ring$fn__35415 invokeStatic ring.cljc 25]
  [bidi.ring$fn__35415 invoke ring.cljc 21]
  [bidi.ring$fn__35400$G__35395__35409 invoke ring.cljc 16]
  [puppetlabs.puppetdb.middleware$fn__36708$make_pdb_handler__36717$fn__36720$fn__36722 invoke middleware.clj 419]
  [puppetlabs.puppetdb.middleware$wrap_with_illegal_argument_catch$fn__36551 invoke middleware.clj 99]
  [puppetlabs.puppetdb.middleware$verify_accepts_content_type$fn__36562 invoke middleware.clj 139]
  [puppetlabs.puppetdb.middleware$verify_content_type$fn__36574 invoke middleware.clj 169]
  [puppetlabs.puppetdb.middleware$verify_sync_version$fn__36658 invoke middleware.clj 354]
  [puppetlabs.puppetdb.middleware$wrap_with_metrics$fn__36619$fn__36628 invoke middleware.clj 272]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317$fn__26318 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics.proxy$java.lang.Object$Callable$7da976d4 call nil -1]
  [com.codahale.metrics.Timer time Timer.java 101]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317$fn__26318 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics.proxy$java.lang.Object$Callable$7da976d4 call nil -1]
  [com.codahale.metrics.Timer time Timer.java 101]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317$fn__26318 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics.proxy$java.lang.Object$Callable$7da976d4 call nil -1]
  [com.codahale.metrics.Timer time Timer.java 101]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317$fn__26318 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics.proxy$java.lang.Object$Callable$7da976d4 call nil -1]
  [com.codahale.metrics.Timer time Timer.java 101]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_$fn__26316$fn__26317 invoke metrics.clj 23]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_ invokeStatic metrics.clj 26]
  [puppetlabs.puppetdb.utils.metrics$multitime_BANG__STAR_ invoke metrics.clj 15]
  [puppetlabs.puppetdb.middleware$wrap_with_metrics$fn__36619 invoke middleware.clj 271]
  [puppetlabs.puppetdb.middleware$wrap_with_globals$fn__36546 invoke middleware.clj 93]
  [puppetlabs.puppetdb.middleware$wrap_with_exception_handling$fn__36555 invoke middleware.clj 114]
  [puppetlabs.puppetdb.http.server$build_app$fn__43107 invoke server.clj 80]
  [compojure.core$routing$fn__36019 invoke core.clj 151]
  [clojure.core$some invokeStatic core.clj 2718]
  [clojure.core$some invoke core.clj 2709]
  [compojure.core$routing invokeStatic core.clj 151]
  [compojure.core$routing doInvoke core.clj 148]
  [clojure.lang.RestFn invoke RestFn.java 423]
  [puppetlabs.puppetdb.pdb_routing$wrap_with_context$fn__46956 invoke pdb_routing.clj 32]
  [compojure.core$if_context$fn__36043 invoke core.clj 218]
  [compojure.core$routing$fn__36019 invoke core.clj 151]
  [clojure.core$some invokeStatic core.clj 2718]
  [clojure.core$some invoke core.clj 2709]
  [compojure.core$routing invokeStatic core.clj 151]
  [compojure.core$routing doInvoke core.clj 148]
  [clojure.lang.RestFn applyTo RestFn.java 139]
  [clojure.core$apply invokeStatic core.clj 669]
  [clojure.core$apply invoke core.clj 662]
  [compojure.core$routes$fn__36023 invoke core.clj 156]
  [compojure.core$routing$fn__36019 invoke core.clj 151]
  [clojure.core$some invokeStatic core.clj 2718]
  [clojure.core$some invoke core.clj 2709]
  [compojure.core$routing invokeStatic core.clj 151]
  [compojure.core$routing doInvoke core.clj 148]
  [clojure.lang.RestFn invoke RestFn.java 460]
  [puppetlabs.puppetdb.pdb_routing$pdb_app$fn__46967 invoke pdb_routing.clj 60]
  [compojure.core$if_context$fn__36043 invoke core.clj 218]
  [ring.middleware.params$wrap_params$fn__36514 invoke params.clj 67]
  [puppetlabs.puppetdb.middleware$wrap_with_certificate_cn$fn__36536 invoke middleware.clj 75]
  [puppetlabs.puppetdb.middleware$wrap_with_default_body$fn__36541 invoke middleware.clj 82]
  [puppetlabs.puppetdb.middleware$wrap_with_debug_logging$fn__36519 invoke middleware.clj 39]
  [puppetlabs.i18n.core$locale_negotiator$fn__272 invoke core.clj 361]
  [puppetlabs.trapperkeeper.services.webserver.jetty9_core$ring_handler$fn__48624 invoke jetty9_core.clj 460]
  [puppetlabs.trapperkeeper.services.webserver.jetty9_core.proxy$org.eclipse.jetty.server.handler.AbstractHandler$ff19274a handle nil -1]
  [org.eclipse.jetty.server.handler.HandlerWrapper handle HandlerWrapper.java 127]
  [org.eclipse.jetty.server.handler.ScopedHandler nextHandle ScopedHandler.java 235]
  [org.eclipse.jetty.server.handler.ContextHandler doHandle ContextHandler.java 1440]
  [org.eclipse.jetty.server.handler.ScopedHandler nextScope ScopedHandler.java 190]
  [org.eclipse.jetty.server.handler.ContextHandler doScope ContextHandler.java 1355]
  [org.eclipse.jetty.server.handler.ScopedHandler handle ScopedHandler.java 141]
  [org.eclipse.jetty.server.handler.ContextHandlerCollection handle ContextHandlerCollection.java 234]
  [org.eclipse.jetty.server.handler.HandlerCollection handle HandlerCollection.java 146]
  [org.eclipse.jetty.server.handler.gzip.GzipHandler handle GzipHandler.java 772]
  [org.eclipse.jetty.server.handler.RequestLogHandler handle RequestLogHandler.java 54]
  [com.puppetlabs.trapperkeeper.services.webserver.jetty9.utils.MDCRequestLogHandler handle MDCRequestLogHandler.java 36]
  [org.eclipse.jetty.server.handler.StatisticsHandler handle StatisticsHandler.java 181]
  [org.eclipse.jetty.server.handler.HandlerWrapper handle HandlerWrapper.java 127]
  [org.eclipse.jetty.server.Server handle Server.java 516]
  [org.eclipse.jetty.server.HttpChannel lambda$handle$1 HttpChannel.java 487]
  [org.eclipse.jetty.server.HttpChannel dispatch HttpChannel.java 732]
  [org.eclipse.jetty.server.HttpChannel handle HttpChannel.java 479]
  [org.eclipse.jetty.server.HttpConnection onFillable HttpConnection.java 277]
  [org.eclipse.jetty.io.AbstractConnection$ReadCallback succeeded AbstractConnection.java 311]
  [org.eclipse.jetty.io.FillInterest fillable FillInterest.java 105]
  [org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint onFillable SslConnection.java 555]
  [org.eclipse.jetty.io.ssl.SslConnection onFillable SslConnection.java 410]
  [org.eclipse.jetty.io.ssl.SslConnection$2 succeeded SslConnection.java 164]
  [org.eclipse.jetty.io.FillInterest fillable FillInterest.java 105]
  [org.eclipse.jetty.io.ChannelEndPoint$1 run ChannelEndPoint.java 104]
  [org.eclipse.jetty.util.thread.strategy.EatWhatYouKill runTask EatWhatYouKill.java 338]
  [org.eclipse.jetty.util.thread.strategy.EatWhatYouKill doProduce EatWhatYouKill.java 315]
  [org.eclipse.jetty.util.thread.strategy.EatWhatYouKill tryProduce EatWhatYouKill.java 173]
  [org.eclipse.jetty.util.thread.strategy.EatWhatYouKill run EatWhatYouKill.java 131]
  [org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread run ReservedThreadExecutor.java 409]
  [org.eclipse.jetty.util.thread.QueuedThreadPool runJob QueuedThreadPool.java 883]
  [org.eclipse.jetty.util.thread.QueuedThreadPool$Runner run QueuedThreadPool.java 1034]
  [java.lang.Thread run Thread.java 829]]}

Doing a curl request does not trigger this.

austb commented 7 months ago

The tool is not sending properly serialized json.

The proper curl command would look like

curl 'http://localhost:8080/pdb/query/v4/reports' \
  -H 'Content-Type:application/json' \
  -d '{"query":["and",["=","latest_report?","true"],["=","certname","puppet01.example.com"]]}'

and you can reproduce the error you are seeing by adding an additional layer of quotation marks in curl

curl 'http://localhost:8080/pdb/query/v4/reports' \
  -H 'Content-Type:application/json' \
  -d '"{\"query\":[\"and\",[\"=\",\"latest_report?\",\"true\"],[\"=\",\"certname\",\"puppet01.example.com\"]]}"'
elfranne commented 7 months ago

@austb thanks !