Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Incorrect Implementation of --file Option in kn func invoke #2545

Open
Assassin141 opened this issue Oct 18, 2024 · 3 comments
Open

Bug: Incorrect Implementation of --file Option in kn func invoke #2545

Assassin141 opened this issue Oct 18, 2024 · 3 comments
Labels
kind/bug Categorizes issue or PR as related to a bug. kind/good-first-issue Denotes an issue ready for a new contributor.

Comments

@Assassin141
Copy link

Summary

The implementation of the --file option in the kn func invoke command encodes the file content as a base64 string, but it is not decoded before being sent, resulting in errors when invoking functions via HTTP or CloudEvents.

Steps to Reproduce

  1. Create a JSON file, e.g., test.json, with the following content:
    {
        "id": 123
    }
  2. Invoke the function with the command:
    kn func invoke --file ./test.json --content-type application/json --format http -v
  3. The output shows:
    Sending values
      ID: 
      Source: /boson/fn
      Type: boson.fn
      ContentType: application/json
      Data: ewogICAgImlkIjogMTIzCn0=
    
  4. Similarly, invoke the function using CloudEvents format:
    kn func invoke --file ./test.json --content-type application/json --format cloudevent -v
  5. The resulting error is:
    Error: invalid character 'e' looking for beginning of value
    

Analysis

The sequence of operations in func/cmd/invoke.go shows that when --file is specified, the file content gets read and encoded as a base64 string (m.Data), but this encoded data is not being decoded before being sent in the request. This inconsistency leads to the errors observed.

Relevant code snippet from func/cmd/invoke.go:

if cfg.File != "" {
    content, err := os.ReadFile(cfg.File)
    if err != nil {
        return err
    }
    m.Data = base64.StdEncoding.EncodeToString(content)
}

The data is sent without decoding in the sendEvent and sendPost functions located in func/pkg/functions/invoke.go. For example, in the sendEvent function, the decoding code is as follows:

err = json.Unmarshal([]byte(m.Data), &d)

At this point, m.Data is still a base64 string (e.g., ewogICAgImlkIjogMTIzCn0=), which cannot be decoded into JSON.

Expected Behavior

When using the --file option, the command should read the content of the specified file and send it in the same format as if the --data option had been provided.

Environment

  • Knative version:
* Serving
  - serving.knative.dev/v1 (knative-serving v0.42.1-0.20240820122005-5f5f6d820b03)
* Eventing
  - sources.knative.dev/v1 (knative-eventing v0.42.1-0.20240828134450-34f9cd384dea)
  - eventing.knative.dev/v1 (knative-eventing v0.42.1-0.20240828134450-34f9cd384dea)
@Assassin141 Assassin141 changed the title Bug : Incorrect Implementation of --file Option in kn func invoke Bug: Incorrect Implementation of --file Option in kn func invoke Oct 21, 2024
@lkingland
Copy link
Member

Thanks for the bug report!

We'll add this to our development roadmap, but PRs are welcome ;)

@lkingland lkingland moved this to ⭐ New in Functions WG Roadmap Oct 29, 2024
@lkingland lkingland added kind/good-first-issue Denotes an issue ready for a new contributor. kind/bug Categorizes issue or PR as related to a bug. labels Oct 29, 2024
@siddhesh0705
Copy link

Hi @Assassin141 and @lkingland ,
I’m Siddhesh Sangale, and I’m new to open-source contributions. I’m eager to get started by tackling some beginner-
friendly tasks that can help me build confidence and gain experience. While I’m just beginning my open-source journey, I’m excited to grow my skills and contribute to impactful projects.

I’m looking forward to working on something meaningful and gradually taking on bigger challenges as I learn. I would appreciate any guidance or suggestions for good starting points.

can you appoint me with this issue ?

@tarunsunny3
Copy link
Contributor

Hi @lkingland
Would love to work on this one, if it's unassigned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. kind/good-first-issue Denotes an issue ready for a new contributor.
Projects
Status: ⭐ New
Development

No branches or pull requests

4 participants