Skip to content

Commit

Permalink
feat: Add support of configuring host to use Satellite server
Browse files Browse the repository at this point in the history
* Card ID: CCT-993
* Introduced new command "configure" and immediately added
  sub-command "satellite" to this command. "satellite" subcommand
  has --url CLI option and you can set hostname or URL of
  server Satellite server.
  * When valid URL or hostname is provided, then rhc tries to
    read /api/ping endpoint and it tries to verify that given
    server is Satellite server
  * When Satellite server is verified, then it tries to download
    bootstrap script from /pub/katello-rhsm-consumer
  * When script is downloaded, then this script is run as root
    user.
* Is little bit risky to run some script downloaded from web
  server. For this reason we will try to use different approach
  in the future. Satellite server will provide public REST API
  endpoints providing CA certs and rendered rhsm.conf. We will
  simply download these files and install them to system.
  Unfortunately, these REST API endpoints will be available
  in some future version of Satellite.
* Directory /var/lib/rhc is created during installation of RPM
  package
  • Loading branch information
jirihnidek committed Nov 28, 2024
1 parent 2d11822 commit 2413479
Show file tree
Hide file tree
Showing 5 changed files with 377 additions and 0 deletions.
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)
}
}

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.
cmd := exec.Command(satelliteScriptPath)
err = cmd.Run()
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
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
2 changes: 2 additions & 0 deletions rhc.spec.in
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ make PREFIX=%{_prefix} \
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 All @@ -104,6 +105,7 @@ fi
%{_mandir}/man1/*
%{_unitdir}/*
%{_presetdir}/*
%dir %{_sharedstatedir}/rhc


%files compat
Expand Down
Loading

0 comments on commit 2413479

Please sign in to comment.