1
1
package integration
2
2
3
3
import (
4
+ "net/http"
4
5
"testing"
5
6
"time"
6
7
8
+ "golang.org/x/net/context"
9
+ "golang.org/x/oauth2"
7
10
"k8s.io/apimachinery/pkg/api/errors"
8
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
12
"k8s.io/apimachinery/pkg/util/wait"
@@ -43,7 +46,7 @@ func TestOAuthExpiration(t *testing.T) {
43
46
44
47
{
45
48
zero := int32 (0 )
46
- nonexpiring , err := clusterAdminClient .OAuthClients ().Create (& oauthapi.OAuthClient {
49
+ client , err := clusterAdminClient .OAuthClients ().Create (& oauthapi.OAuthClient {
47
50
ObjectMeta : metav1.ObjectMeta {Name : "nonexpiring" },
48
51
RespondWithChallenges : true ,
49
52
RedirectURIs : []string {"http://localhost" },
@@ -54,22 +57,59 @@ func TestOAuthExpiration(t *testing.T) {
54
57
t .Fatal (err )
55
58
}
56
59
57
- nonExpiringTokenOpts := tokencmd .NewRequestTokenOptions (anonConfig , nil , "username" , "password" )
58
- nonExpiringTokenOpts .ClientID = nonexpiring .Name
59
- nonexpiringToken , err := nonExpiringTokenOpts .RequestToken ()
60
+ testExpiringOAuthFlows (t , clusterAdminClient , client , anonConfig , 0 )
61
+ }
62
+
63
+ {
64
+ ten := int32 (10 )
65
+ client , err := clusterAdminClient .OAuthClients ().Create (& oauthapi.OAuthClient {
66
+ ObjectMeta : metav1.ObjectMeta {Name : "shortexpiring" },
67
+ RespondWithChallenges : true ,
68
+ RedirectURIs : []string {"http://localhost" },
69
+ AccessTokenMaxAgeSeconds : & ten ,
70
+ GrantMethod : oauthapi .GrantHandlerAuto ,
71
+ })
72
+ if err != nil {
73
+ t .Fatal (err )
74
+ }
75
+
76
+ token := testExpiringOAuthFlows (t , clusterAdminClient , client , anonConfig , 10 )
77
+
78
+ // Ensure the token goes away after the time expiration
79
+ if err := wait .Poll (1 * time .Second , time .Minute , func () (bool , error ) {
80
+ _ , err := clusterAdminClient .OAuthAccessTokens ().Get (token , metav1.GetOptions {})
81
+ if errors .IsNotFound (err ) {
82
+ return true , nil
83
+ }
84
+ if err != nil {
85
+ return false , err
86
+ }
87
+ return false , nil
88
+ }); err != nil {
89
+ t .Fatal (err )
90
+ }
91
+ }
92
+ }
93
+
94
+ func testExpiringOAuthFlows (t * testing.T , clusterAdminClient * client.Client , oauthclient * oauthapi.OAuthClient , anonConfig * restclient.Config , expectedExpires int ) string {
95
+
96
+ {
97
+ tokenOpts := tokencmd .NewRequestTokenOptions (anonConfig , nil , "username" , "password" )
98
+ tokenOpts .ClientID = oauthclient .Name
99
+ token , err := tokenOpts .RequestToken ()
60
100
if err != nil {
61
101
t .Fatal (err )
62
102
}
63
103
64
104
// Make sure we can use the token, and it represents who we expect
65
- nonExpiringUserConfig := * anonConfig
66
- nonExpiringUserConfig .BearerToken = nonexpiringToken
67
- nonExpiringUserClient , err := client .New (& nonExpiringUserConfig )
105
+ userConfig := * anonConfig
106
+ userConfig .BearerToken = token
107
+ userClient , err := client .New (& userConfig )
68
108
if err != nil {
69
109
t .Fatalf ("Unexpected error: %v" , err )
70
110
}
71
111
72
- user , err := nonExpiringUserClient .Users ().Get ("~" , metav1.GetOptions {})
112
+ user , err := userClient .Users ().Get ("~" , metav1.GetOptions {})
73
113
if err != nil {
74
114
t .Fatalf ("Unexpected error: %v" , err )
75
115
}
@@ -78,63 +118,96 @@ func TestOAuthExpiration(t *testing.T) {
78
118
}
79
119
80
120
// Make sure the token exists with the overridden time
81
- tokenObj , err := clusterAdminClient .OAuthAccessTokens ().Get (nonexpiringToken , metav1.GetOptions {})
121
+ tokenObj , err := clusterAdminClient .OAuthAccessTokens ().Get (token , metav1.GetOptions {})
82
122
if err != nil {
83
123
t .Fatal (err )
84
124
}
85
- if tokenObj .ExpiresIn != 0 {
86
- t .Fatalf ("Expected expiration of 0 , got %#v" , tokenObj .ExpiresIn )
125
+ if tokenObj .ExpiresIn != int64 ( expectedExpires ) {
126
+ t .Fatalf ("Expected expiration of %d , got %#v" , expectedExpires , tokenObj .ExpiresIn )
87
127
}
88
128
}
89
129
90
130
{
91
- ten := int32 (10 )
92
- shortexpiring , err := clusterAdminClient .OAuthClients ().Create (& oauthapi.OAuthClient {
93
- ObjectMeta : metav1.ObjectMeta {Name : "shortexpiring" },
94
- RespondWithChallenges : true ,
95
- RedirectURIs : []string {"http://localhost" },
96
- AccessTokenMaxAgeSeconds : & ten ,
97
- GrantMethod : oauthapi .GrantHandlerAuto ,
98
- })
131
+ rt , err := restclient .TransportFor (anonConfig )
132
+ if err != nil {
133
+ t .Fatal (err )
134
+ }
135
+
136
+ conf := & oauth2.Config {
137
+ ClientID : oauthclient .Name ,
138
+ ClientSecret : oauthclient .Secret ,
139
+ RedirectURL : oauthclient .RedirectURIs [0 ],
140
+ Endpoint : oauth2.Endpoint {
141
+ AuthURL : anonConfig .Host + "/oauth/authorize" ,
142
+ TokenURL : anonConfig .Host + "/oauth/token" ,
143
+ },
144
+ }
145
+
146
+ // get code
147
+ req , err := http .NewRequest ("GET" , conf .AuthCodeURL ("" ), nil )
148
+ if err != nil {
149
+ t .Fatal (err )
150
+ }
151
+ req .SetBasicAuth ("username" , "password" )
152
+ resp , err := rt .RoundTrip (req )
153
+ if err != nil {
154
+ t .Fatal (err )
155
+ }
156
+ if resp .StatusCode != http .StatusFound {
157
+ t .Fatalf ("unexpected status %v" , resp .StatusCode )
158
+ }
159
+ location , err := resp .Location ()
160
+ if err != nil {
161
+ t .Fatal (err )
162
+ }
163
+ code := location .Query ().Get ("code" )
164
+ if len (code ) == 0 {
165
+ t .Fatalf ("Unexpected response: %v" , location )
166
+ }
167
+
168
+ // Make sure the code exists with the default time
169
+ codeObj , err := clusterAdminClient .OAuthAuthorizeTokens ().Get (code , metav1.GetOptions {})
99
170
if err != nil {
100
171
t .Fatal (err )
101
172
}
173
+ if codeObj .ExpiresIn != (5 * 60 ) {
174
+ t .Fatalf ("Expected expiration of %d, got %#v" , (5 * 60 ), codeObj .ExpiresIn )
175
+ }
102
176
103
- expiringTokenOpts := tokencmd .NewRequestTokenOptions (anonConfig , nil , "username" , "password" )
104
- expiringTokenOpts .ClientID = shortexpiring .Name
105
- expiringToken , err := expiringTokenOpts .RequestToken ()
177
+ // Use the custom HTTP client when requesting a token.
178
+ httpClient := & http.Client {Transport : rt }
179
+ ctx := context .WithValue (context .Background (), oauth2 .HTTPClient , httpClient )
180
+ oauthToken , err := conf .Exchange (ctx , code )
106
181
if err != nil {
107
182
t .Fatal (err )
108
183
}
184
+ token := oauthToken .AccessToken
109
185
110
186
// Make sure we can use the token, and it represents who we expect
111
- expiringUserConfig := * anonConfig
112
- expiringUserConfig .BearerToken = expiringToken
113
- expiringUserClient , err := client .New (& expiringUserConfig )
187
+ userConfig := * anonConfig
188
+ userConfig .BearerToken = token
189
+ userClient , err := client .New (& userConfig )
114
190
if err != nil {
115
191
t .Fatalf ("Unexpected error: %v" , err )
116
192
}
117
193
118
- user , err := expiringUserClient .Users ().Get ("~" , metav1.GetOptions {})
194
+ user , err := userClient .Users ().Get ("~" , metav1.GetOptions {})
119
195
if err != nil {
120
196
t .Fatalf ("Unexpected error: %v" , err )
121
197
}
122
198
if user .Name != "username" {
123
199
t .Fatalf ("Expected username as the user, got %v" , user )
124
200
}
125
201
126
- // Ensure the token goes away after the time expiration
127
- if err := wait .Poll (1 * time .Second , time .Minute , func () (bool , error ) {
128
- _ , err := clusterAdminClient .OAuthAccessTokens ().Get (expiringToken , metav1.GetOptions {})
129
- if errors .IsNotFound (err ) {
130
- return true , nil
131
- }
132
- if err != nil {
133
- return false , err
134
- }
135
- return false , nil
136
- }); err != nil {
202
+ // Make sure the token exists with the overridden time
203
+ tokenObj , err := clusterAdminClient .OAuthAccessTokens ().Get (token , metav1.GetOptions {})
204
+ if err != nil {
137
205
t .Fatal (err )
138
206
}
207
+ if tokenObj .ExpiresIn != int64 (expectedExpires ) {
208
+ t .Fatalf ("Expected expiration of %d, got %#v" , expectedExpires , tokenObj .ExpiresIn )
209
+ }
210
+
211
+ return token
139
212
}
140
213
}
0 commit comments