-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an advanced auth config validation logic (#32)
* Add an advanced auth config validation logic * Make validation to fail if AdminAuthConfig is null
- Loading branch information
Showing
21 changed files
with
143 additions
and
55 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
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/com/kuvaszuptime/kuvasz/config/AdminAuthConfig.kt
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,21 @@ | ||
package com.kuvaszuptime.kuvasz.config | ||
|
||
import com.kuvaszuptime.kuvasz.validation.UsernamePasswordNotEquals | ||
import io.micronaut.context.annotation.ConfigurationProperties | ||
import io.micronaut.context.annotation.Context | ||
import io.micronaut.core.annotation.Introspected | ||
import javax.validation.constraints.NotBlank | ||
import javax.validation.constraints.Size | ||
|
||
@ConfigurationProperties("admin-auth") | ||
@UsernamePasswordNotEquals | ||
@Context | ||
@Introspected | ||
class AdminAuthConfig { | ||
@NotBlank | ||
var username: String? = null | ||
|
||
@NotBlank | ||
@Size(min = 12) | ||
var password: String? = null | ||
} |
11 changes: 0 additions & 11 deletions
11
src/main/kotlin/com/kuvaszuptime/kuvasz/config/AppConfig.kt
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/DatabaseEventHandler.kt
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
8 changes: 4 additions & 4 deletions
8
src/main/kotlin/com/kuvaszuptime/kuvasz/handlers/LogEventHandler.kt
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
2 changes: 1 addition & 1 deletion
2
...n/com/kuvaszuptime/kuvasz/events/Event.kt → ...n/com/kuvaszuptime/kuvasz/models/Event.kt
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
6 changes: 3 additions & 3 deletions
6
src/main/kotlin/com/kuvaszuptime/kuvasz/repositories/UptimeEventRepository.kt
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
8 changes: 4 additions & 4 deletions
8
src/main/kotlin/com/kuvaszuptime/kuvasz/services/EventDispatcher.kt
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
6 changes: 3 additions & 3 deletions
6
src/main/kotlin/com/kuvaszuptime/kuvasz/services/UptimeChecker.kt
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
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/kuvaszuptime/kuvasz/validation/UsernamePasswordNotEquals.kt
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,26 @@ | ||
package com.kuvaszuptime.kuvasz.validation | ||
|
||
import com.kuvaszuptime.kuvasz.config.AdminAuthConfig | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.validation.validator.constraints.ConstraintValidator | ||
import javax.inject.Singleton | ||
import javax.validation.Constraint | ||
|
||
@Retention(AnnotationRetention.RUNTIME) | ||
@Constraint(validatedBy = []) | ||
annotation class UsernamePasswordNotEquals( | ||
val message: String = "Admin username and password should not be equal" | ||
) | ||
|
||
@Factory | ||
class UsernamePasswordValidatorFactory { | ||
|
||
@Singleton | ||
fun usernamePasswordValidator(): ConstraintValidator<UsernamePasswordNotEquals, AdminAuthConfig> { | ||
return ConstraintValidator { adminAuthConfig, _, _ -> | ||
if (adminAuthConfig != null) { | ||
adminAuthConfig.username!!.toLowerCase() != adminAuthConfig.password!!.toLowerCase() | ||
} else false | ||
} | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
src/test/kotlin/com/kuvaszuptime/kuvasz/config/AdminAuthConfigTest.kt
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 @@ | ||
package com.kuvaszuptime.kuvasz.config | ||
|
||
import io.kotest.assertions.exceptionToMessage | ||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.core.spec.style.BehaviorSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.env.PropertySource | ||
import io.micronaut.context.exceptions.BeanInstantiationException | ||
|
||
class AdminAuthConfigTest : BehaviorSpec({ | ||
given("an AdminAuthConfig bean") { | ||
`when`("password is less than 12 characters long") { | ||
val properties = PropertySource.of( | ||
"test", | ||
mapOf( | ||
"admin-auth.username" to "test-user", | ||
"admin-auth.password" to "tooShortPas" | ||
) | ||
) | ||
then("ApplicationContext should throw a BeanInstantiationException") { | ||
val exception = shouldThrow<BeanInstantiationException> { | ||
ApplicationContext.run(properties) | ||
} | ||
exceptionToMessage(exception).contains("password - size must be between 12") shouldBe true | ||
} | ||
} | ||
|
||
`when`("username or password is blank") { | ||
val properties = PropertySource.of( | ||
"test", | ||
mapOf( | ||
"admin-auth.username" to "", | ||
"admin-auth.password" to "" | ||
) | ||
) | ||
then("ApplicationContext should throw a BeanInstantiationException") { | ||
val exception = shouldThrow<BeanInstantiationException> { | ||
ApplicationContext.run(properties) | ||
} | ||
exceptionToMessage(exception).contains("username - must not be blank") shouldBe true | ||
exceptionToMessage(exception).contains("password - must not be blank") shouldBe 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package com.kuvaszuptime.kuvasz.mocks | ||
|
||
import com.kuvaszuptime.kuvasz.config.AppConfig | ||
import com.kuvaszuptime.kuvasz.config.AdminAuthConfig | ||
import io.micronaut.security.authentication.UsernamePasswordCredentials | ||
|
||
fun generateCredentials(appConfig: AppConfig, valid: Boolean) = | ||
fun generateCredentials(authConfig: AdminAuthConfig, valid: Boolean) = | ||
UsernamePasswordCredentials( | ||
appConfig.user, | ||
if (valid) appConfig.password else "bad-pass" | ||
authConfig.username, | ||
if (valid) authConfig.password else "bad-pass" | ||
) |
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
6 changes: 3 additions & 3 deletions
6
src/test/kotlin/com/kuvaszuptime/kuvasz/services/UptimeCheckerTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.kuvaszuptime.kuvasz.testutils | ||
|
||
import com.kuvaszuptime.kuvasz.events.Event | ||
import com.kuvaszuptime.kuvasz.models.Event | ||
import io.reactivex.subscribers.TestSubscriber | ||
|
||
fun <T : Event> T.toSubscriber(testSubscriber: TestSubscriber<T>) = testSubscriber.onNext(this) |
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