Skip to content

Conversation

@wallrj-cyberark
Copy link
Member

Some improvements to the UnmarshalJSON function (which was introduced in #707):

  • Refactor UnmarshalJSON to use prioritized type decoding for Data field
  • Add strict unmarshalling helper to disallow unknown fields
  • Return errors for empty or unknown data types in Data field
  • Add unit tests for valid and invalid JSON scenarios

Testing

The "echo server" test was using a arbitrary data type, which is no longer allowed, so I switched it to DataReading. Here's the output of the updated test:

$ go test ./pkg/echo/... -v -count 1
=== RUN   TestEchoServerRequestResponse
-- POST /api/v1/datareadings -> created 201
received 1 readings:
0:
ClusterID: test_suite_cluster
Data gatherer: dummy
Timestamp: 2025-09-05T06:15:33+01:00
SchemaVersion: 2.0.0
Data: &{ClusterID: ServerVersion:v1.20.0}
-----
-- error 400 -> invalid method. Expected POST, received GET
--- PASS: TestEchoServerRequestResponse (0.00s)
PASS
ok      github.com/jetstack/preflight/pkg/echo  0.010s

The machinehub outputmode test still works:

go test ./cmd/... -v -count 1 -run '.*/machinehub'
=== RUN   TestOutputModes
=== RUN   TestOutputModes/machinehub
    agent_test.go:71: Running child process /tmp/go-build2356754855/b001/cmd.test -test.run=^TestOutputModes/machinehub$
    agent_test.go:80: STDOUT
        I0905 06:19:25.669617   59271 run.go:58] "Starting" logger="Run" version="development" commit=""
        I0905 06:19:25.669859   59271 config.go:448] "Output mode selected" logger="Run" mode="MachineHub" reason="--machine-hub was specified"
        I0905 06:19:25.669897   59271 run.go:116] "Healthz endpoints enabled" logger="Run.APIServer" addr=":8081" path="/healthz"
        I0905 06:19:25.669919   59271 run.go:120] "Readyz endpoints enabled" logger="Run.APIServer" addr=":8081" path="/readyz"
        I0905 06:19:25.669961   59271 run.go:269] "Pod event recorder disabled" logger="Run" reason="The agent does not appear to be running in a Kubernetes cluster." detail="When running in a Kubernetes cluster the following environment variables must be set: POD_NAME, POD_NODE, POD_UID, POD_NAMESPACE"
        I0905 06:19:25.669979   59271 run.go:308] "Reading data from local file" logger="Run.gatherAndOutputData" inputPath="/home/richard/projects/jetstack/jetstack-secure/examples/machinehub/input.json"
        I0905 06:19:25.670377   59271 run.go:432] "Starting" logger="Run.APIServer.ListenAndServe" addr=":8081"
        I0905 06:19:26.008565   59271 round_trippers.go:632] "Response" logger="Run.gatherAndOutputData.postData" verb="GET" url="https://platform-discovery.integration-cyberark.cloud/api/public/tenant-discovery?bySubdomain=tlskp-test" status="200 OK" milliseconds=337
        I0905 06:19:26.559188   59271 round_trippers.go:632] "Response" logger="Run.gatherAndOutputData.postData" verb="POST" url="https://anb5751.id.integration-cyberark.cloud/Security/StartAuthentication" status="200 OK" milliseconds=548
        I0905 06:19:26.559776   59271 identity.go:292] "made successful request to StartAuthentication" logger="Run.gatherAndOutputData.postData" source="Identity.doStartAuthentication" summary="NewPackage"
        I0905 06:19:27.173640   59271 round_trippers.go:632] "Response" logger="Run.gatherAndOutputData.postData" verb="POST" url="https://anb5751.id.integration-cyberark.cloud/Security/AdvanceAuthentication" status="200 OK" milliseconds=613
        I0905 06:19:27.174260   59271 identity.go:402] "successfully completed AdvanceAuthentication request to CyberArk Identity; login complete" logger="Run.gatherAndOutputData.postData" username=""
        I0905 06:19:32.908654   59271 round_trippers.go:632] "Response" logger="Run.gatherAndOutputData.postData" verb="POST" url="https://tlskp-test.inventory.integration-cyberark.cloud/api/ingestions/kubernetes/snapshot-links" status="200 OK" milliseconds=5734
        I0905 06:19:33.378071   59271 round_trippers.go:632] "Response" logger="Run.gatherAndOutputData.postData" verb="PUT" url="" status="200 OK" milliseconds=468
        I0905 06:19:33.378305   59271 run.go:417] "Data sent successfully" logger="Run.gatherAndOutputData.postData"        
        I0905 06:19:33.378351   59271 run.go:446] "Shutting down" logger="Run.APIServer.ListenAndServe" addr=":8081"        
        I0905 06:19:33.378646   59271 run.go:461] "Shutdown complete" logger="Run.APIServer.ListenAndServe" addr=":8081"
        
    agent_test.go:81: STDERR
        
--- PASS: TestOutputModes (7.80s)
    --- PASS: TestOutputModes/machinehub (7.80s)
PASS
ok      github.com/jetstack/preflight/cmd       7.864s

- Refactor UnmarshalJSON to use prioritized type decoding for Data field
- Add strict unmarshalling helper to disallow unknown fields
- Return errors for empty or unknown data types in Data field
- Add unit tests for valid and invalid JSON scenarios

Signed-off-by: Richard Wall <richard.wall@cyberark.com>
@wallrj-cyberark wallrj-cyberark merged commit 4d53d77 into master Sep 5, 2025
2 checks passed
@wallrj-cyberark wallrj-cyberark deleted the VC-43403-outputmode-integration-tests-2 branch September 5, 2025 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants