remove oauth server dependency from most integration tests
package util
import (
+ "encoding/base64"
"fmt"
"math/rand"
"os"
@@ -8,6 +9,8 @@ import (
"path/filepath"
"time"
+ "github.com/pborman/uuid"
+
kerrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
@@ -20,10 +23,10 @@ import (
configapi "github.com/openshift/origin/pkg/cmd/server/api"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
- "github.com/openshift/origin/pkg/cmd/util/tokencmd"
oauthapi "github.com/openshift/origin/pkg/oauth/apis/oauth"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset"
"github.com/openshift/origin/pkg/serviceaccounts"
+ userapi "github.com/openshift/origin/pkg/user/apis/user"
userclient "github.com/openshift/origin/pkg/user/generated/internalclientset"
)
@@ -62,14 +65,52 @@ func GetClusterAdminClientConfigOrDie(adminKubeConfigFile string) *restclient.Co
return conf
}
-func GetClientForUser(clientConfig *restclient.Config, username string) (kclientset.Interface, *restclient.Config, error) {
- token, err := tokencmd.RequestToken(clientConfig, nil, username, "password")
+func GetClientForUser(clusterAdminConfig *restclient.Config, username string) (kclientset.Interface, *restclient.Config, error) {
+ userClient, err := userclient.NewForConfig(clusterAdminConfig)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ user, err := userClient.User().Users().Get(username, metav1.GetOptions{})
+ if err != nil {
+ user = &userapi.User{
+ ObjectMeta: metav1.ObjectMeta{Name: username},
+ }
+ user, err = userClient.User().Users().Create(user)
+ if err != nil {
+ return nil, nil, err
+ }
+ }
+
+ oauthClient, err := oauthclient.NewForConfig(clusterAdminConfig)
if err != nil {
return nil, nil, err
}
- userClientConfig := restclient.AnonymousClientConfig(clientConfig)
- userClientConfig.BearerToken = token
+ oauthClientObj := &oauthapi.OAuthClient{
+ ObjectMeta: metav1.ObjectMeta{Name: "test-integration-client"},
+ }
+ if _, err := oauthClient.Oauth().OAuthClients().Create(oauthClientObj); err != nil && !kerrs.IsAlreadyExists(err) {
+ return nil, nil, err
+ }
+
+ randomToken := uuid.NewRandom()
+ accesstoken := base64.RawURLEncoding.EncodeToString([]byte(randomToken))
+ for i := len(accesstoken); i < 32; i++ {
+ accesstoken = accesstoken + "A"
+ }
+ token := &oauthapi.OAuthAccessToken{
+ ObjectMeta: metav1.ObjectMeta{Name: accesstoken},
+ ClientName: oauthClientObj.Name,
+ UserName: username,
+ UserUID: string(user.UID),
+ }
+ if _, err := oauthClient.Oauth().OAuthAccessTokens().Create(token); err != nil {
+ return nil, nil, err
+ }
+
+ userClientConfig := restclient.AnonymousClientConfig(clusterAdminConfig)
+ userClientConfig.BearerToken = token.Name
kubeClientset, err := kclientset.NewForConfig(userClientConfig)
if err != nil {
