-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: client-credential模式每次认证都会覆盖之前的token #2715
- Loading branch information
Showing
5 changed files
with
134 additions
and
37 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
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
77 changes: 77 additions & 0 deletions
77
...b/biz-job/src/main/kotlin/com/tencent/bkrepo/job/batch/task/clean/OauthTokenCleanupJob.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,77 @@ | ||
package com.tencent.bkrepo.job.batch.task.clean | ||
|
||
import com.tencent.bkrepo.common.metadata.constant.ID | ||
import com.tencent.bkrepo.job.batch.base.DefaultContextMongoDbJob | ||
import com.tencent.bkrepo.job.batch.base.JobContext | ||
import com.tencent.bkrepo.job.batch.utils.TimeUtils | ||
import com.tencent.bkrepo.job.config.properties.OauthTokenCleanupJobProperties | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
import org.springframework.data.mongodb.core.query.Criteria | ||
import org.springframework.data.mongodb.core.query.Query | ||
import org.springframework.data.mongodb.core.query.isEqualTo | ||
import org.springframework.data.mongodb.core.query.where | ||
import org.springframework.stereotype.Component | ||
import java.time.Duration | ||
import java.time.Instant | ||
import java.time.ZoneId | ||
import kotlin.reflect.KClass | ||
|
||
|
||
@Component | ||
@EnableConfigurationProperties(OauthTokenCleanupJobProperties::class) | ||
class OauthTokenCleanupJob( | ||
private val properties: OauthTokenCleanupJobProperties | ||
) : DefaultContextMongoDbJob<OauthTokenCleanupJob.OauthToken>(properties) { | ||
override fun collectionNames(): List<String> { | ||
return listOf(COLLECTION_NAME) | ||
} | ||
|
||
override fun buildQuery(): Query { | ||
return Query(where(OauthToken::expireSeconds).gt(0)) | ||
} | ||
|
||
override fun mapToEntity(row: Map<String, Any?>): OauthToken { | ||
return OauthToken(row) | ||
} | ||
|
||
override fun entityClass(): KClass<OauthToken> { | ||
return OauthToken::class | ||
} | ||
|
||
override fun run(row: OauthToken, collectionName: String, context: JobContext) { | ||
val accessTokenExpireTime = row.issuedAt.plusSeconds(row.expireSeconds!!) | ||
val reservedTime = accessTokenExpireTime.plusSeconds(properties.reservedDuration.seconds) | ||
if (Instant.now().isAfter(reservedTime)) { | ||
val query = Query(Criteria.where(ID).isEqualTo(row.id)) | ||
mongoTemplate.remove(query, COLLECTION_NAME) | ||
context.success.incrementAndGet() | ||
logger.info("clean up oauth token: ${row.id}, ${row.issuedAt.epochSecond}, ${row.expireSeconds}") | ||
} | ||
context.total.incrementAndGet() | ||
} | ||
|
||
override fun getLockAtMostFor(): Duration { | ||
return Duration.ofDays(7) | ||
} | ||
|
||
data class OauthToken( | ||
var id: String, | ||
var issuedAt: Instant, | ||
val expireSeconds: Long?, | ||
) { | ||
constructor(map: Map<String, Any?>) : this( | ||
map[OauthToken::id.name].toString(), | ||
TimeUtils.parseMongoDateTimeStr(map[OauthToken::issuedAt.name].toString()) | ||
?.atZone(ZoneId.systemDefault()) | ||
?.toInstant() ?: Instant.now(), | ||
map[OauthToken::expireSeconds.name]?.toString()?.toLong(), | ||
) | ||
} | ||
|
||
companion object { | ||
private const val COLLECTION_NAME = "oauth_token" | ||
private val logger = LoggerFactory.getLogger(OauthTokenCleanupJob::class.java) | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...rc/main/kotlin/com/tencent/bkrepo/job/config/properties/OauthTokenCleanupJobProperties.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,11 @@ | ||
package com.tencent.bkrepo.job.config.properties | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
import java.time.Duration | ||
|
||
@ConfigurationProperties(value = "job.oauth-token-clean-up") | ||
class OauthTokenCleanupJobProperties( | ||
override var enabled: Boolean = true, | ||
override var cron: String = "0 0 10 1/3 * ?", | ||
var reservedDuration: Duration = Duration.ofDays(7), | ||
) : MongodbJobProperties() |