Skip to content

Commit 8a2a2d3

Browse files
committed
f
1 parent 6818624 commit 8a2a2d3

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

cmd/installer/cli/install.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ func preRunInstallCommon(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimec
397397
flags.licenseBytes = b
398398

399399
// validate the the license is indeed a license file
400-
l, err := helpers.ParseLicense(flags.licenseFile)
400+
l, err := helpers.ParseLicenseFromBytes(b)
401401
if err != nil {
402402
var notALicenseFileErr helpers.ErrNotALicenseFile
403403
if errors.As(err, &notALicenseFileErr) {
@@ -425,6 +425,22 @@ func preRunInstallCommon(cmd *cobra.Command, flags *InstallCmdFlags, rc runtimec
425425
flags.airgapMetadata = metadata
426426
}
427427

428+
// sync the license if we are in the manager experience and a license is provided and we are
429+
// not in airgap mode
430+
if flags.enableManagerExperience && flags.license != nil && !flags.isAirgap {
431+
replicatedAPI, err := newReplicatedAPIClient(flags.license, flags.clusterID)
432+
if err != nil {
433+
return fmt.Errorf("failed to create replicated API client: %w", err)
434+
}
435+
436+
updatedLicense, licenseBytes, err := syncLicense(cmd.Context(), replicatedAPI, flags.license)
437+
if err != nil {
438+
return fmt.Errorf("failed to sync license: %w", err)
439+
}
440+
flags.license = updatedLicense
441+
flags.licenseBytes = licenseBytes
442+
}
443+
428444
var err error
429445
flags.embeddedAssetsSize, err = goods.SizeOfEmbeddedAssets()
430446
if err != nil {
@@ -627,22 +643,6 @@ func runManagerExperienceInstall(
627643
return fmt.Errorf("get kotsadm namespace: %w", err)
628644
}
629645

630-
replicatedAPI, err := newReplicatedAPIClient(flags.license, flags.clusterID)
631-
if err != nil {
632-
return fmt.Errorf("failed to create replicated API client: %w", err)
633-
}
634-
635-
// Sync license before starting the manager experience (online only)
636-
isAirgap := flags.airgapBundle != ""
637-
if !isAirgap {
638-
updatedLicense, licenseBytes, err := syncLicense(ctx, replicatedAPI, flags.license)
639-
if err != nil {
640-
return fmt.Errorf("failed to sync license: %w", err)
641-
}
642-
flags.license = updatedLicense
643-
flags.licenseBytes = licenseBytes
644-
}
645-
646646
// this is necessary because the api listens on all interfaces,
647647
// and we only know the interface to use when the user selects it in the ui
648648
ipAddresses, err := netutils.ListAllValidIPAddresses()
@@ -913,7 +913,7 @@ func verifyAndPrompt(ctx context.Context, cmd *cobra.Command, appSlug string, fl
913913
}
914914

915915
logrus.Debugf("checking license matches")
916-
license, err := getLicenseFromFilepath(flags.licenseFile)
916+
license, err := verifyLicense(flags.license)
917917
if err != nil {
918918
return err
919919
}
@@ -985,29 +985,24 @@ func ensureAdminConsolePassword(flags *InstallCmdFlags) error {
985985
return nil
986986
}
987987

988-
func getLicenseFromFilepath(licenseFile string) (*kotsv1beta1.License, error) {
988+
func verifyLicense(license *kotsv1beta1.License) (*kotsv1beta1.License, error) {
989989
rel := release.GetChannelRelease()
990990

991991
// handle the three cases that do not require parsing the license file
992992
// 1. no release and no license, which is OK
993993
// 2. no license and a release, which is not OK
994994
// 3. a license and no release, which is not OK
995-
if rel == nil && licenseFile == "" {
995+
if rel == nil && license == nil {
996996
// no license and no release, this is OK
997997
return nil, nil
998-
} else if rel == nil && licenseFile != "" {
998+
} else if rel == nil && license != nil {
999999
// license is present but no release, this means we would install without vendor charts and k0s overrides
10001000
return nil, fmt.Errorf("a license was provided but no release was found in binary, please rerun without the license flag")
1001-
} else if rel != nil && licenseFile == "" {
1001+
} else if rel != nil && license == nil {
10021002
// release is present but no license, this is not OK
10031003
return nil, fmt.Errorf("no license was provided for %s and one is required, please rerun with '--license <path to license file>'", rel.AppSlug)
10041004
}
10051005

1006-
license, err := helpers.ParseLicense(licenseFile)
1007-
if err != nil {
1008-
return nil, fmt.Errorf("failed to parse the license file at %q, please ensure it is not corrupt: %w", licenseFile, err)
1009-
}
1010-
10111006
// Check if the license matches the application version data
10121007
if rel.AppSlug != license.Spec.AppSlug {
10131008
// if the app is different, we will not be able to provide the correct vendor supplied charts and k0s overrides

cmd/installer/cli/upgrade.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,18 +236,39 @@ func preRunUpgrade(ctx context.Context, flags UpgradeCmdFlags, upgradeConfig *up
236236
return fmt.Errorf("failed to stat data directory: %w", err)
237237
}
238238

239-
// Validate the license is indeed a license file
240-
license, err := getLicenseFromFilepath(flags.licenseFile)
241-
if err != nil {
242-
return err
243-
}
244-
upgradeConfig.license = license
245239
data, err := os.ReadFile(flags.licenseFile)
246240
if err != nil {
247241
return fmt.Errorf("failed to read license file: %w", err)
248242
}
249243
upgradeConfig.licenseBytes = data
250244

245+
// validate the the license is indeed a license file
246+
l, err := helpers.ParseLicenseFromBytes(data)
247+
if err != nil {
248+
var notALicenseFileErr helpers.ErrNotALicenseFile
249+
if errors.As(err, &notALicenseFileErr) {
250+
return fmt.Errorf("failed to parse the license file at %q, please ensure it is not corrupt: %w", flags.licenseFile, err)
251+
}
252+
253+
return fmt.Errorf("failed to parse license file: %w", err)
254+
}
255+
upgradeConfig.license = l
256+
257+
// sync the license if a license is provided and we are not in airgap mode
258+
if upgradeConfig.license != nil && flags.airgapBundle == "" {
259+
replicatedAPI, err := newReplicatedAPIClient(upgradeConfig.license, upgradeConfig.clusterID)
260+
if err != nil {
261+
return fmt.Errorf("failed to create replicated API client: %w", err)
262+
}
263+
264+
updatedLicense, licenseBytes, err := syncLicense(ctx, replicatedAPI, upgradeConfig.license)
265+
if err != nil {
266+
return fmt.Errorf("failed to sync license: %w", err)
267+
}
268+
upgradeConfig.license = updatedLicense
269+
upgradeConfig.licenseBytes = licenseBytes
270+
}
271+
251272
// Continue using "kotsadm" namespace if it exists for backwards compatibility, otherwise use the appSlug
252273
ns, err := runtimeconfig.KotsadmNamespace(ctx, kcli)
253274
if err != nil {
@@ -444,22 +465,6 @@ func runManagerExperienceUpgrade(
444465
ctx context.Context, flags UpgradeCmdFlags, upgradeConfig upgradeConfig, rc runtimeconfig.RuntimeConfig,
445466
metricsReporter metrics.ReporterInterface, appTitle string, targetVersion string, initialVersion string,
446467
) (finalErr error) {
447-
replicatedAPI, err := newReplicatedAPIClient(upgradeConfig.license, upgradeConfig.clusterID)
448-
if err != nil {
449-
return fmt.Errorf("failed to create replicated API client: %w", err)
450-
}
451-
452-
// Sync license before starting the manager experience (online only)
453-
isAirgap := flags.airgapBundle != ""
454-
if !isAirgap {
455-
updatedLicense, licenseBytes, err := syncLicense(ctx, replicatedAPI, upgradeConfig.license)
456-
if err != nil {
457-
return fmt.Errorf("failed to sync license: %w", err)
458-
}
459-
upgradeConfig.license = updatedLicense
460-
upgradeConfig.licenseBytes = licenseBytes
461-
}
462-
463468
apiConfig := apiOptions{
464469
APIConfig: apitypes.APIConfig{
465470
InstallTarget: apitypes.InstallTarget(flags.target),

pkg/helpers/parse.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func ParseLicense(fpath string) (*kotsv1beta1.License, error) {
3939
if err != nil {
4040
return nil, fmt.Errorf("failed to read license file: %w", err)
4141
}
42-
return ParseLicenseFromString(string(data))
42+
return ParseLicenseFromBytes(data)
4343
}
4444

45-
// ParseLicenseFromString parses the license from a YAML string
46-
func ParseLicenseFromString(yamlContent string) (*kotsv1beta1.License, error) {
45+
// ParseLicenseFromBytes parses the license from a byte slice
46+
func ParseLicenseFromBytes(data []byte) (*kotsv1beta1.License, error) {
4747
var license kotsv1beta1.License
48-
if err := kyaml.Unmarshal([]byte(yamlContent), &license); err != nil {
48+
if err := kyaml.Unmarshal(data, &license); err != nil {
4949
return nil, ErrNotALicenseFile{Err: err}
5050
}
5151
if license.Spec.LicenseID == "" {

0 commit comments

Comments
 (0)