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

Enable/disable API keys UI given API keys config setting #4559

Merged
merged 12 commits into from
Sep 7, 2020
Prev Previous commit
Next Next commit
Block API keys endpoints if disabled in the config; add tests
  • Loading branch information
ikapelyukhin committed Aug 27, 2020
commit 182ee52590a297a762b1182b84c502618056ef0e
32 changes: 32 additions & 0 deletions src/jetstream/apikeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,30 @@ import (
"errors"
"net/http"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/interfaces/config"
"github.com/labstack/echo"
log "github.com/sirupsen/logrus"
)

func (p *portalProxy) checkIfAPIKeysEnabled(userGUID string) error {
if p.Config.APIKeysEnabled == config.APIKeysConfigEnum.Disabled {
log.Info("API keys are disabled")
return errors.New("API keys are disabled")
} else if p.Config.APIKeysEnabled == config.APIKeysConfigEnum.AdminOnly {
user, err := p.StratosAuthService.GetUser(userGUID)
if err != nil {
return err
}

if !user.Admin {
log.Info("API keys are disabled for non-admin users")
return errors.New("API keys are disabled for non-admin users")
}
}

return nil
}

func (p *portalProxy) addAPIKey(c echo.Context) error {
log.Debug("addAPIKey")

Expand All @@ -18,6 +38,10 @@ func (p *portalProxy) addAPIKey(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "Comment can't be empty")
}

if err := p.checkIfAPIKeysEnabled(userGUID); err != nil {
return echo.NewHTTPError(http.StatusForbidden, err.Error())
}

apiKey, err := p.APIKeysRepository.AddAPIKey(userGUID, comment)
if err != nil {
log.Errorf("Error adding API key: %v", err)
Expand All @@ -32,6 +56,10 @@ func (p *portalProxy) listAPIKeys(c echo.Context) error {

userGUID := c.Get("user_id").(string)

if err := p.checkIfAPIKeysEnabled(userGUID); err != nil {
return echo.NewHTTPError(http.StatusForbidden, err.Error())
}

apiKeys, err := p.APIKeysRepository.ListAPIKeys(userGUID)
if err != nil {
log.Errorf("Error listing API keys: %v", err)
Expand All @@ -51,6 +79,10 @@ func (p *portalProxy) deleteAPIKey(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "API key guid can't be empty")
}

if err := p.checkIfAPIKeysEnabled(userGUID); err != nil {
return echo.NewHTTPError(http.StatusForbidden, err.Error())
}

if err := p.APIKeysRepository.DeleteAPIKey(userGUID, keyGUID); err != nil {
log.Errorf("Error deleting API key: %v", err)
return errors.New("Error deleting API key")
Expand Down
Loading