forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
170 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package apiserver | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/runtime/serializer" | ||
"k8s.io/apiserver/pkg/registry/rest" | ||
genericapiserver "k8s.io/apiserver/pkg/server" | ||
restclient "k8s.io/client-go/rest" | ||
coreclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/core/internalversion" | ||
|
||
osclient "github.com/openshift/origin/pkg/client" | ||
configapi "github.com/openshift/origin/pkg/cmd/server/api" | ||
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth" | ||
oauthapiv1 "github.com/openshift/origin/pkg/oauth/apis/oauth/v1" | ||
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion" | ||
accesstokenetcd "github.com/openshift/origin/pkg/oauth/registry/oauthaccesstoken/etcd" | ||
authorizetokenetcd "github.com/openshift/origin/pkg/oauth/registry/oauthauthorizetoken/etcd" | ||
clientetcd "github.com/openshift/origin/pkg/oauth/registry/oauthclient/etcd" | ||
clientauthetcd "github.com/openshift/origin/pkg/oauth/registry/oauthclientauthorization/etcd" | ||
saoauth "github.com/openshift/origin/pkg/serviceaccounts/oauthclient" | ||
"k8s.io/apimachinery/pkg/apimachinery/registered" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
type OAuthAPIServerConfig struct { | ||
GenericConfig *genericapiserver.Config | ||
|
||
CoreAPIServerClientConfig *restclient.Config | ||
ServiceAccountMethod configapi.GrantHandlerType | ||
|
||
// TODO these should all become local eventually | ||
Scheme *runtime.Scheme | ||
Registry *registered.APIRegistrationManager | ||
Codecs serializer.CodecFactory | ||
|
||
makeV1Storage sync.Once | ||
v1Storage map[string]rest.Storage | ||
v1StorageErr error | ||
} | ||
|
||
type OAuthAPIServer struct { | ||
GenericAPIServer *genericapiserver.GenericAPIServer | ||
} | ||
|
||
type completedConfig struct { | ||
*OAuthAPIServerConfig | ||
} | ||
|
||
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver. | ||
func (c *OAuthAPIServerConfig) Complete() completedConfig { | ||
c.GenericConfig.Complete() | ||
|
||
return completedConfig{c} | ||
} | ||
|
||
// SkipComplete provides a way to construct a server instance without config completion. | ||
func (c *OAuthAPIServerConfig) SkipComplete() completedConfig { | ||
return completedConfig{c} | ||
} | ||
|
||
// New returns a new instance of OAuthAPIServer from the given config. | ||
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*OAuthAPIServer, error) { | ||
genericServer, err := c.OAuthAPIServerConfig.GenericConfig.SkipComplete().New("oauth.openshift.io-apiserver", delegationTarget) // completion is done in Complete, no need for a second time | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s := &OAuthAPIServer{ | ||
GenericAPIServer: genericServer, | ||
} | ||
|
||
v1Storage, err := c.V1RESTStorage() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
apiGroupInfo := genericapiserver.NewDefaultAPIGroupInfo(oauthapiv1.GroupName, c.Registry, c.Scheme, metav1.ParameterCodec, c.Codecs) | ||
apiGroupInfo.GroupMeta.GroupVersion = oauthapiv1.SchemeGroupVersion | ||
apiGroupInfo.VersionedResourcesStorageMap[oauthapiv1.SchemeGroupVersion.Version] = v1Storage | ||
if err := s.GenericAPIServer.InstallAPIGroup(&apiGroupInfo); err != nil { | ||
return nil, err | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
func (c *OAuthAPIServerConfig) V1RESTStorage() (map[string]rest.Storage, error) { | ||
c.makeV1Storage.Do(func() { | ||
c.v1Storage, c.v1StorageErr = c.newV1RESTStorage() | ||
}) | ||
|
||
return c.v1Storage, c.v1StorageErr | ||
} | ||
|
||
func (c *OAuthAPIServerConfig) newV1RESTStorage() (map[string]rest.Storage, error) { | ||
|
||
clientStorage, err := clientetcd.NewREST(c.GenericConfig.RESTOptionsGetter) | ||
if err != nil { | ||
return nil, fmt.Errorf("error building REST storage: %v", err) | ||
} | ||
|
||
// If OAuth is disabled, set the strategy to Deny | ||
saAccountGrantMethod := oauthapi.GrantHandlerDeny | ||
if len(c.ServiceAccountMethod) > 0 { | ||
// Otherwise, take the value provided in master-config.yaml | ||
saAccountGrantMethod = oauthapi.GrantHandlerType(c.ServiceAccountMethod) | ||
} | ||
|
||
oauthClient, err := oauthclient.NewForConfig(c.GenericConfig.LoopbackClientConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
coreClient, err := coreclient.NewForConfig(c.CoreAPIServerClientConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
deprecatedClient, err := osclient.New(c.CoreAPIServerClientConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
combinedOAuthClientGetter := saoauth.NewServiceAccountOAuthClientGetter(coreClient, coreClient, deprecatedClient, oauthClient.OAuthClients(), saAccountGrantMethod) | ||
authorizeTokenStorage, err := authorizetokenetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter) | ||
if err != nil { | ||
return nil, fmt.Errorf("error building REST storage: %v", err) | ||
} | ||
accessTokenStorage, err := accesstokenetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter) | ||
if err != nil { | ||
return nil, fmt.Errorf("error building REST storage: %v", err) | ||
} | ||
clientAuthorizationStorage, err := clientauthetcd.NewREST(c.GenericConfig.RESTOptionsGetter, combinedOAuthClientGetter) | ||
if err != nil { | ||
return nil, fmt.Errorf("error building REST storage: %v", err) | ||
} | ||
|
||
v1Storage := map[string]rest.Storage{} | ||
v1Storage["oAuthAuthorizeTokens"] = authorizeTokenStorage | ||
v1Storage["oAuthAccessTokens"] = accessTokenStorage | ||
v1Storage["oAuthClients"] = clientStorage | ||
v1Storage["oAuthClientAuthorizations"] = clientAuthorizationStorage | ||
return v1Storage, nil | ||
} |