-1

My requirement is to call the second API (Service B) as part of a single request to Service A from an authenticated user.

I am practicing go-swagger framework through a planned project. One of the point I am trying to implement is an authentic call from microservice-A to microservice-B. I have implemented without authentication but could not authenticate this internal-call.
I face an EOF error on microservice-A side and service stops. I also see runtime error: index out of range [1] with length 1 error on microservice-B side.

Following is the code of ms-A (followed goswagger documentation) to communicate with ms-B:

transport := httptransport.New(apiclient.DefaultHost, apiclient.DefaultBasePath, apiclient.DefaultSchemes)
clientm := apiclient.New(transport, strfmt.Default)
bearerTokenAuth:= httptransport.BearerToken(os.Getenv("Authorization"))

resp, err := clientm.Show.Show(show.NewShowParams(),bearerTokenAuth)
if err != nil {
    log.Fatal(err)
}
fmt.Printf("%#v\n", resp.Payload.Prompt)

The Show endpoint in microservice-B is simple entrypoint for testig this internal communication. I do have added security to the path in yaml file as following:

/show:
    get:
      description: "To test the entrypoint"
      operationId: "show"
      tags:
        - "show"
      security:
        - Bearer: [ ]
      responses:
        200:
          description: "Success response on hiting endpoint"
          schema:
            $ref: "#/definitions/Show"
        400:
          description: Bad Request
        404:
          description: items not found
        500:
          schema:
            type: string
          description: Server error

Errors

Microservice-A Error
//service-A which is used to hit show-endpoint of service-B
go run cmd/ustore-server/main.go --scheme http --port=8080
2021/10/08 01:47:00 Serving ustore at http://127.0.0.1:8080
2021/10/08 01:47:06 Get "http://localhost:9090/v1/show": EOF
exit status 1
Microservice-B Error
go run ./cmd/datastore-server/main.go --port=9090
2021/10/08 01:32:36 Serving datastore at http://127.0.0.1:9090
2021/10/08 01:34:14 http: panic serving 127.0.0.1:46040: runtime error: index out of range [1] with length 1
goroutine 23 [running]:
MenyT
  • 1,653
  • 1
  • 8
  • 19
Artaghal
  • 21
  • 5
  • I think the EOF in Microservice-A is just because Microservice-B panicked while serving the request and dropped the connection instead of sending anything. MS-A found that the reader it was holding on to ended unexpectedly, hence EOF. Without more info on what MS-B is doing it's hard to diagnose this, as that's probably where the problem sits. An out-of-range error could come up anywhere. Here it looks like you've looked for a second item in a slice with only one element, but there's no way to tell where that happened without the code. – TroyHurts Oct 08 '21 at 09:33
  • 1
    @TroyHurts Thanks... you have well explained to me the EOF. I have Share the swagger.yaml of the service-B I thought that would help as goswagger automatically handles the handler and middleware. And Yeah you are write I was stuck in token handling. I didn't managed the header for the service-B. Its working now and posted the correction in my code. Thanks – Artaghal Oct 08 '21 at 09:52

1 Answers1

0

After looking into parameters and the above mentioned errors, finally I solved the issue and now its working.

Here what I changed is the code in service-A:

    //token received for user authentication by service-A
    bearerHeader := params.HTTPRequest.Header.Get("Authorization")
    
    //connection with service-B client
    transport := httptransport.New(apiclient.DefaultHost,apiclient.DefaultBasePath,apiclient.DefaultSchemes)
    clientm := apiclient.New(transport, strfmt.Default)
    
    //spliting the key and token(string) and pass only the token part
    bearerTokenAuth:= httptransport.BearerToken(strings.Split(bearerHeader, " ")[1])
    //receive response of the service-B endpoint
    resp, err := clientm.Show.Show(show.NewShowParams(),bearerTokenAuth)
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Printf("%#v\n", resp.Payload.Prompt)
Artaghal
  • 21
  • 5