Skip to content

Commit

Permalink
internal: Add a ServerType to the configuration
Browse files Browse the repository at this point in the history
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à <[email protected]>
  • Loading branch information
mssola committed Oct 8, 2024
1 parent b7159ce commit 56d6ac2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
6 changes: 3 additions & 3 deletions cmd/suseconnect/suseconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.")
}
Expand Down
31 changes: 27 additions & 4 deletions internal/connect/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions internal/connect/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/connect/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 56d6ac2

Please sign in to comment.