Update clairvoyant

This commit is contained in:
Christoph Hagen 2023-09-08 10:05:55 +02:00
parent e7aa2774df
commit 29a72032c6
4 changed files with 16 additions and 30 deletions

View File

@ -8,13 +8,17 @@ let package = Package(
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/vapor/vapor", from: "4.0.0"), .package(url: "https://github.com/vapor/vapor", from: "4.0.0"),
.package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.5.0"), .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.9.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantVapor", from: "0.2.0"),
.package(url: "https://github.com/christophhagen/ClairvoyantBinaryCodable", from: "0.3.0"),
], ],
targets: [ targets: [
.target(name: "App", .target(name: "App",
dependencies: [ dependencies: [
.product(name: "Vapor", package: "vapor"), .product(name: "Vapor", package: "vapor"),
.product(name: "Clairvoyant", package: "Clairvoyant"), .product(name: "Clairvoyant", package: "Clairvoyant"),
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
], ],
swiftSettings: [ swiftSettings: [
// Enable better optimizations when building in Release configuration. Despite the use of // Enable better optimizations when building in Release configuration. Despite the use of

View File

@ -1,8 +1,7 @@
import Foundation import Foundation
import Clairvoyant
import Vapor import Vapor
final class Authenticator: MetricAccessManager { final class Authenticator {
private var writers: Set<String> private var writers: Set<String>
@ -10,7 +9,6 @@ final class Authenticator: MetricAccessManager {
self.writers = Set(writers) self.writers = Set(writers)
} }
func hasAuthorization(for key: String) -> Bool { func hasAuthorization(for key: String) -> Bool {
// Note: This is not a constant-time compare, so there may be an opportunity // Note: This is not a constant-time compare, so there may be an opportunity
// for timing attack here. Sets perform hashed lookups, so this may be less of an issue, // for timing attack here. Sets perform hashed lookups, so this may be less of an issue,
@ -20,20 +18,6 @@ final class Authenticator: MetricAccessManager {
writers.contains(key) writers.contains(key)
} }
func metricListAccess(isAllowedForToken accessToken: AccessToken) throws {
guard let key = String(data: accessToken, encoding: .utf8) else {
return
}
guard hasAuthorization(for: key) else {
throw MetricError.accessDenied
}
}
func metricAccess(to metric: MetricId, isAllowedForToken accessToken: AccessToken) throws {
try metricListAccess(isAllowedForToken: accessToken)
}
func authorize(_ request: Request) throws { func authorize(_ request: Request) throws {
guard let key = request.headers.first(name: "key") else { guard let key = request.headers.first(name: "key") else {
throw Abort(.badRequest) // 400 throw Abort(.badRequest) // 400

View File

@ -67,8 +67,8 @@ final class CapServer {
didSet { didSet {
scheduleSave() scheduleSave()
Task { Task {
try? await capCountMetric.update(caps.count) _ = try? await capCountMetric.update(caps.count)
try? await imageCountMetric.update(imageCount) _ = try? await imageCountMetric.update(imageCount)
} }
} }
} }

View File

@ -1,6 +1,10 @@
import Vapor import Vapor
import Foundation import Foundation
import Clairvoyant import Clairvoyant
import ClairvoyantVapor
import ClairvoyantBinaryCodable
private var provider: VaporMetricProvider!
public func configure(_ app: Application) async throws { public func configure(_ app: Application) async throws {
@ -10,12 +14,7 @@ public func configure(_ app: Application) async throws {
let config = Config(loadFrom: resourceDirectory) let config = Config(loadFrom: resourceDirectory)
let authenticator = Authenticator(writers: config.writers) let authenticator = Authenticator(writers: config.writers)
let monitor = await MetricObserver( let monitor = MetricObserver(logFileFolder: config.logURL, logMetricId: "caps.log")
logFolder: config.logURL,
accessManager: authenticator,
logMetricId: "caps.log")
// All new metrics are automatically registered with the standard observer
MetricObserver.standard = monitor MetricObserver.standard = monitor
let status = try await Metric<ServerStatus>("caps.status", let status = try await Metric<ServerStatus>("caps.status",
@ -28,7 +27,8 @@ public func configure(_ app: Application) async throws {
let server = await CapServer(in: URL(fileURLWithPath: publicDirectory)) let server = await CapServer(in: URL(fileURLWithPath: publicDirectory))
await monitor.registerRoutes(app) provider = .init(observer: monitor, accessManager: config.writers)
provider.registerRoutes(app)
if config.serveFiles { if config.serveFiles {
let middleware = FileMiddleware(publicDirectory: publicDirectory) let middleware = FileMiddleware(publicDirectory: publicDirectory)
@ -52,7 +52,5 @@ func log(_ message: String) {
print(message) print(message)
return return
} }
Task { observer.log(message)
await observer.log(message)
}
} }