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

RpcReply.OK not set correctly due to XML unmarshalling of boolean is broken #34

Open
emasean opened this issue Nov 10, 2017 · 11 comments
Open
Labels
Milestone

Comments

@emasean
Copy link

emasean commented Nov 10, 2017

The following rpc-reply is received:

 <rpc-reply message-id="101"
            xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
   <ok/>
 </rpc-reply>

But the unmarshalled RpcReply.Ok did not set to true.

After checking the unmarshalling format:

 // RPCReply defines a reply to a RPC request
 type RPCReply struct {
   XMLName  xml.Name   `xml:"rpc-reply"`
   Errors   []RPCError `xml:"rpc-error,omitempty"`
   Data     string     `xml:",innerxml"`
   Ok       bool       `xml:",omitempty"`
   RawReply string     `xml:"-"`
 }

the boolean unmarshalling seems to be broken according to the following site

So making the following RPCReply:

 // RPCReply defines a reply to a RPC request
 type RPCReply struct {
   XMLName  xml.Name   `xml:"rpc-reply"`
   Errors   []RPCError `xml:"rpc-error,omitempty"`
   Data     string     `xml:",innerxml"`
   Ok       *struct{}       `xml:"ok,omitempty"`
   RawReply string     `xml:"-"`
 }

and check if Ok != nil will make it work

@nemith nemith added the bug label Feb 18, 2018
@nemith
Copy link
Collaborator

nemith commented Feb 18, 2018

I think we can create a custom unmarshaler here.

@wtucker
Copy link
Contributor

wtucker commented Feb 18, 2018

Does the RPCReply.Ok flag tell us anything useful? From what I've been able to find the RPCReply is only returned if no error was generated. If something does go wrong, RPCError is raised instead.

@wtucker
Copy link
Contributor

wtucker commented Feb 18, 2018

https://www.juniper.net/documentation/en_US/junos/topics/reference/tag-summary/netconf-ok.html

"Indicates that the NETCONF server successfully performed a requested operation that changes the state or contents of the device configuration."

Using the validate command as an example:
<rpc><validate><source><candidate/></source></validate></rpc>

The <ok/> tag is outside of the <commit-results/> tree, so I guess the intent is to save you from having to look inside that blob to understand if everything was "OK"

<commit-results>
</commit-results>
<ok/>
</rpc-reply>
]]>]]>```

@wtucker
Copy link
Contributor

wtucker commented Feb 18, 2018

Sample of validate response with bad config (should raise RPCError):

<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/15.1F4/junos">
<commit-results>
<rpc-error>
<error-type>application</error-type>
<error-tag>invalid-value</error-tag>
<error-severity>error</error-severity>
<error-path>[edit]</error-path>
<error-message>mgd: Missing mandatory statement: 'root-authentication'</error-message>
<error-info>
<bad-element>system</bad-element>
</error-info>
</rpc-error>
<rpc-error>
<error-type>protocol</error-type>
<error-tag>operation-failed</error-tag>
<error-severity>error</error-severity>
<error-message>
configuration check-out failed: (missing mandatory statements)
</error-message>
</rpc-error>
</commit-results>
</rpc-reply>

@wtucker
Copy link
Contributor

wtucker commented Feb 18, 2018

Sample from a lab QFX that had MPLS enabled but not licensed:

<rpc><validate><source><candidate/></source></validate></rpc>
<rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/16.1R3/junos">
<commit-results>
<rpc-error>
<error-severity>warning</error-severity>
<error-path>[edit protocols]</error-path>
<error-message>mgd: requires 'mpls' license</error-message>
<error-info>
<bad-element>mpls</bad-element>
</error-info>
</rpc-error>
<rpc-error>
<error-severity>warning</error-severity>
<error-path>[edit protocols]</error-path>
<error-message>mgd: requires 'bgp' license</error-message>
<error-info>
<bad-element>bgp</bad-element>
</error-info>
</rpc-error>
<routing-engine junos:style="normal">
<name>fpc0</name>
<commit-check-success/>
</routing-engine>
</commit-results>
<ok/>
</rpc-reply>
]]>]]>

@yhzs8
Copy link

yhzs8 commented Feb 18, 2018

Does the RPCReply.Ok flag tell us anything useful? From what I've been able to find the RPCReply is only returned if no error was generated. If something does go wrong, RPCError is raised instead.

For me it is more intuitive to check if the response is OK first (the normal flow) rather than if response contains error first (the exception flow). I believe this is another workaround that works but The protocol wrapper should work as stated in the RFC: https://tools.ietf.org/html/rfc6241#page-19

wtucker pushed a commit to wtucker/go-netconf that referenced this issue Feb 18, 2018
wtucker pushed a commit to wtucker/go-netconf that referenced this issue Feb 18, 2018
wtucker pushed a commit to wtucker/go-netconf that referenced this issue Feb 19, 2018
Alternate implementation to work around XML unmarshaling limitations in
Golang.  I'm not a fan of this method since it breaks the API, but I'm
providing it for completeness.

For my money I'd rather see 8e66346
merged and then have the upstream XML module fixed to add a "flag"
field option.

closes Juniper#34
@wtucker
Copy link
Contributor

wtucker commented Feb 19, 2018

For me it is more intuitive to check if the response is OK first (the normal flow) rather than if response contains error first (the exception flow). I believe this is another workaround that works but The protocol wrapper should work as stated in the RFC: https://tools.ietf.org/html/rfc6241#page-19

I've filed #55 to update documentation so it explains that the Ok flag doesn't mean what one might expect.

There are two branches that fix setting the RPCReply.Ok flag processing, but both have drawbacks. I'm open to alternate suggestions for handle the unmarshaling.

@nemith
Copy link
Collaborator

nemith commented Feb 19, 2018

Agreed that it should follow the RFC. We worked on this package through the NANOG hackathon and may have some ideas on unmarshaling it, but still experimenting.

@nemith
Copy link
Collaborator

nemith commented Feb 19, 2018

This should work: https://play.golang.org/p/Qyl_i0Ihf96 @wtucker can you update your code/tests with this method?

emasean added a commit to emasean/go-netconf that referenced this issue May 11, 2018
@vithubati

This comment was marked as abuse.

@nemith
Copy link
Collaborator

nemith commented Jul 22, 2022

This is fixed in v2. (I understand the xml package so much better than 10 years ago)

@nemith nemith added this to the v2 milestone Jul 22, 2022
nemith added a commit that referenced this issue Mar 13, 2024
Adds support for returning RPC errors as Go errors. This does it by
always returning a list of RPCErrors (even when there is one) and
supporting the `Unwrap() []error` implicit interface for `errors`
package (Go 1.20 and later)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants