Skip to content

Commit

Permalink
[release-4.14] MGMT-16843: Ensure valid hostname during install
Browse files Browse the repository at this point in the history
Cherry pick of PR openshift#788
https://issues.redhat.com/browse/MGMT-16843
Some processes during install time depend on the hostname and an
invalid hostname (e.g. 'localhost', having capital letters, etc.)
that could cause the install to fail.

Extra validation is added when checking the hostname to ensure it
is valid. A random hostname will be assigned if the current
hostname is invalid.
  • Loading branch information
CrystalChun committed Feb 23, 2024
1 parent 02dc117 commit b8a3d98
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.assisted-installer-build
Expand Up @@ -2,7 +2,7 @@ FROM registry.ci.openshift.org/openshift/release:golang-1.19
ENV GO111MODULE=on
ENV GOFLAGS=""

COPY --from=quay.io/app-sre/golangci-lint:v1.53.1 /usr/bin/golangci-lint /usr/bin/golangci-lint
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/bin v1.53.1
RUN yum install -y docker && \
yum clean all

Expand Down
14 changes: 8 additions & 6 deletions src/installer/installer.go
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/google/uuid"
"github.com/openshift/assisted-installer/src/main/drymock"
"github.com/openshift/assisted-service/pkg/secretdump"
"github.com/openshift/assisted-service/pkg/validations"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
Expand Down Expand Up @@ -304,7 +305,7 @@ func (i *installer) startBootstrap() error {
remove the work done by 30-local-dns-prepender. This will cause DNS issue in bootkube and it will fail to complete
successfully
*/
err = i.checkLocalhostName()
err = i.checkHostname()
if err != nil {
i.log.Error(err)
return err
Expand Down Expand Up @@ -856,24 +857,25 @@ func (i *installer) createSingleNodeMasterIgnition() (string, error) {
return singleNodeMasterIgnitionPath, nil
}

func (i *installer) checkLocalhostName() error {
func (i *installer) checkHostname() error {
if i.DryRunEnabled {
return nil
}

i.log.Infof("Start checking localhostname")
i.log.Infof("Start checking hostname")
hostname, err := i.ops.GetHostname()
if err != nil {
i.log.Errorf("Failed to get hostname from kernel, err %s157", err)
return err
}
if hostname != "localhost" {
i.log.Infof("hostname is not localhost, no need to do anything")

if err := validations.ValidateHostname(hostname); err == nil && hostname != "localhost" {
i.log.Infof("hostname [%s] is not localhost or invalid, no need to do anything", hostname)
return nil
}

data := fmt.Sprintf("random-hostname-%s", uuid.New().String())
i.log.Infof("write data into /etc/hostname")
i.log.Infof("Hostname [%s] is invalid, generated random hostname [%s] and writing data into /etc/hostname")
return i.ops.CreateRandomHostname(data)
}

Expand Down
48 changes: 41 additions & 7 deletions src/installer/installer_test.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/openshift/assisted-installer/src/k8s_client"
"github.com/openshift/assisted-installer/src/ops"
"github.com/openshift/assisted-service/models"
"github.com/openshift/assisted-service/pkg/validations"
)

func TestValidator(t *testing.T) {
Expand Down Expand Up @@ -239,7 +240,8 @@ var _ = Describe("installer HostRoleMaster role", func() {
}
checkLocalHostname := func(hostname string, err error) {
mockops.EXPECT().GetHostname().Return(hostname, err).Times(1)
if hostname == "localhost" {
validateErr := validations.ValidateHostname(hostname)
if hostname == "localhost" || validateErr != nil {
mockops.EXPECT().CreateRandomHostname(gomock.Any()).Return(nil).Times(1)
}
}
Expand Down Expand Up @@ -319,7 +321,38 @@ var _ = Describe("installer HostRoleMaster role", func() {
{string(models.HostStageRebooting)},
})
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
WaitMasterNodesSucccess()
waitForBootkubeSuccess()
bootkubeStatusSuccess()
waitForETCDBootstrapSuccess()
bootstrapETCDStatusSuccess()
resolvConfSuccess()
waitForControllerSuccessfully(conf.ClusterID)
//HostRoleMaster flow:
downloadHostIgnitionSuccess(infraEnvId, hostId, "master-host-id.ign")
writeToDiskSuccess(gomock.Any())
reportLogProgressSuccess()
setBootOrderSuccess(gomock.Any())
uploadLogsSuccess(true)
ironicAgentDoesntExist()
rebootSuccess()
ret := installerObj.InstallNode()
Expect(ret).Should(BeNil())
})
It("bootstrap role happy flow with invalid hostname", func() {
updateProgressSuccess([][]string{{string(models.HostStageStartingInstallation), conf.Role},
{string(models.HostStageWaitingForControlPlane), waitingForBootstrapToPrepare},
{string(models.HostStageWaitingForControlPlane), waitingForMastersStatusInfo},
{string(models.HostStageInstalling), string(models.HostRoleMaster)},
{string(models.HostStageWritingImageToDisk)},
{string(models.HostStageRebooting)},
})
bootstrapSetup()
checkLocalHostname("InvalidHostname", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -406,7 +439,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
})
extractIgnitionToFS("extract failure", fmt.Errorf("extract failed"))
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -456,7 +489,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
{string(models.HostStageWaitingForControlPlane), waitingForBootstrapToPrepare},
})
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
err := fmt.Errorf("Failed to restart NetworkManager")
restartNetworkManager(err)
//HostRoleMaster flow:
Expand Down Expand Up @@ -512,7 +545,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
})
extractIgnitionToFS("extract failure", fmt.Errorf("extract failed"))
bootstrapSetup()
checkLocalHostname("not localhost", nil)
checkLocalHostname("notlocalhost", nil)
restartNetworkManager(nil)
prepareControllerSuccess()
startServicesSuccess()
Expand Down Expand Up @@ -931,7 +964,8 @@ var _ = Describe("installer HostRoleMaster role", func() {
}
checkLocalHostname := func(hostname string, err error) {
mockops.EXPECT().GetHostname().Return(hostname, err).Times(1)
if hostname == "localhost" {
validateErr := validations.ValidateHostname(hostname)
if hostname == "localhost" || validateErr != nil {
mockops.EXPECT().CreateRandomHostname(gomock.Any()).Return(nil).Times(1)
}
}
Expand Down Expand Up @@ -1000,7 +1034,7 @@ var _ = Describe("installer HostRoleMaster role", func() {
// single node bootstrap flow
singleNodeBootstrapSetup()
err := fmt.Errorf("Failed to restart NetworkManager")
checkLocalHostname("not localhost", err)
checkLocalHostname("notlocalhost", err)
ret := installerObj.InstallNode()
Expect(ret).Should(Equal(err))
})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Expand Up @@ -428,6 +428,7 @@ github.com/openshift/assisted-service/pkg/ocm
github.com/openshift/assisted-service/pkg/requestid
github.com/openshift/assisted-service/pkg/secretdump
github.com/openshift/assisted-service/pkg/transaction
github.com/openshift/assisted-service/pkg/validations
github.com/openshift/assisted-service/restapi
github.com/openshift/assisted-service/restapi/operations
github.com/openshift/assisted-service/restapi/operations/events
Expand Down

0 comments on commit b8a3d98

Please sign in to comment.