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

API Keys: Make feature configurable for different user types #4540

Merged
merged 7 commits into from
Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add a special type and parsing for the new config option
  • Loading branch information
ikapelyukhin committed Aug 25, 2020
commit 297d45be0956a874f641ef7c317bbca316da896c
40 changes: 39 additions & 1 deletion src/jetstream/repository/interfaces/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,39 @@ import (

const secretsDir = "/etc/secrets"

// APIKeysConfigValue - special type for configuring whether API keys feature is enabled
type APIKeysConfigValue string

// APIKeysConfigEnum - defines possible configuration values for Stratos API keys feature
var APIKeysConfigEnum = struct {
Disabled APIKeysConfigValue
AdminOnly APIKeysConfigValue
AllUsers APIKeysConfigValue
}{
Disabled: "disabled",
AdminOnly: "admin_only",
AllUsers: "all_users",
}

// verifies that given string is a valid config value (i.e., present in APIKeysConfigEnum)
func parseAPIKeysConfigValue(input string) (APIKeysConfigValue, error) {
t := reflect.TypeOf(APIKeysConfigEnum)
v := reflect.ValueOf(APIKeysConfigEnum)

var allowedValues []string

for i := 0; i < t.NumField(); i++ {
allowedValue := string(v.Field(i).Interface().(APIKeysConfigValue))
if allowedValue == input {
return APIKeysConfigValue(input), nil
}

allowedValues = append(allowedValues, allowedValue)
}

return "", fmt.Errorf("Invalid value %q, allowed values: %q", input, allowedValues)
}

var urlType *url.URL

// Load the given pointer to struct with values from the environment and the
Expand Down Expand Up @@ -119,7 +152,12 @@ func SetStructFieldValue(value reflect.Value, field reflect.Value, val string) e
b, err = strconv.ParseBool(val)
newVal = b
case reflect.String:
newVal = val
apiKeysConfigType := reflect.TypeOf((*APIKeysConfigValue)(nil)).Elem()
if typ == apiKeysConfigType {
newVal, err = parseAPIKeysConfigValue(val)
} else {
newVal = val
}
default:
if typ == reflect.TypeOf(urlType) {
newVal, err = url.Parse(val)
Expand Down
2 changes: 2 additions & 0 deletions src/jetstream/repository/interfaces/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"
"net/url"

"github.com/cloudfoundry-incubator/stratos/src/jetstream/repository/interfaces/config"
"github.com/gorilla/sessions"
"github.com/labstack/echo"
)
Expand Down Expand Up @@ -369,6 +370,7 @@ type PortalConfig struct {
DatabaseProviderName string
EnableTechPreview bool `configName:"ENABLE_TECH_PREVIEW"`
CanMigrateDatabaseSchema bool
APIKeysEnabled config.APIKeysConfigValue `configName:"API_KEYS_ENABLED"`
// CanMigrateDatabaseSchema indicates if we can safely perform migrations
// This depends on the deployment mechanism and the database config
// e.g. if running in Cloud Foundry with a shared DB, then only the 0-index application instance
Expand Down