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
Add APIKeysEnabled to /info, check it in the middleware
  • Loading branch information
ikapelyukhin committed Aug 27, 2020
commit 21add38f08861c8e6bff7cbdfb833855cd926f59
1 change: 1 addition & 0 deletions src/jetstream/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (p *portalProxy) getInfo(c echo.Context) (*interfaces.Info, error) {
s.Configuration.TechPreview = p.Config.EnableTechPreview
s.Configuration.ListMaxSize = p.Config.UIListMaxSize
s.Configuration.ListAllowLoadMaxed = p.Config.UIListAllowLoadMaxed
s.Configuration.APIKeysEnabled = string(p.Config.APIKeysEnabled)

// Only add diagnostics information if the user is an admin
if uaaUser.Admin {
Expand Down
6 changes: 6 additions & 0 deletions src/jetstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ func main() {
}()
log.Info("Session data store initialized.")

// Setting default value for APIKeysEnabled
if portalConfig.APIKeysEnabled == "" {
log.Debug(`APIKeysEnabled not set, setting to "admin_only"`)
portalConfig.APIKeysEnabled = config.APIKeysConfigEnum.AdminOnly
}

// Setup the global interface for the proxy
portalProxy := newPortalProxy(portalConfig, databaseConnectionPool, sessionStore, sessionStoreOptions, envLookup)
portalProxy.SessionDataStore = sessionDataStore
Expand Down
21 changes: 21 additions & 0 deletions src/jetstream/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
log "github.com/sirupsen/logrus"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/interfaces"
"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/interfaces/config"
)

const cfSessionCookieName = "JSESSIONID"
Expand Down Expand Up @@ -323,6 +324,12 @@ func (p *portalProxy) apiKeyMiddleware(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
log.Debug("apiKeyMiddleware")

// skipping thise middleware if API keys are disabled
if p.Config.APIKeysEnabled == config.APIKeysConfigEnum.Disabled {
log.Debugf("apiKeyMiddleware: API keys are disabled, skipping")
return h(c)
}

apiKeySecret, err := getAPIKeyFromHeader(c)
if err != nil {
log.Debugf("apiKeyMiddleware: %v", err)
Expand All @@ -341,6 +348,20 @@ func (p *portalProxy) apiKeyMiddleware(h echo.HandlerFunc) echo.HandlerFunc {
return h(c)
}

// checking if user is an admin if API keys are enabled for admins only
if p.Config.APIKeysEnabled == config.APIKeysConfigEnum.AdminOnly {
user, err := p.StratosAuthService.GetUser(apiKey.UserGUID)
if err != nil {
log.Errorf("apiKeyMiddleware: %v", err)
return h(c)
}

if !user.Admin {
log.Debugf("apiKeyMiddleware: user isn't admin, skipping")
return h(c)
}
}

c.Set(APIKeySkipperContextKey, true)
c.Set("user_id", apiKey.UserGUID)

Expand Down
7 changes: 4 additions & 3 deletions src/jetstream/repository/interfaces/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ type Info struct {
PluginConfig map[string]string `json:"plugin-config,omitempty"`
Diagnostics *Diagnostics `json:"diagnostics,omitempty"`
Configuration struct {
TechPreview bool `json:"enableTechPreview"`
ListMaxSize int64 `json:"listMaxSize,omitempty"`
ListAllowLoadMaxed bool `json:"listAllowLoadMaxed,omitempty"`
TechPreview bool `json:"enableTechPreview"`
ListMaxSize int64 `json:"listMaxSize,omitempty"`
ListAllowLoadMaxed bool `json:"listAllowLoadMaxed,omitempty"`
APIKeysEnabled string `json:"APIKeysEnabled"`
} `json:"config"`
}

Expand Down