Skip to content

Commit b35ce5b

Browse files
authored
feat(api): sync license before templating (#3109)
1 parent 1d50ffb commit b35ce5b

File tree

13 files changed

+1084
-215
lines changed

13 files changed

+1084
-215
lines changed

cmd/installer/cli/install.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,22 @@ func preRunInstall(cmd *cobra.Command, flags *installFlags, rc runtimeconfig.Run
370370
return nil, err
371371
}
372372

373+
// sync the license if we are in the manager experience and a license is provided and we are
374+
// not in airgap mode
375+
if installCfg.enableManagerExperience && installCfg.license != nil && !installCfg.isAirgap {
376+
replicatedAPI, err := newReplicatedAPIClient(installCfg.license, installCfg.clusterID)
377+
if err != nil {
378+
return nil, fmt.Errorf("failed to create replicated API client: %w", err)
379+
}
380+
381+
updatedLicense, licenseBytes, err := syncLicense(cmd.Context(), replicatedAPI, installCfg.license)
382+
if err != nil {
383+
return nil, fmt.Errorf("failed to sync license: %w", err)
384+
}
385+
installCfg.license = updatedLicense
386+
installCfg.licenseBytes = licenseBytes
387+
}
388+
373389
// Set runtime config values from flags
374390
rc.SetAdminConsolePort(flags.adminConsolePort)
375391
ki.SetAdminConsolePort(flags.adminConsolePort)
@@ -702,7 +718,7 @@ func verifyAndPrompt(ctx context.Context, cmd *cobra.Command, appSlug string, fl
702718
}
703719

704720
logrus.Debugf("checking license matches")
705-
license, err := getLicenseFromFilepath(flags.licenseFile)
721+
license, err := verifyLicense(installCfg.license)
706722
if err != nil {
707723
return err
708724
}
@@ -780,29 +796,24 @@ func ensureAdminConsolePassword(flags *installFlags) error {
780796
return nil
781797
}
782798

783-
func getLicenseFromFilepath(licenseFile string) (*kotsv1beta1.License, error) {
799+
func verifyLicense(license *kotsv1beta1.License) (*kotsv1beta1.License, error) {
784800
rel := release.GetChannelRelease()
785801

786802
// handle the three cases that do not require parsing the license file
787803
// 1. no release and no license, which is OK
788804
// 2. no license and a release, which is not OK
789805
// 3. a license and no release, which is not OK
790-
if rel == nil && licenseFile == "" {
806+
if rel == nil && license == nil {
791807
// no license and no release, this is OK
792808
return nil, nil
793-
} else if rel == nil && licenseFile != "" {
809+
} else if rel == nil && license != nil {
794810
// license is present but no release, this means we would install without vendor charts and k0s overrides
795811
return nil, fmt.Errorf("a license was provided but no release was found in binary, please rerun without the license flag")
796-
} else if rel != nil && licenseFile == "" {
812+
} else if rel != nil && license == nil {
797813
// release is present but no license, this is not OK
798814
return nil, fmt.Errorf("no license was provided for %s and one is required, please rerun with '--license <path to license file>'", rel.AppSlug)
799815
}
800816

801-
license, err := helpers.ParseLicense(licenseFile)
802-
if err != nil {
803-
return nil, fmt.Errorf("failed to parse the license file at %q, please ensure it is not corrupt: %w", licenseFile, err)
804-
}
805-
806817
// Check if the license matches the application version data
807818
if rel.AppSlug != license.Spec.AppSlug {
808819
// if the app is different, we will not be able to provide the correct vendor supplied charts and k0s overrides
@@ -1156,16 +1167,6 @@ func validateAdminConsolePassword(password, passwordCheck string) bool {
11561167
return true
11571168
}
11581169

1159-
func replicatedAppURL() string {
1160-
domains := getDomains()
1161-
return netutils.MaybeAddHTTPS(domains.ReplicatedAppDomain)
1162-
}
1163-
1164-
func proxyRegistryURL() string {
1165-
domains := getDomains()
1166-
return netutils.MaybeAddHTTPS(domains.ProxyRegistryDomain)
1167-
}
1168-
11691170
func waitForNode(ctx context.Context) error {
11701171
kcli, err := kubeutils.KubeClient()
11711172
if err != nil {

cmd/installer/cli/install_config.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cli
33
import (
44
"context"
55
"crypto/tls"
6+
"errors"
67
"fmt"
78
"os"
89

@@ -98,8 +99,14 @@ func buildInstallConfig(flags *installFlags) (*installConfig, error) {
9899
}
99100
installCfg.licenseBytes = b
100101

101-
l, err := helpers.ParseLicense(flags.licenseFile)
102+
// validate the license is indeed a license file
103+
l, err := helpers.ParseLicenseFromBytes(b)
102104
if err != nil {
105+
var notALicenseFileErr helpers.ErrNotALicenseFile
106+
if errors.As(err, &notALicenseFileErr) {
107+
return nil, fmt.Errorf("failed to parse the license file at %q, please ensure it is not corrupt: %w", flags.licenseFile, err)
108+
}
109+
103110
return nil, fmt.Errorf("failed to parse license file: %w", err)
104111
}
105112
installCfg.license = l

0 commit comments

Comments
 (0)