Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions cmd/container-suseconnect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ func main() {
app.Name = "container-suseconnect"
app.Version = cs.Version
app.Usage = "Access zypper repositories from within containers"
app.UsageText =
`This application can be used to retrieve basic metadata about SLES
app.UsageText = `This application can be used to retrieve basic metadata about SLES
related products and module extensions.

Please use the 'list-products' subcommand for listing all currently
Expand All @@ -55,6 +54,7 @@ func main() {
// Switch the default application behavior in relation to the basename
defaultUsageAdditionZypp := ""
defaultUsageAdditionListProducts := ""

switch filepath.Base(os.Args[0]) {
case "container-suseconnect-zypp":
app.Action = runZypperPlugin
Expand Down Expand Up @@ -108,24 +108,29 @@ func requestProducts() ([]cs.Product, error) {
// config from "mounted" files if the service is not available
if err := regionsrv.ServerReachable(); err == nil {
log.Printf("containerbuild-regionsrv reachable, reading config\n")

cloudCfg, err := regionsrv.ReadConfigFromServer()
if err != nil {
return nil, err
}

credentials.Username = cloudCfg.Username
credentials.Password = cloudCfg.Password
credentials.InstanceData = cloudCfg.InstanceData

suseConnectData.SccURL = "https://" + cloudCfg.ServerFqdn
suseConnectData.Insecure = false

if cloudCfg.Ca != "" {
regionsrv.SafeCAFile(cloudCfg.Ca)
}

regionsrv.UpdateHostsFile(cloudCfg.ServerFqdn, cloudCfg.ServerIP)
} else {
if err := cs.ReadConfiguration(&credentials); err != nil {
return nil, err
}

if err := cs.ReadConfiguration(&suseConnectData); err != nil {
return nil, err
}
Expand All @@ -135,6 +140,7 @@ func requestProducts() ([]cs.Product, error) {
if err != nil {
return nil, err
}

log.Printf("Installed product: %v\n", installedProduct)
log.Printf("Registration server set to %v\n", suseConnectData.SccURL)

Expand Down Expand Up @@ -188,6 +194,7 @@ func runListModules(_ *cli.Context) error {

fmt.Printf("All available modules:\n\n")
cs.ListModules(os.Stdout, products)

return nil
}

Expand All @@ -200,5 +207,6 @@ func runListProducts(_ *cli.Context) error {

fmt.Printf("All available products:\n\n")
cs.ListProducts(os.Stdout, products, "none")

return nil
}
9 changes: 8 additions & 1 deletion internal/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func getLocationPath(locations []string) string {
return path
}
}

return ""
}

Expand All @@ -68,6 +69,7 @@ func ReadConfiguration(config Configuration) error {
if config.onLocationsNotFound() {
return nil
}

return loggedError(GetCredentialsError, "Warning: SUSE credentials not found: %v - automatic handling of repositories not done.", config.locations())
}

Expand All @@ -83,18 +85,21 @@ func ReadConfiguration(config Configuration) error {
// Parses the contents given by the reader and updated the given configuration.
func parse(config Configuration, reader io.Reader) error {
scanner := bufio.NewScanner(reader)

for scanner.Scan() {
// Comments & empty lines.
if strings.IndexAny(scanner.Text(), "#-") == 0 {
continue
}

if scanner.Text() == "" {
continue
}

// Each line should be constructed as 'key' 'separator' 'value'.
line := scanner.Text()
// Each line should be constructed as 'key' 'separator' 'value'.
parts := strings.SplitN(line, string(config.separator()), 2)

if len(parts) != 2 {
return loggedError(GetCredentialsError, "Can't parse line: %v", line)
}
Expand All @@ -108,8 +113,10 @@ func parse(config Configuration, reader io.Reader) error {
if err := scanner.Err(); err != nil {
return loggedError(GetCredentialsError, "Error when scanning configuration: %v", err)
}

if err := config.afterParseCheck(); err != nil {
return err
}

return nil
}
11 changes: 11 additions & 0 deletions internal/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestGetLocationPath(t *testing.T) {
"does/not/exist",
"testdata/products-sle12.json",
}

path = getLocationPath(strs)
if path != "testdata/products-sle12.json" {
t.Fatalf("Wrong location path: %v", path)
Expand Down Expand Up @@ -62,10 +63,12 @@ func TestNotFound(t *testing.T) {
var cfg NotFoundConfiguration

prepareLogger()

err := ReadConfiguration(&cfg)
if err == nil || err.Error() != "Warning: SUSE credentials not found: [] - automatic handling of repositories not done." {
t.Fatalf("Wrong error: %v", err)
}

shouldHaveLogged(t, "Warning: SUSE credentials not found: [] - automatic handling of repositories not done.")
}

Expand Down Expand Up @@ -94,11 +97,13 @@ func TestNotAllowed(t *testing.T) {
var cfg NotAllowedConfiguration

prepareLogger()

err := ReadConfiguration(&cfg)
msg := "Can't open /etc/shadow file: open /etc/shadow: permission denied"
if err == nil || err.Error() != msg {
t.Fatal("Wrong error")
}

shouldHaveLogged(t, msg)
}

Expand All @@ -110,12 +115,15 @@ func TestParseInvalid(t *testing.T) {
file.Close()
t.Fatal("There should be an error here")
}

prepareLogger()

err = parse(cfg, file)
msg := "Error when scanning configuration: invalid argument"
if err == nil || err.Error() != msg {
t.Fatal("Wrong error")
}

shouldHaveLogged(t, msg)
}

Expand Down Expand Up @@ -154,11 +162,14 @@ func TestParseFailNoSeparator(t *testing.T) {
var cfg ErrorAfterParseConfiguration

str := strings.NewReader("keywithoutvalue")

prepareLogger()

err := parse(cfg, str)
msg := "Can't parse line: keywithoutvalue"
if err == nil || err.Error() != msg {
t.Fatal("Wrong error")
}

shouldHaveLogged(t, msg)
}
3 changes: 2 additions & 1 deletion internal/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (cr *Credentials) locations() []string {
}

func (cr *Credentials) onLocationsNotFound() bool {

env_user := os.Getenv("SCC_CREDENTIAL_USERNAME")
env_pass := os.Getenv("SCC_CREDENTIAL_PASSWORD")

Expand Down Expand Up @@ -73,8 +72,10 @@ func (cr *Credentials) afterParseCheck() error {
if cr.Username == "" {
return loggedError(GetCredentialsError, "Can't find username")
}

if cr.Password == "" {
return loggedError(GetCredentialsError, "Can't find password")
}

return nil
}
5 changes: 5 additions & 0 deletions internal/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestCredentials(t *testing.T) {
if cr.separator() != '=' {
t.Fatal("Wrong separator")
}

prepareLogger()
err := cr.afterParseCheck()
msg := "Can't find username"
Expand Down Expand Up @@ -107,15 +108,19 @@ func TestIntegrationCredentials(t *testing.T) {
if err != nil {
t.Fatal("This should've been a successful run")
}

if mock.cr.Username != "SCC_a6994b1d3ae14b35agc7cef46b4fff9a" {
t.Fatal("Unexpected name value")
}

if mock.cr.Password != "10yb1x6bd159g741ad420fd5aa5083e4" {
t.Fatal("Unexpected password value")
}

if mock.cr.SystemToken != "36531d07-a283-441b-a02a-1cd9a88b0d5d" {
t.Fatal("Unexpected system_token value")
}

if mock.cr.onLocationsNotFound() {
t.Fatalf("It should've been false")
}
Expand Down
7 changes: 3 additions & 4 deletions internal/installed_product.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func parseInstalledProduct(reader io.Reader) (InstalledProduct, error) {
var p InstalledProduct
err := xml.Unmarshal(xmlData, &p)
if err != nil {
return InstalledProduct{},
loggedError(InstalledProductError, "Can't parse base product file: %v", err.Error())
return InstalledProduct{}, loggedError(InstalledProductError, "Can't parse base product file: %v", err.Error())
}

return p, nil
}

Expand All @@ -74,8 +74,7 @@ func readInstalledProduct(provider ProductProvider) (InstalledProduct, error) {

xmlFile, err := os.Open(provider.Location())
if err != nil {
return InstalledProduct{},
loggedError(InstalledProductError, "Can't open base product file: %v", err.Error())
return InstalledProduct{}, loggedError(InstalledProductError, "Can't open base product file: %v", err.Error())
}
defer xmlFile.Close()

Expand Down
8 changes: 8 additions & 0 deletions internal/installed_product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestFailNonExistantProduct(t *testing.T) {
if err == nil {
t.Fatal("This file should not exist...")
}

if err.Error() != "No base product detected" {
t.Fatal("Wrong error message")
}
Expand All @@ -50,6 +51,7 @@ func TestFailNotAllowedProduct(t *testing.T) {
if err == nil {
t.Fatal("This file should not be available...")
}

if err.Error() != "Can't open base product file: open /etc/shadow: permission denied" {
t.Fatal("Wrong error message")
}
Expand All @@ -68,6 +70,7 @@ func TestFailBadFormattedProduct(t *testing.T) {
if err == nil {
t.Fatal("This file should have a bad format")
}

if err.Error() != "Can't parse base product file: EOF" {
t.Fatal("Wrong error message")
}
Expand All @@ -86,15 +89,19 @@ func TestMockProvider(t *testing.T) {
if err != nil {
t.Fatal("It should've read it just fine")
}

if p.Identifier != "SLES" {
t.Fatal("Wrong product name")
}

if p.Version != "12" {
t.Fatal("Wrong product version")
}

if p.Arch != "x86_64" {
t.Fatal("Wrong product arch")
}

if p.String() != "SLES-12-x86_64" {
t.Fatal("Wrong product string")
}
Expand All @@ -109,6 +116,7 @@ func TestSUSE(t *testing.T) {
if err == nil {
t.Fatal("It should fail")
}

return
}

Expand Down
4 changes: 1 addition & 3 deletions internal/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func getLogWritter(path string) (io.WriteCloser, error) {
return nil, fmt.Errorf("log path is not absulute: %s", path)
}

lf, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)

lf, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o640)
if err != nil {
return nil, err
}
Expand All @@ -61,7 +60,6 @@ func getLogWritter(path string) (io.WriteCloser, error) {
}

_, err = lf.WriteString(fmt.Sprintf("container-suseconnect %s\n", Version))

if err != nil {
lf.Close()
return nil, err
Expand Down
Loading