Skip to content

Commit

Permalink
🔀 Google Application Default credentials (#40)
Browse files Browse the repository at this point in the history
* Update for ADC

* Update dependencies

* Remove unnecessary concurrency extensions

* Build universal binary for deploy job

* Update Xcode 16.1
  • Loading branch information
olejnjak committed Nov 10, 2024
1 parent 829793c commit 542cb1a
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 367 deletions.
14 changes: 4 additions & 10 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ jobs:
key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
restore-keys: |
${{ runner.os }}-spm-
- name: Build ARM64
- name: Build
run: |
swift build -c release --arch arm64
mv `swift build -c release --arch arm64 --show-bin-path`/torino torino-arm64
- name: Build AMD64
run: |
swift build -c release --arch x86_64
mv `swift build -c release --arch x86_64 --show-bin-path`/torino torino-x86_64
swift build -c release --arch x86_64 --arch arm64
mv `swift build -c release --arch x86_64 --arch arm64 --show-bin-path`/torino torino
- name: Release
uses: softprops/action-gh-release@v2
with:
generate_release_notes: true
files: |
torino-arm64
torino-x86_64
files: torino
2 changes: 1 addition & 1 deletion .github/xcode-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15.2
16.1
126 changes: 76 additions & 50 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,52 +1,78 @@
{
"object": {
"pins": [
{
"package": "jwt-kit",
"repositoryURL": "https://github.com/vapor/jwt-kit",
"state": {
"branch": null,
"revision": "87ce13a1df913ba4d51cf00606df7ef24d455571",
"version": "4.7.0"
}
},
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "fddd1c00396eed152c45a46bea9f47b98e59301d",
"version": "1.2.0"
}
},
{
"package": "swift-crypto",
"repositoryURL": "https://github.com/apple/swift-crypto.git",
"state": {
"branch": null,
"revision": "71ae6adf89ba5346a209ec7f48dbb571a7e8ad1e",
"version": "2.2.1"
}
},
{
"package": "swift-system",
"repositoryURL": "https://github.com/apple/swift-system.git",
"state": {
"branch": null,
"revision": "836bc4557b74fe6d2660218d56e3ce96aff76574",
"version": "1.1.1"
}
},
{
"package": "swift-tools-support-core",
"repositoryURL": "https://github.com/apple/swift-tools-support-core",
"state": {
"branch": null,
"revision": "0b77e67c484e532444ceeab60119b8536f8cd648",
"version": "0.3.0"
}
}
]
},
"version": 1
"originHash" : "767a949f0d5e38ecc331a58fdd65d4ef898f3f3c46f43ab0e770b5530dd50607",
"pins" : [
{
"identity" : "google-auth-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/olejnjak/google-auth-swift",
"state" : {
"revision" : "630ebdd31375b62e71565cfea97cf322ba79a2a6",
"version" : "0.1.0"
}
},
{
"identity" : "jwt-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/jwt-kit",
"state" : {
"revision" : "02a0fa600eee1bdc892013d62fc795fc623a5cc3",
"version" : "5.1.0"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "41982a3656a71c768319979febd796c6fd111d5c",
"version" : "1.5.0"
}
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
}
},
{
"identity" : "swift-certificates",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-certificates.git",
"state" : {
"revision" : "1fbb6ef21f1525ed5faf4c95207b9c11bea27e94",
"version" : "1.6.1"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "06dc63c6d8da54ee11ceb268cde1fa68161afc96",
"version" : "3.9.1"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "9cb486020ebf03bfa5b5df985387a14a98744537",
"version" : "1.6.1"
}
},
{
"identity" : "swift-tools-support-core",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-tools-support-core",
"state" : {
"revision" : "5b130e04cc939373c4713b91704b0c47ceb36170",
"version" : "0.7.1"
}
}
],
"version" : 3
}
47 changes: 36 additions & 11 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// swift-tools-version:5.5
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Torino",
platforms: [.macOS(.v11)],
platforms: [.macOS(.v13)],
products: [
.executable(
name: "torino",
Expand All @@ -17,9 +17,18 @@ let package = Package(
),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", .upToNextMajor(from: "1.0.1")),
.package(url: "https://github.com/apple/swift-tools-support-core", .upToNextMajor(from: "0.2.0")),
.package(url: "https://github.com/vapor/jwt-kit", .upToNextMajor(from: "4.2.6")),
.package(
url: "https://github.com/apple/swift-argument-parser",
from: "1.0.1"
),
.package(
url: "https://github.com/apple/swift-tools-support-core",
from: "0.2.0"
),
.package(
url: "https://github.com/olejnjak/google-auth-swift",
from: "0.1.0"
)
],
targets: [
.executableTarget(
Expand All @@ -31,8 +40,14 @@ let package = Package(
dependencies: [
"GCP_Remote",
"Logger",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
.product(
name: "ArgumentParser",
package: "swift-argument-parser"
),
.product(
name: "SwiftToolsSupport-auto",
package: "swift-tools-support-core"
),
]
),
.testTarget(
Expand All @@ -43,15 +58,25 @@ let package = Package(
name: "GCP_Remote",
dependencies: [
"Logger",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
.product(name: "JWTKit", package: "jwt-kit")
.product(
name: "SwiftToolsSupport-auto",
package: "swift-tools-support-core"
),
.product(
name: "GoogleAuth",
package: "google-auth-swift"
)
]
),
.target(
name: "Logger",
dependencies: [
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
.product(
name: "SwiftToolsSupport-auto",
package: "swift-tools-support-core"
),
]
),
]
],
swiftLanguageModes: [.v5]
)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ Torino download --prefix "Swift-5.5"

### Remote caching

Torino currently supports remote cache stored in GCP buckets. To support that you need to provide two environment variables:
Torino currently supports remote cache stored in GCP buckets. To support that you need to provide an environment variables:

`TORINO_GCP_BUCKET` - name of bucket that will be used for storage<br>

For authorization you can either use `TORINO_GCP_SERVICE_ACCOUNT_PATH` or [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials)
`TORINO_GCP_SERVICE_ACCOUNT_PATH` - location of service account that will be used for access to specified bucket

### Environment configuration
Expand Down
8 changes: 8 additions & 0 deletions Sources/GCP_Remote/Extensions/TokenExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Foundation
import GoogleAuth

extension Token {
func addToRequest(_ request: inout URLRequest) {
request.setValue(tokenType + " " + accessToken, forHTTPHeaderField: "Authorization")
}
}
87 changes: 0 additions & 87 deletions Sources/GCP_Remote/Extensions/URLSessionExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,91 +7,4 @@ struct RequestError: Error {

extension URLSession {
static let torino = URLSession(configuration: .ephemeral)

@available(*, deprecated, renamed: "data(request:)")
func syncDataTask(for request: URLRequest) throws -> (Data?, URLResponse?) {
let semaphore = DispatchSemaphore(value: 0)

var resultData: Data?
var resultResponse: URLResponse?
var resultError: Error?

let task = dataTask(with: request) { data, response, error in
resultData = data
resultResponse = response
resultError = error
semaphore.signal()
}

task.resume()
semaphore.wait()

if let error = resultError {
throw error
}

if let httpResponse = (resultResponse as? HTTPURLResponse),
(200...299).contains(httpResponse.statusCode) {
return (resultData, resultResponse)
}

throw RequestError(response: resultResponse, data: resultData)
}
}

// Swift Concurrency API is available from macOS 12,
// to support lower deployment target we need following extensions
@available(macOS, deprecated: 12.0, message: "Use the built-in API instead")
extension URLSession {
@discardableResult
func data(request: URLRequest) async throws -> (Data, URLResponse) {
try await withUnsafeThrowingContinuation { continuation in
let task = self.dataTask(
with: request,
completionHandler: Self.taskCompletion(continuation: continuation)
)

task.resume()
}
}

@discardableResult
func data(url: URL) async throws -> (Data, URLResponse) {
try await data(request: URLRequest(url: url))
}

@discardableResult
func upload(request: URLRequest, fromFile file: URL) async throws -> (Data, URLResponse) {
try await withUnsafeThrowingContinuation { continuation in
let task = self.uploadTask(
with: request,
fromFile: file,
completionHandler: Self.taskCompletion(continuation: continuation)
)

task.resume()
}
}

private static func taskCompletion(continuation: UnsafeContinuation<(Data, URLResponse), Error>) -> (Data?, URLResponse?, Error?) -> () {
{ data, response, error in
guard let data = data, let response = response else {
let error = error ?? URLError(.badServerResponse)
return continuation.resume(throwing: error)
}

if let httpResponse = (response as? HTTPURLResponse),
(200...299).contains(httpResponse.statusCode) {
continuation.resume(returning: (data, response))
} else {
continuation.resume(
throwing:
URLError(
.cannotParseResponse,
userInfo: [:]
)
)
}
}
}
}
Loading

0 comments on commit 542cb1a

Please sign in to comment.