Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the @avaialble directive to restrict the code being able to execute only under iOS 13 and above and add Authorization support. #3

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ let package = Package(
.library(name: "LoggingELK", targets: ["LoggingELK"])
],
dependencies: [
.package(url: "https://github.com/apple/swift-log.git", .upToNextMinor(from: "1.4.0")),
.package(url: "https://github.com/swift-server/async-http-client.git", .upToNextMinor(from: "1.5.0"))
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.4.0")),
.package(url: "https://github.com/swift-server/async-http-client.git", .upToNextMajor(from: "1.5.0"))
],
targets: [
.target(
Expand Down
4 changes: 4 additions & 0 deletions Sources/LoggingELK/LogstashLogHandler+HTTPFormatting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ extension LogstashLogHandler {
}

// Set headers that always stay consistent over all requests
if let authorization = self.authorization {
httpRequest.headers.add(name: "Authorization", value: authorization.value)
}

httpRequest.headers.add(name: "Content-Type", value: "application/json")
httpRequest.headers.add(name: "Accept", value: "application/json")
// Keep-alive header to keep the connection open
Expand Down
5 changes: 5 additions & 0 deletions Sources/LoggingELK/LogstashLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import AsyncHTTPClient

/// `LogstashLogHandler` is a simple implementation of `LogHandler` for directing
/// `Logger` output to Logstash via HTTP requests
@available(iOS 13.0, *)
public struct LogstashLogHandler: LogHandler {
/// The label of the `LogHandler`
let label: String
Expand All @@ -22,6 +23,8 @@ public struct LogstashLogHandler: LogHandler {
static var port: Int?
/// Specifies if the HTTP connection to Logstash should be encrypted via TLS (so HTTPS instead of HTTP)
static var useHTTPS: Bool?
/// Specifies the authorization schema for the HTTP request
static var authorization: Authorizable?
/// The `EventLoopGroup` which is used to create the `HTTPClient`
static var eventLoopGroup: EventLoopGroup?
/// Used to log background activity of the `LogstashLogHandler` and `HTTPClient`
Expand Down Expand Up @@ -89,6 +92,7 @@ public struct LogstashLogHandler: LogHandler {
public static func setup(hostname: String,
port: Int,
useHTTPS: Bool = false,
authorization: Authorizable? = nil,
eventLoopGroup: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: (System.coreCount != 1) ? System.coreCount / 2 : 1),
backgroundActivityLogger: Logger = Logger(label: "backgroundActivity-logstashHandler"),
uploadInterval: TimeAmount = TimeAmount.seconds(3),
Expand All @@ -101,6 +105,7 @@ public struct LogstashLogHandler: LogHandler {
Self.hostname = hostname
Self.port = port
Self.useHTTPS = useHTTPS
Self.authorization = authorization
Self.eventLoopGroup = eventLoopGroup
Self.backgroundActivityLogger = backgroundActivityLogger
Self.uploadInterval = uploadInterval
Expand Down
55 changes: 55 additions & 0 deletions Sources/LoggingELK/Utils/Authorization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Authentication.swift
//
//
// Created by Oleg Bragin on 09.11.2022.
//

import Foundation

/// Describes the property which should be used to set the authorization header
/// to access a remote resource
public protocol Authorizable {
/// Should return authorization header value.
var value: String { get }
}

/// Defines the most commonly used authroization type names: Basic and Bearer
/// Basically define the first part of overall Authorization header value, e.g.:
/// `Basic <token>`, etc.
public enum AuthorizationType: String {
case basic
case bearer

public var name: String {
switch self {
case .basic:
return "Basic"
case .bearer:
return "Bearer"
}
}
}

/// Defines the default way of providing the authorization header to caller
public struct Authorization: Authorizable {
/// Type of authorization
let type: AuthorizationType
/// Token string, which should generated outside
let token: String

/// Initialize a new authroization structure to provide access to
/// some protected url
/// - Parameters:
/// - type: authorization type, e.g. `basic`, `bearer`, ...
/// - token: generated authorization token
public init(type: AuthorizationType, token: String) {
self.type = type
self.token = token
}

/// A string concatenated from type name and token itself for authroization header field
public var value: String {
return "\(type.name) \(token)"
}
}