-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-issues-1597] Enforce authentication in User Tasks rest…
… endpoints (#3758) * [incubator-kie-issues-1597] Enforce authentication in User Tasks rest endpoints * Added configs * - formatting * - fix config
- Loading branch information
Showing
15 changed files
with
414 additions
and
77 deletions.
There are no files selected for viewing
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
40 changes: 40 additions & 0 deletions
40
api/kogito-api/src/main/java/org/kie/kogito/auth/IdentityProviderFactory.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,40 @@ | ||
/* | ||
* 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.kie.kogito.auth; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Factory that resolves the {@link IdentityProvider} | ||
*/ | ||
public interface IdentityProviderFactory { | ||
|
||
/** | ||
* Enables (true) using the application security context when resolving current User Identity. Defaults to false. | ||
*/ | ||
String KOGITO_SECURITY_AUTH_ENABLED = "kogito.security.auth.enabled"; | ||
|
||
/** | ||
* Comma-separated list of roles that allow identity impersonation when resolving the actual User Identity. | ||
*/ | ||
String KOGITO_SECURITY_AUTH_IMPERSONATION_ALLOWED_FOR_ROLES = "kogito.security.auth.impersonation.allowed-for-roles"; | ||
|
||
IdentityProvider getOrImpersonateIdentity(String user, Collection<String> roles); | ||
} |
52 changes: 52 additions & 0 deletions
52
api/kogito-api/src/main/java/org/kie/kogito/auth/impl/IdentityProviderFactoryImpl.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,52 @@ | ||
/* | ||
* 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.kie.kogito.auth.impl; | ||
|
||
import java.util.Collection; | ||
|
||
import org.kie.kogito.auth.IdentityProvider; | ||
import org.kie.kogito.auth.IdentityProviderFactory; | ||
import org.kie.kogito.auth.IdentityProviders; | ||
|
||
public class IdentityProviderFactoryImpl implements IdentityProviderFactory { | ||
|
||
private final IdentityProvider identityProvider; | ||
private final KogitoAuthConfig config; | ||
|
||
public IdentityProviderFactoryImpl(IdentityProvider identityProvider, KogitoAuthConfig config) { | ||
this.identityProvider = identityProvider; | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public IdentityProvider getOrImpersonateIdentity(String user, Collection<String> roles) { | ||
|
||
if (!config.isEnabled()) { | ||
return IdentityProviders.of(user, roles); | ||
} | ||
|
||
Collection<String> identityRoles = identityProvider.getRoles(); | ||
if (config.getRolesThatAllowImpersonation().stream().anyMatch(identityRoles::contains)) { | ||
return IdentityProviders.of(user, roles); | ||
} | ||
|
||
return identityProvider; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
api/kogito-api/src/main/java/org/kie/kogito/auth/impl/KogitoAuthConfig.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,41 @@ | ||
/* | ||
* 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.kie.kogito.auth.impl; | ||
|
||
import java.util.Collection; | ||
|
||
public class KogitoAuthConfig { | ||
|
||
private final boolean enabled; | ||
private final Collection<String> rolesThatAllowImpersonation; | ||
|
||
public KogitoAuthConfig(boolean enabled, Collection<String> rolesThatAllowImpersonation) { | ||
this.enabled = enabled; | ||
this.rolesThatAllowImpersonation = rolesThatAllowImpersonation; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
public Collection<String> getRolesThatAllowImpersonation() { | ||
return rolesThatAllowImpersonation; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
api/kogito-api/src/test/java/org/kie/kogito/auth/impl/IdentityProviderFactoryImplTest.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,70 @@ | ||
/* | ||
* 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.kie.kogito.auth.impl; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.kie.kogito.auth.IdentityProviders; | ||
|
||
public class IdentityProviderFactoryImplTest { | ||
|
||
private static final String KOGITO_IDENTITY_USER = "john"; | ||
private static final Collection<String> KOGITO_IDENTITY_ROLES = List.of("IT", "task-operator"); | ||
private static final Collection<String> KOGITO_IDENTITY_IMPERSONATOR_ROLES = List.of("root", "task-admin"); | ||
private static final String TEST_USER = "katty"; | ||
private static final Collection<String> TEST_ROLES = List.of("HR", "task-operator"); | ||
|
||
@Test | ||
public void testResolveIdentityWithAuthDisabled() { | ||
KogitoAuthConfig config = new KogitoAuthConfig(false, KOGITO_IDENTITY_IMPERSONATOR_ROLES); | ||
IdentityProviderFactoryImpl identityProviderFactory = new IdentityProviderFactoryImpl(IdentityProviders.of(KOGITO_IDENTITY_USER, KOGITO_IDENTITY_ROLES), config); | ||
|
||
Assertions.assertThat(identityProviderFactory.getOrImpersonateIdentity(TEST_USER, TEST_ROLES)) | ||
.isNotNull() | ||
.hasFieldOrPropertyWithValue("name", TEST_USER) | ||
.matches(identityProvider -> identityProvider.getRoles().containsAll(TEST_ROLES)); | ||
} | ||
|
||
@Test | ||
public void testResolveIdentityWithAuthEnabled() { | ||
KogitoAuthConfig config = new KogitoAuthConfig(true, KOGITO_IDENTITY_IMPERSONATOR_ROLES); | ||
IdentityProviderFactoryImpl identityProviderFactory = new IdentityProviderFactoryImpl(IdentityProviders.of(KOGITO_IDENTITY_USER, KOGITO_IDENTITY_ROLES), config); | ||
|
||
Assertions.assertThat(identityProviderFactory.getOrImpersonateIdentity(TEST_USER, TEST_ROLES)) | ||
.isNotNull() | ||
.hasFieldOrPropertyWithValue("name", KOGITO_IDENTITY_USER) | ||
.matches(identityProvider -> identityProvider.getRoles().containsAll(KOGITO_IDENTITY_ROLES)); | ||
} | ||
|
||
@Test | ||
public void testResolveImpersonatedIdentityWithAuthEnabled() { | ||
KogitoAuthConfig config = new KogitoAuthConfig(true, KOGITO_IDENTITY_IMPERSONATOR_ROLES); | ||
IdentityProviderFactoryImpl identityProviderFactory = new IdentityProviderFactoryImpl(IdentityProviders.of(KOGITO_IDENTITY_USER, KOGITO_IDENTITY_IMPERSONATOR_ROLES), config); | ||
|
||
Assertions.assertThat(identityProviderFactory.getOrImpersonateIdentity(TEST_USER, TEST_ROLES)) | ||
.isNotNull() | ||
.hasFieldOrPropertyWithValue("name", TEST_USER) | ||
.matches(identityProvider -> identityProvider.getRoles().containsAll(TEST_ROLES)); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.