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: [
.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: [
.target(name: "App",
dependencies: [
.product(name: "Vapor", package: "vapor"),
.product(name: "Clairvoyant", package: "Clairvoyant"),
.product(name: "ClairvoyantVapor", package: "ClairvoyantVapor"),
.product(name: "ClairvoyantBinaryCodable", package: "ClairvoyantBinaryCodable"),
],
swiftSettings: [
// Enable better optimizations when building in Release configuration. Despite the use of

View File

@ -1,8 +1,7 @@
import Foundation
import Clairvoyant
import Vapor
final class Authenticator: MetricAccessManager {
final class Authenticator {
private var writers: Set<String>
@ -10,7 +9,6 @@ final class Authenticator: MetricAccessManager {
self.writers = Set(writers)
}
func hasAuthorization(for key: String) -> Bool {
// 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,
@ -20,20 +18,6 @@ final class Authenticator: MetricAccessManager {
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 {
guard let key = request.headers.first(name: "key") else {
throw Abort(.badRequest) // 400

View File

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

View File

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