Skip to content

Commit

Permalink
Custom password regression fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Krystian Panek committed Mar 14, 2021
1 parent 9ade880 commit 9fe4d39
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ open class InstanceHttpClient(private val aem: AemExtension, val instance: Insta

init {
baseUrl.set(instance.httpUrl)
basicCredentials = instance.credentials
authorizationPreemptive.set(true)
basicCredentials = when {
instance is LocalInstance && !instance.authAvailable -> Instance.CREDENTIALS_DEFAULT
else -> instance.credentials
}

connectionTimeout.apply {
convention(30_000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ class LocalInstance(aem: AemExtension) : Instance(aem) {
@get:JsonIgnore
val created: Boolean get() = locked(LOCK_CREATE)

@get:JsonIgnore
val initialized: Boolean get() = locked(LOCK_INIT)

@get:JsonIgnore
val installDir get() = quickstartDir.resolve("install")

Expand Down Expand Up @@ -412,12 +409,28 @@ class LocalInstance(aem: AemExtension) : Instance(aem) {
@get:JsonIgnore
val runningOther get() = available && (dir != runningDir)

fun destroy() = localManager.destroy(this)

@get:JsonIgnore
val initialized: Boolean get() = locked(LOCK_INIT)

internal fun init(callback: LocalInstance.() -> Unit) {
apply(callback)
lock(LOCK_INIT)
}

fun destroy() = localManager.destroy(this)
@get:JsonIgnore
val authAvailable: Boolean get() = initialized || locked(LOCK_AUTH)

internal fun checkAuthBecameAvailable(): Boolean {
if (!authAvailable && credentials != CREDENTIALS_DEFAULT) {
if (sync.status.checkUnauthorized()) {
lock(LOCK_AUTH)
return true
}
}
return false
}

private fun lockFile(name: String) = dir.resolve("$name.lock")

Expand All @@ -429,10 +442,10 @@ class LocalInstance(aem: AemExtension) : Instance(aem) {
if (running) {
throw LocalInstanceException("Instance is running so resetting password on $this is not possible!")
}
if ((force || localManager.resetPassword.get()) && initialized) {
oakRun.resetPassword(user, password)
} else {
logger.debug("Skipping resetting password on $this (feature is disabled)")
when {
!initialized -> logger.debug("Skipping resetting password on $this (not initialized)")
force || localManager.resetPassword.get() -> oakRun.resetPassword(user, password)
else -> logger.debug("Skipping resetting password on $this (feature is disabled)")
}
}

Expand All @@ -451,5 +464,7 @@ class LocalInstance(aem: AemExtension) : Instance(aem) {
const val LOCK_CREATE = "create"

const val LOCK_INIT = "init"

const val LOCK_AUTH = "auth"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ class AwaitUpAction(aem: AemExtension) : DefaultAction(aem) {

checks {
listOf(
timeout(timeoutOptions),
init(initOptions),
help(helpOptions),
bundles(bundlesOptions),
events(eventsOptions),
installer(installerOptions),
components(componentsOptions),
unchanged(unchangedOptions)
init(initOptions),
timeout(timeoutOptions),
help(helpOptions),
bundles(bundlesOptions),
events(eventsOptions),
installer(installerOptions),
components(componentsOptions),
unchanged(unchangedOptions)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,11 @@ package com.cognifide.gradle.aem.common.instance.check
class InitCheck(group: CheckGroup) : DefaultCheck(group) {

override fun check() = instance.whenLocal {
if (!initialized) {
logger.info("Checking auth on $instance")

// TODO write lock when authorized, do not block other checks but force instanhttpclient to use other creds basing on lock
val authorizable = state(sync.status.checkAuthorizable())
if (!authorizable) {
statusLogger.error(
"Auth not ready",
"Cannot authorize on $instance"
)
}
if (state(checkAuthBecameAvailable())) {
statusLogger.error(
"Auth became available",
"Switching auth credentials for $instance"
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cognifide.gradle.aem.common.instance.service.status

import com.cognifide.gradle.aem.common.instance.Instance
import com.cognifide.gradle.aem.common.instance.InstanceService
import com.cognifide.gradle.aem.common.instance.InstanceSync
import com.cognifide.gradle.aem.common.instance.service.osgi.OsgiFramework
Expand Down Expand Up @@ -63,15 +64,19 @@ class Status(sync: InstanceSync) : InstanceService(sync) {
}

/**
* Check if instance authorizes requests.
* Check if instance started authorizes requests with target credentials.
* AEM on first run by default starts running with default admin password then after some time applies target one.
*/
fun checkAuthorizable(): Boolean = try {
instance.sync {
http.get(authorizablePath.get()) { it.statusLine.statusCode } == HttpStatus.SC_OK
fun checkUnauthorized(): Boolean {
return try {
instance.sync {
http.basicCredentials = Instance.CREDENTIALS_DEFAULT
http.get(authorizablePath.get()) { it.statusLine.statusCode } == HttpStatus.SC_UNAUTHORIZED
}
} catch (e: CommonException) {
logger.debug("Cannot check for unauthorized on $instance!", e)
false
}
} catch (e: CommonException) {
logger.debug("Cannot check authorization on $instance!", e)
false
}

/**
Expand Down

0 comments on commit 9fe4d39

Please sign in to comment.