Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/21 builtin users #27

Merged
merged 25 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8ca933a
#21 divided base and jwt config
dk1844 Mar 29, 2023
8548acf
#21 - application.properties replaced by application.yaml
dk1844 Mar 29, 2023
7610416
#21 DummyAuthenticationProvider.scala -> ConfigUsersAuthenticationPro…
dk1844 Mar 30, 2023
5b3a58b
#21 basic UserConfig validation on app startup
dk1844 Apr 13, 2023
d541f0c
#21 basic UserConfig - validation - validates groups presence, may be…
dk1844 Apr 13, 2023
0202ada
#21 cleanup
dk1844 Apr 13, 2023
dbc00db
#21 users config tests started
dk1844 Apr 14, 2023
bd3c403
#21 userconfig validation for duplicates added
dk1844 Apr 18, 2023
a8f570d
#21 jwtConfig custom validation added
dk1844 Apr 18, 2023
970b5cc
#21 added test for ConfigUsersAuthenticationProvider
dk1844 Apr 18, 2023
5cd8a07
#21 removed debug code
dk1844 Apr 18, 2023
b6113d1
#21 debug logging reverted
dk1844 Apr 18, 2023
341867d
#21 invalid algorithm wrapped in ConfigValidationException
dk1844 Apr 18, 2023
83a146e
#21 headers fix
dk1844 Apr 18, 2023
3f37625
Merge branch 'master' into feature/21-builtin-users
dk1844 Apr 21, 2023
ec6d665
#21 buildfix after merge
dk1844 Apr 21, 2023
33a239b
#21 formatting, usersConfig unitTest update (email is now optional)
dk1844 Apr 21, 2023
d14835a
#21 SecurityConfig divided into SecurityConfig and AuthManagerConfig.
dk1844 Apr 25, 2023
dcc45e0
#21 config string "" unification, integers just-# unification
dk1844 Apr 25, 2023
297c29f
#21 introduced sealed trait ConfigValidationResult (with container im…
dk1844 Apr 26, 2023
2799e6f
#21 ConfigValidationResult.failOnValidationError (internally called C…
dk1844 Apr 26, 2023
9ae4b63
Merge branch 'master' into feature/21-builtin-users
dk1844 May 2, 2023
4c1f562
#21 rebase of PR #30 - config build fix
dk1844 May 2, 2023
2e5a790
#21 ldap/hc todo rewording
dk1844 May 2, 2023
003bab5
#21 config groups fix
dk1844 May 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions service/src/main/resources/application.properties

This file was deleted.

60 changes: 60 additions & 0 deletions service/src/main/resources/application.yaml
jakipatryk marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
logingw:
rest:
# Rest General Config
jwt:
exp-time: 4
alg-name: "RS256"
config:
some-key: "BETA"

# Users (config-defined)
users:
known-users:
-
username: "user1"
password: "password1"
groups:
-
username: "TestUser"
password: "password123"
email: "[email protected]"
groups:
- "groupA"
- "groupB"

# Rest Auth Config (AD)
auth:
ad:
ldap:
domain: "some.domain.com"
url: "ldaps://some.domain.com:636/"
search-filter: "(samaccountname={1})"

# App Config
spring:
application:
name: "login-gateway"
jmx:
enabled: true
server:
port: 9090

# Health Check Config + JMX Config
springdoc:
show-actuator: true

