github.com/openshift/installer@v1.4.17/pkg/asset/installconfig/platformcredscheck.go (about)

     1  package installconfig
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/openshift/installer/pkg/asset"
    10  	azureconfig "github.com/openshift/installer/pkg/asset/installconfig/azure"
    11  	gcpconfig "github.com/openshift/installer/pkg/asset/installconfig/gcp"
    12  	ibmcloudconfig "github.com/openshift/installer/pkg/asset/installconfig/ibmcloud"
    13  	openstackconfig "github.com/openshift/installer/pkg/asset/installconfig/openstack"
    14  	ovirtconfig "github.com/openshift/installer/pkg/asset/installconfig/ovirt"
    15  	powervsconfig "github.com/openshift/installer/pkg/asset/installconfig/powervs"
    16  	"github.com/openshift/installer/pkg/types"
    17  	"github.com/openshift/installer/pkg/types/aws"
    18  	"github.com/openshift/installer/pkg/types/azure"
    19  	"github.com/openshift/installer/pkg/types/baremetal"
    20  	"github.com/openshift/installer/pkg/types/external"
    21  	"github.com/openshift/installer/pkg/types/gcp"
    22  	"github.com/openshift/installer/pkg/types/ibmcloud"
    23  	"github.com/openshift/installer/pkg/types/none"
    24  	"github.com/openshift/installer/pkg/types/nutanix"
    25  	"github.com/openshift/installer/pkg/types/openstack"
    26  	"github.com/openshift/installer/pkg/types/ovirt"
    27  	"github.com/openshift/installer/pkg/types/powervs"
    28  	"github.com/openshift/installer/pkg/types/vsphere"
    29  )
    30  
    31  // PlatformCredsCheck is an asset that checks the platform credentials, asks for them or errors out if invalid
    32  // the cluster.
    33  type PlatformCredsCheck struct {
    34  }
    35  
    36  var _ asset.Asset = (*PlatformCredsCheck)(nil)
    37  
    38  // Dependencies returns the dependencies for PlatformCredsCheck
    39  func (a *PlatformCredsCheck) Dependencies() []asset.Asset {
    40  	return []asset.Asset{
    41  		&InstallConfig{},
    42  	}
    43  }
    44  
    45  // Generate queries for input from the user.
    46  func (a *PlatformCredsCheck) Generate(ctx context.Context, dependencies asset.Parents) error {
    47  	ic := &InstallConfig{}
    48  	dependencies.Get(ic)
    49  
    50  	var err error
    51  	platform := ic.Config.Platform.Name()
    52  	switch platform {
    53  	case aws.Name:
    54  		_, err := ic.AWS.Session(ctx)
    55  		if err != nil {
    56  			return err
    57  		}
    58  	case gcp.Name:
    59  		client, err := gcpconfig.NewClient(ctx)
    60  		if err != nil {
    61  			return err
    62  		}
    63  
    64  		errorList := gcpconfig.ValidateCredentialMode(client, ic.Config)
    65  		if errorList != nil {
    66  			return errors.Wrap(errorList.ToAggregate(), "validating credentials")
    67  		}
    68  	case ibmcloud.Name:
    69  		// A pre-existing installConfig with potential serviceEndpoints would be required,
    70  		// but doesn't exist at this time (generating an installConfig), so we pass nil
    71  		_, err = ibmcloudconfig.NewClient(nil)
    72  		if err != nil {
    73  			return errors.Wrap(err, "creating IBM Cloud session")
    74  		}
    75  	case powervs.Name:
    76  		_, err = powervsconfig.NewClient()
    77  		if err != nil {
    78  			return errors.Wrap(err, "creating IBM Cloud session")
    79  		}
    80  	case openstack.Name:
    81  		_, err = openstackconfig.GetSession(ic.Config.Platform.OpenStack.Cloud)
    82  		if err != nil {
    83  			return errors.Wrap(err, "creating OpenStack session")
    84  		}
    85  	case baremetal.Name, external.Name, none.Name, vsphere.Name, nutanix.Name:
    86  		// no creds to check
    87  	case azure.Name:
    88  		azureSession, err := ic.Azure.Session()
    89  		if err != nil {
    90  			return errors.Wrap(err, "creating Azure session")
    91  		}
    92  		switch azureSession.AuthType {
    93  		case azureconfig.ClientCertificateAuth, azureconfig.ManagedIdentityAuth:
    94  			if ic.Config.CredentialsMode != types.ManualCredentialsMode {
    95  				return fmt.Errorf("authentication with client certificates or managed identity is only supported in manual credentials mode")
    96  			}
    97  		}
    98  	case ovirt.Name:
    99  		con, err := ovirtconfig.NewConnection()
   100  		if err != nil {
   101  			return errors.Wrap(err, "creating Engine connection")
   102  		}
   103  		err = con.Test()
   104  		if err != nil {
   105  			return errors.Wrap(err, "testing Engine connection")
   106  		}
   107  	default:
   108  		err = fmt.Errorf("unknown platform type %q", platform)
   109  	}
   110  
   111  	return err
   112  }
   113  
   114  // Name returns the human-friendly name of the asset.
   115  func (a *PlatformCredsCheck) Name() string {
   116  	return "Platform Credentials Check"
   117  }