Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support of configuring host to use Satellite server #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
130 changes: 130 additions & 0 deletions configure_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package main

import (
"fmt"
"github.com/briandowns/spinner"
"github.com/urfave/cli/v2"
"os"
"os/exec"
"path/filepath"
"time"
)

// beforeSatelliteAction is run before satelliteAction is run. It is used
// for checking CLI options.
func beforeSatelliteAction(ctx *cli.Context) error {
// First check if machine-readable format is used
err := setupFormatOption(ctx)
if err != nil {
return err
}

satelliteUrlStr := ctx.String("url")
if satelliteUrlStr == "" {
return fmt.Errorf("no url provided using --url CLI option")
}

return checkForUnknownArgs(ctx)
}

// satelliteAction tries to get bootstrap script from Satellite server and run it.
// When it is not possible to download the script or running script returns
// non-zero exit code, then error is returned.
//
// It is really risky to download to run some script downloaded from the URL as a
// root user without any restriction. For this reason, we at least check that
// provided URL is URL of Satellite server.
//
// We would like to use different approach in the future. We would like to use
// some API endpoints not restricted by username & password for getting CA certs
// and rendered rhsm.conf, because it would be more secure, but it is not possible ATM
func satelliteAction(ctx *cli.Context) error {
var configureSatelliteResult ConfigureSatelliteResult
configureSatelliteResult.format = ctx.String("format")
satelliteUrlStr := ctx.String("url")

uid := os.Getuid()
if uid != 0 {
errMsg := "non-root user cannot connect system"
exitCode := 1
return cli.Exit(fmt.Errorf("error: %satSpinner", errMsg), exitCode)
}

hostname, err := os.Hostname()
if uiSettings.isMachineReadable {
configureSatelliteResult.Hostname = hostname
}
if err != nil {
exitCode := 1
if uiSettings.isMachineReadable {
configureSatelliteResult.HostnameError = err.Error()
return cli.Exit(configureSatelliteResult, exitCode)
} else {
return cli.Exit(err, exitCode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a wrapping error message here?

Suggested change
return cli.Exit(err, exitCode)
return cli.Exit(fmt.Errorf("could not acquire hostname: %w", err), exitCode)

}
}

satelliteUrl, err := normalizeSatelliteScriptUrl(satelliteUrlStr)
if err != nil {
return cli.Exit(fmt.Errorf("could not parse satellite url: %w", err), 1)
}

configureSatelliteResult.SatelliteServerHostname = satelliteUrl.Hostname()
configureSatelliteResult.SatelliteServerScriptUrl = satelliteUrl.String()

var satSpinner *spinner.Spinner = nil
if uiSettings.isRich {
satSpinner = spinner.New(spinner.CharSets[9], 100*time.Millisecond)
satSpinner.Suffix = fmt.Sprintf(" Configuring '%v' to use Satellite %v", hostname, satelliteUrl.Host)
satSpinner.Start()
// Stop spinner after running function
defer func() { satSpinner.Stop() }()
}

err = pingSatelliteServer(satelliteUrl, satSpinner)
if err != nil {
return cli.Exit(fmt.Errorf("unable to verify that given server is Satellite server: %v", err), 1)
}

configureSatelliteResult.IsServerSatellite = true

// Create file for script file
satelliteScriptPath := filepath.Join(VarLibDir, "katello-rhsm-consumer")
defer os.Remove(satelliteScriptPath)
scriptFile, err := os.Create(satelliteScriptPath)
if err != nil {
return cli.Exit(fmt.Errorf("could not create %v file: %w", satelliteScriptPath, err), 1)
}
defer scriptFile.Close()
err = os.Chmod(satelliteScriptPath, 0700)
if err != nil {
return cli.Exit(fmt.Errorf("could not set permissions on %v file: %w", satelliteScriptPath, err), 1)
}

err = downloadSatelliteScript(scriptFile, satelliteUrl, satSpinner)
if err != nil {
return cli.Exit(fmt.Errorf("could not download satellite bootstrap script: %w", err), 1)
}

if satSpinner != nil {
satSpinner.Suffix = fmt.Sprintf(" Configuring '%v' to use Satellite server: %v", hostname, satelliteUrl.Host)
}
// Run the script. It should install CA certificate, change configuration of rhsm.conf.
// In theory, it can do almost anything.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scary. We should follow this up with a distributed SELinux policy to restrict the scope execution for rhc in general.

cmd := exec.Command(satelliteScriptPath)
err = cmd.Run()
subpop marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return cli.Exit(fmt.Errorf("execution of %v script failed: %w", satelliteScriptPath, err), 1)
}

configureSatelliteResult.HostConfigured = true

if uiSettings.isRich {
satSpinner.Suffix = ""
satSpinner.Stop()
}

interactivePrintf("Host '%v' configured to use Satellite server: %v\n", hostname, satelliteUrl.Host)

return cli.Exit(configureSatelliteResult, 0)
}
4 changes: 4 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
SysconfDir string
LocalstateDir string
DbusInterfacesDir string
VarLibDir string
)

func init() {
Expand Down Expand Up @@ -75,6 +76,9 @@ func init() {
if DbusInterfacesDir == "" {
DbusInterfacesDir = filepath.Join(DataDir, "dbus-1", "interfaces")
}
if VarLibDir == "" {
VarLibDir = filepath.Join(LocalstateDir, "lib")
}

if ShortName == "" {
ShortName = "rhc"
Expand Down
2 changes: 2 additions & 0 deletions dist/srpm/rhc.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export %gomodulesmode
%meson_install
install --directory %{buildroot}%{_unitdir}
install --directory %{buildroot}%{_sysconfdir}/rhc
mkdir -p %{buildroot}%{_sharedstatedir}/rhc
ln -sf yggdrasil.service %{buildroot}%{_unitdir}/rhcd.service
ln -sf ../yggdrasil/config.toml %{buildroot}%{_sysconfdir}/rhc/config.toml

Expand Down Expand Up @@ -144,6 +145,7 @@ fi
%{_mandir}/man1/*
%{_unitdir}/rhc-canonical-facts.*
%{_presetdir}/*
%dir %{_sharedstatedir}/rhc

%files compat
%{_unitdir}/rhcd.service
Expand Down
27 changes: 27 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ func main() {
Before: beforeConnectAction,
Action: connectAction,
},
{
Name: "configure",
Usage: "Configure the host",
UsageText: fmt.Sprintf("%v configure [sub-command]", app.Name),
Subcommands: []*cli.Command{
{
Name: "satellite",
Usage: "Configure the host to use with Satellite server",
UsageText: fmt.Sprintf("%v configure satellite [command options]", app.Name),
Description: "The satellite sub-command configure the system for connection to a Satellite server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
Usage: "URL of the Satellite server",
Aliases: []string{"u"},
},
&cli.StringFlag{
Name: "format",
Usage: "prints output of satellite configure in machine-readable format (supported formats: \"json\")",
Aliases: []string{"f"},
},
},
Before: beforeSatelliteAction,
Action: satelliteAction,
},
},
},
{
Name: "disconnect",
Flags: []cli.Flag{
Expand Down
Loading
Loading