Skip to content

Commit 17522b4

Browse files
authored
OWLS-84517: Scaling failed when setting Dedicated to true (#1921)
* Rest authentication of requests should use namespace scope for Dedicated selection strategy * remove System.out.println from unit test
1 parent 4d6617e commit 17522b4

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed

operator/src/main/java/oracle/kubernetes/operator/Main.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ public static DomainNamespaceSelectionStrategy getDomainNamespaceSelectionStrate
497497
return strategy;
498498
}
499499

500+
public static String getOperatorNamespace() {
501+
return operatorNamespace;
502+
}
503+
500504
public static boolean isDedicated() {
501505
return DomainNamespaceSelectionStrategy.Dedicated.equals(getDomainNamespaceSelectionStrategy());
502506
}

operator/src/main/java/oracle/kubernetes/operator/helpers/AuthenticationProxy.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
public class AuthenticationProxy {
1616
private static final LoggingFacade LOGGER = LoggingFactory.getLogger("Operator", "Operator");
1717

18-
private static final AuthorizationProxy authorizationProxy = new AuthorizationProxy();
18+
private static AuthorizationProxy authorizationProxy = new AuthorizationProxy();
1919

2020
/**
2121
* Check if the specified access token can be authenticated.
@@ -25,7 +25,7 @@ public class AuthenticationProxy {
2525
* @return V1TokenReviewStatus containing either info about the authenticated user or an error
2626
* explaining why the user couldn't be authenticated
2727
*/
28-
public V1TokenReviewStatus check(String principal, String token) {
28+
public V1TokenReviewStatus check(String principal, String token, String namespace) {
2929

3030
LOGGER.entering(principal); // Don't expose the token since it's a credential
3131

@@ -37,8 +37,8 @@ public V1TokenReviewStatus check(String principal, String token) {
3737
AuthorizationProxy.Operation.create,
3838
AuthorizationProxy.Resource.TOKENREVIEWS,
3939
null,
40-
AuthorizationProxy.Scope.cluster,
41-
null);
40+
namespace == null ? AuthorizationProxy.Scope.cluster : AuthorizationProxy.Scope.namespace,
41+
namespace);
4242
if (allowed) {
4343
result = new CallBuilder().createTokenReview(prepareTokenReview(token));
4444
} else {

operator/src/main/java/oracle/kubernetes/operator/rest/RestBackendImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.kubernetes.client.openapi.models.V1ObjectMeta;
2626
import io.kubernetes.client.openapi.models.V1TokenReviewStatus;
2727
import io.kubernetes.client.openapi.models.V1UserInfo;
28+
import oracle.kubernetes.operator.Main;
2829
import oracle.kubernetes.operator.helpers.AuthenticationProxy;
2930
import oracle.kubernetes.operator.helpers.AuthorizationProxy;
3031
import oracle.kubernetes.operator.helpers.AuthorizationProxy.Operation;
@@ -133,7 +134,8 @@ private String getNamespace(String domainUid) {
133134

134135
private V1UserInfo authenticate(String accessToken) {
135136
LOGGER.entering();
136-
V1TokenReviewStatus status = atn.check(principal, accessToken);
137+
V1TokenReviewStatus status = atn.check(principal, accessToken,
138+
Main.isDedicated() ? Main.getOperatorNamespace() : null);
137139
if (status == null) {
138140
throw new AssertionError(LOGGER.formatMessage(MessageKeys.NULL_TOKEN_REVIEW_STATUS));
139141
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2020, Oracle Corporation and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package oracle.kubernetes.operator.helpers;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
import com.meterware.simplestub.Memento;
10+
import com.meterware.simplestub.StaticStubSupport;
11+
import oracle.kubernetes.operator.helpers.AuthorizationProxy.Scope;
12+
import oracle.kubernetes.utils.TestUtils;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
import static org.hamcrest.Matchers.equalTo;
18+
import static org.hamcrest.junit.MatcherAssert.assertThat;
19+
20+
public class AuthenticationProxyTest {
21+
22+
private final List<Memento> mementos = new ArrayList<>();
23+
private final KubernetesTestSupport testSupport = new KubernetesTestSupport();
24+
private final AuthorizationProxyStub authorizationProxyStub = new AuthorizationProxyStub();
25+
26+
/**
27+
* Setup test.
28+
* @throws Exception on failure
29+
*/
30+
@Before
31+
public void setUp() throws Exception {
32+
mementos.add(TestUtils.silenceOperatorLogger());
33+
mementos.add(testSupport.install());
34+
mementos.add(
35+
StaticStubSupport.install(AuthenticationProxy.class, "authorizationProxy", authorizationProxyStub));
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
mementos.forEach(Memento::revert);
41+
}
42+
43+
@Test
44+
public void verify_authorizationScope_isCluster_whenNamespaceIsNull() {
45+
AuthenticationProxy authorizationProxy = new AuthenticationProxy();
46+
authorizationProxy.check("", "", null);
47+
assertThat(authorizationProxyStub.scope, equalTo(Scope.cluster));
48+
}
49+
50+
@Test
51+
public void verify_authorizationScope_isNamespace_whenNamespaceIsDefined() {
52+
AuthenticationProxy authorizationProxy = new AuthenticationProxy();
53+
authorizationProxy.check("", "", "NS");
54+
assertThat(authorizationProxyStub.scope, equalTo(Scope.namespace));
55+
}
56+
57+
private class AuthorizationProxyStub extends AuthorizationProxy {
58+
Scope scope;
59+
60+
public boolean check(
61+
String principal,
62+
Operation operation,
63+
Resource resource,
64+
String resourceName,
65+
Scope scope,
66+
String namespaceName) {
67+
this.scope = scope;
68+
return true;
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)