Skip to content

Commit 20e0bab

Browse files
authored
feat!: update swift-confidential (#22)
* feat!: update swift-confidential * chore: separate mise config for ubuntu
1 parent e39ff50 commit 20e0bab

File tree

9 files changed

+162
-126
lines changed

9 files changed

+162
-126
lines changed

.github/workflows/codeql.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ jobs:
8282
fi
8383
8484
if [[ "${{ steps.changes.outputs.swift }}" == "true" ]]; then
85-
# Turning off CodeQL for a while, since it's doesn't work with Swift 6.2.0
86-
# matrix_include="$(echo "$matrix_include" | jq -c '. += [{"language": "swift", "build-mode": "manual"}]')"
87-
printf ''
85+
matrix_include="$(echo "$matrix_include" | jq -c '. += [{"language": "swift", "build-mode": "manual"}]')"
86+
# printf ''
8887
fi
8988
9089
echo "matrix={\"include\":$matrix_include}" >> $GITHUB_OUTPUT

.github/workflows/templates/prepare-swift/action.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,24 @@ runs:
4242
apt-get install -y zip
4343
fi
4444
45+
- name: Empty mise.toml for Linux
46+
shell: bash
47+
if: ${{ runner.os == 'Linux' }}
48+
run: |
49+
rm -rf mise.toml
50+
touch mise.toml
51+
52+
- name: Install mise tools
53+
if: ${{ runner.os == 'Linux' }}
54+
uses: jdx/mise-action@5ac50f778e26fac95da98d50503682459e86d566
55+
with:
56+
mise_toml: |
57+
[tools]
58+
shfmt = "latest"
59+
shellcheck = "latest"
60+
4561
- name: Install mise tools
62+
if: ${{ runner.os != 'Linux' }}
4663
uses: jdx/mise-action@5ac50f778e26fac95da98d50503682459e86d566
4764

4865
- name: Select Swift Version

.version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.1
1+
0.0.2

Package.resolved

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ enum SwiftConfidentialSource {
2020

2121
var packageDependency: PackageDescription.Package.Dependency {
2222
switch self {
23-
case .upstream: .package(url: "https://github.com/securevale/swift-confidential.git", from: "0.4.1")
23+
case .upstream: .package(url: "https://github.com/securevale/swift-confidential.git", from: "0.5.1")
2424
case .fork: .package(url: "https://github.com/nekrich/swift-confidential.git", branch: "master")
2525
}
2626
}
@@ -51,14 +51,14 @@ enum YamsSource {
5151

5252
var packageDependency: PackageDescription.Package.Dependency {
5353
switch self {
54-
case .upstream: .package(url: "https://github.com/jpsim/Yams.git", from: "6.1.0")
54+
case .upstream: .package(url: "https://github.com/jpsim/Yams.git", from: "6.2.1")
5555
case .fork: .package(url: "https://github.com/nekrich/Yams.git", branch: "main")
5656
}
5757
}
5858
}
5959

6060
let swiftConfidentialSource: SwiftConfidentialSource = .fork
61-
let yamsSource: YamsSource = .fork
61+
let yamsSource: YamsSource = .upstream
6262

6363
enum Targets {
6464
static func targetBundle(
@@ -134,10 +134,7 @@ enum Targets {
134134
commandBundle(
135135
name: "ObfuscateSecrets",
136136
dependencies: [.target(name: "EnvSubst"), .target(name: "Shell"), swiftConfidentialSource.targetDependency],
137-
testsDependencies: [
138-
.product(name: "ConfidentialKit", package: "swift-confidential", condition: .when(platforms: [.macOS])),
139-
swiftConfidentialSource.targetDependency,
140-
],
137+
testsDependencies: [swiftConfidentialSource.targetDependency],
141138
commandDependencies: [.target(name: "EnvSubstCommand"), .target(name: "ExportSecretsCommand")]
142139
)
143140
}
@@ -201,7 +198,7 @@ let package = Package(
201198
dependencies: [
202199
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.6.1")),
203200
.package(url: "https://github.com/swiftlang/swift-format.git", .upToNextMajor(from: "602.0.0")),
204-
.package(url: "https://github.com/swiftlang/swift-syntax.git", "602.0.0"..<"603.0.0"),
201+
.package(url: "https://github.com/swiftlang/swift-syntax.git", .upToNextMajor(from: "602.0.0")),
205202
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.6.4")),
206203
swiftConfidentialSource.packageDependency, yamsSource.packageDependency,
207204
],

Tests/ObfuscateSecretsTests/ConfidentialWrapperTests.swift

Lines changed: 10 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#if canImport(ConfidentialKit)
2-
import ConfidentialKit
1+
#if !os(Linux)
32
import Foundation
43
import Shell
54
import Testing
@@ -48,95 +47,23 @@
4847
value: $ENV_VARIABLE_NAME2
4948
"""
5049

51-
/// The resolved values of the obfuscated secret variables.
52-
static let obfuscatedSecretValues: [String: String] = ["variable_name1": "bar", "variable_name2": "baz"]
53-
5450
/// Expected output w/o data and nonce.
5551
static let expectedString: String = """
5652
import ConfidentialKit
57-
import Foundation
58-
59-
extension ConfidentialKit.Obfuscation.Secret {
6053
61-
@ConfidentialKit.Obfuscated<Swift.String>(deobfuscateData)
62-
public static var variable_name1: ConfidentialKit.Obfuscation.Secret = .init(data: [], nonce: 0)
54+
extension ConfidentialCore.Obfuscation.Secret {
6355
64-
@ConfidentialKit.Obfuscated<Swift.String>(deobfuscateData)
65-
public static var variable_name2: ConfidentialKit.Obfuscation.Secret = .init(data: [], nonce: 0)
66-
67-
@inline(__always)
68-
private static func deobfuscateData(_ data: Foundation.Data, nonce: Swift.UInt64) throws -> Foundation.Data {
69-
try ConfidentialKit.Obfuscation.Encryption.DataCrypter(algorithm: .aes128GCM)
70-
.deobfuscate(data, nonce: nonce)
56+
public static #Obfuscate(algorithm: .custom([.encrypt(algorithm: .aes128GCM)])) {
57+
let variable_name1 = "bar"
58+
let variable_name2 = "baz"
7159
}
7260
}
7361
"""
7462

75-
// MARK: Regexes
76-
77-
/// Bytes regex.
78-
///
79-
/// Matches `0xFF`. First group is the string byte representation: `FF`.
80-
let byteRegex: Regex = /0x([0-9a-fA-F]{1,2}),?\s?+/
81-
/// Data init regex.
82-
///
83-
/// Matches `(data: [0xFF, 0xFF...], nonce: 000)`.
84-
let dataReplacementRegex: Regex = #/\(data:\s?+\[((0x([0-9a-fA-F]{1,2}),?\s?+)+)\],\s?+nonce:\s?+(\d+)\)/#
85-
/// Secret variable regex.
86-
///
87-
/// Matches: `var variableName: Type = .init(data: [0xFF, 0xFF...], nonce: 000)`.
88-
/// First group is the variable name (`variableName`).
89-
/// Third group is the bytes list (`0xFF, 0xFF...`). We match it with the `byteRegex` to get the byte string.
90-
/// Sixth group is the nonce value (`value`).
91-
let secretVariableRegex: Regex =
92-
#/var\s+(.*)\s?+:\s?+.*=\s+.*(\(data:\s?+\[((0x([0-9a-fA-F]{1,2}),?\s?+)+)\],\s?+nonce:\s?+(\d+)\))/#
93-
94-
// MARK: Validators
95-
96-
private func validateContentsWithoutDataAndNonce(
97-
obfuscatedString: String,
98-
_ sourceLocation: SourceLocation = #_sourceLocation,
99-
) throws {
100-
let obfuscatedString = obfuscatedString.replacing(dataReplacementRegex, with: "(data: [], nonce: 0)")
101-
102-
#expect(obfuscatedString == Self.expectedString, sourceLocation: sourceLocation)
103-
}
104-
105-
private func validateSecretValues(
106-
_ secretValues: [String: String],
107-
in obfuscatedString: String,
108-
_ sourceLocation: SourceLocation = #_sourceLocation,
109-
) throws {
110-
let crypter = Obfuscation.Encryption.DataCrypter(algorithm: .aes128GCM)
111-
112-
let deobfuscationResult = try obfuscatedString.matches(of: secretVariableRegex)
113-
.reduce(into: [String: String]()) { (accum, globalMatch) in
114-
let variableName = String(globalMatch.output.1)
115-
116-
let obfuscatedBytes = try globalMatch.output.3 // Get matches for all `0xFF`
117-
.matches(of: byteRegex) // Convert to a byte
118-
.map { byteMatch in try #require(UInt8(byteMatch.output.1, radix: 16), sourceLocation: sourceLocation) }
119-
120-
let nonceValue: UInt64 = try #require(UInt64(globalMatch.output.6), sourceLocation: sourceLocation)
121-
122-
let secret = ConfidentialKit.Obfuscation.Secret(data: obfuscatedBytes, nonce: nonceValue)
123-
let obfuscated = ConfidentialKit.Obfuscated<Swift.String>(wrappedValue: secret, crypter.deobfuscate)
124-
125-
accum[variableName] = obfuscated.projectedValue
126-
}
127-
128-
#expect(deobfuscationResult == secretValues, sourceLocation: sourceLocation)
129-
}
130-
131-
func validate(
132-
outputFileURL: URL,
133-
secretValues: [String: String],
134-
_ sourceLocation: SourceLocation = #_sourceLocation,
135-
) throws {
63+
func validate(outputFileURL: URL, _ sourceLocation: SourceLocation = #_sourceLocation, ) throws {
13664
let obfuscatedString = try String(contentsOf: outputFileURL, encoding: .utf8)
13765

138-
try validateContentsWithoutDataAndNonce(obfuscatedString: obfuscatedString, sourceLocation)
139-
try validateSecretValues(secretValues, in: obfuscatedString, sourceLocation)
66+
#expect(obfuscatedString == Self.expectedString, sourceLocation: sourceLocation)
14067
}
14168
}
14269

@@ -147,7 +74,7 @@
14774
// When running in Xcode: tests run in a temp dir, and mise fails to recognize tool, because is not currently active.
14875
// If swift-confidential is not found by /usr/bin/which - install it with mise.
14976
if (try? Shell.which(cliToolName: "swift-confidential")) == nil {
150-
do { try Shell.Mise().use(cliToolName: "ubi:securevale/swift-confidential", version: "0.4.1") }
77+
do { try Shell.Mise().use(cliToolName: "github:securevale/swift-confidential", version: "0.5.1") }
15178
catch { #expect(Bool(false), "Unexpected error while installing swift-confidential: \(error)") }
15279
}
15380

@@ -167,7 +94,7 @@
16794
catch { #expect(Bool(false), "Got error: \(error)") }
16895

16996
// THEN
170-
try validate(outputFileURL: self.outputFileURL, secretValues: Self.obfuscatedSecretValues)
97+
try validate(outputFileURL: self.outputFileURL)
17198
}
17299
}
173100

@@ -188,7 +115,7 @@
188115
)
189116

190117
// THEN
191-
try validate(outputFileURL: self.outputFileURL, secretValues: Self.obfuscatedSecretValues)
118+
try validate(outputFileURL: self.outputFileURL)
192119
}
193120
}
194121
#endif

0 commit comments

Comments
 (0)