Skip to content

Commit

Permalink
Don't use partial XPath when sending "set" config operations
Browse files Browse the repository at this point in the history
Support for partial XPaths have been removed in PAN-OS 11.0.3-h3, and
full XPaths are supported across all PAN-OS versions we're targetting.
  • Loading branch information
kklimonda-cl committed Jul 29, 2024
1 parent 11eb059 commit ec4fec9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
15 changes: 15 additions & 0 deletions assets/pango/xml/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package xml

import "bytes"

func StripEntryElement(data []byte) []byte {
if !bytes.HasPrefix(data, []byte("<entry")) || !bytes.HasSuffix(data, []byte("</entry>")) {
return data
}

var startIdx, endIdx int
startIdx = bytes.Index(data, []byte(">"))
endIdx = len(data) - len("</entry>")

return data[startIdx+1 : endIdx]
}
2 changes: 2 additions & 0 deletions pkg/translate/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ func RenderImports(templateTypes ...string) (string, error) {
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/version", "")
case "service":
manager.AddStandardImport("context", "")
manager.AddStandardImport("encoding/xml", "")
manager.AddStandardImport("fmt", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/errors", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/util", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/xmlapi", "")
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/xml", "pangoxml")
case "filtering":
manager.AddSdkImport("github.com/PaloAltoNetworks/pango/filtering", "")
case "audit":
Expand Down
25 changes: 19 additions & 6 deletions templates/sdk/service.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ return nil, err

{{- if .Entry}}
path, err := loc.XpathWithEntryName(vn, entry.Name)
if err != nil {
return nil, err
}
{{- else}}
path, err := loc.Xpath(vn)
if err != nil {
return nil, err
}
path = path[:len(path)-1]
{{- end}}
if err != nil {
return nil, err
}

{{- if .Entry}}
createSpec, err := specifier(entry)
Expand All @@ -70,10 +74,19 @@ if err != nil {
return nil, err
}

data, err := xml.Marshal(createSpec)
if err != nil {
return nil, err
}

// Optionally remove top entry element from marshalled data to fullfill
// PAN-OS API requirements.
data = pangoxml.StripEntryElement(data)

cmd := &xmlapi.Config{
Action: "set",
Xpath: util.AsXpath(path[:len(path)-1]),
Element: createSpec,
Xpath: util.AsXpath(path),
Element: string(data),
Target: s.client.GetTarget(),
}

Expand Down Expand Up @@ -1302,4 +1315,4 @@ func (s *Service) RemoveFromImport(ctx context.Context, loc Location, entry Entr
return resp.Comments, nil
}
{{- end}}
{{- end}}
{{- end}}

0 comments on commit ec4fec9

Please sign in to comment.