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

Consume changes go-mod-core-contracts v0.1.56 #2481

Closed
wants to merge 2 commits into from

Conversation

jdharms
Copy link
Contributor

@jdharms jdharms commented Apr 8, 2020

Updates to edgex-go to consume changes to go-mod-core-contracts
from the following PRs:

edgexfoundry/go-mod-core-contracts#233

edgexfoundry/go-mod-core-contracts#231

This is being clubbed into one PR on the edgex-go side because both
PRs on go-mod-core-contracts have already been merged.

Fixes #2439 and Fixes #2480 in edgex-go.

Replaces ugorji/go with fxamacker/cbor for security reasons.

Reflects the system management client functionality being
moved from the "general" client in go-mod-core-contracts
to an "agent" client.

Signed-off-by: Daniel Harms [email protected]

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

Issue Number:

#2439 #2480

Does this PR introduce a breaking change?

  • Yes
  • No

bnevis-i
bnevis-i previously approved these changes Apr 8, 2020
Copy link
Collaborator

@bnevis-i bnevis-i left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Note: github.com/fxamacker/cbor has only two maintainers.

@x448
Copy link

x448 commented Apr 8, 2020

@jdharms in the old code you didn't modify, the call to ioutil.ReadAll(reader) can lead to denial of service attacks using large payloads (if another program is providing the data, etc.)

// Read reads and converts the request's CBOR event data into an Event struct
func (cr cborReader) Read(reader io.Reader, ctx *context.Context) (models.Event, error) {
c := context.WithValue(*ctx, clients.ContentType, clients.ContentTypeCBOR)
event := models.Event{}
bytes, err := ioutil.ReadAll(reader)

It would be safer to use io.LimitReader with a reasonable size limit appropriate for edgex-go and then pass the limited reader directly to the CBOR decoder.

Even without io.LimitReader, it's still safer to pass io.Reader directly to the decoder.

// optionally use io.LimitReader r with size limit appropriate to edgex-go
... 
decoder := cbor.NewDecoder(reader)  // decoder will read in up to 512 byte chunks
err := decoder.Decode(&event)

I only looked at one function and didn't review other parts of io.go.

@x448
Copy link

x448 commented Apr 9, 2020

Note: github.com/fxamacker/cbor has only two maintainers.

@bnevis-i by comparison, the CBOR library used by edgex-go has zero contributors based on commit activity since August 15, 2019. For context, security problems were found in Sep 2019.

If small team size (without context) is an indicator of risk, take a look at a 27 contributor CBOR library written in Rust looking for maintainers in part because the maintainer says,

Within the past year it became increasingly clear to me that I don't understand all the details and interactions of different features. This is in part because I accepted pull requests implementing new features but did not review the code thoroughly enough.

To avoid such problems, fxamacker/cbor won't accept pull requests if it harms maintainability. In fact, oasislabs/oasis-core cited maintainability as a factor in choosing fxamacker/cbor.

(click to expand) Comparison Charts

Number of contributors

Contributor counts (based on commits) from Aug 15, 2019 - Apr 8, 2020 are:

  • 2 contributors -- fxamacker/cbor (proposed new CBOR library)
  • 0 contributors -- ugorji/go (current CBOR library used by edgex-go)

Graphs showing activity from Aug 11, 2019 - Apr 8, 2020:

For fxamacker/cbor:

image

For ugorji/go:

image

Source:

@mkbhanda
Copy link
Contributor

mkbhanda commented Apr 9, 2020

I agree with both comments @x448 has made. Please use a limited reader. Also consider making the limit a constant that we could possibly configure, defaulting to something we think sensible today. If you like, could make this a separate issue. For the future, do keep patches separate for different issues, easier on the reviewers.

)

// GeneralType is an alias for general.GeneralClient and hides implementation detail.
type GeneralType general.GeneralClient
// AgentType is an alias for general.GeneralClient and hides implementation detail.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to:
s/general.GeneralClient/agent.AgentClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you.

@jdharms
Copy link
Contributor Author

jdharms commented Apr 9, 2020

I will make the change to use a limit reader and re-request.

I clubbed the PRs because currently both changes on the go-mod-core-contracts side have been merged, and they include interface changes (the sma client functionality). This means any other go-mod-core-contracts changes won't be able to be used by EdgeX unless it incorporates these changes, so I wanted to unblock development there as quickly as possible.

In the future I'll keep them separate.

jdharms added 2 commits April 9, 2020 10:25
Updates to edgex-go to consume changes to go-mod-core-contracts
from the following PRs:

edgexfoundry/go-mod-core-contracts#233

edgexfoundry/go-mod-core-contracts#231

This is being clubbed into one PR on the edgex-go side because both
PRs on go-mod-core-contracts have already been merged.

Fixes edgexfoundry#2439 and Fixes edgexfoundry#2480 in edgex-go.

Replaces ugorji/go with fxamacker/cbor for security reasons.

Reflects the system management client functionality being
moved from the "general" client in go-mod-core-contracts
to an "agent" client.

Signed-off-by: Daniel Harms <[email protected]>
* Use LimitReader when reading bytes to pass to CBOR encoder
* Fix some renaming inside comments my IDE didn't pick up

Signed-off-by: Daniel Harms <[email protected]>
@sonarqubecloud
Copy link

sonarqubecloud bot commented Apr 9, 2020

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities (and Security Hotspot 0 Security Hotspots to review)
Code Smell A 0 Code Smells

No Coverage information No Coverage information
2.7% 2.7% Duplication

@jdharms jdharms requested review from bnevis-i and mkbhanda April 9, 2020 14:29
@tsconn23 tsconn23 dismissed mkbhanda’s stale review April 9, 2020 15:28

Sorry, we're working on something.

Copy link
Member

@tsconn23 tsconn23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug.

1.) Bring up core-data
2.) Bring up sys-mgmt-agent
3.) Make the following call GET http://localhost:48090/api/v1/metrics/edgex-core-data

That should return metrics for the core-data service but it returns a 404.

[
  {
    "operation": "metrics",
    "service": "edgex-core-data",
    "executor": "direct-service",
    "Success": false,
    "errorMessage": "404 - 404 page not found\n"
  }
]

The root cause actually lies in go-mod-core-contracts here
https://github.com/edgexfoundry/go-mod-core-contracts/blob/9132f554263fd61cb848f5b965b4d1aa9442ed97/clients/agent/client.go#L73

rc.UrlClient points to the host/port of core-data and the second parameter of clients.GetRequest should be the route. The value being constructed in this case is /api/v1/metrics/edgex-core-data which doesn't exist. I'm unclear why suffix is needed at all but in this case, it's clearly obfuscating the route resulting in the 404.

Recommend checking the other AgentClient methods as well since they use the same pattern.

When I remove use of suffix and change the line to
body, err := clients.GetRequest(ctx, clients.ApiMetricsRoute, rc.urlClient)
the correct result is returned

[
  {
    "operation": "metrics",
    "service": "edgex-core-data",
    "executor": "direct-service",
    "Success": true,
    "result": {
      "cpuUsedPercent": -1,
      "memoryUsed": 72155384,
      "raw": {
        "Memory": {
          "Alloc": 1028584,
          "TotalAlloc": 1028584,
          "Sys": 72155384,
          "Mallocs": 12029,
          "Frees": 1796,
          "LiveObjects": 10233
        },
        "CpuBusyAvg": -1
      }
    }
  }
]

@tsconn23
Copy link
Member

DIscussed the above bug with @jdharms and @michaelestrin. The requirements were clarified and Daniel will rework his changes in go-mod-core-contracts before creating another integration PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants