File fix-libsuseconnect-and-pci.patch of Package suseconnect-ng.42510

From 6ed2e5cefb4301b84bbb62e2b5ccebdc76aff1cf Mon Sep 17 00:00:00 2001
From: sampsone <esampson@suse.com>
Date: Fri, 20 Feb 2026 08:13:35 -0600
Subject: [PATCH] SMT requires a distro_target in the payload.

---
 internal/connect/client.go                   | 27 ++++++++++----------
 internal/connect/wrapper.go                  | 24 +++++++++--------
 pkg/registration/product.go                  |  9 +++++++
 pkg/registration/product_test.go             | 21 +++++++++++++++
 pkg/registration/register.go                 | 13 +++++++---
 third_party/libsuseconnect/libsuseconnect.go |  2 +-
 6 files changed, 67 insertions(+), 29 deletions(-)

diff --git a/internal/connect/client.go b/internal/connect/client.go
index ffc9fdb..8144f0b 100644
--- a/internal/connect/client.go
+++ b/internal/connect/client.go
@@ -53,28 +53,27 @@ func Register(api WrappedAPI, opts *Options) error {
 		printInformation(fmt.Sprintf("Registering system to %s", opts.ServerName()), opts)
 	}
 
-	if err := api.RegisterOrKeepAlive(opts.Token, opts.InstanceDataFile, opts.EnableSystemUptimeTracking); err != nil {
-		return err
-	}
-
 	installReleasePkg := true
