From ec4fec9534df4686275ace4e946144929580324f Mon Sep 17 00:00:00 2001 From: Krzysztof Klimonda Date: Mon, 29 Jul 2024 11:14:28 +0200 Subject: [PATCH] Don't use partial XPath when sending "set" config operations 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. --- assets/pango/xml/utils.go | 15 +++++++++++++++ pkg/translate/imports.go | 2 ++ templates/sdk/service.tmpl | 25 +++++++++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 assets/pango/xml/utils.go diff --git a/assets/pango/xml/utils.go b/assets/pango/xml/utils.go new file mode 100644 index 00000000..bb2cf71c --- /dev/null +++ b/assets/pango/xml/utils.go @@ -0,0 +1,15 @@ +package xml + +import "bytes" + +func StripEntryElement(data []byte) []byte { + if !bytes.HasPrefix(data, []byte("")) { + return data + } + + var startIdx, endIdx int + startIdx = bytes.Index(data, []byte(">")) + endIdx = len(data) - len("") + + return data[startIdx+1 : endIdx] +} diff --git a/pkg/translate/imports.go b/pkg/translate/imports.go index 1974ed3d..d75dac65 100644 --- a/pkg/translate/imports.go +++ b/pkg/translate/imports.go @@ -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": diff --git a/templates/sdk/service.tmpl b/templates/sdk/service.tmpl index 3a5b6a4f..ddfadc19 100644 --- a/templates/sdk/service.tmpl +++ b/templates/sdk/service.tmpl @@ -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) @@ -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(), } @@ -1302,4 +1315,4 @@ func (s *Service) RemoveFromImport(ctx context.Context, loc Location, entry Entr return resp.Comments, nil } {{- end}} -{{- end}} \ No newline at end of file +{{- end}}