package asa import ( "errors" "fmt" "strings" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) // ROMUpgradePayload Expected payload for a rommon upgrade operation type ROMUpgradePayload struct { // UpgradeFile : Filename of upgrade firmware to use. Can either have been prestaged or this process will upload it if not found UpgradeFile string `json:"upgrade_file"` // MD5 : Used to verify the file on the device, or to verify a the new file that is uploaded MD5 string `json:"md5"` // TargetVersion : Version firewall should have after upgrade (also checks it's not already at this version) TargetVersion string `json:"target_version"` } func (r *ROMUpgradePayload) Valid(c *gin.Context) bool { valid := true if r.UpgradeFile == "" { valid = false _ = c.Error(errors.New("upgrade_file is blank")) } if r.MD5 == "" { valid = false _ = c.Error(errors.New("md5 cannot be blank")) } if r.TargetVersion == "" { valid = false _ = c.Error(errors.New("target_version cannot be blank")) } return valid } func (a *ASA) checkROMUpgradeFile(payload ROMUpgradePayload) error { a.log.Debug("checkROMUpgradeFile()") a.log.Debugf("Verifying file %s exists", payload.UpgradeFile) exists, err := a.FileExist(payload.UpgradeFile) if err != nil { return err } // Does the MD5 of the file on the device match what we were given? upload := true // We'll assume it's not good and we'll need to reupload it if exists { a.log.Debug("Upgrade file present") verify, err := a.VerifyFile("md5", payload.UpgradeFile) if err != nil { return fmt.Errorf("error verifying existing file: %w", err) } if verify.Md5 != nil { if *verify.Md5 == payload.MD5 { a.log.Debug("MD5 verification of file successful") upload = false // File exists and is good, no need to reupload } } } else { a.log.Debug("Upgrade file does not already exist on device") } if upload { a.log.Debug("Uploading upgrade file to device") file := fmt.Sprintf("rax.mirror.rackspace.com/netsec.devops/%s", payload.UpgradeFile) res, err := a.HTTPToFlash(file, 120) if err != nil { return fmt.Errorf("error uploading file to device: %v", err) } logrus.WithField("message", res).Info("upgrade file loaded to device") } return nil } // ROMUpgrade instructs the ASA to upgrade it's firmware to the file given in the payload. func (a *ASA) ROMUpgrade(payload ROMUpgradePayload, safetyOff bool) (string, error) { a.log.Debug("ASA.ROMUpgrade()") // Verify it's a 5508 a.log.Debug("Fetching version information for model verification") if err := a.GetVersion_HTTP(); err != nil { return "", fmt.Errorf("error getting version for model verification: %w", err) } if a.Status.Version == nil || a.Status.Version.Hardware == nil || a.Status.Version.Hardware.Platform == nil { return "", errors.New("version information is nil even after GetVersion") } a.log.WithField("platform", *a.Status.Version.Hardware.Platform).Debug("Version information retrieved") if *a.Status.Version.Hardware.Platform != "ASA5508" { return "", fmt.Errorf("this command is currently only supported on 5508's, this appears to be a '%s'", *a.Status.Version.Hardware.Platform) } // Check if we're already at target version a.log.Debug("Gathering module information") if err := a.GatherModules("all", false); err != nil { return "", fmt.Errorf("error getting module information (to check existing firmware version) from device: %w", err) } mod, ok := a.Modules["1"] if !ok { return "", fmt.Errorf("unable to find module information for firmware version check") } a.log.WithField("version", mod.FWVersion).Debug("Current firmware revision retrieved") if mod.MacAddressRange.FWVersion == payload.TargetVersion { return "", fmt.Errorf("firewall is already at version %s", mod.MacAddressRange.FWVersion) } // Check if the file is already there, and upload it if not or md5 isn't correct if err := a.checkROMUpgradeFile(payload); err != nil { return "", err } a.log.Debug("Entering conf t") res, err := a.sshClient.PostCommand("conf t", "# ", 30) if err != nil { return "", fmt.Errorf("error entering conf t: %v", err) } cmd := fmt.Sprintf("upgrade rommon disk0:%s", payload.UpgradeFile) a.log.WithField("command", cmd).Debug("Executing upgrade command") prompt := `(?i)(?:Save\? \[Y\]es/\[N\]o:|reload\? \[confirm\])` res, err = a.sshClient.PostReCommand(cmd, prompt, 15) if err != nil { return "", fmt.Errorf("error executing upgrade command: %v", err) } if strings.Contains(strings.ToLower(*res.Response), "system config has been modified") { a.log.Debug("running config has been changed but not saved, aborting") res, err = a.sshClient.PostReCommand("nn", "# ", 10) if err != nil { return "", fmt.Errorf("error aborting because of unsaved system config: %v", err) } return "", fmt.Errorf("running config has been changed but not saved, aborting rommon upgrade") } if !strings.Contains(strings.ToLower(*res.Response), "[confirm]") { return "", fmt.Errorf("did not receive expected reload prompt after upgrade command") } if safetyOff { a.log.Debug("Safety is off, sending y to reload prompt") res, err = a.sshClient.SendText("y", "Performing upgrade on rom-monitor", 10) if err != nil { // we want a prompt timeout, since sshapi can't look for the "right" prompt in the response we'll have to assume // prompt timeout == good reload, we'll confirm afterwards lowerErr := strings.ToLower(err.Error()) if !strings.Contains(lowerErr, "prompt timeout") { // the error doesn't contain prompt timeout, so report it as an actual error return "", fmt.Errorf("error responding to reload prompt: %v", err) } if strings.Contains(lowerErr, "shutdown now") { return "upgrade command sent successfully to device, reboot in progress", nil } return "", fmt.Errorf("unknown error encountered: %v", err) } return *res.Response, errors.New("unknown error encountered. expected prompt timeout error, but received no error") } else { // abort, safety wasn't off a.log.Debug("Safety is on, responding N to reload prompt") res, err = a.sshClient.SendText("n", "# ", 10) if err != nil { return "", fmt.Errorf("error responding to reload prompt: %v", err) } } return "Upgrade command sent to device, but safety is on so responded to reload prompt with 'n'. Device has NOT been upgraded.", nil }