-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add securityDefinitions to the generated OpenAPI spec #14745
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,12 +60,14 @@ import ( | |
kversion "k8s.io/kubernetes/pkg/version" | ||
|
||
"github.com/openshift/origin/pkg/api" | ||
"github.com/openshift/origin/pkg/authorization/authorizer/scope" | ||
"github.com/openshift/origin/pkg/cmd/flagtypes" | ||
configapi "github.com/openshift/origin/pkg/cmd/server/api" | ||
"github.com/openshift/origin/pkg/cmd/server/cm" | ||
"github.com/openshift/origin/pkg/cmd/server/crypto" | ||
"github.com/openshift/origin/pkg/cmd/server/election" | ||
cmdflags "github.com/openshift/origin/pkg/cmd/util/flags" | ||
oauthutil "github.com/openshift/origin/pkg/oauth/util" | ||
openapigenerated "github.com/openshift/origin/pkg/openapi" | ||
securityapi "github.com/openshift/origin/pkg/security/apis/security" | ||
"github.com/openshift/origin/pkg/version" | ||
|
@@ -441,7 +443,7 @@ func buildKubeApiserverConfig( | |
genericConfig.DisabledPostStartHooks.Insert(rbacrest.PostStartHookName) | ||
genericConfig.AdmissionControl = admissionControl | ||
genericConfig.RequestContextMapper = requestContextMapper | ||
genericConfig.OpenAPIConfig = DefaultOpenAPIConfig() | ||
genericConfig.OpenAPIConfig = DefaultOpenAPIConfig(masterConfig) | ||
genericConfig.SwaggerConfig = apiserver.DefaultSwaggerConfig() | ||
genericConfig.SwaggerConfig.PostBuildHandler = customizeSwaggerDefinition | ||
_, loopbackClientConfig, err := configapi.GetInternalKubeClient(masterConfig.MasterClients.OpenShiftLoopbackKubeConfig, masterConfig.MasterClients.OpenShiftLoopbackClientConnectionOverrides) | ||
|
@@ -606,7 +608,43 @@ func BuildKubernetesMasterConfig( | |
return kmaster, nil | ||
} | ||
|
||
func DefaultOpenAPIConfig() *openapicommon.Config { | ||
func DefaultOpenAPIConfig(config configapi.MasterConfig) *openapicommon.Config { | ||
securityDefinitions := spec.SecurityDefinitions{} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on http://swagger.io/specification/#securityDefinitionsObject we should include OAuth2 info here using logic similar to https://github.com/openshift/origin/blob/master/pkg/cmd/server/origin/nonapiserver.go#L91-L93 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure where to source the right info, for example about flow type and scopes, and no other code in the origin repo uses the oauth2 type yet, it seem any outh based on any kind of token is defined just as an apikey bearer token... |
||
if len(config.ServiceAccountConfig.PublicKeyFiles) > 0 { | ||
securityDefinitions["BearerToken"] = &spec.SecurityScheme{ | ||
SecuritySchemeProps: spec.SecuritySchemeProps{ | ||
Type: "apiKey", | ||
Name: "authorization", | ||
In: "header", | ||
Description: "Bearer Token authentication", | ||
}, | ||
} | ||
} | ||
if config.OAuthConfig != nil { | ||
baseUrl := config.OAuthConfig.MasterPublicURL | ||
securityDefinitions["Oauth2Implicit"] = &spec.SecurityScheme{ | ||
SecuritySchemeProps: spec.SecuritySchemeProps{ | ||
Type: "oauth2", | ||
Flow: "implicit", | ||
AuthorizationURL: oauthutil.OpenShiftOAuthAuthorizeURL(baseUrl), | ||
Scopes: scope.DefaultSupportedScopesMap(), | ||
}, | ||
} | ||
securityDefinitions["Oauth2AccessToken"] = &spec.SecurityScheme{ | ||
SecuritySchemeProps: spec.SecuritySchemeProps{ | ||
Type: "oauth2", | ||
Flow: "accessCode", | ||
AuthorizationURL: oauthutil.OpenShiftOAuthAuthorizeURL(baseUrl), | ||
TokenURL: oauthutil.OpenShiftOAuthTokenURL(baseUrl), | ||
Scopes: scope.DefaultSupportedScopesMap(), | ||
}, | ||
} | ||
} | ||
if configapi.UseTLS(config.ServingInfo.ServingInfo) { | ||
// No support in Swagger's OpenAPI sepc v.2 ¯\_(ツ)_/¯ | ||
// TODO: Add x509 specification once available | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should have a separate GitHub issue to track this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @simo5 tag the issue you opened here. |
||
} | ||
|
||
return &openapicommon.Config{ | ||
GetDefinitions: openapigenerated.GetOpenAPIDefinitions, | ||
IgnorePrefixes: []string{"/swaggerapi", "/healthz", "/controllers", "/metrics", "/version/openshift", "/brokers"}, | ||
|
@@ -708,6 +746,7 @@ func DefaultOpenAPIConfig() *openapicommon.Config { | |
Description: "Default Response.", | ||
}, | ||
}, | ||
SecurityDefinitions: &securityDefinitions, | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ import ( | |
configapi "github.com/openshift/origin/pkg/cmd/server/api" | ||
"github.com/openshift/origin/pkg/cmd/server/crypto" | ||
cmdutil "github.com/openshift/origin/pkg/cmd/util" | ||
oauthutil "github.com/openshift/origin/pkg/oauth/util" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package dependency was previously excluded intentionally. The asset server should not depend on the oauth package at all. The two are logically independent. |
||
oversion "github.com/openshift/origin/pkg/version" | ||
) | ||
|
||
|
@@ -217,8 +218,8 @@ func (c *AssetConfig) addHandlers(handler http.Handler) (http.Handler, error) { | |
KubernetesAddr: masterURL.Host, | ||
KubernetesPrefix: server.DefaultLegacyAPIPrefix, | ||
KubernetesResources: k8sResources.List(), | ||
OAuthAuthorizeURI: OpenShiftOAuthAuthorizeURL(masterURL.String()), | ||
OAuthTokenURI: OpenShiftOAuthTokenURL(masterURL.String()), | ||
OAuthAuthorizeURI: oauthutil.OpenShiftOAuthAuthorizeURL(masterURL.String()), | ||
OAuthTokenURI: oauthutil.OpenShiftOAuthTokenURL(masterURL.String()), | ||
OAuthRedirectBase: c.Options.PublicURL, | ||
OAuthClientID: OpenShiftWebConsoleClientID, | ||
LogoutURI: c.Options.LogoutURL, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package util | ||
|
||
import ( | ||
"path" | ||
|
||
"github.com/openshift/origin/pkg/auth/server/tokenrequest" | ||
"github.com/openshift/origin/pkg/oauth/server/osinserver" | ||
) | ||
|
||
const ( | ||
OpenShiftOAuthAPIPrefix = "/oauth" | ||
) | ||
|
||
func OpenShiftOAuthAuthorizeURL(masterAddr string) string { | ||
return masterAddr + path.Join(OpenShiftOAuthAPIPrefix, osinserver.AuthorizePath) | ||
} | ||
func OpenShiftOAuthTokenURL(masterAddr string) string { | ||
return masterAddr + path.Join(OpenShiftOAuthAPIPrefix, osinserver.TokenPath) | ||
} | ||
func OpenShiftOAuthTokenRequestURL(masterAddr string) string { | ||
return masterAddr + path.Join(OpenShiftOAuthAPIPrefix, tokenrequest.RequestTokenEndpoint) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this to have a consistent order so use
return sets.StringKeySet(defaultSupportedScopesMap).List()
(you can store this as a variable if you want to).sets
="k8s.io/apimachinery/pkg/util/sets"