-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: super basic RBAC starting point
- Loading branch information
Showing
18 changed files
with
466 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import com.auth0.jwt.JWTVerifier | |
import com.auth0.jwt.algorithms.Algorithm | ||
import com.auth0.jwt.exceptions.JWTVerificationException | ||
import com.auth0.jwt.interfaces.DecodedJWT | ||
import com.stabledata.plugins.Roles | ||
import com.stabledata.plugins.UserCredentials | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import java.util.* | ||
|
@@ -20,12 +21,19 @@ fun getStableJwtSecret(): String { | |
.withClaim("email", userCredentials.email) | ||
.withClaim("team", userCredentials.team) | ||
.withClaim("id", userCredentials.id) | ||
.withClaim("role", userCredentials.role) | ||
.withExpiresAt(Date(System.currentTimeMillis() + oneWeekInMillis)) | ||
.sign(Algorithm.HMAC256(getStableJwtSecret())) | ||
} | ||
|
||
fun generateTokenForTesting(): String { | ||
val token = generateJwtTokenWithCredentials(UserCredentials("[email protected]", "test", "fake.id")) | ||
fun generateTokenForTesting(withRole: String? = Roles.Default): String { | ||
val fakeCreds = UserCredentials( | ||
"[email protected]", | ||
"test", | ||
"fake.id", | ||
withRole | ||
) | ||
val token = generateJwtTokenWithCredentials(fakeCreds) | ||
return token | ||
} | ||
|
||
|
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,76 @@ | ||
package com.stabledata.dao | ||
|
||
import com.stabledata.endpoint.io.AccessRequest | ||
import org.jetbrains.exposed.sql.* | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
import java.util.* | ||
|
||
data class AccessRecord( | ||
val id: String, | ||
val teamId: String, | ||
val type: String, | ||
val role: String, | ||
val operation: String?, | ||
val path: String? | ||
) | ||
|
||
object AccessTable: Table("stable.access") { | ||
val accessId = uuid("id") | ||
val teamId = varchar("team_id", 255) | ||
val kind = varchar("type", 5).check { | ||
it inList listOf("grant", "deny") | ||
} | ||
val role = varchar("role", 255) | ||
val operation = varchar("operation", 255).nullable() | ||
val path = varchar("path", 255).nullable() | ||
|
||
init { | ||
check("either_operation_or_path") { | ||
(operation.isNotNull() and path.isNull()) or (operation.isNull() and path.isNotNull()) | ||
} | ||
} | ||
|
||
fun insertFromRequest(type: String, team: String, record: AccessRequest) { | ||
AccessTable.insert { row -> | ||
row[accessId] = UUID.fromString(record.id) | ||
row[kind] = type | ||
row[teamId] = team | ||
row[role] = record.role | ||
row[path] = record.path | ||
row[operation] = record.operation | ||
|
||
|
||
|
||
} | ||
} | ||
|
||
fun findMatchingRules(operationOrPath: String, team: String, checkRole: String): Pair<List<AccessRecord>, List<AccessRecord>> { | ||
val rules = transaction { | ||
// Later: make paths matchable in parts | ||
AccessTable | ||
.select { | ||
(teamId eq team) and | ||
(role eq checkRole) and | ||
( | ||
(operation eq operationOrPath) or | ||
(path eq operationOrPath) | ||
) | ||
} | ||
.map { | ||
AccessRecord( | ||
id = it[accessId].toString(), | ||
teamId = it[teamId], | ||
type = it[kind], | ||
role = it[role], | ||
operation = it[operation], | ||
path = it[path] | ||
) | ||
} | ||
} | ||
|
||
val allowingRules = rules.filter { it.type == "grant" } | ||
val blockingRules = rules.filter { it.type == "deny" } | ||
|
||
return Pair(allowingRules, blockingRules) | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/com/stabledata/endpoint/AccessCreateRoute.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,58 @@ | ||
package com.stabledata.endpoint | ||
|
||
import com.stabledata.Ably | ||
import com.stabledata.dao.AccessTable | ||
import com.stabledata.dao.LogsTable | ||
import com.stabledata.endpoint.io.AccessRequest | ||
import com.stabledata.plugins.JWT_NAME | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import org.jetbrains.exposed.exceptions.ExposedSQLException | ||
import org.jetbrains.exposed.sql.transactions.transaction | ||
|
||
fun Application.configureAccessCreateRoute() { | ||
|
||
val logger = KotlinLogging.logger {} | ||
|
||
routing { | ||
authenticate(JWT_NAME) { | ||
post("access/grant") { | ||
val (access, user, envelope, logEntry) = contextualize( | ||
"access/create" | ||
) { postData -> | ||
AccessRequest.fromJSON(postData) | ||
} ?: return@post | ||
|
||
// slightly borrowed, but not crazy use case issues in logs anyway | ||
logEntry.path("grant") | ||
|
||
logger.debug { "Create collection requested by ${user.id} with event id ${envelope.eventId}" } | ||
|
||
try { | ||
val finalLogEntry = logEntry.build() | ||
|
||
transaction { | ||
AccessTable.insertFromRequest("grant", user.team, access) | ||
LogsTable.insertLogEntry(finalLogEntry) | ||
Ably.publish(user.team, "collection/create", finalLogEntry) | ||
} | ||
|
||
logger.debug {"Collection access control record for path: ${access.path} or operation: ${access.operation}" } | ||
|
||
return@post call.respond( | ||
HttpStatusCode.Created, | ||
finalLogEntry | ||
) | ||
|
||
} catch (e: ExposedSQLException) { | ||
logger.error { "Create access record failed: ${e.localizedMessage}" } | ||
return@post call.respond(HttpStatusCode.InternalServerError, e.localizedMessage) | ||
} | ||
} | ||
} | ||
} | ||
} |
6 changes: 5 additions & 1 deletion
6
.../com/stabledata/endpoint/SchemaRouting.kt → ...stabledata/endpoint/ApplicationRouting.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
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
24 changes: 24 additions & 0 deletions
24
src/main/kotlin/com/stabledata/endpoint/io/AccessRequest.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,24 @@ | ||
package com.stabledata.endpoint.io | ||
|
||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
@Serializable | ||
data class AccessRequest ( | ||
val id: String, | ||
val role: String, | ||
val operation: String?, | ||
val path: String? | ||
) { | ||
companion object { | ||
fun fromJSON (json: String): AccessRequest { | ||
val jsonParser = Json { | ||
ignoreUnknownKeys = true | ||
isLenient = true | ||
encodeDefaults = true | ||
explicitNulls = false | ||
} | ||
return jsonParser.decodeFromString<AccessRequest>(json) | ||
} | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.