litmuschaos / litmus

Litmus helps SREs and developers practice chaos engineering in a Cloud-native way. Chaos experiments are published at the ChaosHub (https://hub.litmuschaos.io). Community notes is at https://hackmd.io/a4Zu_sH4TZGeih-xCimi3Q
https://litmuschaos.io
Apache License 2.0
4.43k stars 693 forks source link

Minor issue: Internal Server Error in `registerInfra` when description is `null` #4228

Open smitthakkar96 opened 1 year ago

smitthakkar96 commented 1 year ago

Context:

We are trying to automate the installation of Chaos Infra in different namespaces using Kyverno. When playing around with the API to figure out how the registration flow works, I ran into this issue.

What happened:

When the description field in registerInfra call is empty as the docs say it's not required, the request fails with 500 Internal Server Error.

Logs:

runtime error: invalid memory address or nil pointer dereference

goroutine 363924 [running]:
runtime/debug.Stack()
    /usr/local/go/src/runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
    /usr/local/go/src/runtime/debug/stack.go:16 +0x19
github.com/99designs/gqlgen/graphql.DefaultRecover({0x7fb0d1b441f8?, 0x30f8ec7a68?}, {0x1bdbf20?, 0x3038ee0?})
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/recovery.go:16 +0x71
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated.(*executionContext)._Mutation_registerInfra.func1()
    /gql-server/graph/generated/generated.go:19355 +0x71
panic({0x1bdbf20, 0x3038ee0})
    /usr/local/go/src/runtime/panic.go:884 +0x213
github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure.(*infraService).RegisterInfra(0xc0006a42a0, {0x21e22c0, 0xc000d0bce0}, {0xc0002ca1d2, 0x24}, {{0xc000d050f0, 0xf}, {0xc000d050e0, 0x8}, {0xc000d05100, ...}, ...})
    /gql-server/pkg/chaos_infrastructure/service.go:135 +0x97b
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph.(*mutationResolver).RegisterInfra(0xc00051e380, {0x21e22c0, 0xc000d0bce0}, {0xc0002ca1d2, 0x24}, {{0xc000d050f0, 0xf}, {0xc000d050e0, 0x8}, {0xc000d05100, ...}, ...})
    /gql-server/graph/chaos_infrastructure.resolvers.go:36 +0x255
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated.(*executionContext)._Mutation_registerInfra.func2({0x21e22c0?, 0xc000d0bce0})
    /gql-server/graph/generated/generated.go:19376 +0x1de
github.com/99designs/gqlgen/graphql/executor.processExtensions.func3({0x21e22c0?, 0xc000d0bce0?}, 0xc000d0bce0?)
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/executor/extensions.go:62 +0x26
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated.(*executionContext)._Mutation_registerInfra(0xc000afff50, {0x21e22c0, 0xc000d0bcb0}, {0xc000d0e400?, {0xc004565180?, 0x1?, 0x1?}})
    /gql-server/graph/generated/generated.go:19374 +0x2e3
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated.(*executionContext)._Mutation(0xc000afff50, {0x21e22c0, 0xc000d0bc80}, {0xc000aff920, 0x1, 0x1})
    /gql-server/graph/generated/generated.go:31765 +0xfd7
github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/generated.(*executableSchema).Exec.func2({0x21e22c0?, 0xc000d0bc80?})
    /gql-server/graph/generated/generated.go:4326 +0x5b
github.com/99designs/gqlgen/graphql/executor.(*Executor).DispatchOperation.func1.1.1({0x21e22c0, 0xc000d0bc80})
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/executor/executor.go:105 +0x2b
github.com/99designs/gqlgen/graphql/executor.processExtensions.func2({0x21e22c0?, 0xc000d0bc80?}, 0x1b2f860?)
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/executor/extensions.go:59 +0x26
github.com/99designs/gqlgen/graphql/executor.(*Executor).DispatchOperation.func1.1({0x21e22c0, 0xc000d0bc20})
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/executor/executor.go:104 +0x110
github.com/99designs/gqlgen/graphql/handler/transport.POST.Do({}, {0x7fb0d180aa98, 0xc000664400}, 0xc000f2e900, {0x21d4ef0, 0xc0004d4630})
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/handler/transport/http_post.go:52 +0x308
github.com/99designs/gqlgen/graphql/handler.(*Server).ServeHTTP(0xc000526600, {0x7fb0d180aa98, 0xc000664400}, 0xc000f2e900)
    /go/pkg/mod/github.com/99designs/gqlgen@v0.11.3/graphql/handler/server.go:115 +0x273
github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization.Middleware.func1(0xc000664400)
    /gql-server/pkg/authorization/middleware.go:39 +0x2a5
github.com/gin-gonic/gin.(*Context).Next(...)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174
github.com/gin-gonic/gin.CustomRecoveryWithWriter.func1(0xc000664400)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/recovery.go:102 +0x82
github.com/gin-gonic/gin.(*Context).Next(...)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174
github.com/litmuschaos/litmus/chaoscenter/graphql/server/api/middleware.StructuredLogger.func1(0xc000664400)
    /gql-server/api/middleware/logger.go:26 +0x9a
github.com/gin-gonic/gin.(*Context).Next(...)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/context.go:174
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc000456340, 0xc000664400)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:620 +0x66b
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc000456340, {0x21e1130?, 0xc000e7a000}, 0xc000860900)
    /go/pkg/mod/github.com/gin-gonic/gin@v1.9.0/gin.go:576 +0x1dd
net/http.serverHandler.ServeHTTP({0x21d3100?}, {0x21e1130, 0xc000e7a000}, 0xc000860900)
    /usr/local/go/src/net/http/server.go:2936 +0x316
net/http.(*conn).serve(0xc0009c6000, {0x21e22c0, 0xc00027b3e0})
    /usr/local/go/src/net/http/server.go:1995 +0x612
created by net/http.(*Server).Serve
    /usr/local/go/src/net/http/server.go:3089 +0x5ed

This works if you set the description as an empty string

What you expected to happen:

The request should be successful, or the documentation should say description is a mandatory field.

How to reproduce it (as minimally and precisely as possible):

mutation registerInfra($req: RegisterInfraRequest!) {
  registerInfra(
    projectID: "<PROJECT_ID>"
    request: $req
  ) {
    manifest
    token
    infraID
    name
  }
}
{
  "req":{
    "environmentID": "testing1",
    "name": "testing-cluster2",
    "infrastructureType": "Kubernetes",
    "infraScope": "namespace",
    "platformName": "Kubernetes",
    "infraNamespace": "sre-enablement",
    "infraNsExists": true,
    "infraSaExists": false,
    "serviceAccount": "sre-enablement-chaos",
    "skipSsl": false
  }
}

Setting description to empty solves the problem, so it's not a big deal, but putting this out there so if someone is trying to use API, they don't waste 2 hours trying to figure out why the API is not working as expected.

vanshBhatia-A4k9 commented 1 year ago

Will take a look at this issue, thanks for reporting @smitthakkar96