Skip to content

Commit

Permalink
Merge pull request #432 from appgate/SA-22098-optional-global-setting…
Browse files Browse the repository at this point in the history
…s-read

Make global-settings read optional on backup
  • Loading branch information
kajes committed Jun 21, 2023
2 parents b7fd7f6 + e414837 commit e645177
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
5 changes: 3 additions & 2 deletions cmd/appliance/upgrade/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/appgate/sdp-api-client-go/api/v19/openapi"
"github.com/appgate/sdpctl/pkg/api"
appliancepkg "github.com/appgate/sdpctl/pkg/appliance"
"github.com/appgate/sdpctl/pkg/appliance/change"
"github.com/appgate/sdpctl/pkg/cmdutil"
Expand Down Expand Up @@ -429,7 +430,7 @@ func prepareRun(cmd *cobra.Command, args []string, opts *prepareUpgradeOptions)
exists := true
if _, err := a.FileStatus(ctx, logServerZipName); err != nil {
// if we dont get 404, return err
if errors.Is(err, appliancepkg.ErrFileNotFound) {
if errors.Is(err, api.ErrFileNotFound) {
exists = false
} else {
return err
Expand Down Expand Up @@ -514,7 +515,7 @@ func prepareRun(cmd *cobra.Command, args []string, opts *prepareUpgradeOptions)
existingFile, err := a.FileStatus(ctx, opts.filename)
if err != nil {
// if we dont get 404, return err
if errors.Is(err, appliancepkg.ErrFileNotFound) {
if errors.Is(err, api.ErrFileNotFound) {
shouldUpload = true
} else {
return err
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/apiutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ type Error struct {
Errors []error
}

var (
ErrFileNotFound = stderrors.New("File not found")
ForbiddenErr error = &Error{
StatusCode: 403,
Err: stderrors.New("403 Forbidden"),
}
UnavailableErr error = &Error{
StatusCode: 503,
Err: stderrors.New("503 Service Unavailable"),
}
)

func (e *Error) Error() string {
if e.Err != nil {
return fmt.Sprintf("HTTP %d - %s", e.StatusCode, e.Err.Error())
Expand Down
4 changes: 1 addition & 3 deletions pkg/appliance/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ func (a *Appliance) Stats(ctx context.Context, orderBy []string, descending bool
return status, response, nil
}

var ErrFileNotFound = errors.New("File not found")

// FileStatus Get the status of a File uploaded to the current Controller.
func (a *Appliance) FileStatus(ctx context.Context, filename string) (*openapi.File, error) {
log := logrus.WithField("file", filename)
Expand All @@ -164,7 +162,7 @@ func (a *Appliance) FileStatus(ctx context.Context, filename string) (*openapi.F
defer log.WithField("status", f.GetStatus()).Info("got file status")
if err != nil {
if r.StatusCode == http.StatusNotFound {
return f, fmt.Errorf("%q: %w", filename, ErrFileNotFound)
return f, fmt.Errorf("%q: %w", filename, api.ErrFileNotFound)
}
return f, api.HTTPErrorResponse(r, err)
}
Expand Down
18 changes: 15 additions & 3 deletions pkg/appliance/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"html/template"
"io"
"net/http"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -92,6 +93,10 @@ func PerformBackup(cmd *cobra.Command, args []string, opts *BackupOpts) (map[str
}
backupEnabled, err := backupEnabled(ctx, app.APIClient, token, opts.NoInteractive)
if err != nil {
// This most likely means that the user does not have permission to read global settings, which is fine, so we skip the entire enable/disable prompt
if errors.Is(err, api.ForbiddenErr) {
goto NO_ENABLE_CHECK
}
if opts.NoInteractive {
return backupIDs, errors.New("Backup failed due to error while --no-interactive flag is set")
}
Expand All @@ -104,6 +109,7 @@ func PerformBackup(cmd *cobra.Command, args []string, opts *BackupOpts) (map[str
return backupIDs, errors.New("Backup API is disabled in the collective. Use the 'sdpctl appliance backup api' command to enable it")
}

NO_ENABLE_CHECK:
appliances, err := app.List(ctx, nil, []string{"name"}, false)
if err != nil {
return backupIDs, err
Expand Down Expand Up @@ -261,6 +267,9 @@ func PerformBackup(cmd *cobra.Command, args []string, opts *BackupOpts) (map[str
}
b.backupID, err = backupAPI.Initiate(ctx, b.applianceID, logs, audit)
if err != nil {
if errors.Is(err, api.UnavailableErr) {
return b, errors.New("The backup API is disabled")
}
return b, err
}
log.WithFields(f).Info("Initiated backup")
Expand Down Expand Up @@ -299,7 +308,7 @@ func PerformBackup(cmd *cobra.Command, args []string, opts *BackupOpts) (map[str
defer wg.Done()
backedUp, err := b(appliance, t)
if err != nil {
errorChannel <- fmt.Errorf("Could not backup %s %s", appliance.GetName(), err)
errorChannel <- fmt.Errorf("Could not backup %s: %s", appliance.GetName(), err)
return
}
backups <- backedUp
Expand All @@ -316,13 +325,13 @@ func PerformBackup(cmd *cobra.Command, args []string, opts *BackupOpts) (map[str
backupIDs[b.applianceID] = b.backupID
log.WithField("file", b.destination).Info("Wrote backup file")
}
var result error
var result *multierror.Error
for err := range errorChannel {
log.Error(err)
result = multierror.Append(err)
}

return backupIDs, result
return backupIDs, result.ErrorOrNil()
}

func CleanupBackup(opts *BackupOpts, IDs map[string]string) error {
Expand Down Expand Up @@ -420,6 +429,9 @@ func BackupPrompt(appliances []openapi.Appliance, preSelected []openapi.Applianc
func backupEnabled(ctx context.Context, client *openapi.APIClient, token string, noInteraction bool) (bool, error) {
settings, response, err := client.GlobalSettingsApi.GlobalSettingsGet(ctx).Authorization(token).Execute()
if err != nil {
if response.StatusCode == http.StatusForbidden {
return false, api.ForbiddenErr
}
return false, api.HTTPErrorResponse(response, err)
}
enabled := settings.GetBackupApiEnabled()
Expand Down
4 changes: 4 additions & 0 deletions pkg/appliance/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"net/http"
"os"
"time"

Expand Down Expand Up @@ -33,6 +34,9 @@ func (b *Backup) Initiate(ctx context.Context, applianceID string, logs, audit b
}
status, response, err := b.APIClient.ApplianceBackupApi.AppliancesIdBackupPost(ctx, applianceID).Authorization(b.Token).AppliancesIdBackupPostRequest(o).Execute()
if err != nil {
if response.StatusCode == http.StatusServiceUnavailable {
return "", api.UnavailableErr
}
return "", api.HTTPErrorResponse(response, err)
}

Expand Down

0 comments on commit e645177

Please sign in to comment.