@@ -17,6 +17,7 @@ limitations under the License.
17
17
package options
18
18
19
19
import (
20
+ "github.com/brancz/kube-rbac-proxy/pkg/authn/identityheaders"
20
21
"os"
21
22
"path/filepath"
22
23
"reflect"
@@ -125,3 +126,93 @@ func Test_parseAuthorizationConfigFile(t *testing.T) {
125
126
})
126
127
}
127
128
}
129
+
130
+ func TestProxyOptions_Validate (t * testing.T ) {
131
+ type fields struct {
132
+ Upstream string
133
+ UpstreamForceH2C bool
134
+ UpstreamCAFile string
135
+ UpstreamClientCertFile string
136
+ UpstreamClientKeyFile string
137
+ UpstreamHeader * identityheaders.AuthnHeaderConfig
138
+ AuthzConfigFileName string
139
+ AllowPaths []string
140
+ IgnorePaths []string
141
+ ProxyEndpointsPort int
142
+ TokenAudiences []string
143
+ AllowLegacyServiceAccountTokens bool
144
+ DisableHTTP2Serving bool
145
+ }
146
+
147
+ userKey := "User"
148
+ groupKey := "Group"
149
+
150
+ tests := []struct {
151
+ name string
152
+ fields fields
153
+ wantErr bool
154
+ }{
155
+ {
156
+ name : "valid config with explicit token audience" ,
157
+ fields : fields {
158
+ Upstream : "http://127.0.0.1" ,
159
+ TokenAudiences : []string {"kube-apiserver" },
160
+ AllowLegacyServiceAccountTokens : false ,
161
+ UpstreamHeader : & identityheaders.AuthnHeaderConfig {
162
+ UserFieldName : userKey ,
163
+ GroupsFieldName : groupKey ,
164
+ },
165
+ },
166
+ wantErr : false ,
167
+ },
168
+ {
169
+ name : "legacy tokens not allowed (empty audiences, flag false)" ,
170
+ fields : fields {
171
+ Upstream : "http://127.0.0.1" ,
172
+ TokenAudiences : []string {},
173
+ AllowLegacyServiceAccountTokens : false ,
174
+ UpstreamHeader : & identityheaders.AuthnHeaderConfig {
175
+ UserFieldName : userKey ,
176
+ GroupsFieldName : groupKey ,
177
+ },
178
+ },
179
+ wantErr : true ,
180
+ },
181
+ {
182
+ name : "legacy tokens allowed (empty audiences, flag true)" ,
183
+ fields : fields {
184
+ Upstream : "http://127.0.0.1" ,
185
+ TokenAudiences : []string {},
186
+ AllowLegacyServiceAccountTokens : true ,
187
+ UpstreamHeader : & identityheaders.AuthnHeaderConfig {
188
+ UserFieldName : userKey ,
189
+ GroupsFieldName : groupKey ,
190
+ },
191
+ },
192
+ wantErr : false ,
193
+ },
194
+ }
195
+ for _ , tt := range tests {
196
+ t .Run (tt .name , func (t * testing.T ) {
197
+ o := & ProxyOptions {
198
+ Upstream : tt .fields .Upstream ,
199
+ UpstreamForceH2C : tt .fields .UpstreamForceH2C ,
200
+ UpstreamCAFile : tt .fields .UpstreamCAFile ,
201
+ UpstreamClientCertFile : tt .fields .UpstreamClientCertFile ,
202
+ UpstreamClientKeyFile : tt .fields .UpstreamClientKeyFile ,
203
+ UpstreamHeader : tt .fields .UpstreamHeader ,
204
+ AuthzConfigFileName : tt .fields .AuthzConfigFileName ,
205
+ AllowPaths : tt .fields .AllowPaths ,
206
+ IgnorePaths : tt .fields .IgnorePaths ,
207
+ ProxyEndpointsPort : tt .fields .ProxyEndpointsPort ,
208
+ TokenAudiences : tt .fields .TokenAudiences ,
209
+ AllowLegacyServiceAccountTokens : tt .fields .AllowLegacyServiceAccountTokens ,
210
+ DisableHTTP2Serving : tt .fields .DisableHTTP2Serving ,
211
+ }
212
+ errs := o .Validate ()
213
+ if (len (errs ) > 0 ) != tt .wantErr {
214
+ t .Errorf ("Validate() errors = %v, wantErr %v" , errs , tt .wantErr )
215
+ }
216
+ })
217
+ }
218
+ }
0 commit comments