Skip to content

Commit

Permalink
Merge pull request #270 from SUSE/suseconnect-do-not-generate-docker-…
Browse files Browse the repository at this point in the history
…on-pubcloud

Do not generate docker configuration on pubcloud
  • Loading branch information
mssola authored Oct 28, 2024
2 parents 0f577e4 + 21e14d0 commit 4b63a24
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 18 deletions.
13 changes: 7 additions & 6 deletions cmd/suseconnect/suseconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func main() {
fmt.Printf("URL \"%s\" not valid: %s\n", baseURL, err)
os.Exit(1)
}
connect.CFG.BaseURL = baseURL
connect.CFG.ChangeBaseURL(baseURL)
writeConfig = true
}
if fsRoot != "" {
Expand Down Expand Up @@ -261,11 +261,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 @@ -298,8 +298,9 @@ func main() {
}
}

// After successful registration we try to set labels
if len(labels) > 0 {
// After successful registration we try to set labels if we are
// targetting SCC.
if connect.CFG.IsScc() && len(labels) > 0 {
err := connect.AssignAndCreateLabels(strings.Split(labels, ","))
if err != nil && !jsonFlag {
fmt.Printf("Problem setting labels for this system: %s\n", err)
Expand All @@ -316,7 +317,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
17 changes: 9 additions & 8 deletions internal/connect/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,14 @@ func announceOrUpdate(quiet bool) error {
}

if err = cred.CreateCredentials(login, password, "", cred.SystemCredentialsPath(CFG.FsRoot)); err == nil {
util.Debug.Print("\nAdding SUSE registry system authentication configuration ...")
setupRegistryAuthentication(login, password)
// If the user is authenticated against the SCC, then setup the Docker
// Registry configuration for the system. Otherwise, if the system is
// behind a proxy (e.g. RMT), this step might fail and it's best to
// avoid it (see bsc#1231185).
if CFG.IsScc() {
util.Debug.Print("\nAdding SUSE registry system authentication configuration ...")
setupRegistryAuthentication(login, password)
}
}
return err
}
Expand All @@ -405,14 +411,9 @@ func UpToDate() bool {
return upToDate()
}

// URLDefault returns true if using https://scc.suse.com
func URLDefault() bool {
return CFG.BaseURL == defaultBaseURL
}

func printInformation(action string, jsonOutput bool) {
var server string
if URLDefault() {
if CFG.IsScc() {
server = "SUSE Customer Center"
} else {
server = "registration proxy " + CFG.BaseURL
Expand Down
53 changes: 49 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 (
UnknownProvider ServerType = iota
SccProvider
RmtProvider
)

// 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 @@ -84,6 +93,12 @@ func (c Config) Save() error {
func (c *Config) Load() {
f, err := os.Open(c.Path)
if err != nil {
// If we failed at parsing the configuration, we can make further
// assumptions based on the base URL being used.
if c.BaseURL == defaultBaseURL {
c.ServerType = SccProvider
}

util.Debug.Println(err)
return
}
Expand All @@ -92,6 +107,22 @@ func (c *Config) Load() {
util.Debug.Printf("Config after parsing: %+v", c)
}

// Change the base url to be used when talking to the server to the one being
// provided.
func (c *Config) ChangeBaseURL(baseUrl string) {
c.BaseURL = baseUrl

// When making an explicit change of the URL, we can further detect which
// kind of server we are dealing with. For now, let's keep it simple, and if
// it's the defaultBaseURL then we assume it to be SccProvider, otherwise
// RmtProvider.
if c.BaseURL == defaultBaseURL {
c.ServerType = SccProvider
} else {
c.ServerType = RmtProvider
}
}

func parseConfig(r io.Reader, c *Config) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
Expand Down Expand Up @@ -123,6 +154,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 = SccProvider
}
}

// MergeJSON merges attributes of jsn that match Config fields
Expand All @@ -131,3 +167,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 == SccProvider
}
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 = UnknownProvider
if err := c1.Save(); err != nil {
t.Fatalf("Unable to write config: %s", err)
}
c2 := NewConfig()
c2.Path = path
c2.Load()
c2.ServerType = UnknownProvider
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 == UnknownProvider {
if api := resp.Header.Get("Scc-Api-Version"); api == sccAPIVersion {
CFG.ServerType = SccProvider
} else {
CFG.ServerType = RmtProvider
}
}

// 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 4b63a24

Please sign in to comment.