forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve][broker] Add test to verify authRole cannot change (apache#1…
- Loading branch information
1 parent
557b72d
commit 0231ad3
Showing
3 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
.../src/test/java/org/apache/pulsar/broker/auth/MockAlwaysExpiredAuthenticationProvider.java
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,46 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.auth; | ||
|
||
import org.apache.pulsar.broker.authentication.AuthenticationState; | ||
import org.apache.pulsar.common.api.AuthData; | ||
|
||
import javax.naming.AuthenticationException; | ||
import javax.net.ssl.SSLSession; | ||
import java.net.SocketAddress; | ||
|
||
/** | ||
* Class that provides the same authentication semantics as the {@link MockAuthenticationProvider} except | ||
* that this one initializes the {@link MockAlwaysExpiredAuthenticationState} class to support testing | ||
* expired authentication and auth refresh. | ||
*/ | ||
public class MockAlwaysExpiredAuthenticationProvider extends MockAuthenticationProvider { | ||
|
||
@Override | ||
public String getAuthMethodName() { | ||
return "always-expired"; | ||
} | ||
|
||
@Override | ||
public AuthenticationState newAuthState(AuthData authData, | ||
SocketAddress remoteAddress, | ||
SSLSession sslSession) throws AuthenticationException { | ||
return new MockAlwaysExpiredAuthenticationState(this); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...ker/src/test/java/org/apache/pulsar/broker/auth/MockAlwaysExpiredAuthenticationState.java
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,73 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.auth; | ||
|
||
import org.apache.pulsar.broker.authentication.AuthenticationDataCommand; | ||
import org.apache.pulsar.broker.authentication.AuthenticationDataSource; | ||
import org.apache.pulsar.broker.authentication.AuthenticationState; | ||
import org.apache.pulsar.common.api.AuthData; | ||
|
||
import javax.naming.AuthenticationException; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* Class to use when verifying the behavior around expired authentication data because it will always return | ||
* true when isExpired is called. | ||
*/ | ||
public class MockAlwaysExpiredAuthenticationState implements AuthenticationState { | ||
final MockAlwaysExpiredAuthenticationProvider provider; | ||
AuthenticationDataSource authenticationDataSource; | ||
volatile String authRole; | ||
|
||
MockAlwaysExpiredAuthenticationState(MockAlwaysExpiredAuthenticationProvider provider) { | ||
this.provider = provider; | ||
} | ||
|
||
|
||
@Override | ||
public String getAuthRole() throws AuthenticationException { | ||
if (authRole == null) { | ||
throw new AuthenticationException("Must authenticate first."); | ||
} | ||
return authRole; | ||
} | ||
|
||
@Override | ||
public AuthData authenticate(AuthData authData) throws AuthenticationException { | ||
authenticationDataSource = new AuthenticationDataCommand(new String(authData.getBytes(), UTF_8)); | ||
authRole = provider.authenticate(authenticationDataSource); | ||
return null; | ||
} | ||
|
||
@Override | ||
public AuthenticationDataSource getAuthDataSource() { | ||
return authenticationDataSource; | ||
} | ||
|
||
@Override | ||
public boolean isComplete() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isExpired() { | ||
return true; | ||
} | ||
} |
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