From 56d6ac2ea48c595fe3bc43575f15e11fb82e3688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miquel=20Sabat=C3=A9=20Sol=C3=A0?= Date: Thu, 3 Oct 2024 09:40:23 +0200 Subject: [PATCH] internal: Add a ServerType to the configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows the internal configuration of SUSEConnect to understand on which kind of server it's trying to connect. This is relevant in case we want to perform some operations on SCC and some others on RMT. Signed-off-by: Miquel Sabaté Solà --- cmd/suseconnect/suseconnect.go | 6 +++--- internal/connect/config.go | 31 +++++++++++++++++++++++++++---- internal/connect/config_test.go | 2 ++ internal/connect/connection.go | 12 ++++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/cmd/suseconnect/suseconnect.go b/cmd/suseconnect/suseconnect.go index cbe2fcc9..0984bda0 100644 --- a/cmd/suseconnect/suseconnect.go +++ b/cmd/suseconnect/suseconnect.go @@ -259,11 +259,11 @@ func main() { fmt.Print(string(out)) } else { - if instanceDataFile != "" && connect.URLDefault() { + if instanceDataFile != "" && connect.CFG.IsScc() { fmt.Print("Please use --instance-data only in combination ") fmt.Print("with --url pointing to your RMT or SMT server\n") os.Exit(1) - } else if connect.URLDefault() && token == "" && product.value == "" { + } else if connect.CFG.IsScc() && token == "" && product.value == "" { flag.Usage() os.Exit(1) } else if isSumaManaged() { @@ -304,7 +304,7 @@ func main() { } func maybeBrokenSMTError() error { - if !connect.URLDefault() && !connect.UpToDate() { + if !connect.CFG.IsScc() && !connect.UpToDate() { return fmt.Errorf("Your Registration Proxy server doesn't support this function. " + "Please update it and try again.") } diff --git a/internal/connect/config.go b/internal/connect/config.go index 53517039..9cafb257 100644 --- a/internal/connect/config.go +++ b/internal/connect/config.go @@ -26,6 +26,15 @@ const ( defaultEnableSystemUptimeTracking = false ) +// Kinds of servers which are supported by SUSEConnect. +type ServerType uint64 + +const ( + Unknown ServerType = iota + Scc + Rmt +) + // Config holds the config! type Config struct { Path string @@ -40,10 +49,10 @@ type Config struct { Email string `json:"email"` AutoAgreeEULA bool EnableSystemUptimeTracking bool - - NoZypperRefresh bool - AutoImportRepoKeys bool - SkipServiceInstall bool + ServerType ServerType + NoZypperRefresh bool + AutoImportRepoKeys bool + SkipServiceInstall bool } // NewConfig returns a Config with defaults @@ -123,6 +132,11 @@ func parseConfig(r io.Reader, c *Config) { util.Debug.Printf("Cannot parse line \"%s\" from %s", line, c.Path) } } + + // Set the server type depending on what we parsed from the configuration. + if c.BaseURL == defaultBaseURL { + c.ServerType = Scc + } } // MergeJSON merges attributes of jsn that match Config fields @@ -131,3 +145,12 @@ func (c *Config) MergeJSON(jsn string) error { util.Debug.Printf("Merged options: %+v", c) return err } + +// Returns true if we detected that the configuration points to SCC. +// +// NOTE: this will be reliable if the configuration file already pointed to SCC, +// but it might need to be filled in upon HTTP requests to further guess if it's +// a Glue instance running on localhost or similar developer-only scenarios. +func (c *Config) IsScc() bool { + return c.ServerType == Scc +} diff --git a/internal/connect/config_test.go b/internal/connect/config_test.go index a01ddcf5..ae91fd86 100644 --- a/internal/connect/config_test.go +++ b/internal/connect/config_test.go @@ -57,12 +57,14 @@ func TestSaveLoad(t *testing.T) { c1 := NewConfig() c1.Path = path c1.AutoAgreeEULA = true + c1.ServerType = Unknown if err := c1.Save(); err != nil { t.Fatalf("Unable to write config: %s", err) } c2 := NewConfig() c2.Path = path c2.Load() + c2.ServerType = Unknown if !reflect.DeepEqual(c1, c2) { t.Errorf("got %+v, expected %+v", c2, c1) } diff --git a/internal/connect/connection.go b/internal/connect/connection.go index 615158a0..188288e3 100644 --- a/internal/connect/connection.go +++ b/internal/connect/connection.go @@ -155,6 +155,18 @@ func callHTTP(verb, path string, body []byte, query map[string]string, auth auth } defer resp.Body.Close() + // If we failed to detect which server type was being used when loading the + // configuration, we can actually further inspect it via some of the headers + // that are returned by Glue vs RMT. Hence, if the server type is unknown, + // make an educated guess now. + if CFG.ServerType == Unknown { + if api := resp.Header.Get("Scc-Api-Version"); api == sccAPIVersion { + CFG.ServerType = Scc + } else { + CFG.ServerType = Rmt + } + } + // For each request SCC might update the System token for a given system. // This will be given through the `System-Token` header, so we have to grab // this here and store it for the next request.