-	product := opts.Product
-	if product.IsEmpty() {
+	if opts.Product.IsEmpty() {
 		base, err := zypper.BaseProduct()
 		if err != nil {
 			return err
 		}
-		product = base
+		opts.Product = base
 		installReleasePkg = false
 	}
 
-	if service, err := registerProduct(conn, opts, product, installReleasePkg); err == nil {
+	if err := api.RegisterOrKeepAlive(opts); err != nil {
+		return err
+	}
+
+	if service, err := registerProduct(conn, opts, opts.Product, installReleasePkg); err == nil {
 		out.Products = append(out.Products, ProductService{
 			Product: ProductOut{
-				Name:       product.Name,
-				Identifier: product.Identifier,
-				Version:    product.Version,
-				Arch:       product.Arch,
+				Name:       opts.Product.Name,
+				Identifier: opts.Product.Identifier,
+				Version:    opts.Product.Version,
+				Arch:       opts.Product.Arch,
 			},
 			Service: ServiceOut{
 				Id:   service.ID,
@@ -86,8 +85,8 @@ func Register(api WrappedAPI, opts *Options) error {
 		return err
 	}
 
-	if product.IsBase {
-		p, err := registration.FetchProductInfo(conn, product.Identifier, product.Version, product.Arch)
+	if opts.Product.IsBase {
+		p, err := registration.FetchProductInfo(conn, opts.Product.Identifier, opts.Product.Version, opts.Product.Arch)
 		if err != nil {
 			return err
 		}
diff --git a/internal/connect/wrapper.go b/internal/connect/wrapper.go
index aab721f..4455ca3 100644
--- a/internal/connect/wrapper.go
+++ b/internal/connect/wrapper.go
@@ -19,8 +19,8 @@ import (
 // when unit testing
 type WrappedAPI interface {
 	KeepAlive(uptimeTracking bool) error
-	Register(regcode, instanceDataFile string) error
-	RegisterOrKeepAlive(regcode, instanceDataFile string, uptimeTracking bool) error
+	Register(opts *Options) error
+	RegisterOrKeepAlive(opts *Options) error
 	IsRegistered() bool
 	AssignLabels(labels []string) ([]labels.Label, error)
 
@@ -129,8 +129,9 @@ func (w Wrapper) KeepAlive(uptimeTracking bool) error {
 	return err
 }
 
-func (w Wrapper) Register(regcode, instanceDataFile string) error {
+func (w Wrapper) Register(opts *Options) error {
 	hwinfo, err := FetchSystemInformation()
+
 	if err != nil {
 		return fmt.Errorf("could not fetch system's information: %v", err)
 	}
@@ -140,27 +141,28 @@ func (w Wrapper) Register(regcode, instanceDataFile string) error {
 	// "extra" data. This will be used inside of the `registration.Register`
 	// code.
 	extraData := registration.ExtraData{}
-	if instanceDataFile != "" {
-		data, err := os.ReadFile(instanceDataFile)
+	if opts.InstanceDataFile != "" {
+		data, err := os.ReadFile(opts.InstanceDataFile)
 		if err != nil {
 			return err
 		}
 		extraData["instance_data"] = string(data)
 	}
 
-	// NOTE: we are not interested in the code. Hence, we don't save it
-	// anywhere.
-	_, err = registration.Register(w.Connection, regcode, hostname, hwinfo, extraData)
+	// add distro_target to extra data
+	extraData["distro_target"] = opts.Product.DistroTarget()
+
+	_, err = registration.Register(w.Connection, opts.Token, hostname, hwinfo, extraData)
 	return err
 }
 
 // RegisterOrKeepAlive calls either `Register` or `KeepAlive` depending on
 // whether the current system is registered or not.
-func (w Wrapper) RegisterOrKeepAlive(regcode, instanceDataFile string, uptimeTracking bool) error {
+func (w Wrapper) RegisterOrKeepAlive(opts *Options) error {
 	if w.Registered {
-		return w.KeepAlive(uptimeTracking)
+		return w.KeepAlive(opts.EnableSystemUptimeTracking)
 	}
-	return w.Register(regcode, instanceDataFile)
+	return w.Register(opts)
 }
 
 func (w Wrapper) IsRegistered() bool {
diff --git a/pkg/registration/product.go b/pkg/registration/product.go
index 7c82e4a..bb7a02e 100644
--- a/pkg/registration/product.go
+++ b/pkg/registration/product.go
@@ -149,6 +149,15 @@ func (p Product) FindExtension(triplet string) (Product, error) {
 	return Product{}, fmt.Errorf("extension not found")
 }
 
+func (p Product) DistroTarget() string {
+	identifier := strings.ToLower(p.Identifier)
+	if strings.HasPrefix(identifier, "sle") {
+		identifier = "sle"
+	}
+	version := strings.Split(p.Version, ".")[0]
+	return identifier + "-" + version + "-" + p.Arch
+}
+
 // Transforms the current product into a list of extensions.
 func (p Product) ToExtensionsList() []Product {
 	res := make([]Product, 0)
diff --git a/pkg/registration/product_test.go b/pkg/registration/product_test.go
index baff8ad..a9f4c15 100644
--- a/pkg/registration/product_test.go
+++ b/pkg/registration/product_test.go
@@ -20,6 +20,27 @@ func TestProductToTriplet(t *testing.T) {
 	assert.Equal("SLES/15.5/x86_64", product.ToTriplet())
 }
 
+func TestProductDistroTargetSle(t *testing.T) {
+	assert := assert.New(t)
+
+	productJson := fixture(t, "pkg/registration/product_tree.json")
+	product := Product{}
+
+	assert.NoError(json.Unmarshal(productJson, &product))
+	assert.Equal("sle-15-x86_64", product.DistroTarget())
+}
+
+func TestProductDistroTargetNotSle(t *testing.T) {
+	assert := assert.New(t)
+
+	productJson := fixture(t, "pkg/registration/product_tree.json")
+	product := Product{}
+
+	assert.NoError(json.Unmarshal(productJson, &product))
+	product.Identifier = "not-suse-really"
+	assert.Equal("not-suse-really-15-x86_64", product.DistroTarget())
+}
+
 func TestProductTraverseExtensionsFull(t *testing.T) {
 	assert := assert.New(t)
 
diff --git a/pkg/registration/register.go b/pkg/registration/register.go
index a8cb088..b83b913 100644
--- a/pkg/registration/register.go
+++ b/pkg/registration/register.go
@@ -7,7 +7,8 @@ import (
 )
 
 type announceRequest struct {
-	Hostname string `json:"hostname"`
+	Hostname     string `json:"hostname"`
+	DistroTarget string `json:"distro_target,omitempty"`
 
 	requestWithInformation
 }
@@ -28,6 +29,14 @@ func Register(conn connection.Connection, regcode, hostname string, systemInform
 		Hostname: hostname,
 	}
 
+	// distro_target may be data in extra data, but only needed for smt
+	// and it must be at the same level in the payload as hostname
+	// so, if it is present add to payload and remove from extraData
+	if _, ok := extraData["distro_target"]; ok {
+		payload.DistroTarget = extraData["distro_target"].(string)
+		delete(extraData, "distro_target")
+	}
+
 	enrichWithSystemInformation(&payload.requestWithInformation, systemInformation)
 	enrichErr := enrichWithExtraData(&payload.requestWithInformation, extraData)
 	if enrichErr != nil {
@@ -35,14 +44,12 @@ func Register(conn connection.Connection, regcode, hostname string, systemInform
 	}
 
 	creds := conn.GetCredentials()
-
 	request, buildErr := conn.BuildRequest("POST", "/connect/subscriptions/systems", payload)
 	if buildErr != nil {
 		return 0, buildErr
 	}
 
 	connection.AddRegcodeAuth(request, regcode)
-
 	response, doErr := conn.Do(request)
 	if doErr != nil {
 		return 0, doErr
diff --git a/third_party/libsuseconnect/libsuseconnect.go b/third_party/libsuseconnect/libsuseconnect.go
index df93f87..17356ec 100644
--- a/third_party/libsuseconnect/libsuseconnect.go
+++ b/third_party/libsuseconnect/libsuseconnect.go
@@ -68,7 +68,7 @@ func announce_system(clientParams, distroTarget *C.char) *C.char {
 	opts := loadConfig(C.GoString(clientParams))
 	api := connect.NewWrappedAPI(opts)
 
-	if err := connect.Register(api, opts); err != nil {
+	if err := api.Register(opts); err != nil {
 		return C.CString(errorToJSON(err))
 	}
 
-- 
2.52.0

openSUSE Build Service is sponsored by