Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backward compatibility #8491

Merged
merged 10 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 102 additions & 29 deletions components/automate-cli/cmd/chef-automate/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ import (
"github.com/spf13/cobra"
)

type VersionCommand struct {
Command string `json:"command"`
Status string `json:"status"`
ErrorCode int `json:"error_code"`
ErrorDescription string `json:"error_description"`
ErrorCause string `json:"error_cause"`
ErrorStackTrace string `json:"error_stack_trace"`
ErrorRecovery string `json:"error_recovery"`
ErrorType string `json:"error_type"`
Result struct {
ClientVersion string `json:"client_version"`
ClientGitSha string `json:"client_git_sha"`
ManifestVersion string `json:"manifest_version"`
ManifestGitSha string `json:"manifest_git_sha"`
} `json:"result"`
}

type LicenseResult struct {
Result LicenseStatus `json:"result"`
ErrorType string `json:"error_type"`
Expand All @@ -42,20 +59,23 @@ type ExpirationDate struct {
const commercial = "commercial"

type LExecutor interface {
runCommandOnSingleAutomateNode(cmd *cobra.Command, args []string) (string, error)
runCommandOnSingleAutomateNodeForErr(cmd *cobra.Command, args []string) (string, error)
}

type LicenseExecutor struct{}

func (e LicenseExecutor) runCommandOnSingleAutomateNode(cmd *cobra.Command, args []string) (string, error) {
func (e LicenseExecutor) runCommandOnSingleAutomateNodeForErr(cmd *cobra.Command, args []string) (string, error) {
return RunLicenseCmdOnSingleAutomateNode(cmd, args)
}

func runTheCommandOnHA(cmd *cobra.Command, args []string, e LExecutor) error {
output, err := e.runCommandOnSingleAutomateNode(cmd, args)
func runTheCommandOnHAErr(cmd *cobra.Command, args []string, e LExecutor) error {
output, err := e.runCommandOnSingleAutomateNodeForErr(cmd, args)
if err != nil {
return err
}
if output == "" {
return nil
}

licenseResult := &LicenseResult{}
err = json.Unmarshal([]byte(output), &licenseResult)
Expand All @@ -82,27 +102,74 @@ func RunLicenseCmdOnSingleAutomateNode(cmd *cobra.Command, args []string) (strin
hostIP: ips[0],
timeout: 10,
}

sshUtil := NewSSHUtil(sshConfig)
script := "sudo chef-automate license status --result-json /hab/license.json"

script := "sudo chef-automate version --result-json /hab/version.json"
_, err = sshUtil.connectAndExecuteCommandOnRemoteSuppressLog(script, true, true)
if err != nil {
return "", err
}

script = "sudo cat /hab/version.json"
readVersion, err := sshUtil.connectAndExecuteCommandOnRemoteSuppressLog(script, true, true)
if err != nil {
return "", err
}

var versionCMD VersionCommand
if err = json.Unmarshal([]byte(readVersion), &versionCMD); err != nil {
return "", err
}

if !compareVersions(versionCMD.Result.ManifestVersion, "4.12.69") {
return "", nil
}

script = "sudo chef-automate license status --result-json /hab/license.json"
sshUtil.connectAndExecuteCommandOnRemoteSuppressLog(script, true, true)

script = "sudo cat /hab/license.json"
readLicense, err := sshUtil.connectAndExecuteCommandOnRemoteSuppressLog(script, true, true)
if err != nil {
return "", err
}
return readLicense, nil

}

func runTheCommandOnHAWarn(cmd *cobra.Command, args []string, e LExecutor) error {
output, err := e.runCommandOnSingleAutomateNodeForErr(cmd, args)
if err != nil {
return err
}

if output == "" {
return nil
}

licenseResult := &LicenseResult{}
err = json.Unmarshal([]byte(output), &licenseResult)
if err != nil {
return err
}

warnIfLicenseNearExpiry(licenseResult)
return nil
}

func checkLicenseStatusForExpiry(cmd *cobra.Command, args []string) error {
if isA2HARBFileExist() {
if err := runTheCommandOnHA(cmd, args, LicenseExecutor{}); err != nil {
if err := runTheCommandOnHAErr(cmd, args, LicenseExecutor{}); err != nil {
return err
}
} else {
if allow, err := AllowLicenseEnforcement(); err != nil && allow {
allow, err := AllowLicenseEnforcement()
if err != nil {
return err
}

if allow {
err := commandPrePersistent(cmd)
if err != nil {
return status.Wrap(err, status.CommandExecutionError, "unable to set command parent settings")
Expand Down Expand Up @@ -135,24 +202,20 @@ func AllowLicenseEnforcement() (bool, error) {
"Request for Chef Automate package manifest failed",
)
}

return compareVersions(response.BuildTimestamp, "4.12.69"), nil
}

// compare version check the v1 version is greater than or equal to v2 version
// if v1 <= v2 => false
// if v1 > v2 => true
func compareVersions(v1, v2 string) bool {

v2Slice := strings.Split(v2, ".")

// write the logic
if strings.Contains(v1, ".") {
v1Slice := strings.Split(v1, ".")

//if len(v1Slice) != len(v2Slice) {
// return false
//}

if toInt(v1Slice[0]) > toInt(v2Slice[0]) {
return true
} else if toInt(v1Slice[0]) < toInt(v2Slice[0]) {
Expand Down Expand Up @@ -181,21 +244,18 @@ func toInt(str string) int {
return v1int
}

func timeStampToTime(ts string) (time.Time, error) {
i, err := strconv.ParseInt(ts, 10, 64)
if err != nil {
return time.Time{}, err
}
return time.Unix(i, 0), nil
}

func WarnLicenseStatusForExpiry(cmd *cobra.Command, args []string) error {
if isA2HARBFileExist() {
if err := runTheCommandOnHA(cmd, args, LicenseExecutor{}); err != nil {
if err := runTheCommandOnHAWarn(cmd, args, LicenseExecutor{}); err != nil {
return err
}
} else {
if allow, err := AllowLicenseEnforcement(); err != nil && allow {
allow, err := AllowLicenseEnforcement()
if err != nil {
return err
}

if allow {
err := commandPrePersistent(cmd)
if err != nil {
return status.Wrap(err, status.CommandExecutionError, "unable to set command parent settings")
Expand All @@ -210,6 +270,7 @@ func WarnLicenseStatusForExpiry(cmd *cobra.Command, args []string) error {
return nil
}
}

return nil
}

Expand Down Expand Up @@ -311,14 +372,14 @@ func checkLicenseExpiry(licenseResult *LicenseResult) error {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! To avoid any future disruption to your DevOps processes, update your license! Please contact the Account Team or email us at chef-account-team@progress.com for further assistance.", licenseDate))
} else {
return status.New(
status.LicenseError, fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", graceDate))
status.LicenseError, fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", licenseDate))
}
} else {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", licenseDate))
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", graceDate))
}
} else { // for trail and internal licenses
if daysUntilExpiration > 0 {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! or does not exist! Please get in touch with the Account Team for further assistance.", licenseDate))
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! Please get in touch with the Account Team for further assistance.", licenseDate))
} else {
return status.New(
status.LicenseError, "Your Progress® Chef® Automate™ license has expired! You no longer have access to Chef Automate. Please contact the Account Team to upgrade to an Enterprise License.")
Expand All @@ -329,6 +390,18 @@ func checkLicenseExpiry(licenseResult *LicenseResult) error {
}

func warnIfLicenseNearExpiry(licenseResult *LicenseResult) {
if licenseResult.Result.LicenseId == "" {
if licenseResult.ErrorType != "" {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(status.New(
status.DeploymentServiceCallError,
licenseResult.ErrorDescription,
).Error())

}
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn("Your Progress® Chef® Automate™ license has expired or does not exist! You no longer have access to Chef Automate. Please contact the Account Team to upgrade to an Enterprise License.")
return
}

// Calculate the license valid date
licenseValidDate := time.Unix(licenseResult.Result.ExpirationDate.Seconds, 0) // gives unix time stamp in utc
gracePeriodDuration := 60
Expand All @@ -349,16 +422,16 @@ func warnIfLicenseNearExpiry(licenseResult *LicenseResult) {
if daysUntilExpiration <= aboutToExpire && daysUntilExpiration > 0 {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! To avoid any future disruption to your DevOps processes, update your license! Please contact the Account Team or email us at chef-account-team@progress.com for further assistance.", licenseDate))
} else {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", graceDate))
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", licenseDate))
}
} else {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", licenseDate))
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license expired on %s and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com.", graceDate))
}
} else { // for trail and internal licenses
if daysUntilExpiration > 0 {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! or does not exist! Please get in touch with the Account Team for further assistance.", licenseDate))
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn(fmt.Sprintf("Your Progress® Chef® Automate™ license is set to expire on %s! Please get in touch with the Account Team for further assistance.", licenseDate))
} else {
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn("Your Progress® Chef® Automate™ license has expired! You no longer have access to Chef Automate. Please contact the Account Team to upgrade to an Enterprise License.")
cli.NewWriter(os.Stdout, os.Stderr, os.Stdin).Warn("Your Progress® Chef® Automate™ license has expired or does not exist! You no longer have access to Chef Automate. Please contact the Account Team to upgrade to an Enterprise License.")
}
}
}
8 changes: 7 additions & 1 deletion components/automate-cli/cmd/chef-automate/helper_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func Test_compareVersions(t *testing.T) {
Expand All @@ -27,4 +28,9 @@ func Test_compareVersions(t *testing.T) {
isNew := compareVersions("5.0.0", v2)
require.True(t, isNew)
})
t.Run("Test 5", func(t *testing.T) {
isNew := compareVersions("4.12.123", v2)
require.True(t, isNew)
})

}
5 changes: 2 additions & 3 deletions components/automate-cli/cmd/chef-automate/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ func newIAMRestoreDefaultAdminAccessCmd() *cobra.Command {
Short: "Restore the factory default admin user, team, and access",
Long: "Recreate the admin user, admin team, and related admin policy as needed " +
"to restore to factory default and update the admin user's password",
PersistentPreRunE: checkLicenseStatusForExpiry,
RunE: runRestoreDefaultAdminAccessAdminCmd,
Args: cobra.ExactArgs(1),
RunE: runRestoreDefaultAdminAccessAdminCmd,
Args: cobra.ExactArgs(1),
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func Test_checkLicenseExpiry(t *testing.T) {
},
},
},
wantErr: errors.New("Your Progress® Chef® Automate™ license expired on 15-09-2024 and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com."),
wantErr: errors.New("Your Progress® Chef® Automate™ license expired on 20-07-2024 and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at chef-account-team@progress.com."),
},
{
name: "No License is applied",
Expand Down
11 changes: 5 additions & 6 deletions components/automate-cli/cmd/chef-automate/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ func init() {
}

var maintenanceCmd = &cobra.Command{
Use: "maintenance [on|off]",
Short: "Put Chef Automate into or out of maintenance mode",
Long: "Chef Automate maintenance mode keeps all services running but rejects new connections at the load balancer so that maintenance operations can be performed.",
Args: cobra.ExactArgs(1),
PersistentPreRunE: checkLicenseStatusForExpiry,
RunE: runMaintenanceCmd,
Use: "maintenance [on|off]",
Short: "Put Chef Automate into or out of maintenance mode",
Long: "Chef Automate maintenance mode keeps all services running but rejects new connections at the load balancer so that maintenance operations can be performed.",
Args: cobra.ExactArgs(1),
RunE: runMaintenanceCmd,
Annotations: map[string]string{
docs.Tag: docs.FrontEnd,
},
Expand Down
7 changes: 4 additions & 3 deletions components/automate-cli/cmd/chef-automate/start_linx.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ var startCmdFlags = struct {
}{}

var startCommand = &cobra.Command{
Use: "start",
Short: "Start Chef Automate",
RunE: runStartCmd,
Use: "start",
Short: "Start Chef Automate",
PersistentPreRunE: WarnLicenseStatusForExpiry,
RunE: runStartCmd,
Annotations: map[string]string{
docs.Tag: docs.BastionHost,
},
Expand Down
5 changes: 2 additions & 3 deletions components/automate-cli/cmd/chef-automate/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ var upgradeStatusCmd = &cobra.Command{
Use: "status",
Short: "Get upgrade status of Chef Automate",
Long: "Get upgrade status of Chef Automate",
// PersistentPreRunE: checkLicenseStatusForExpiry,
RunE: statusUpgradeCmd,
Args: cobra.MaximumNArgs(0),
RunE: statusUpgradeCmd,
Args: cobra.MaximumNArgs(0),
Annotations: map[string]string{
docs.Tag: docs.FrontEnd,
},
Expand Down
Loading