From 2a1360573f7cc9495813dd39fc2daee56cd2cb96 Mon Sep 17 00:00:00 2001 From: Oleg Bragin Date: Mon, 24 Oct 2022 23:54:30 +0300 Subject: [PATCH 1/6] Add restriction in code to avoid compile time error during package consumer build. --- Sources/LoggingELK/LogstashLogHandler.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/LoggingELK/LogstashLogHandler.swift b/Sources/LoggingELK/LogstashLogHandler.swift index 63ac70f..7fd6c6e 100644 --- a/Sources/LoggingELK/LogstashLogHandler.swift +++ b/Sources/LoggingELK/LogstashLogHandler.swift @@ -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 From ed677c0952391b866e26126c3267d604a0f31104 Mon Sep 17 00:00:00 2001 From: Oleg Bragin Date: Wed, 9 Nov 2022 18:45:09 +0400 Subject: [PATCH 2/6] Add authorization support for upload requests --- .../LogstashLogHandler+HTTPFormatting.swift | 4 ++ Sources/LoggingELK/LogstashLogHandler.swift | 2 + Sources/LoggingELK/Utils/Authorization.swift | 45 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 Sources/LoggingELK/Utils/Authorization.swift diff --git a/Sources/LoggingELK/LogstashLogHandler+HTTPFormatting.swift b/Sources/LoggingELK/LogstashLogHandler+HTTPFormatting.swift index 9e3d3f9..f9e62fd 100644 --- a/Sources/LoggingELK/LogstashLogHandler+HTTPFormatting.swift +++ b/Sources/LoggingELK/LogstashLogHandler+HTTPFormatting.swift @@ -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 diff --git a/Sources/LoggingELK/LogstashLogHandler.swift b/Sources/LoggingELK/LogstashLogHandler.swift index 63ac70f..0f0464c 100644 --- a/Sources/LoggingELK/LogstashLogHandler.swift +++ b/Sources/LoggingELK/LogstashLogHandler.swift @@ -22,6 +22,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` diff --git a/Sources/LoggingELK/Utils/Authorization.swift b/Sources/LoggingELK/Utils/Authorization.swift new file mode 100644 index 0000000..b154291 --- /dev/null +++ b/Sources/LoggingELK/Utils/Authorization.swift @@ -0,0 +1,45 @@ +// +// 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 `, 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 + + /// A string concatenated from type name and token itself for authroization header field + public var value: String { + return "\(type.name) \(token)" + } +} From 175696e2a9dc4fef80cd7e77840b47cf2fd4f421 Mon Sep 17 00:00:00 2001 From: Oleg Bragin Date: Wed, 9 Nov 2022 19:45:03 +0400 Subject: [PATCH 3/6] Add authorization support --- Sources/LoggingELK/LogstashLogHandler.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/LoggingELK/LogstashLogHandler.swift b/Sources/LoggingELK/LogstashLogHandler.swift index 0f0464c..5cbf178 100644 --- a/Sources/LoggingELK/LogstashLogHandler.swift +++ b/Sources/LoggingELK/LogstashLogHandler.swift @@ -91,6 +91,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), @@ -103,6 +104,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 From b31746e6c8976425d99774e51cd943673144313d Mon Sep 17 00:00:00 2001 From: Oleg Bragin Date: Thu, 10 Nov 2022 13:12:16 +0400 Subject: [PATCH 4/6] Add authorization support --- Sources/LoggingELK/Utils/Authorization.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/LoggingELK/Utils/Authorization.swift b/Sources/LoggingELK/Utils/Authorization.swift index b154291..66659f9 100644 --- a/Sources/LoggingELK/Utils/Authorization.swift +++ b/Sources/LoggingELK/Utils/Authorization.swift @@ -38,6 +38,16 @@ public struct Authorization: Authorizable { /// 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)" From 28600fdc6ab866a11622a223a60b002ebde675f0 Mon Sep 17 00:00:00 2001 From: Oleg Bragin Date: Sat, 12 Nov 2022 19:46:39 +0400 Subject: [PATCH 5/6] MOBN-562: iOS: Implement logging with ELK cluster support --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 06d0b13..f0bef21 100644 --- a/Package.swift +++ b/Package.swift @@ -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( From 46ade0321e4008476aebc902363493b914cde61d Mon Sep 17 00:00:00 2001 From: Olena Zola <> Date: Mon, 6 Jan 2025 11:56:59 +0200 Subject: [PATCH 6/6] MOBN-2266 Updated the minimum deployment iOS version --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index f0bef21..55c3ea0 100644 --- a/Package.swift +++ b/Package.swift @@ -7,7 +7,7 @@ let package = Package( name: "swift-log-elk", platforms: [ .macOS(.v10_15), - .iOS(.v13) + .iOS(.v15) ], products: [ .library(name: "LoggingELK", targets: ["LoggingELK"])