management:
health:
ldap:
#TODO: Enable Ldap check for actuator/health when fully Implemented - issue #34
enabled: "false"
endpoints:
jmx:
exposure:
include: "health"
web:
exposure:
include: "health"
endpoint:
health:
show-details: "never"
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ import org.springframework.context.annotation._
`type` = SecuritySchemeType.HTTP,
scheme = "basic"
)
@Configuration
@SpringBootApplication()
@PropertySource(Array("classpath:application.properties"))
@ConfigurationPropertiesScan(Array("za.co.absa.logingw.rest.config"))
@ConfigurationPropertiesScan(Array("za.co.absa.logingw.rest.config")) // look for configuration in this package (not related to path in config file)
class Application extends SpringBootServletInitializer {

override def configure(application: SpringApplicationBuilder): SpringApplicationBuilder =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed 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 za.co.absa.logingw.rest

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.{Bean, Configuration}
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import za.co.absa.logingw.rest.config.{ActiveDirectoryLDAPConfig, UsersConfig}
import za.co.absa.logingw.rest.provider.ConfigUsersAuthenticationProvider
import za.co.absa.logingw.rest.provider.ad.ldap.ActiveDirectoryLDAPAuthenticationProvider

@Configuration
class AuthManagerConfig @Autowired()(
// TODO make it autowired but only if in config AD LDAP provider is set up (#28)
usersConfig: UsersConfig,
adLDAPConfig: ActiveDirectoryLDAPConfig
){

@Bean
def authManager(http: HttpSecurity): AuthenticationManager = {
val authenticationManagerBuilder = http.getSharedObject(classOf[AuthenticationManagerBuilder])

// TODO: take which providers and in which order to use from config (#28)
authenticationManagerBuilder
// if it is not null, on auth failure infinite recursion happens
.parentAuthenticationManager(null)
// currently, comment out or reorder the auth providers you want to use - #28
.authenticationProvider(
new ConfigUsersAuthenticationProvider(usersConfig)
)
.authenticationProvider(
new ActiveDirectoryLDAPAuthenticationProvider(adLDAPConfig)
)
.build
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,10 @@
package za.co.absa.logingw.rest

import org.springframework.context.annotation.{Bean, Configuration}
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.http.SessionCreationPolicy
import org.springframework.security.web.SecurityFilterChain
import za.co.absa.logingw.rest.config.ActiveDirectoryLDAPConfig
import za.co.absa.logingw.rest.provider.DummyAuthenticationProvider
import za.co.absa.logingw.rest.provider.ad.ldap.ActiveDirectoryLDAPAuthenticationProvider

@Configuration
@EnableWebSecurity
Expand Down Expand Up @@ -56,28 +51,6 @@ class SecurityConfig {
http.build()
}

@Bean
def authManager(http: HttpSecurity): AuthenticationManager = {
val authenticationManagerBuilder = http.getSharedObject(classOf[AuthenticationManagerBuilder])

// TODO make it autowired but only if in confid AD LDAP provider is set up (#28)
val adLDAPConfig = ActiveDirectoryLDAPConfig(
"some.domain.com",
"ldaps://some.domain.com:636/",
"(samaccountname={1})"
)

// TODO: take which providers and in which order to use from config (#28)
authenticationManagerBuilder
// if it is not null, on auth failure infinite recursion happens
.parentAuthenticationManager(null)
.authenticationProvider(
new ActiveDirectoryLDAPAuthenticationProvider(adLDAPConfig)
)
.authenticationProvider(
new DummyAuthenticationProvider()
)
.build
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,5 @@ import org.springframework.boot.context.properties.{ConfigurationProperties, Con
@ConstructorBinding
@ConfigurationProperties(prefix = "logingw.rest.config")
case class BaseConfig(
algName: String,
expTime: Int,
someKey: String = "BETA"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed 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 za.co.absa.logingw.rest.config

import io.jsonwebtoken.SignatureAlgorithm
import org.springframework.boot.context.properties.{ConfigurationProperties, ConstructorBinding}
import za.co.absa.logingw.rest.config.validation.{ConfigValidatable, ConfigValidationException, ConfigValidationResult}
import za.co.absa.logingw.rest.config.validation.ConfigValidationResult.{ConfigValidationError, ConfigValidationSuccess}

import javax.annotation.PostConstruct
import scala.util.{Failure, Success, Try}

@ConstructorBinding
@ConfigurationProperties(prefix = "logingw.rest.jwt")
case class JwtConfig(
algName: String,
expTime: Int
) extends ConfigValidatable {

@PostConstruct
def init(): Unit = {
this.validate().throwOnErrors()
}

/** May throw ConfigValidationException or IllegalArgumentException */
override def validate(): ConfigValidationResult = {

val algValidation = Try {
SignatureAlgorithm.valueOf(algName)
} match {
case Success(_) => ConfigValidationSuccess
case Failure(e: IllegalArgumentException) if e.getMessage.contains("No enum constant") =>
ConfigValidationError(ConfigValidationException(s"Invalid algName '$algName' was given."))
case Failure(e) => throw e
}

val expTimeResult = if (expTime < 1) {
ConfigValidationError(ConfigValidationException("expTime must be positive (hours)"))
} else ConfigValidationSuccess

algValidation.merge(expTimeResult)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed 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 za.co.absa.logingw.rest.config

import org.springframework.boot.context.properties.{ConfigurationProperties, ConstructorBinding}
import za.co.absa.logingw.rest.config.validation.ConfigValidationResult.{ConfigValidationError, ConfigValidationSuccess}
import za.co.absa.logingw.rest.config.validation.{ConfigValidatable, ConfigValidationException, ConfigValidationResult}

import javax.annotation.PostConstruct

@ConstructorBinding
@ConfigurationProperties(prefix = "logingw.rest.users")
case class UsersConfig(
knownUsers: Array[UserConfig],
) extends ConfigValidatable {
lazy val knownUsersMap: Map[String, UserConfig] = knownUsers
.map { entry => (entry.username, entry) }
.toMap

// todo validation is done using a custom trait/method -- Issue #24 validation
// Until is resolved https://github.com/spring-projects/spring-boot/issues/33669
override def validate(): ConfigValidationResult = {
Option(knownUsers).map { existingKnownUsers =>

val kuDuplicatesResult = {
val groupedByUsers = existingKnownUsers.groupBy(_.username)
if (groupedByUsers.size < existingKnownUsers.size) {
val duplicates = groupedByUsers.filter { case (username, configs) => configs.length > 1 }.keys
ConfigValidationError(ConfigValidationException(s"knownUsers contain duplicates, duplicated usernames: ${duplicates.mkString(", ")}"))
} else ConfigValidationSuccess
}

val usersResult = existingKnownUsers.map(_.validate()).toList

(kuDuplicatesResult :: usersResult)
.foldLeft[ConfigValidationResult](ConfigValidationSuccess)(ConfigValidationResult.merge)

}.getOrElse(ConfigValidationError(ConfigValidationException("knownUsers is missing")))
}

@PostConstruct
def init(): Unit = {
this.validate().throwOnErrors()
}
}

@ConstructorBinding
case class UserConfig(
username: String,
password: String,
email: String, // may be null
jakipatryk marked this conversation as resolved.
Show resolved Hide resolved
groups: Array[String]
) extends ConfigValidatable {

override def toString: String = {
s"UserConfig($username, $password, $email, ${Option(groups).map(_.toList)})"
}

override def validate(): ConfigValidationResult = {
val results = Seq(
Option(username)
.map(_ => ConfigValidationSuccess)
.getOrElse(ConfigValidationError(ConfigValidationException("username is empty"))),

Option(password)
.map(_ => ConfigValidationSuccess)
.getOrElse(ConfigValidationError(ConfigValidationException("password is empty"))),

Option(groups)
.map(_ => ConfigValidationSuccess)
.getOrElse(ConfigValidationError(ConfigValidationException("groups are missing (empty groups are allowed)!")))

)

results.foldLeft[ConfigValidationResult](ConfigValidationSuccess)(ConfigValidationResult.merge)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed 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 za.co.absa.logingw.rest.config.validation

trait ConfigValidatable {
def validate(): ConfigValidationResult

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2023 ABSA Group Limited
*
* Licensed 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 za.co.absa.logingw.rest.config.validation

case class ConfigValidationException(msg: String) extends Exception(msg)

Loading